KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
25 import org.netbeans.lib.cvsclient.command.*;
26 import org.netbeans.lib.cvsclient.connection.*;
27 import org.netbeans.lib.cvsclient.event.*;
28 import org.netbeans.lib.cvsclient.request.*;
29
30 /**
31  * The status command looks up the status of files in the repository
32  * @author Robert Greig
33  */

34 public class DiffCommand extends BasicCommand {
35     /**
36      * The event manager to use
37      */

38     protected EventManager eventManager;
39
40     /**
41      * Holds value of property beforeDate.
42      */

43     private String JavaDoc beforeDate1;
44
45     /**
46      * Holds value of property firstRevision.
47      */

48     private String JavaDoc revision1;
49
50     /**
51      * Holds value of property secondRevision.
52      */

53     private String JavaDoc revision2;
54
55     /**
56      * Holds value of property beforeDate2.
57      */

58     private String JavaDoc beforeDate2;
59
60     /**
61      * Keyword substitution. The -k switch in command line cvs.
62      */

63     private String JavaDoc keywordSubst;
64
65     /** Holds value of property ignoreAllWhitespace. */
66     private boolean ignoreAllWhitespace;
67
68     /** Holds value of property ignoreBlankLines. */
69     private boolean ignoreBlankLines;
70
71     /** Holds value of property ignoreCase. */
72     private boolean ignoreCase;
73
74     /** Holds value of property ignoreSpaceChange. */
75     private boolean ignoreSpaceChange;
76
77     /** Holds value of property contextDiff. */
78     private boolean contextDiff;
79
80     /** Holds value of property unifiedDiff. */
81     private boolean unifiedDiff;
82
83     /**
84      * Construct a new diff command
85      */

86     public DiffCommand() {
87     }
88
89     /**
90      * Create a builder for this command.
91      * @param eventMan the event manager used to receive events.
92      */

93     public Builder createBuilder(EventManager eventMan) {
94         if (isContextDiff() || isUnifiedDiff()) {
95             return null;
96         }
97         return new SimpleDiffBuilder(eventMan, this);
98     }
99
100     /**
101      * Execute a command
102      * @param client the client services object that provides any necessary
103      * services to this command, including the ability to actually process
104      * all the requests.
105      */

106     public void execute(ClientServices client, EventManager em)
107             throws CommandException, AuthenticationException {
108         client.ensureConnection();
109
110         eventManager = em;
111
112         super.execute(client, em);
113
114         try {
115             // parameters come now..
116
addRDSwitches();
117             if (getKeywordSubst() != null && !getKeywordSubst().equals("")) { //NOI18N
118
requests.add(new ArgumentRequest("-k" + getKeywordSubst())); //NOI18N
119
}
120
121             addArgumentRequest(isIgnoreAllWhitespace(), "-w"); //NOI18N
122
addArgumentRequest(isIgnoreBlankLines(), "-B"); //NOI18N
123
addArgumentRequest(isIgnoreSpaceChange(), "-b"); //NOI18N
124
addArgumentRequest(isIgnoreCase(), "-i"); //NOI18N
125
addArgumentRequest(isContextDiff(), "-c"); //NOI18N
126
addArgumentRequest(isUnifiedDiff(), "-u"); //NOI18N
127

128             addRequestForWorkingDirectory(client);
129             addArgumentRequests();
130             addRequest(CommandRequest.DIFF);
131             client.processRequests(requests);
132         }
133         catch (CommandException ex) {
134             throw ex;
135         }
136         catch (Exception JavaDoc ex) {
137             throw new CommandException(ex, ex.getLocalizedMessage());
138         }
139         finally {
140             requests.clear();
141         }
142     }
143
144     /**
145      * includes the logic of setting the -r and -D switches to the diff command
146      */

147     private void addRDSwitches() {
148         if (getRevision2() != null) {
149             requests.add(1, new ArgumentRequest("-r")); //NOI18N
150
requests.add(2, new ArgumentRequest(getRevision2()));
151         }
152         else {
153             if (getBeforeDate2() != null) {
154                 requests.add(1, new ArgumentRequest("-D " + getBeforeDate2())); //NOI18N
155
}
156         }
157         // -r switch has precendence over the -d switch - is that right??
158
if (getRevision1() != null) {
159             requests.add(1, new ArgumentRequest("-r")); //NOI18N
160
requests.add(2, new ArgumentRequest(getRevision1()));
161         }
162         else {
163             if (getBeforeDate1() != null) {
164                 requests.add(1, new ArgumentRequest("-D " + getBeforeDate1())); //NOI18N
165
}
166             else {
167                 // when neither revision nor flag is set for the command, it is assumed
168
// that the second parameters are not set either..
169
return;
170             }
171         }
172     }
173
174     /** called when server responses with "ok" or "error", (when the command finishes)
175      */

176     public void commandTerminated(TerminationEvent e) {
177         if (builder != null) {
178             builder.outputDone();
179         }
180     }
181
182     /** Getter for property beforeDate.
183      * @return Value of property beforeDate.
184      */

185     public String JavaDoc getBeforeDate1() {
186         return beforeDate1;
187     }
188
189     /** Setter for property beforeDate.
190      * @param beforeDate New value of property beforeDate.
191      */

192     public void setBeforeDate1(String JavaDoc beforeDate) {
193         this.beforeDate1 = beforeDate;
194     }
195
196     /** Getter for property firstRevision.
197      * @return Value of property firstRevision.
198      */

199     public String JavaDoc getRevision1() {
200         return revision1;
201     }
202
203     /** Setter for property firstRevision.
204      * @param firstRevision New value of property firstRevision.
205      */

206     public void setRevision1(String JavaDoc firstRevision) {
207         revision1 = firstRevision;
208     }
209
210     /** Getter for property secondRevision.
211      * @return Value of property secondRevision.
212      */

213     public String JavaDoc getRevision2() {
214         return revision2;
215     }
216
217     /** Setter for property secondRevision.
218      * @param secondRevision New value of property secondRevision.
219      */

220     public void setRevision2(String JavaDoc secondRevision) {
221         this.revision2 = secondRevision;
222     }
223
224     /** Getter for property beforeDate2.
225      * @return Value of property beforeDate2.
226      */

227     public String JavaDoc getBeforeDate2() {
228         return beforeDate2;
229     }
230
231     /** Setter for property beforeDate2.
232      * @param beforeDate2 New value of property beforeDate2.
233      */

234     public void setBeforeDate2(String JavaDoc beforeDate2) {
235         this.beforeDate2 = beforeDate2;
236     }
237
238     /**
239      * Getter for property keywordSubst.
240      * @return Value of property keywordSubst.
241      */

242     public String JavaDoc getKeywordSubst() {
243         return keywordSubst;
244     }
245
246     /**
247      * Setter for property keywordSubst.
248      * @param keywordSubst New value of property keywordSubst.
249      */

250     public void setKeywordSubst(String JavaDoc keywordSubst) {
251         this.keywordSubst = keywordSubst;
252     }
253
254     /** This method returns how the command would looklike when typed on the command line.
255      * Each command is responsible for constructing this information.
256      * @returns <command's name> [<parameters>] files/dirs. Example: checkout -p CvsCommand.java
257      *
258      */

259     public String JavaDoc getCVSCommand() {
260         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc("diff "); //NOI18N
261
toReturn.append(getCVSArguments());
262         File[] files = getFiles();
263         if (files != null) {
264             for (int index = 0; index < files.length; index++) {
265                 toReturn.append(files[index].getName() + " "); //NOI18N
266
}
267         }
268         return toReturn.toString();
269     }
270
271     /** takes the arguments and sets the command. To be mainly
272      * used for automatic settings (like parsing the .cvsrc file)
273      * @return true if the option (switch) was recognized and set
274      */

275     public boolean setCVSCommand(char opt, String JavaDoc optArg) {
276         if (opt == 'R') {
277             setRecursive(true);
278         }
279         else if (opt == 'l') {
280             setRecursive(false);
281         }
282         else if (opt == 'r') {
283             if (getRevision1() == null) {
284                 setRevision1(optArg);
285             }
286             else {
287                 setRevision2(optArg);
288             }
289         }
290         else if (opt == 'D') {
291             if (getBeforeDate1() == null) {
292                 setBeforeDate1(optArg);
293             }
294             else {
295                 setBeforeDate2(optArg);
296             }
297         }
298         else if (opt == 'k') {
299             setKeywordSubst(optArg);
300         }
301         else if (opt == 'w') {
302             setIgnoreAllWhitespace(true);
303         }
304         else if (opt == 'b') {
305             setIgnoreSpaceChange(true);
306         }
307         else if (opt == 'B') {
308             setIgnoreBlankLines(true);
309         }
310         else if (opt == 'i') {
311             setIgnoreCase(true);
312         }
313         else if (opt == 'c') {
314             setContextDiff(true);
315         }
316         else if (opt == 'u') {
317             setUnifiedDiff(true);
318         }
319         else {
320             return false;
321         }
322         return true;
323     }
324
325     /**
326      * String returned by this method defines which options are available for this particular command
327      */

328     public String JavaDoc getOptString() {
329         return "Rlr:D:k:wBbicu"; //NOI18N
330
}
331
332     /**
333      * resets all switches in the command. After calling this method,
334      * the command should have no switches defined and should behave defaultly.
335      */

336     public void resetCVSCommand() {
337         setRecursive(true);
338         setRevision1(null);
339         setRevision2(null);
340         setBeforeDate1(null);
341         setBeforeDate2(null);
342         setKeywordSubst(null);
343         setIgnoreAllWhitespace(false);
344         setIgnoreBlankLines(false);
345         setIgnoreCase(false);
346         setIgnoreSpaceChange(false);
347         setContextDiff(false);
348         setUnifiedDiff(false);
349     }
350
351     /**
352      * Returns the arguments of the command in the command-line style.
353      * Similar to getCVSCommand() however without the files and command's name
354      */

355     public String JavaDoc getCVSArguments() {
356         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc(""); //NOI18N
357
if (getKeywordSubst() != null && getKeywordSubst().length() > 0) {
358             toReturn.append("-k" + getKeywordSubst() + " "); //NOI18N
359
}
360         if (!isRecursive()) {
361             toReturn.append("-l "); //NOI18N
362
}
363         if (getRevision1() != null) {
364             toReturn.append("-r " + getRevision1() + " "); //NOI18N
365
}
366         if (getBeforeDate1() != null) {
367             toReturn.append("-D " + getBeforeDate1() + " "); //NOI18N
368
}
369         if (getRevision2() != null) {
370             toReturn.append("-r " + getRevision2() + " "); //NOI18N
371
}
372         if (getBeforeDate2() != null) {
373             toReturn.append("-D " + getBeforeDate2() + " "); //NOI18N
374
}
375         if (isIgnoreAllWhitespace()) {
376             toReturn.append("-w "); //NOI18N
377
}
378         if (isIgnoreBlankLines()) {
379             toReturn.append("-B "); //NOI18N
380
}
381         if (isIgnoreCase()) {
382             toReturn.append("-i "); //NOI18N
383
}
384         if (isIgnoreSpaceChange()) {
385             toReturn.append("-b "); //NOI18N
386
}
387         if (isContextDiff()) {
388             toReturn.append("-c ");//NOI18N
389
}
390         if (isUnifiedDiff()) {
391             toReturn.append("-u ");//NOI18N
392
}
393         return toReturn.toString();
394     }
395
396     /** true if all the whitespace differences should be ignored. (-w)
397      * @return Value of property ignoreAllWhitespace.
398      */

399     public boolean isIgnoreAllWhitespace() {
400         return this.ignoreAllWhitespace;
401     }
402
403     /** Setter for property ignoreAllWhitespace.
404      * true if all the whitespace differences should be ignored. (-w)
405      * @param ignoreAllWhitespace New value of property ignoreAllWhitespace.
406      */

407     public void setIgnoreAllWhitespace(boolean ignoreAllWhitespace) {
408         this.ignoreAllWhitespace = ignoreAllWhitespace;
409     }
410
411     /** Getter for property ignoreBlankLines.
412      * @return Value of property ignoreBlankLines.
413      */

414     public boolean isIgnoreBlankLines() {
415         return this.ignoreBlankLines;
416     }
417
418     /** Setter for property ignoreBlankLines.
419      * @param ignoreBlankLines New value of property ignoreBlankLines.
420      */

421     public void setIgnoreBlankLines(boolean ignoreBlankLines) {
422         this.ignoreBlankLines = ignoreBlankLines;
423     }
424
425     /** Getter for property ignoreCase.
426      * @return Value of property ignoreCase.
427      */

428     public boolean isIgnoreCase() {
429         return this.ignoreCase;
430     }
431
432     /** Setter for property ignoreCase.
433      * @param ignoreCase New value of property ignoreCase.
434      */

435     public void setIgnoreCase(boolean ignoreCase) {
436         this.ignoreCase = ignoreCase;
437     }
438
439     /** Getter for property ignoreSpaceChange.
440      * @return Value of property ignoreSpaceChange.
441      */

442     public boolean isIgnoreSpaceChange() {
443         return this.ignoreSpaceChange;
444     }
445
446     /** Setter for property ignoreSpaceChange.
447      * @param ignoreSpaceChange New value of property ignoreSpaceChange.
448      */

449     public void setIgnoreSpaceChange(boolean ignoreSpaceChange) {
450         this.ignoreSpaceChange = ignoreSpaceChange;
451     }
452
453     /**
454      * equals to the -c switch of cvs
455      * Getter for property contextDiff.
456      * @return Value of property contextDiff.
457      */

458     public boolean isContextDiff() {
459         return this.contextDiff;
460     }
461
462     /**
463      * equals to the -c switch of cvs
464      * Setter for property contextDiff.
465      * @param contextDiff New value of property contextDiff.
466      */

467     public void setContextDiff(boolean contextDiff) {
468         this.contextDiff = contextDiff;
469     }
470
471     /**
472      * equals to the -u switch of cvs
473      * Getter for property unifiedDiff.
474      * @return Value of property unifiedDiff.
475      */

476     public boolean isUnifiedDiff() {
477         return this.unifiedDiff;
478     }
479
480     /**
481      * equals to the -u switch of cvs.
482      * Setter for property unifiedDiff.
483      * @param unifiedDiff New value of property unifiedDiff.
484      */

485     public void setUnifiedDiff(boolean unifiedDiff) {
486         this.unifiedDiff = unifiedDiff;
487     }
488
489 }
490
Popular Tags