2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.api.diff;
45
import java.nio.charset.Charset;
46
import java.nio.charset.UnsupportedCharsetException;
48
import org.openide.util.io.ReaderInputStream;
49
import org.openide.util.Lookup;
50
import org.openide.util.lookup.Lookups;
51
import org.openide.filesystems.FileUtil;
52
import org.netbeans.api.queries.FileEncodingQuery;
55
* This class provides streams and information about them to be used by diff
58
* @author Martin Entlicher
60
public abstract class StreamSource extends Object {
63
* Get the name of the source.
65
public abstract String getName();
68
* Get the title of the source.
70
public abstract String getTitle();
73
* Get the MIME type of the source.
75
public abstract String getMIMEType();
78
* Hint for a diff visualizer about editability of this source. The source will only be made editable if it provides
79
* some editable entity in its lookup (eg. FileObject) and this method returns true and the diff visualizer supports it.
81
* @return true if this source can be editable in the diff visualizer, false otherwise
84
public boolean isEditable() {
89
* Source lookup that may define the content of this source. In case the lookup does not provide anything
90
* usable, createReader() is used instead. Diff engines can process these inputs:
92
* <li> instance of {@link org.openide.filesystems.FileObject} - in this case, the content of the source is defined
93
* by calling DataObject.find(fileObject).openDocument(). If the source is editable then it is
94
* saved back via SaveCookie.save() when the Diff component closes.
95
* <li> instance of {@link javax.swing.text.Document} - in this case, the content of the source is defined
96
* by this Document and the source will NOT be editable.
99
* For compatibility purposes, it is still adviced to fully implement createReader() as older Diff providers may
100
* not use this method of obtaining the source.
102
* @return an instance of Lookup
105
public Lookup getLookup() {
106
return Lookups.fixed();
110
* Create a reader, that reads the source.
112
public abstract Reader createReader() throws IOException ;
115
* Create a writer, that writes to the source.
116
* @param conflicts The list of conflicts remaining in the source.
117
* Can be <code>null</code> if there are no conflicts.
118
* @return The writer or <code>null</code>, when no writer can be created.
120
public abstract Writer createWriter(Difference[] conflicts) throws IOException ;
123
* Close the stream source. This method, is called when this object
124
* will never be asked for the streams any more and thus can
125
* release it's resources in this method.
127
public void close() {
131
* Create the default implementation of <code>StreamSource</code>, that has
132
* just reader and no writer.
134
public static StreamSource createSource(String name, String title, String MIMEType, Reader r) {
135
return new Impl(name, title, MIMEType, r);
139
* Create the default implementation of <code>StreamSource</code>, that has
140
* just reader and writer from/to a file.
142
public static StreamSource createSource(String name, String title, String MIMEType, File file) {
143
return new Impl(name, title, MIMEType, file);
147
* Private implementation to be returned by the static methods.
149
private static class Impl extends StreamSource {
152
private String title;
153
private String MIMEType;
155
private File readerSource;
158
private Charset encoding;
160
Impl(String name, String title, String MIMEType, Reader r) {
163
this.MIMEType = MIMEType;
165
this.readerSource = null;
168
if (r instanceof InputStreamReader) {
170
encoding = Charset.forName(((InputStreamReader) r).getEncoding());
171
} catch (UnsupportedCharsetException e) {
172
// ignore, encoding will be null
177
Impl(String name, String title, String MIMEType, File file) {
180
this.MIMEType = MIMEType;
181
this.readerSource = null;
184
encoding = FileEncodingQuery.getEncoding(FileUtil.toFileObject(file));
187
private File createReaderSource(Reader r) throws IOException {
189
tmp = FileUtil.normalizeFile(File.createTempFile("sss", "tmp"));
192
InputStream in = null;
193
OutputStream out = null;
195
if (encoding == null) {
196
in = new ReaderInputStream(r);
198
ByteArrayOutputStream baos = new ByteArrayOutputStream();
199
copyStreamsCloseAll(new OutputStreamWriter(baos, encoding), r);
200
in = new ByteArrayInputStream(baos.toByteArray());
202
org.openide.filesystems.FileUtil.copy(in, out = new FileOutputStream(tmp));
204
if (in != null) in.close();
205
if (out != null) out.close();
210
public String getName() {
214
public String getTitle() {
218
public String getMIMEType() {
222
public Reader createReader() throws IOException {
224
return new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
226
synchronized (this) {
228
readerSource = createReaderSource(r);
232
if (encoding == null) {
233
return new BufferedReader(new FileReader(readerSource));
235
return new BufferedReader(new InputStreamReader(new FileInputStream(readerSource), encoding));
240
public Writer createWriter(Difference[] conflicts) throws IOException {
241
if (conflicts != null && conflicts.length > 0) return null;
243
if (encoding == null) {
244
return new BufferedWriter(new FileWriter(file));
246
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding));
253
private static void copyStreamsCloseAll(Writer writer, Reader reader) throws IOException {
254
char [] buffer = new char[4096];
256
while ((n = reader.read(buffer)) != -1) {
257
writer.write(buffer, 0, n);