KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > GlobalOptions


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 the CVS Client Library.
16  * The Initial Developer of the Original Software is Robert Greig.
17  * Portions created by Robert Greig are Copyright (C) 2000.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Robert Greig.
21  *****************************************************************************/

22 package org.netbeans.lib.cvsclient.command;
23
24 import java.io.File JavaDoc;
25 import java.util.*;
26
27 import org.netbeans.lib.cvsclient.request.*;
28
29 /**
30  * Provides access to global options for a specific command.
31  * These are options traditionally set in the command line CVS tool before the
32  * command name, for example in the command:
33  * <pre>cvs -n update -dP</pre>
34  * -n is a global options but -dP are options specific to the update command.
35  *
36  * <br>Note that you can have different global options for each command you
37  * execute (just like command-line CVS).
38  *
39  * @author Robert Greig
40  */

41 public class GlobalOptions implements Cloneable JavaDoc {
42
43     private List variables;
44     /**
45      * Determines whether no changes should be done to the local files.
46      * This is useful to request files, that would be updated.
47      */

48     private boolean doNoChanges;
49
50     /**
51      * Whether to make checked out files read only (read/write is the default).
52      */

53     private boolean checkedOutFilesReadOnly;
54
55     /**
56      * The CVS root to use.
57      */

58     private String JavaDoc cvsRoot;
59
60     /**
61      * Whether to use Gzip-compression.
62      */

63     private boolean useGzip = true;
64     
65     /**
66      * The gzip compression level.
67      */

68     private int compressionLevel = 0;
69
70     /**
71      * Supresses logging of the command in CVSROOT/history in the repository.
72      */

73     private boolean noHistoryLogging;
74
75     /**
76      * The cvs gets more quiet than without this switch.
77      * However it still prints the important stuff.
78      * Note: If this switch is used, the Builder stuff and parsing of the output
79      * might break.
80      */

81     private boolean moderatelyQuiet;
82
83     /**
84      * Is even more quiet.
85      * Commands which primary function is to send info, still do print something
86      * (diff, etc.), however other command are completely quiet.
87      * Note: If this switch is used, the Builder stuff and parsing of the output
88      * will break.
89      */

90     private boolean veryQuiet;
91
92     /**
93      * Traces the execution of the command. Useful for tracing down what is
94      * causing problems.
95      * Note: If this switch is used, the Builder stuff and parsing of the output
96      * might break.
97      */

98     private boolean traceExecution;
99     
100     /**
101      * Whether a help information should be displayed - usage of the command.
102      */

103     private boolean showHelp;
104
105     /**
106      * Whether a version information should be displayed.
107      */

108     private boolean showVersion;
109     
110     /**
111      * Whether to use ~/.cvsrc file or ignore it.
112      */

113     private boolean ignoreCvsrc;
114     
115     /**
116      * The directory that is used for temporary files.
117      */

118     private File JavaDoc tempDir;
119     
120     /**
121      * The editor, that is used to edit the commit message.
122      */

123     private String JavaDoc editor;
124
125     /**
126      * Explicitly lists files/folders that must be left intact by the command.
127      */

128     private File JavaDoc[] exclusions;
129     
130     public GlobalOptions() {
131         variables = new ArrayList();
132     }
133     
134     /**
135      * Sets list of non-modifiable files/folders. The client will effectively ignore all server responses
136      * that concern these files/folders or files beneath them.
137      *
138      * @param exclusions array of non-modifiable files/folders
139      */

140     public void setExclusions(File JavaDoc[] exclusions) {
141         this.exclusions = exclusions;
142     }
143
144     /**
145      * Returns list of non-modifiable files/folders. The client effectively ignores all server responses
146      * that concern these files/folders or files beneath them.
147      *
148      * @return File [] list of files/folders that must be left intact by the command.
149      */

150     public File JavaDoc[] getExclusions() {
151         return exclusions;
152     }
153
154     /**
155      * Tests whether the file is not modifiable as set by {@link #setExclusions(java.io.File[])}.
156      *
157      * @param file file to test
158      * @return true if the file must not be modified on disk, false otherwise
159      */

160     public boolean isExcluded(File JavaDoc file) {
161         if (exclusions != null) {
162             for (int i = 0; i < exclusions.length; i++) {
163                 if (isParentOrEqual(exclusions[i], file)) return true;
164             }
165         }
166         return false;
167     }
168     
169     /**
170      * Tests parent/child relationship of files.
171      *
172      * @param parent file to be parent of the second parameter
173      * @param file file to be a child of the first parameter
174      * @return true if the second parameter represents the same file as the first parameter OR is its descendant (child)
175      */

176     private static boolean isParentOrEqual(File JavaDoc parent, File JavaDoc file) {
177         for (; file != null; file = file.getParentFile()) {
178             if (file.equals(parent)) return true;
179         }
180         return false;
181     }
182     
183     /**
184      * Creates a list of requests.
185      * Only those global options are included that can be sent to server (-q,
186      * -Q, -l, -t, -r, -n).
187      * To be added to the request list sent to the server.
188      */

189     public List createRequestList() {
190         List requestList = new LinkedList();
191         if (variables.size() > 0) {
192             Iterator it = variables.iterator();
193             while (it.hasNext()) {
194                 String JavaDoc keyValue = it.next().toString();
195                 requestList.add(new SetRequest(keyValue));
196             }
197         }
198         if (isNoHistoryLogging()) {
199             requestList.add(new GlobalOptionRequest("-l")); //NOI18N
200
}
201         if (isDoNoChanges()) {
202             requestList.add(new GlobalOptionRequest("-n")); //NOI18N
203
}
204         if (isModeratelyQuiet()) {
205             requestList.add(new GlobalOptionRequest("-q")); //NOI18N
206
}
207         if (isVeryQuiet()) {
208             requestList.add(new GlobalOptionRequest("-Q")); //NOI18N
209
}
210         if (isTraceExecution()) {
211             requestList.add(new GlobalOptionRequest("-t")); //NOI18N
212
}
213         return requestList;
214     }
215
216     /**
217      * Returns a String that defines which options are available for global
218      * options.
219      */

220     public String JavaDoc getOptString() {
221         return "Hvnfd:lqQtrws:z:T:e:"; //NOI18N
222
}
223
224     /**
225      * EQUALS to Command.setCVSCommand()
226      */

227     public boolean setCVSCommand(char opt, String JavaDoc optArg) {
228         if (opt == 'n') {
229             setDoNoChanges(true);
230         }
231         else if (opt == 'd') {
232             setCVSRoot(optArg);
233         }
234         else if (opt == 'l') {
235             setNoHistoryLogging(true);
236         }
237         else if (opt == 'q') {
238             setModeratelyQuiet(true);
239         }
240         else if (opt == 'Q') {
241             setVeryQuiet(true);
242         }
243         else if (opt == 't') {
244             setTraceExecution(true);
245         }
246         else if (opt == 't') {
247             setTraceExecution(true);
248         }
249         else if (opt == 'r') {
250             setCheckedOutFilesReadOnly(true);
251         }
252         else if (opt == 'w') {
253             setCheckedOutFilesReadOnly(false);
254         }
255         else if (opt == 's') {
256             setCvsVariable(optArg);
257         }
258         else if (opt == 'z') {
259             try {
260                 setCompressionLevel(Integer.parseInt(optArg));
261             } catch (NumberFormatException JavaDoc nfex) {
262                 
263             }
264         }
265         else if (opt == 'H') {
266             setShowHelp(true);
267         }
268         else if (opt == 'v') {
269             setShowVersion(true);
270         }
271         else if (opt == 'f') {
272             setIgnoreCvsrc(true);
273         }
274         else if (opt == 'T') {
275             setTempDir(new File JavaDoc(optArg));
276         }
277         else if (opt == 'e') {
278             setEditor(optArg);
279         }
280         else {
281             return false;
282         }
283         return true;
284     }
285
286     /**
287      * Resets all switches in the command to the default behaviour.
288      * After calling this method, the command should behave defaultly.
289      * EQUALS to Command.resetCVSCommand()
290      */

291     public void resetCVSCommand() {
292         setCheckedOutFilesReadOnly(false);
293         setDoNoChanges(false);
294         setModeratelyQuiet(false);
295         setNoHistoryLogging(false);
296         setTraceExecution(false);
297         setUseGzip(true);
298         setCompressionLevel(0);
299         setVeryQuiet(false);
300         setShowHelp(false);
301         setShowVersion(false);
302         setIgnoreCvsrc(false);
303         setTempDir(null);
304         setEditor(null);
305         setCVSRoot("");
306         clearCvsVariables();
307     }
308
309     /**
310      * Equals to the Command.getCVSCommand() functionality.
311      * Returns all the current switches in the command-line cvs style.
312      */

313     public String JavaDoc getCVSCommand() {
314         StringBuffer JavaDoc switches = new StringBuffer JavaDoc();
315         if (isDoNoChanges()) {
316             switches.append("-n "); //NOI18N
317
}
318         if (isNoHistoryLogging()) {
319             switches.append("-l "); //NOI18N
320
}
321         if (isModeratelyQuiet()) {
322             switches.append("-q "); //NOI18N
323
}
324         if (isVeryQuiet()) {
325             switches.append("-Q "); //NOI18N
326
}
327         if (isTraceExecution()) {
328             switches.append("-t "); //NOI18N
329
}
330         if (isCheckedOutFilesReadOnly()) {
331             switches.append("-r "); //NOI18N
332
}
333         if (variables.size() > 0) {
334             Iterator it = variables.iterator();
335             while (it.hasNext()) {
336                 String JavaDoc keyValue = it.next().toString();
337                 switches.append("-s " + keyValue + " "); //NOI18N
338
}
339         }
340         if (compressionLevel != 0) {
341             switches.append("-z ");
342             switches.append(Integer.toString(compressionLevel));
343             switches.append(" ");
344         }
345         if (isIgnoreCvsrc()) {
346             switches.append("-f ");
347         }
348         if (tempDir != null) {
349             switches.append("-T ");
350             switches.append(tempDir.getAbsolutePath());
351             switches.append(" ");
352         }
353         if (editor != null) {
354             switches.append("-e ");
355             switches.append(editor);
356             switches.append(" ");
357         }
358         return switches.toString();
359     }
360
361     /**
362      * Adds one cvs internal enviroment variable.
363      * @param variable The format is NAME=VALUE.
364      */

365     public void setCvsVariable(String JavaDoc variable) {
366         variables.add(variable);
367     }
368
369     /**
370      * Clears the list of cvs internal enviroment variables.
371      */

372
373     public void clearCvsVariables() {
374         this.variables.clear();
375     }
376
377     /**
378      * Sets the cvs internal enviroment variables.
379      * It will clear any vrisables previously set.
380      * @param variables array of strings in format "KEY=VALUE".
381      */

382     public void setCvsVariables(String JavaDoc[] variables) {
383         clearCvsVariables();
384         for (int i = 0; i < variables.length; i++) {
385             String JavaDoc variable = variables[i];
386             this.variables.add(variable);
387         }
388     }
389
390     public String JavaDoc[] getCvsVariables() {
391         String JavaDoc[] vars = new String JavaDoc[variables.size()];
392         vars = (String JavaDoc[])variables.toArray(vars);
393         return vars;
394     }
395     
396     
397     /**
398      * Sets whether no changes should be done to the files.
399      */

400     public void setDoNoChanges(boolean doNoChanges) {
401         this.doNoChanges = doNoChanges;
402     }
403
404     /**
405      * Returns whether no changes should be done to the files.
406      */

407     public boolean isDoNoChanges() {
408         return doNoChanges;
409     }
410
411     /**
412      * Are checked out files read only.
413      * @return the answer
414      */

415     public boolean isCheckedOutFilesReadOnly() {
416         return checkedOutFilesReadOnly;
417     }
418
419     /**
420      * Set whether checked out files are read only. False is the default.
421      * @param readOnly true for readonly, false for read/write (default)
422      */

423     public void setCheckedOutFilesReadOnly(boolean readOnly) {
424         checkedOutFilesReadOnly = readOnly;
425     }
426
427     /**
428      * Get the CVS root
429      * @return the CVS root value, e.g. :pserver:user@host@/usr/local/cvs
430      */

431     public String JavaDoc getCVSRoot() {
432         return cvsRoot;
433     }
434
435     /**
436      * Set the CVS root
437      * @param cvsRoot CVS root to use
438      */

439     public void setCVSRoot(String JavaDoc cvsRoot) {
440         this.cvsRoot = cvsRoot;
441     }
442
443     /**
444      * Set whether to use Gzip for file transmission/reception
445      * @param useGzip true if gzip should be used, false otherwise
446      */

447     public void setUseGzip(boolean useGzip) {
448         this.useGzip = useGzip;
449     }
450
451     /**
452      * Get whether to use Gzip
453      * @return true if Gzip should be used, false otherwise
454      */

455     public boolean isUseGzip() {
456         return useGzip;
457     }
458
459     /**
460      * Getter for property compressionLevel.
461      * @return Value of property compressionLevel.
462      */

463     public int getCompressionLevel() {
464         return compressionLevel;
465     }
466     
467     /**
468      * Setter for property compressionLevel.
469      * @param compressionLevel New value of property compressionLevel.
470      */

471     public void setCompressionLevel(int compressionLevel) {
472         this.compressionLevel = compressionLevel;
473     }
474     
475     /** Getter for property noHistoryLogging.
476      * @return Value of property noHistoryLogging.
477      */

478     public boolean isNoHistoryLogging() {
479         return noHistoryLogging;
480     }
481
482     /** Setter for property noHistoryLogging.
483      * @param noHistoryLogging New value of property noHistoryLogging.
484      */

485     public void setNoHistoryLogging(boolean noHistoryLogging) {
486         this.noHistoryLogging = noHistoryLogging;
487     }
488
489     /** Getter for property moderatelyQuiet.
490      * @return Value of property moderatelyQuiet.
491      */

492     public boolean isModeratelyQuiet() {
493         return moderatelyQuiet;
494     }
495
496     /** Setter for property moderatelyQuiet.
497      * @param moderatelyQuiet New value of property moderatelyQuiet.
498      */

499     public void setModeratelyQuiet(boolean moderatelyQuiet) {
500         this.moderatelyQuiet = moderatelyQuiet;
501     }
502
503     /** Getter for property veryQuiet.
504      * @return Value of property veryQuiet.
505      */

506     public boolean isVeryQuiet() {
507         return veryQuiet;
508     }
509
510     /** Setter for property veryQuiet.
511      * @param veryQuiet New value of property veryQuiet.
512      */

513     public void setVeryQuiet(boolean veryQuiet) {
514         this.veryQuiet = veryQuiet;
515     }
516
517     /** Getter for property traceExecution.
518      * @return Value of property traceExecution.
519      */

520     public boolean isTraceExecution() {
521         return traceExecution;
522     }
523
524     /** Setter for property traceExecution.
525      * @param traceExecution New value of property traceExecution.
526      */

527     public void setTraceExecution(boolean traceExecution) {
528         this.traceExecution = traceExecution;
529     }
530
531     /**
532      * Getter for property showHelp.
533      * @return Value of property showHelp.
534      */

535     public boolean isShowHelp() {
536         return showHelp;
537     }
538     
539     /**
540      * Setter for property showHelp.
541      * @param showHelp New value of property showHelp.
542      */

543     public void setShowHelp(boolean showHelp) {
544         this.showHelp = showHelp;
545     }
546     
547     /**
548      * Getter for property showVersion.
549      * @return Value of property showVersion.
550      */

551     public boolean isShowVersion() {
552         return showVersion;
553     }
554     
555     /**
556      * Setter for property showVersion.
557      * @param showVersion New value of property showVersion.
558      */

559     public void setShowVersion(boolean showVersion) {
560         this.showVersion = showVersion;
561     }
562     
563     /**
564      * Getter for property ignoreCvsrc.
565      * @return Value of property ignoreCvsrc.
566      */

567     public boolean isIgnoreCvsrc() {
568         return ignoreCvsrc;
569     }
570     
571     /**
572      * Setter for property ignoreCvsrc.
573      * @param ignoreCvsrc New value of property ignoreCvsrc.
574      */

575     public void setIgnoreCvsrc(boolean ignoreCvsrc) {
576         this.ignoreCvsrc = ignoreCvsrc;
577     }
578     
579     /**
580      * Getter for property tempDir.
581      * @return Value of property tempDir.
582      */

583     public java.io.File JavaDoc getTempDir() {
584         return tempDir;
585     }
586     
587     /**
588      * Setter for property tempDir.
589      * @param tempDir New value of property tempDir.
590      */

591     public void setTempDir(java.io.File JavaDoc tempDir) {
592         this.tempDir = tempDir;
593     }
594     
595     /**
596      * Getter for property editor.
597      * @return Value of property editor.
598      */

599     public String JavaDoc getEditor() {
600         return editor;
601     }
602     
603     /**
604      * Setter for property editor.
605      * @param editor New value of property editor.
606      */

607     public void setEditor(String JavaDoc editor) {
608         this.editor = editor;
609     }
610     
611     /**
612      * This method just calls the Object.clone() and makes it public.
613      */

614     public Object JavaDoc clone() {
615         try {
616             return super.clone();
617         }
618         catch (CloneNotSupportedException JavaDoc ex) {
619             // never can occur
620
return null;
621         }
622     }
623     
624 }
625
Popular Tags