KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > CommandLine


1 /*
2  * Copyright (c) 2003-2006, Franz-Josef Elmer, All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */

25 package classycle;
26
27 import classycle.util.AndStringPattern;
28 import classycle.util.NotStringPattern;
29 import classycle.util.StringPattern;
30 import classycle.util.StringPatternSequence;
31 import classycle.util.TrueStringPattern;
32 import classycle.util.WildCardPattern;
33
34 /**
35  * @author Franz-Josef Elmer
36  */

37 public abstract class CommandLine
38 {
39   private static final String JavaDoc MERGE_INNER_CLASSES = "-mergeInnerClasses";
40   private static final String JavaDoc INCLUDING_CLASSES = "-includingClasses=";
41   private static final String JavaDoc EXCLUDING_CLASSES = "-excludingClasses=";
42   private static final String JavaDoc REFLECTION_PATTERN = "-reflectionPattern=";
43
44   private boolean _mergeInnerClasses;
45   protected boolean _valid = true;
46   protected StringPatternSequence _pattern = new AndStringPattern();
47   protected StringPattern _reflectionPattern;
48   protected String JavaDoc[] _classFiles;
49   
50   public CommandLine(String JavaDoc[] args)
51   {
52     int index = 0;
53     for (;index < args.length && args[index].charAt(0) == '-'; index++)
54     {
55       handleOption(args[index]);
56     }
57     _classFiles = new String JavaDoc[args.length - index];
58     System.arraycopy(args, index, _classFiles, 0, _classFiles.length);
59     if (_classFiles.length == 0)
60     {
61       _valid = false;
62     }
63   }
64   
65   protected void handleOption(String JavaDoc argument)
66   {
67     if (argument.startsWith(MERGE_INNER_CLASSES))
68     {
69       _mergeInnerClasses = true;
70     } else if (argument.startsWith(INCLUDING_CLASSES))
71     {
72       String JavaDoc patterns = argument.substring(INCLUDING_CLASSES.length());
73       _pattern.appendPattern(WildCardPattern.createFromsPatterns(patterns, ","));
74     } else if (argument.startsWith(EXCLUDING_CLASSES))
75     {
76       String JavaDoc patterns = argument.substring(EXCLUDING_CLASSES.length());
77       StringPattern p = WildCardPattern.createFromsPatterns(patterns, ",");
78       _pattern.appendPattern(new NotStringPattern(p));
79     } else if (argument.startsWith(REFLECTION_PATTERN))
80     {
81       String JavaDoc patterns = argument.substring(REFLECTION_PATTERN.length());
82       if (patterns.length() == 0)
83       {
84         _reflectionPattern = new TrueStringPattern();
85       } else {
86         _reflectionPattern = WildCardPattern.createFromsPatterns(patterns, ",");
87       }
88     } else
89     {
90       _valid = false;
91     }
92   }
93
94   /**
95    * Returns all class file descriptors (i.e., class files, directorys,
96    * jar files, or zip files).
97    */

98   public String JavaDoc[] getClassFiles()
99   {
100     return _classFiles;
101   }
102
103   /**
104    * Returns the pattern fully qualified class names have to match. The
105    * pattern is based on the options <tt>-includingClasses</tt> and
106    * <tt>-excludingClasses</tt>. If <tt>-includingClasses</tt> is missing
107    * every classes is included which is not excluded.
108    * If <tt>-excludingClasses</tt> is missing no class is excluded.
109    */

110   public StringPattern getPattern()
111   {
112     return _pattern;
113   }
114   
115   /**
116    * Returns the reflection pattern as extracted from the option
117    * <tt>-reflectionPattern</tt>.
118    */

119   public StringPattern getReflectionPattern()
120   {
121     return _reflectionPattern;
122   }
123   
124   /**
125    * Returns <tt>true</tt> if the command line arguments and options are
126    * valid.
127    */

128   public boolean isValid()
129   {
130     return _valid;
131   }
132
133   /**
134    * Returns <code>true</code> if the command line option
135    * <code>-mergeInnerClasses</code> occured.
136    */

137   public boolean isMergeInnerClasses()
138   {
139     return _mergeInnerClasses;
140   }
141
142   
143   /** Returns the usage of correct command line arguments and options. */
144   public String JavaDoc getUsage()
145   {
146     return "[" + MERGE_INNER_CLASSES + "] "
147         + "[" + INCLUDING_CLASSES + "<pattern1>,<pattern2>,...] "
148         + "[" + EXCLUDING_CLASSES + "<pattern1>,<pattern2>,...] "
149         + "[" + REFLECTION_PATTERN + "<pattern1>,<pattern2>,...] "
150         + "<class files, zip/jar/war/ear files, or folders>";
151   }
152
153   
154 }
155
Popular Tags