KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > dependency > DependencyCheckerCommandLine


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.dependency;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 import classycle.CommandLine;
31 import classycle.util.Text;
32
33 /**
34  * @author Franz-Josef Elmer
35  */

36 public class DependencyCheckerCommandLine extends CommandLine
37 {
38   private static final String JavaDoc DEPENDENCIES = "-dependencies=",
39                               RENDERER = "-renderer=";
40   
41   private String JavaDoc _dependencyDefinition;
42   private ResultRenderer _renderer = new DefaultResultRenderer();
43   
44   public DependencyCheckerCommandLine(String JavaDoc[] args)
45   {
46     super(args);
47   }
48
49   protected void handleOption(String JavaDoc argument)
50   {
51     if (argument.startsWith(DEPENDENCIES))
52     {
53       handleDependenciesOption(argument.substring(DEPENDENCIES.length()));
54     } else if (argument.startsWith(RENDERER)) {
55       handleRenderer(argument.substring(RENDERER.length()));
56     } else {
57       super.handleOption(argument);
58     }
59   }
60
61   /** Returns the usage of correct command line arguments and options. */
62   public String JavaDoc getUsage()
63   {
64     return DEPENDENCIES + "<description>|@<description file> "
65          + "[" + RENDERER + "<fully qualified class name of a ResultRenderer>] "
66                + super.getUsage();
67   }
68   
69   public String JavaDoc getDependencyDefinition()
70   {
71     return _dependencyDefinition;
72   }
73   
74   public ResultRenderer getRenderer()
75   {
76     return _renderer;
77   }
78   
79   private void handleDependenciesOption(String JavaDoc option)
80   {
81     if (option.startsWith("@"))
82     {
83       try
84       {
85         option = Text.readTextFile(new File JavaDoc(option.substring(1)));
86       } catch (IOException JavaDoc e)
87       {
88         System.err.println("Error in reading dependencies description file: "
89                            + e);
90         option = "";
91       }
92     }
93     _dependencyDefinition = option;
94     if (_dependencyDefinition.length() == 0)
95     {
96       _valid = false;
97     }
98   }
99   
100   private void handleRenderer(String JavaDoc className) {
101     try
102     {
103       _renderer = (ResultRenderer) Class.forName(className).newInstance();
104     } catch (Exception JavaDoc e)
105     {
106       System.err.println("Error in creating ResultRenderer " + className
107                          + ": " + e);
108       _valid = false;
109     }
110   }
111
112   public boolean isValid()
113   {
114     return super.isValid() && _dependencyDefinition != null;
115   }
116 }
117
Popular Tags