1 19 20 package org.netbeans.modules.versioning.system.cvss.ui.actions.diff; 21 22 import org.netbeans.api.diff.StreamSource; 23 import org.netbeans.api.diff.Difference; 24 import org.netbeans.modules.versioning.system.cvss.*; 25 import org.netbeans.modules.versioning.util.Utils; 26 import org.netbeans.modules.diff.EncodedReaderFactory; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.FileUtil; 29 30 import java.io.*; 31 import java.util.*; 32 33 import org.openide.util.*; 34 import org.openide.util.lookup.Lookups; 35 import org.openide.loaders.DataObject; 36 import org.openide.loaders.DataObjectNotFoundException; 37 38 43 public class DiffStreamSource extends StreamSource { 44 45 private final File baseFile; 46 private final String revision; 47 private final String title; 48 private String mimeType = "text/plain"; 50 53 private File remoteFile; 54 private boolean binary; 55 56 private ExecutorGroup group; 57 private boolean initialized; 58 59 66 public DiffStreamSource(File baseFile, String revision, String title) { 67 this.baseFile = baseFile; 68 this.revision = revision; 69 this.title = title; 70 } 71 72 public String getName() { 73 return baseFile.getName(); 74 } 75 76 public String getTitle() { 77 return title; 78 } 79 80 public synchronized void setGroup(ExecutorGroup group) { 81 this.group = group; 82 } 83 84 public synchronized String getMIMEType() { 85 try { 86 init(null); 87 } catch (IOException e) { 88 return null; 89 } 90 return mimeType; 91 } 92 93 public synchronized Reader createReader() throws IOException { 94 init(group); 95 if (revision == null || remoteFile == null) return null; 96 if (binary) { 97 return new StringReader(NbBundle.getMessage(DiffStreamSource.class, "BK5001", getTitle())); 98 } else { 99 FileObject remoteFo = FileUtil.toFileObject(remoteFile); 100 return EncodedReaderFactory.getDefault().getReader(remoteFo, null); 101 } 102 } 103 104 public Writer createWriter(Difference[] conflicts) throws IOException { 105 throw new IOException("Operation not supported"); } 107 108 public boolean isEditable() { 109 return VersionsCache.REVISION_CURRENT.equals(revision) && isPrimary(); 110 } 111 112 private boolean isPrimary() { 113 FileObject fo = FileUtil.toFileObject(baseFile); 114 if (fo != null) { 115 try { 116 DataObject dao = DataObject.find(fo); 117 return fo.equals(dao.getPrimaryFile()); 118 } catch (DataObjectNotFoundException e) { 119 } 121 } 122 return true; 123 } 124 125 public synchronized Lookup getLookup() { 126 try { 127 init(null); 128 } catch (IOException e) { 129 return Lookups.fixed(); 130 } 131 if (remoteFile == null || !isPrimary()) return Lookups.fixed(); 132 FileObject remoteFo = FileUtil.toFileObject(remoteFile); 133 if (remoteFo == null) return Lookups.fixed(); 134 135 return Lookups.fixed(remoteFo); 136 } 137 138 144 synchronized void init(ExecutorGroup group) throws IOException { 145 if (initialized) return; 146 initialized = true; 147 if (revision == null) return; 148 binary = !CvsVersioningSystem.getInstance().isText(baseFile); 149 try { 150 if (isEditable()) { 151 remoteFile = VersionsCache.getInstance().getRemoteFile(baseFile, revision, group); 153 } else { 154 File tempFolder = Utils.getTempFolder(); 155 Set<File> allFiles = Utils.getAllDataObjectFiles(baseFile); 159 for (File file : allFiles) { 160 boolean isBase = file.equals(baseFile); 161 try { 162 File rf = VersionsCache.getInstance().getRemoteFile(file, revision, group); 163 File newRemoteFile = new File(tempFolder, file.getName()); 164 Utils.copyStreamsCloseAll(new FileOutputStream(newRemoteFile), new FileInputStream(rf)); 165 newRemoteFile.deleteOnExit(); 166 if (isBase) { 167 remoteFile = newRemoteFile; 168 } 169 } catch (Exception e) { 170 if (isBase) throw e; 171 } 173 } 174 } 175 if (!baseFile.exists() && remoteFile != null && remoteFile.exists()) { 176 binary = !CvsVersioningSystem.getInstance().isText(remoteFile); 177 } 178 } catch (Exception e) { 179 IOException failure = new IOException("Cannot initialize stream source"); failure.initCause(e); 181 throw failure; 182 } 183 FileObject fo = FileUtil.toFileObject(baseFile); 184 if (fo == null && remoteFile != null) { 185 fo = FileUtil.toFileObject(remoteFile); 186 } 187 if (fo != null) { 188 mimeType = fo.getMIMEType(); 189 } else if (binary) { 190 mimeType = "application/octet-stream"; } else { 192 mimeType = "text/plain"; } 194 } 195 } 196 | Popular Tags |