1 19 20 package org.netbeans.api.diff; 21 22 import java.io.BufferedReader ; 23 import java.io.BufferedWriter ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.FileReader ; 28 import java.io.FileWriter ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.io.OutputStream ; 32 import java.io.OutputStreamWriter ; 33 import java.io.Reader ; 34 import java.io.Writer ; 35 import java.io.IOException ; 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 50 public abstract class StreamSource extends Object { 51 52 55 public abstract String getName(); 56 57 60 public abstract String getTitle(); 61 62 65 public abstract String getMIMEType(); 66 67 74 public boolean isEditable() { 75 return false; 76 } 77 78 95 public Lookup getLookup() { 96 return Lookups.fixed(); 97 } 98 99 102 public abstract Reader createReader() throws IOException ; 103 104 110 public abstract Writer createWriter(Difference[] conflicts) throws IOException ; 111 112 117 public void close() { 118 } 119 120 124 public static StreamSource createSource(String name, String title, String MIMEType, Reader r) { 125 return new Impl(name, title, MIMEType, r); 126 } 127 128 132 public static StreamSource createSource(String name, String title, String MIMEType, File file) { 133 return new Impl(name, title, MIMEType, file); 134 } 135 136 139 private static class Impl extends StreamSource { 140 141 private String name; 142 private String title; 143 private String MIMEType; 144 private Reader r; 145 private File readerSource; 146 private Writer w; 147 private File file; 148 private String encoding; 149 150 Impl(String name, String title, String MIMEType, Reader 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 ) { 159 encoding = ((InputStreamReader ) r).getEncoding(); 160 } 161 } 162 163 Impl(String name, String title, String MIMEType, File 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 createReaderSource(Reader r) throws IOException { 174 File tmp = null; 175 tmp = FileUtil.normalizeFile(File.createTempFile("sss", "tmp")); 176 tmp.deleteOnExit(); 177 tmp.createNewFile(); 178 InputStream in = null; 179 OutputStream 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 (tmp)); 187 } finally { 188 if (in != null) in.close(); 189 if (out != null) out.close(); 190 } 191 return tmp; 192 } 193 194 public String getName() { 195 return name; 196 } 197 198 public String getTitle() { 199 return title; 200 } 201 202 public String getMIMEType() { 203 return MIMEType; 204 } 205 206 public Reader createReader() throws IOException { 207 if (file != null) { 208 return new BufferedReader (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 (new FileReader (readerSource)); 218 } else { 219 return new BufferedReader (new InputStreamReader (new FileInputStream (readerSource), encoding)); 220 } 221 } 222 } 223 224 public Writer createWriter(Difference[] conflicts) throws IOException { 225 if (conflicts != null && conflicts.length > 0) return null; 226 if (file != null) { 227 if (encoding == null) { 228 return new BufferedWriter (new FileWriter (file)); 229 } else { 230 return new BufferedWriter (new OutputStreamWriter (new FileOutputStream (file), encoding)); 231 } 232 } else return w; 233 } 234 235 } 236 } 237 | Popular Tags |