Source code: juju/reattore/util/VDocletCLI.java
1 /* Reattore HTTP Server
2
3 Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 $Id: VDocletCLI.java,v 1.2 2003/03/05 05:06:16 michaelh Exp $
20 */
21
22 /* The above header is only there to satisfy the automatic code test
23 tools; the following is the true copyright statement */
24
25 /*
26 Copyright (c) 2002, Mike Williams
27 All rights reserved.
28
29 Redistribution and use in source and binary forms, with or without
30 modification, are permitted provided that the following conditions
31 are met:
32
33 1. Redistributions of source code must retain the above copyright
34 notice, this list of conditions and the following disclaimer.
35 2. Redistributions in binary form must reproduce the above copyright
36 notice, this list of conditions and the following disclaimer in the
37 documentation and/or other materials provided with the distribution.
38
39 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
40 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
42 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
43 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
48 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 */
50
51 package juju.reattore.util;
52
53 import com.thoughtworks.qdox.model.JavaSource;
54 import java.io.File;
55 import java.io.FileReader;
56 import java.io.IOException;
57 import java.io.OutputStreamWriter;
58 import java.util.ArrayList;
59 import vdoclet.docinfo.DocInfo;
60 import vdoclet.docinfo.QDoxBuilder;
61 import vdoclet.Generator;
62
63 /**
64 * Command-line entry-point to vDoclet
65 */
66 public class VDocletCLI {
67
68 //---( State )---
69
70 /** output directory */
71 private File destDir;
72
73 /** control template */
74 private String template;
75
76 /** input files/directories */
77 private String[] sourceFiles;
78
79 /** input path */
80 private String sourcePath;
81
82 /** verbose flag */
83 private boolean verbose = false;
84
85 /** docinfo */
86 private DocInfo docInfo;
87
88 //---( Execution )---
89
90 /** Main entry point.
91
92 @param args Command line args.
93 @throws Exception from the lower layers.
94 */
95 public static void main(String[] args) throws Exception {
96 new VDocletCLI().execute(args);
97 }
98
99 private void execute(String[] args) throws Exception {
100 parseCommandLine(args);
101 validateCommandLine();
102 parseSources();
103 generate();
104 }
105
106 private void log(String msg) {
107 if (verbose) {
108 System.err.println(msg);
109 }
110 }
111
112 /**
113 * Parse the command-line
114 */
115 private void parseCommandLine(String[] args) throws Exception {
116 int i = 0;
117 while (i < args.length && args[i].startsWith("-")) {
118 if (args[i].equals("-d")) {
119 destDir = new File(args[++i]);
120 }
121 else if (args[i].equals("-s")) {
122 sourcePath = args[++i];
123 }
124 else if (args[i].equals("-t")) {
125 template = args[++i];
126 }
127 else if (args[i].equals("-v")) {
128 verbose = true;
129 }
130 else {
131 throw new Exception("Illegal command-line arg: "
132 + args[i]);
133 }
134 i++;
135 }
136 int nFiles = args.length - i;
137 sourceFiles = new String[nFiles];
138 System.arraycopy(args, i, sourceFiles, 0, nFiles);
139 }
140
141 /**
142 * Check that we have all the required args
143 */
144 private void validateCommandLine() throws Exception {
145 if (destDir == null) {
146 throw new Exception("missing -d argument");
147 }
148 if (template == null) {
149 throw new Exception("missing -t argument");
150 }
151 if (sourceFiles.length == 0) {
152 throw new Exception("no source-files provided");
153 }
154 }
155
156 /**
157 * Parse the source-files
158 */
159 private void parseSources() throws IOException {
160 com.thoughtworks.qdox.JavaDocBuilder builder =
161 new com.thoughtworks.qdox.JavaDocBuilder();
162 ArrayList sourceDirList = new ArrayList();
163 for (int i = 0; i < sourceFiles.length; i++) {
164 log("reading " + sourceFiles[i]);
165 File f = new File(sourceFiles[i]);
166 if (f.isDirectory()) {
167 builder.addSourceTree(f);
168 sourceDirList.add(f);
169 }
170 else {
171 builder.addSource(new FileReader(f));
172 }
173 }
174
175 JavaSource[] javaSources = builder.getSources();
176 log(javaSources.length + " source-files");
177 docInfo = QDoxBuilder.build(javaSources);
178
179 if (sourcePath != null) {
180 docInfo.setSourcePath(sourcePath);
181 }
182 else {
183 String[] srcPath =
184 (String[]) sourceDirList.toArray(new String[0]);
185 docInfo.setSourcePath(srcPath);
186 }
187 }
188
189 /**
190 * Kick-start generation
191 */
192 private void generate() throws Exception {
193 Generator generator = new Generator(destDir);
194 generator.setAttribute("docInfo", docInfo);
195 generator.setAttribute("toolbox", new Toolbox());
196 generator.eval(template,
197 new OutputStreamWriter(System.out));
198 }
199
200 }
201