~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to diff/src/org/netbeans/api/diff/StreamSource.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.api.diff;
 
43
 
 
44
import java.io.*;
 
45
import java.nio.charset.Charset;
 
46
import java.nio.charset.UnsupportedCharsetException;
 
47
 
 
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;
 
53
 
 
54
/**
 
55
 * This class provides streams and information about them to be used by diff
 
56
 * and merge services.
 
57
 *
 
58
 * @author  Martin Entlicher
 
59
 */
 
60
public abstract class StreamSource extends Object {
 
61
    
 
62
    /**
 
63
     * Get the name of the source.
 
64
     */
 
65
    public abstract String getName();
 
66
    
 
67
    /**
 
68
     * Get the title of the source.
 
69
     */
 
70
    public abstract String getTitle();
 
71
    
 
72
    /**
 
73
     * Get the MIME type of the source.
 
74
     */
 
75
    public abstract String getMIMEType();
 
76
    
 
77
    /**
 
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.
 
80
     * 
 
81
     * @return true if this source can be editable in the diff visualizer, false otherwise
 
82
     * @since 1.17
 
83
     */ 
 
84
    public boolean isEditable() {
 
85
        return false;
 
86
    }
 
87
 
 
88
    /**
 
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: 
 
91
     * <ul>
 
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.
 
97
     * </ul>
 
98
     * 
 
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.
 
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) throws IOException ;
 
121
    
 
122
    /**
 
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.
 
126
     */
 
127
    public void close() {
 
128
    }
 
129
    
 
130
    /**
 
131
     * Create the default implementation of <code>StreamSource</code>, that has
 
132
     * just reader and no writer.
 
133
     */
 
134
    public static StreamSource createSource(String name, String title, String MIMEType, Reader r) {
 
135
        return new Impl(name, title, MIMEType, r);
 
136
    }
 
137
    
 
138
    /**
 
139
     * Create the default implementation of <code>StreamSource</code>, that has
 
140
     * just reader and writer from/to a file.
 
141
     */
 
142
    public static StreamSource createSource(String name, String title, String MIMEType, File file) {
 
143
        return new Impl(name, title, MIMEType, file);
 
144
    }
 
145
    
 
146
    /**
 
147
     * Private implementation to be returned by the static methods.
 
148
     */
 
149
    private static class Impl extends StreamSource {
 
150
        
 
151
        private String name;
 
152
        private String title;
 
153
        private String MIMEType;
 
154
        private Reader r;
 
155
        private File readerSource;
 
156
        private Writer w;
 
157
        private File file;
 
158
        private Charset encoding;
 
159
        
 
160
        Impl(String name, String title, String MIMEType, Reader r) {
 
161
            this.name = name;
 
162
            this.title = title;
 
163
            this.MIMEType = MIMEType;
 
164
            this.r = r;
 
165
            this.readerSource = null;
 
166
            this.w = null;
 
167
            this.file = null;
 
168
            if (r instanceof InputStreamReader) {
 
169
                try {
 
170
                    encoding = Charset.forName(((InputStreamReader) r).getEncoding());
 
171
                } catch (UnsupportedCharsetException e) {
 
172
                    // ignore, encoding will be null
 
173
                }
 
174
            }
 
175
        }
 
176
        
 
177
        Impl(String name, String title, String MIMEType, File file) {
 
178
            this.name = name;
 
179
            this.title = title;
 
180
            this.MIMEType = MIMEType;
 
181
            this.readerSource = null;
 
182
            this.w = null;
 
183
            this.file = file;
 
184
            encoding = FileEncodingQuery.getEncoding(FileUtil.toFileObject(file));
 
185
        }
 
186
        
 
187
        private File createReaderSource(Reader r) throws IOException {
 
188
            File tmp = null;
 
189
            tmp = FileUtil.normalizeFile(File.createTempFile("sss", "tmp"));
 
190
            tmp.deleteOnExit();
 
191
            tmp.createNewFile();
 
192
            InputStream in = null;
 
193
            OutputStream out = null;
 
194
            try {
 
195
                if (encoding == null) {
 
196
                    in = new ReaderInputStream(r);
 
197
                } else {
 
198
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
199
                    copyStreamsCloseAll(new OutputStreamWriter(baos, encoding), r);
 
200
                    in = new ByteArrayInputStream(baos.toByteArray());
 
201
                }
 
202
                org.openide.filesystems.FileUtil.copy(in, out = new FileOutputStream(tmp));
 
203
            } finally {
 
204
                if (in != null) in.close();
 
205
                if (out != null) out.close();
 
206
            }
 
207
            return tmp;
 
208
        }
 
209
        
 
210
        public String getName() {
 
211
            return name;
 
212
        }
 
213
        
 
214
        public String getTitle() {
 
215
            return title;
 
216
        }
 
217
        
 
218
        public String getMIMEType() {
 
219
            return MIMEType;
 
220
        }
 
221
        
 
222
        public Reader createReader() throws IOException {
 
223
            if (file != null) {
 
224
                return new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
 
225
            } else {
 
226
                synchronized (this) {
 
227
                    if (r != null) {
 
228
                        readerSource = createReaderSource(r);
 
229
                        r = null;
 
230
                    }
 
231
                }
 
232
                if (encoding == null) {
 
233
                    return new BufferedReader(new FileReader(readerSource));
 
234
                } else {
 
235
                    return new BufferedReader(new InputStreamReader(new FileInputStream(readerSource), encoding));
 
236
                }
 
237
            }
 
238
        }
 
239
        
 
240
        public Writer createWriter(Difference[] conflicts) throws IOException {
 
241
            if (conflicts != null && conflicts.length > 0) return null;
 
242
            if (file != null) {
 
243
                if (encoding == null) {
 
244
                    return new BufferedWriter(new FileWriter(file));
 
245
                } else {
 
246
                    return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding));
 
247
                }
 
248
            } else return w;
 
249
        }
 
250
        
 
251
    }
 
252
    
 
253
    private static void copyStreamsCloseAll(Writer writer, Reader reader) throws IOException {
 
254
        char [] buffer = new char[4096];
 
255
        int n;
 
256
        while ((n = reader.read(buffer)) != -1) {
 
257
            writer.write(buffer, 0, n);
 
258
        }
 
259
        writer.close();
 
260
        reader.close();
 
261
    }
 
262
}