001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.api.diff;
043:
044: import java.io.*;
045: import java.nio.charset.Charset;
046: import java.nio.charset.UnsupportedCharsetException;
047:
048: import org.openide.util.io.ReaderInputStream;
049: import org.openide.util.Lookup;
050: import org.openide.util.lookup.Lookups;
051: import org.openide.filesystems.FileUtil;
052: import org.netbeans.api.queries.FileEncodingQuery;
053:
054: /**
055: * This class provides streams and information about them to be used by diff
056: * and merge services.
057: *
058: * @author Martin Entlicher
059: */
060: public abstract class StreamSource extends Object {
061:
062: /**
063: * Get the name of the source.
064: */
065: public abstract String getName();
066:
067: /**
068: * Get the title of the source.
069: */
070: public abstract String getTitle();
071:
072: /**
073: * Get the MIME type of the source.
074: */
075: public abstract String getMIMEType();
076:
077: /**
078: * Hint for a diff visualizer about editability of this source. The source will only be made editable if it provides
079: * some editable entity in its lookup (eg. FileObject) and this method returns true and the diff visualizer supports it.
080: *
081: * @return true if this source can be editable in the diff visualizer, false otherwise
082: * @since 1.17
083: */
084: public boolean isEditable() {
085: return false;
086: }
087:
088: /**
089: * Source lookup that may define the content of this source. In case the lookup does not provide anything
090: * usable, createReader() is used instead. Diff engines can process these inputs:
091: * <ul>
092: * <li> instance of {@link org.openide.filesystems.FileObject} - in this case, the content of the source is defined
093: * by calling DataObject.find(fileObject).openDocument(). If the source is editable then it is
094: * saved back via SaveCookie.save() when the Diff component closes.
095: * <li> instance of {@link javax.swing.text.Document} - in this case, the content of the source is defined
096: * by this Document and the source will NOT be editable.
097: * </ul>
098: *
099: * 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.
101: *
102: * @return an instance of Lookup
103: * @since 1.17
104: */
105: public Lookup getLookup() {
106: return Lookups.fixed();
107: }
108:
109: /**
110: * Create a reader, that reads the source.
111: */
112: public abstract Reader createReader() throws IOException;
113:
114: /**
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.
119: */
120: public abstract Writer createWriter(Difference[] conflicts)
121: throws IOException;
122:
123: /**
124: * Close the stream source. This method, is called when this object
125: * will never be asked for the streams any more and thus can
126: * release it's resources in this method.
127: */
128: public void close() {
129: }
130:
131: /**
132: * Create the default implementation of <code>StreamSource</code>, that has
133: * just reader and no writer.
134: */
135: public static StreamSource createSource(String name, String title,
136: String MIMEType, Reader r) {
137: return new Impl(name, title, MIMEType, r);
138: }
139:
140: /**
141: * Create the default implementation of <code>StreamSource</code>, that has
142: * just reader and writer from/to a file.
143: */
144: public static StreamSource createSource(String name, String title,
145: String MIMEType, File file) {
146: return new Impl(name, title, MIMEType, file);
147: }
148:
149: /**
150: * Private implementation to be returned by the static methods.
151: */
152: private static class Impl extends StreamSource {
153:
154: private String name;
155: private String title;
156: private String MIMEType;
157: private Reader r;
158: private File readerSource;
159: private Writer w;
160: private File file;
161: private Charset encoding;
162:
163: Impl(String name, String title, String MIMEType, Reader r) {
164: this .name = name;
165: this .title = title;
166: this .MIMEType = MIMEType;
167: this .r = r;
168: this .readerSource = null;
169: this .w = null;
170: this .file = null;
171: if (r instanceof InputStreamReader) {
172: try {
173: encoding = Charset.forName(((InputStreamReader) r)
174: .getEncoding());
175: } catch (UnsupportedCharsetException e) {
176: // ignore, encoding will be null
177: }
178: }
179: }
180:
181: Impl(String name, String title, String MIMEType, File file) {
182: this .name = name;
183: this .title = title;
184: this .MIMEType = MIMEType;
185: this .readerSource = null;
186: this .w = null;
187: this .file = file;
188: encoding = FileEncodingQuery.getEncoding(FileUtil
189: .toFileObject(file));
190: }
191:
192: private File createReaderSource(Reader r) throws IOException {
193: File tmp = null;
194: tmp = FileUtil.normalizeFile(File.createTempFile("sss",
195: "tmp"));
196: tmp.deleteOnExit();
197: tmp.createNewFile();
198: InputStream in = null;
199: OutputStream out = null;
200: try {
201: if (encoding == null) {
202: in = new ReaderInputStream(r);
203: } else {
204: ByteArrayOutputStream baos = new ByteArrayOutputStream();
205: copyStreamsCloseAll(new OutputStreamWriter(baos,
206: encoding), r);
207: in = new ByteArrayInputStream(baos.toByteArray());
208: }
209: org.openide.filesystems.FileUtil.copy(in,
210: out = new FileOutputStream(tmp));
211: } finally {
212: if (in != null)
213: in.close();
214: if (out != null)
215: out.close();
216: }
217: return tmp;
218: }
219:
220: public String getName() {
221: return name;
222: }
223:
224: public String getTitle() {
225: return title;
226: }
227:
228: public String getMIMEType() {
229: return MIMEType;
230: }
231:
232: public Reader createReader() throws IOException {
233: if (file != null) {
234: return new BufferedReader(new InputStreamReader(
235: new FileInputStream(file), encoding));
236: } else {
237: synchronized (this ) {
238: if (r != null) {
239: readerSource = createReaderSource(r);
240: r = null;
241: }
242: }
243: if (encoding == null) {
244: return new BufferedReader(new FileReader(
245: readerSource));
246: } else {
247: return new BufferedReader(
248: new InputStreamReader(new FileInputStream(
249: readerSource), encoding));
250: }
251: }
252: }
253:
254: public Writer createWriter(Difference[] conflicts)
255: throws IOException {
256: if (conflicts != null && conflicts.length > 0)
257: return null;
258: if (file != null) {
259: if (encoding == null) {
260: return new BufferedWriter(new FileWriter(file));
261: } else {
262: return new BufferedWriter(new OutputStreamWriter(
263: new FileOutputStream(file), encoding));
264: }
265: } else
266: return w;
267: }
268:
269: }
270:
271: private static void copyStreamsCloseAll(Writer writer, Reader reader)
272: throws IOException {
273: char[] buffer = new char[4096];
274: int n;
275: while ((n = reader.read(buffer)) != -1) {
276: writer.write(buffer, 0, n);
277: }
278: writer.close();
279: reader.close();
280: }
281: }
|