1 19 20 package org.netbeans.lib.cvsclient.command.diff; 21 22 import java.io.*; 23 24 import org.netbeans.lib.cvsclient.command.*; 25 import org.netbeans.lib.cvsclient.event.*; 26 27 32 public class SimpleDiffBuilder implements Builder { 33 34 37 protected EventManager eventManager; 38 39 protected DiffCommand diffCommand; 40 43 protected DiffInformation diffInformation; 44 45 49 protected String fileDirectory; 50 51 protected boolean readingDiffs = false; 52 private static final String UNKNOWN = ": I know nothing about"; private static final String CANNOT_FIND = ": cannot find"; private static final String UNKNOWN_TAG = ": tag"; private static final String EXAM_DIR = ": Diffing"; 57 private static final String FILE = "Index: "; private static final String RCS_FILE = "RCS file: "; private static final String REVISION = "retrieving revision "; private static final String PARAMETERS = "diff "; private DiffInformation.DiffChange currentChange; 62 63 public SimpleDiffBuilder(EventManager eventMan, DiffCommand diffComm) { 64 eventManager = eventMan; 65 diffCommand = diffComm; 66 } 67 68 public void outputDone() { 69 if (diffInformation != null) { 70 if (currentChange != null) { 71 diffInformation.addChange(currentChange); 72 currentChange = null; 73 } 74 eventManager.fireCVSEvent(new FileInfoEvent(this, diffInformation)); 75 diffInformation = null; 76 readingDiffs = false; 77 } 78 } 79 80 public void parseLine(String line, boolean isErrorMessage) { 81 if (readingDiffs) { 82 if (line.startsWith(FILE)) { 83 outputDone(); 84 } 85 else { 86 processDifferences(line); 87 return; 88 } 89 } 90 if (line.indexOf(UNKNOWN) >= 0) { 91 eventManager.fireCVSEvent(new FileInfoEvent(this, diffInformation)); 92 diffInformation = null; 93 return; 94 } 95 if (line.indexOf(EXAM_DIR) >= 0) { 96 fileDirectory = line.substring(line.indexOf(EXAM_DIR) + EXAM_DIR.length()).trim(); 97 return; 98 } 99 if (line.startsWith(FILE)) { 100 processFile(line.substring(FILE.length())); 101 return; 102 } 103 if (line.startsWith(RCS_FILE)) { 104 processRCSfile(line.substring(RCS_FILE.length())); 105 return; 106 } 107 if (line.startsWith(REVISION)) { 108 processRevision(line.substring(REVISION.length())); 109 return; 110 } 111 if (line.startsWith(PARAMETERS)) { 112 processParameters(line.substring(PARAMETERS.length())); 113 readingDiffs = true; 114 return; 115 } 116 } 117 118 122 protected void processFile(String line) { 123 outputDone(); 124 diffInformation = createDiffInformation(); 125 String fileName = line.trim(); 126 if (fileName.startsWith("no file")) { fileName = fileName.substring(8); 128 } 129 diffInformation.setFile(new File(diffCommand.getLocalDirectory(), 130 fileName)); 132 } 133 134 protected void processRCSfile(String line) { 135 if (diffInformation == null) { 136 return; 137 } 138 diffInformation.setRepositoryFileName(line.trim()); 139 } 140 141 protected void processRevision(String line) { 142 if (diffInformation == null) { 143 return; 144 } 145 line = line.trim(); 146 if (diffInformation.getLeftRevision() != null) { 148 diffInformation.setRightRevision(line); 149 } 150 else { 151 diffInformation.setLeftRevision(line); 152 } 153 } 154 155 protected void processParameters(String line) { 156 if (diffInformation == null) { 157 return; 158 } 159 diffInformation.setParameters(line.trim()); 160 } 161 162 public DiffInformation createDiffInformation() { 163 return new DiffInformation(); 164 } 165 166 protected void assignType(DiffInformation.DiffChange change, String line) { 167 int index = 0; 168 int cIndex = line.indexOf('c'); 169 if (cIndex > 0) { 170 change.setType(DiffInformation.DiffChange.CHANGE); 172 index = cIndex; 173 } 174 else { 175 int aIndex = line.indexOf('a'); 176 if (aIndex > 0) { 177 change.setType(DiffInformation.DiffChange.ADD); 179 index = aIndex; 180 } 181 else { 182 int dIndex = line.indexOf('d'); 183 if (dIndex > 0) { 184 change.setType(DiffInformation.DiffChange.DELETE); 186 index = dIndex; 187 } 188 } 189 } 190 String left = line.substring(0, index); 191 change.setLeftRange(getMin(left), getMax(left)); 193 String right = line.substring(index + 1); 194 change.setRightRange(getMin(right), getMax(right)); 196 } 197 198 private int getMin(String line) { 199 String nums = line; 200 int commaIndex = nums.indexOf(','); 201 if (commaIndex > 0) { 202 nums = nums.substring(0, commaIndex); 203 } 204 int min; 205 try { 206 min = Integer.parseInt(nums); 207 } 208 catch (NumberFormatException exc) { 209 min = 0; 210 } 211 return min; 213 } 214 215 private int getMax(String line) { 216 String nums = line; 217 int commaIndex = nums.indexOf(','); 218 if (commaIndex > 0) { 219 nums = nums.substring(commaIndex + 1); 220 } 221 int max; 222 try { 223 max = Integer.parseInt(nums); 224 } 225 catch (NumberFormatException exc) { 226 max = 0; 227 } 228 return max; 230 } 231 232 protected void processDifferences(String line) { 233 char firstChar = line.charAt(0); 234 if (firstChar >= '0' && firstChar <= '9') { 235 if (currentChange != null) { 238 diffInformation.addChange(currentChange); 239 } 240 currentChange = diffInformation.createDiffChange(); 241 assignType(currentChange, line); 242 } 243 if (firstChar == '<') { 244 currentChange.appendLeftLine(line.substring(2)); 246 } 247 if (firstChar == '>') { 248 currentChange.appendRightLine(line.substring(2)); 250 } 251 252 } 253 254 public void parseEnhancedMessage(String key, Object value) { 255 } 256 257 } 258 | Popular Tags |