1 19 20 package org.netbeans.api.retouche.source; 21 22 import java.io.*; 23 import java.util.*; 24 import javax.swing.text.BadLocationException ; 25 import javax.swing.text.Document ; 26 27 import org.openide.cookies.EditorCookie; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileUtil; 30 import org.openide.loaders.DataObject; 31 import org.openide.text.PositionRef; 32 33 43 public final class ModificationResult { 44 45 Map<FileObject, List<Difference>> diffs = new HashMap<FileObject, List<Difference>>(); 46 47 48 ModificationResult() { 49 } 50 51 53 public Set<? extends FileObject> getModifiedFileObjects() { 54 return diffs.keySet(); 55 } 56 57 public List<? extends Difference> getDifferences(FileObject fo) { 58 return diffs.get(fo); 59 } 60 61 65 public void commit() throws IOException { 66 for (Map.Entry<FileObject, List<Difference>> me : diffs.entrySet()) { 67 FileObject fo = me.getKey(); 68 DataObject dObj = DataObject.find(fo); 69 EditorCookie ec = dObj != null ? (EditorCookie) dObj.getCookie(EditorCookie.class) : null; 70 if (ec != null) { 71 Document doc = ec.getDocument(); 72 if (doc != null) { 73 for (Difference diff : me.getValue()) { 74 if (diff.isExcluded()) 75 continue; 76 try { 77 switch (diff.getKind()) { 78 case INSERT: 79 doc.insertString(diff.getStartPosition().getOffset(), diff.getNewText(), null); 80 break; 81 case REMOVE: 82 doc.remove(diff.getStartPosition().getOffset(), diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset()); 83 break; 84 case CHANGE: 85 doc.remove(diff.getStartPosition().getOffset(), diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset()); 86 doc.insertString(diff.getStartPosition().getOffset(), diff.getNewText(), null); 87 break; 88 } 89 } catch (BadLocationException ex) { 90 IOException ioe = new IOException(); 91 ioe.initCause(ex); 92 throw ioe; 93 } 94 } 95 continue; 96 } 97 } 98 InputStream ins = null; 99 ByteArrayOutputStream baos = null; 100 Reader in = null; 101 Writer out = null; 102 try { 103 ins = fo.getInputStream(); 104 baos = new ByteArrayOutputStream(); 105 FileUtil.copy(ins, baos); 106 107 ins.close(); 108 ins = null; 109 byte[] arr = baos.toByteArray(); 110 baos.close(); 111 baos = null; 112 in = new InputStreamReader(new ByteArrayInputStream(arr)); 113 out = new OutputStreamWriter(fo.getOutputStream()); 114 int offset = 0; 115 for (Difference diff : me.getValue()) { 116 if (diff.isExcluded()) 117 continue; 118 int pos = diff.getStartPosition().getOffset(); 119 char[] buff = new char[pos - offset]; 120 int n; 121 if ((n = in.read(buff)) > 0) { 122 out.write(buff, 0, n); 123 offset += n; 124 } 125 switch (diff.getKind()) { 126 case INSERT: 127 out.write(diff.getNewText()); 128 break; 129 case REMOVE: 130 int len = diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset(); 131 in.skip(len); 132 offset += len; 133 break; 134 case CHANGE: 135 len = diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset(); 136 in.skip(len); 137 offset += len; 138 out.write(diff.getNewText()); 139 break; 140 } 141 } 142 char[] buff = new char[1024]; 143 int n; 144 while ((n = in.read(buff)) > 0) 145 out.write(buff, 0, n); 146 } finally { 147 if (ins != null) 148 ins.close(); 149 if (baos != null) 150 baos.close(); 151 if (in != null) 152 in.close(); 153 if (out != null) 154 out.close(); 155 } 156 } 157 } 158 159 public static final class Difference { 160 Kind kind; 161 PositionRef startPos; 162 PositionRef endPos; 163 String oldText; 164 String newText; 165 private boolean excluded; 166 167 Difference(Kind kind, PositionRef startPos, PositionRef endPos, String oldText, String newText) { 168 this.kind = kind; 169 this.startPos = startPos; 170 this.endPos = endPos; 171 this.oldText = oldText; 172 this.newText = newText; 173 this.excluded = false; 174 } 175 176 public Kind getKind() { 177 return kind; 178 } 179 180 public PositionRef getStartPosition() { 181 return startPos; 182 } 183 184 public PositionRef getEndPosition() { 185 return endPos; 186 } 187 188 public String getOldText() { 189 return oldText; 190 } 191 192 public String getNewText() { 193 return newText; 194 } 195 196 public boolean isExcluded() { 197 return excluded; 198 } 199 200 public void exclude(boolean b) { 201 excluded = b; 202 } 203 204 public String toString() { 205 return kind + "<" + startPos.getOffset() + ", " + endPos.getOffset() + ">: " + oldText + " -> " + newText; 206 } 207 208 public static enum Kind { 209 INSERT, 210 REMOVE, 211 CHANGE 212 } 213 } 214 } 215 | Popular Tags |