001: /* *****************************************************************************
002: * Main.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2006-2008 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.js2doc;
011:
012: import org.w3c.dom.Document;
013: import org.apache.log4j.*;
014: import org.apache.log4j.spi.*;
015: import org.apache.log4j.varia.NullAppender;
016: import org.openlaszlo.utils.FileUtils;
017: import java.io.*;
018: import java.util.*;
019:
020: public class Main {
021:
022: private static Logger logger;
023:
024: private static final String[] USAGE = {
025: "Usage: js2doc [OPTION]... FILE...", "", "Options:", "-v",
026: " Write progress information to standard output.",
027: "--test schema",
028: " Test mode: validate result against schema given",
029: "--reprocess", " Reprocess comments", "--help",
030: " Prints this message.", "", "Output options:",
031: "--out outputfile", " Output file", "--dir outputdir",
032: " Output directory (deprecated).", };
033:
034: /** Extracts reference documentation from the file(s) included.
035: *
036: * <p>See the usage string or execute <code>js2doc --help</code>
037: * to see a list of options.
038: *
039: * @param args the command line arguments
040: */
041: public static void main(String args[]) throws IOException {
042: js2doc(args, null, null, null);
043: }
044:
045: static final String[] runtimeOptionStrings = { "swf7", "swf8",
046: "swf9", "dhtml", "svg", "j2me" };
047: static final Set runtimeOptions = new HashSet(Arrays
048: .asList(runtimeOptionStrings));
049: // first element is the alias, subsequent elements are valid runtimes
050: // e.g. js1 === dhtml || j2me || svg
051: static final String[][] runtimeAliasStrings = {
052: { "as2", "swf7", "swf8", "swf9" }, { "as3", "swf9" },
053: { "js1", "dhtml" /*, "j2me", "svg" */} };
054: static final List runtimeAliases = Arrays
055: .asList(runtimeAliasStrings);
056: static final String[] buildOptionStrings = { "debug", "profile" };
057: static final List buildOptions = Arrays.asList(buildOptionStrings);
058:
059: /** This method implements the behavior described in main
060: * but also returns an integer error code.
061: */
062: public static int js2doc(String args[], String logFile,
063: String outFileName, String outDir) throws IOException {
064: logger = Logger.getRootLogger();
065: // Configure logging
066: logger.setLevel(Level.ERROR);
067: PatternLayout layout = new PatternLayout("%m%n");
068: logger.removeAllAppenders();
069: if (logFile == null) {
070: logger.addAppender(new ConsoleAppender(layout));
071: } else {
072: logger
073: .addAppender(new FileAppender(layout, logFile,
074: false));
075: }
076:
077: List files = new Vector();
078: boolean testMode = false;
079: boolean reprocessOnly = false;
080: String libraryID = null;
081: String schemaFilename = null; // used for --test mode
082:
083: for (int i = 0; i < args.length; i++) {
084: String arg = args[i].intern();
085: if (arg.startsWith("-")) {
086: boolean badform = false;
087: if (arg.equals("-v")) {
088: logger.setLevel(Level.ALL);
089: } else if (arg.equals("--test")) {
090: testMode = true;
091: if (i < args.length) {
092: i++;
093: schemaFilename = args[i];
094: } else
095: badform = true;
096: } else if (arg.equals("--reprocess")) {
097: reprocessOnly = true;
098: } else if (arg.equals("--out")) {
099: if (i < args.length) {
100: i++;
101: outFileName = args[i];
102: } else
103: badform = true;
104: } else if (arg.equals("--dir")) {
105: if (i < args.length) {
106: i++;
107: outDir = args[i];
108: } else
109: badform = true;
110: } else if (arg.equals("--libraryid")) {
111: if (i < args.length) {
112: i++;
113: libraryID = args[i];
114: } else
115: badform = true;
116: } else if (arg.equals("--help")) {
117: for (int j = 0; j < USAGE.length; j++) {
118: System.err.println(USAGE[j]);
119: }
120: return 0;
121: } else {
122: badform = true;
123: }
124:
125: if (badform == true) {
126: System.err.println("Bad arg: " + arg);
127: System.err
128: .println("Usage: js2doc [OPTION]... file...");
129: System.err
130: .println("Try `js2doc --help' for more information.");
131: return 1;
132: }
133: } else {
134: files.add(arg);
135: }
136: }
137:
138: if (testMode == true && outDir != null) {
139: System.err
140: .println("Both --test and --dir given; ignoring --dir.");
141: }
142:
143: for (Iterator iter = files.iterator(); iter.hasNext();) {
144: String sourceName = (String) iter.next();
145: if (testMode) {
146: System.err
147: .println("extracting docs and validating from "
148: + sourceName);
149: boolean result = validateAndCompare(sourceName,
150: schemaFilename, libraryID, runtimeOptions,
151: runtimeAliases, buildOptions);
152: System.err.println("result: "
153: + (result ? "success" : "failure"));
154: if (result == false)
155: return 1;
156: } else if (reprocessOnly) {
157: if (files.size() > 1)
158: System.err
159: .println("Reprocessing documentation within "
160: + sourceName);
161: reprocess(sourceName, outFileName, outDir, libraryID,
162: runtimeOptions, runtimeAliases, buildOptions);
163: } else {
164: if (files.size() > 1)
165: System.err.println("Extracting documentation from "
166: + sourceName);
167: process(sourceName, outFileName, outDir, libraryID,
168: runtimeOptions, runtimeAliases, buildOptions);
169: }
170: }
171: return 0;
172: }
173:
174: static public void process(String sourceName, String outFileName,
175: String outDir, String libraryID, Set runtimeOptions,
176: List runtimeAliases, List buildOptions) {
177: File sourceFile = new File(sourceName);
178: if (outFileName == null) {
179: if (outDir == null) {
180: outFileName = FileUtils.getBase(sourceName) + ".xml";
181: } else {
182: outFileName = outDir + File.separator
183: + FileUtils.getBase(sourceFile.getName())
184: + ".xml";
185: }
186: }
187: File scriptFile = new File(outFileName);
188: try {
189: String scriptContents = FileUtils
190: .readFileString(sourceFile);
191: String script = "#file " + sourceName + "\n" + "#line 1\n"
192: + scriptContents;
193: // TODO [jgrandy 2007/01/11] pass in or retrieve JS2DOC_HOME
194: String sourceRoot = System.getProperty("JS2DOC_LIBROOT");
195: Document descr = JS2Doc.toXML(script, sourceFile,
196: sourceRoot, libraryID, runtimeOptions,
197: runtimeAliases, buildOptions);
198:
199: JS2DocUtils.xmlToFile(descr, outFileName);
200:
201: } catch (IOException e) {
202: e.printStackTrace();
203: System.err.println("Couldn't read script file");
204: }
205: }
206:
207: static public void reprocess(String sourceName, String outFileName,
208: String outDir, String libraryID, Set runtimeOptions,
209: List runtimeAliases, List buildOptions) {
210: File sourceFile = new File(sourceName);
211: if (outFileName == null) {
212: if (outDir == null) {
213: outFileName = FileUtils.getBase(sourceName) + ".xml";
214: } else {
215: outFileName = outDir + File.separator
216: + FileUtils.getBase(sourceFile.getName())
217: + ".xml";
218: }
219: }
220: File scriptFile = new File(outFileName);
221: try {
222: String scriptContents = FileUtils
223: .readFileString(sourceFile);
224:
225: Document descr = ReprocessComments
226: .reprocess(scriptContents);
227:
228: JS2DocUtils.xmlToFile(descr, outFileName);
229:
230: } catch (IOException e) {
231: e.printStackTrace();
232: System.err.println("Couldn't read script file");
233: }
234: }
235:
236: static public boolean validateAndCompare(String sourceName,
237: String schemaName, String libraryID, Set runtimeOptions,
238: List runtimeAliases, List buildOptions) {
239: boolean result = true;
240: try {
241: File sourceFile = new File(sourceName);
242: String sourceContents = FileUtils
243: .readFileString(sourceFile);
244: String source = "#file " + sourceName + "\n" + "#line 1\n"
245: + sourceContents;
246: String sourceRoot = System.getProperty("JS2DOC_LIBROOT");
247: Document test = JS2Doc.toXML(source, sourceFile,
248: sourceRoot, libraryID, runtimeOptions,
249: runtimeAliases, buildOptions);
250:
251: String expectName = FileUtils.getBase(sourceName) + ".xml";
252: File expectFile = new File(expectName);
253: String expect = FileUtils.readFileString(expectFile);
254:
255: } catch (java.io.IOException exc) {
256: result = false;
257: exc.printStackTrace();
258: }
259:
260: return result;
261: }
262: }
|