KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > diff > StreamSource


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.diff;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.FileReader JavaDoc;
28 import java.io.FileWriter JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.io.OutputStreamWriter JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.io.Writer JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 import org.openide.util.io.ReaderInputStream;
38 import org.openide.util.Lookup;
39 import org.openide.util.lookup.Lookups;
40 import org.openide.filesystems.FileUtil;
41
42 import org.netbeans.modules.diff.EncodedReaderFactory;
43
44 /**
45  * This class provides streams and information about them to be used by diff
46  * and merge services.
47  *
48  * @author Martin Entlicher
49  */

50 public abstract class StreamSource extends Object JavaDoc {
51     
52     /**
53      * Get the name of the source.
54      */

55     public abstract String JavaDoc getName();
56     
57     /**
58      * Get the title of the source.
59      */

60     public abstract String JavaDoc getTitle();
61     
62     /**
63      * Get the MIME type of the source.
64      */

65     public abstract String JavaDoc getMIMEType();
66     
67     /**
68      * Hint for a diff visualizer about editability of this source. The source will only be made editable if it provides
69      * some editable entity in its lookup (eg. FileObject) and this method returns true and the diff visualizer supports it.
70      *
71      * @return true if this source can be editable in the diff visualizer, false otherwise
72      * @since 1.17
73      */

74     public boolean isEditable() {
75         return false;
76     }
77
78     /**
79      * Source lookup that may define the content of this source. In case the lookup does not provide anything
80      * usable, createReader() is used instead. Diff engines can process these inputs:
81      * <ul>
82      * <li> instance of {@link org.openide.filesystems.FileObject} - in this case, the content of the source is defined
83      * by calling DataObject.find(fileObject).openDocument(). If the source is editable then it is
84      * saved back via SaveCookie.save() when the Diff component closes.
85      * <li> instance of {@link javax.swing.text.Document} - in this case, the content of the source is defined
86      * by this Document and the source will NOT be editable.
87      * </ul>
88      *
89      * For compatibility purposes, it is still adviced to fully implement createReader() as older Diff providers may
90      * not use this method of obtaining the source.
91      *
92      * @return an instance of Lookup
93      * @since 1.17
94      */

95     public Lookup getLookup() {
96         return Lookups.fixed();
97     }
98     
99     /**
100      * Create a reader, that reads the source.
101      */

102     public abstract Reader JavaDoc createReader() throws IOException JavaDoc ;
103     
104     /**
105      * Create a writer, that writes to the source.
106      * @param conflicts The list of conflicts remaining in the source.
107      * Can be <code>null</code> if there are no conflicts.
108      * @return The writer or <code>null</code>, when no writer can be created.
109      */

110     public abstract Writer JavaDoc createWriter(Difference[] conflicts) throws IOException JavaDoc ;
111     
112     /**
113      * Close the stream source. This method, is called when this object
114      * will never be asked for the streams any more and thus can
115      * release it's resources in this method.
116      */

117     public void close() {
118     }
119     
120     /**
121      * Create the default implementation of <code>StreamSource</code>, that has
122      * just reader and no writer.
123      */

124     public static StreamSource createSource(String JavaDoc name, String JavaDoc title, String JavaDoc MIMEType, Reader JavaDoc r) {
125         return new Impl(name, title, MIMEType, r);
126     }
127     
128     /**
129      * Create the default implementation of <code>StreamSource</code>, that has
130      * just reader and writer from/to a file.
131      */

132     public static StreamSource createSource(String JavaDoc name, String JavaDoc title, String JavaDoc MIMEType, File JavaDoc file) {
133         return new Impl(name, title, MIMEType, file);
134     }
135     
136     /**
137      * Private implementation to be returned by the static methods.
138      */

139     private static class Impl extends StreamSource {
140         
141         private String JavaDoc name;
142         private String JavaDoc title;
143         private String JavaDoc MIMEType;
144         private Reader JavaDoc r;
145         private File JavaDoc readerSource;
146         private Writer JavaDoc w;
147         private File JavaDoc file;
148         private String JavaDoc encoding;
149         
150         Impl(String JavaDoc name, String JavaDoc title, String JavaDoc MIMEType, Reader JavaDoc r) {
151             this.name = name;
152             this.title = title;
153             this.MIMEType = MIMEType;
154             this.r = r;
155             this.readerSource = null;
156             this.w = null;
157             this.file = null;
158             if (r instanceof InputStreamReader JavaDoc) {
159                 encoding = ((InputStreamReader JavaDoc) r).getEncoding();
160             }
161         }
162         
163         Impl(String JavaDoc name, String JavaDoc title, String JavaDoc MIMEType, File JavaDoc file) {
164             this.name = name;
165             this.title = title;
166             this.MIMEType = MIMEType;
167             this.readerSource = null;
168             this.w = null;
169             this.file = file;
170             encoding = EncodedReaderFactory.getDefault().getEncoding(file);
171         }
172         
173         private File JavaDoc createReaderSource(Reader JavaDoc r) throws IOException JavaDoc {
174             File JavaDoc tmp = null;
175             tmp = FileUtil.normalizeFile(File.createTempFile("sss", "tmp"));
176             tmp.deleteOnExit();
177             tmp.createNewFile();
178             InputStream JavaDoc in = null;
179             OutputStream JavaDoc out = null;
180             try {
181                 if (encoding == null) {
182                     in = new ReaderInputStream(r);
183                 } else {
184                     in = new ReaderInputStream(r, encoding);
185                 }
186                 org.openide.filesystems.FileUtil.copy(in, out = new FileOutputStream JavaDoc(tmp));
187             } finally {
188                 if (in != null) in.close();
189                 if (out != null) out.close();
190             }
191             return tmp;
192         }
193         
194         public String JavaDoc getName() {
195             return name;
196         }
197         
198         public String JavaDoc getTitle() {
199             return title;
200         }
201         
202         public String JavaDoc getMIMEType() {
203             return MIMEType;
204         }
205         
206         public Reader JavaDoc createReader() throws IOException JavaDoc {
207             if (file != null) {
208                 return new BufferedReader JavaDoc(EncodedReaderFactory.getDefault().getReader(file, MIMEType, encoding));
209             } else {
210                 synchronized (this) {
211                     if (r != null) {
212                         readerSource = createReaderSource(r);
213                         r = null;
214                     }
215                 }
216                 if (encoding == null) {
217                     return new BufferedReader JavaDoc(new FileReader JavaDoc(readerSource));
218                 } else {
219                     return new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(readerSource), encoding));
220                 }
221             }
222         }
223         
224         public Writer JavaDoc createWriter(Difference[] conflicts) throws IOException JavaDoc {
225             if (conflicts != null && conflicts.length > 0) return null;
226             if (file != null) {
227                 if (encoding == null) {
228                     return new BufferedWriter JavaDoc(new FileWriter JavaDoc(file));
229                 } else {
230                     return new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(file), encoding));
231                 }
232             } else return w;
233         }
234         
235     }
236 }
237
Popular Tags