KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > diff > SimpleDiffBuilder


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
28  * Handles the building of a diff information object and the firing of
29  * events when complete objects are built.
30  * @author Milos Kleint
31  */

32 public class SimpleDiffBuilder implements Builder {
33
34     /**
35      * The event manager to use
36      */

37     protected EventManager eventManager;
38
39     protected DiffCommand diffCommand;
40     /**
41      * The diff object that is currently being built
42      */

43     protected DiffInformation diffInformation;
44
45     /**
46      * The directory in which the file being processed lives. This is
47      * relative to the local directory
48      */

49     protected String JavaDoc fileDirectory;
50
51     protected boolean readingDiffs = false;
52     private static final String JavaDoc UNKNOWN = ": I know nothing about"; //NOI18N
53
private static final String JavaDoc CANNOT_FIND = ": cannot find"; //NOI18N
54
private static final String JavaDoc UNKNOWN_TAG = ": tag"; //NOI18N
55
private static final String JavaDoc EXAM_DIR = ": Diffing"; //NOI18N
56

57     private static final String JavaDoc FILE = "Index: "; //NOI18N
58
private static final String JavaDoc RCS_FILE = "RCS file: "; //NOI18N
59
private static final String JavaDoc REVISION = "retrieving revision "; //NOI18N
60
private static final String JavaDoc PARAMETERS = "diff "; //NOI18N
61
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 JavaDoc 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 /* protected void processDifferences(String line) {
119             diffInformation.addToDifferences(line);
120         }
121  */

122     protected void processFile(String JavaDoc line) {
123         outputDone();
124         diffInformation = createDiffInformation();
125         String JavaDoc fileName = line.trim();
126         if (fileName.startsWith("no file")) { //NOI18N
127
fileName = fileName.substring(8);
128         }
129         diffInformation.setFile(new File(diffCommand.getLocalDirectory(),
130 // ((fileDirectory!=null)?fileDirectory: "") + File.separator +
131
fileName));
132     }
133
134     protected void processRCSfile(String JavaDoc line) {
135         if (diffInformation == null) {
136             return;
137         }
138         diffInformation.setRepositoryFileName(line.trim());
139     }
140
141     protected void processRevision(String JavaDoc line) {
142         if (diffInformation == null) {
143             return;
144         }
145         line = line.trim();
146         // first REVISION line is the from-file, the second is the to-file
147
if (diffInformation.getLeftRevision() != null) {
148             diffInformation.setRightRevision(line);
149         }
150         else {
151             diffInformation.setLeftRevision(line);
152         }
153     }
154
155     protected void processParameters(String JavaDoc 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 JavaDoc line) {
167         int index = 0;
168         int cIndex = line.indexOf('c');
169         if (cIndex > 0) {
170             // change type of change
171
change.setType(DiffInformation.DiffChange.CHANGE);
172             index = cIndex;
173         }
174         else {
175             int aIndex = line.indexOf('a');
176             if (aIndex > 0) {
177                 // add type of change
178
change.setType(DiffInformation.DiffChange.ADD);
179                 index = aIndex;
180             }
181             else {
182                 int dIndex = line.indexOf('d');
183                 if (dIndex > 0) {
184                     // delete type of change
185
change.setType(DiffInformation.DiffChange.DELETE);
186                     index = dIndex;
187                 }
188             }
189         }
190         String JavaDoc left = line.substring(0, index);
191 // System.out.println("left part of change=" + left);
192
change.setLeftRange(getMin(left), getMax(left));
193         String JavaDoc right = line.substring(index + 1);
194 // System.out.println("right part of change=" + right);
195
change.setRightRange(getMin(right), getMax(right));
196     }
197
198     private int getMin(String JavaDoc line) {
199         String JavaDoc 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 JavaDoc exc) {
209             min = 0;
210         }
211 // System.out.println("Min=" + min);
212
return min;
213     }
214
215     private int getMax(String JavaDoc line) {
216         String JavaDoc 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 JavaDoc exc) {
226             max = 0;
227         }
228 // System.out.println("Max=" + max);
229
return max;
230     }
231
232     protected void processDifferences(String JavaDoc line) {
233         char firstChar = line.charAt(0);
234         if (firstChar >= '0' && firstChar <= '9') {
235             // we got a new difference here
236
// System.out.println("new Change=" + line);
237
if (currentChange != null) {
238                 diffInformation.addChange(currentChange);
239             }
240             currentChange = diffInformation.createDiffChange();
241             assignType(currentChange, line);
242         }
243         if (firstChar == '<') {
244 // System.out.println("Left line=" + line);
245
currentChange.appendLeftLine(line.substring(2));
246         }
247         if (firstChar == '>') {
248 // System.out.println("right line=" + line);
249
currentChange.appendRightLine(line.substring(2));
250         }
251
252     }
253
254     public void parseEnhancedMessage(String JavaDoc key, Object JavaDoc value) {
255     }
256
257 }
258
Popular Tags