KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > javacc > JJDoc


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.javacc;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Hashtable JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.Task;
29 import org.apache.tools.ant.taskdefs.Execute;
30 import org.apache.tools.ant.taskdefs.LogStreamHandler;
31 import org.apache.tools.ant.types.Commandline;
32 import org.apache.tools.ant.types.CommandlineJava;
33 import org.apache.tools.ant.types.Path;
34 import org.apache.tools.ant.util.JavaEnvUtils;
35
36 /**
37  * Runs the JJDoc compiler compiler.
38  *
39  */

40 public class JJDoc extends Task {
41
42     // keys to optional attributes
43
private static final String JavaDoc OUTPUT_FILE = "OUTPUT_FILE";
44     private static final String JavaDoc TEXT = "TEXT";
45     private static final String JavaDoc ONE_TABLE = "ONE_TABLE";
46
47     private final Hashtable JavaDoc optionalAttrs = new Hashtable JavaDoc();
48
49     private String JavaDoc outputFile = null;
50     private boolean plainText = false;
51
52     private static final String JavaDoc DEFAULT_SUFFIX_HTML = ".html";
53     private static final String JavaDoc DEFAULT_SUFFIX_TEXT = ".txt";
54
55     // required attributes
56
private File JavaDoc targetFile = null;
57     private File JavaDoc javaccHome = null;
58
59     private CommandlineJava cmdl = new CommandlineJava();
60
61
62     /**
63      * Sets the TEXT BNF documentation option.
64      * @param plainText a <code>boolean</code> value.
65      */

66     public void setText(boolean plainText) {
67         optionalAttrs.put(TEXT, plainText ? Boolean.TRUE : Boolean.FALSE);
68         this.plainText = plainText;
69     }
70
71     /**
72      * Sets the ONE_TABLE documentation option.
73      * @param oneTable a <code>boolean</code> value.
74      */

75     public void setOnetable(boolean oneTable) {
76         optionalAttrs.put(ONE_TABLE, oneTable ? Boolean.TRUE : Boolean.FALSE);
77     }
78
79     /**
80      * The outputfile to write the generated BNF documentation file to.
81      * If not set, the file is written with the same name as
82      * the JavaCC grammar file with a suffix .html or .txt.
83      * @param outputFile the name of the output file.
84      */

85     public void setOutputfile(String JavaDoc outputFile) {
86         this.outputFile = outputFile;
87     }
88
89     /**
90      * The javacc grammar file to process.
91      * @param target the grammar file.
92      */

93     public void setTarget(File JavaDoc target) {
94         this.targetFile = target;
95     }
96
97     /**
98      * The directory containing the JavaCC distribution.
99      * @param javaccHome the home directory.
100      */

101     public void setJavacchome(File JavaDoc javaccHome) {
102         this.javaccHome = javaccHome;
103     }
104
105     /**
106      * Constructor
107      */

108     public JJDoc() {
109         cmdl.setVm(JavaEnvUtils.getJreExecutable("java"));
110     }
111
112     /**
113      * Do the task.
114      * @throws BuildException if there is an error.
115      */

116     public void execute() throws BuildException {
117
118         // load command line with optional attributes
119
Enumeration JavaDoc iter = optionalAttrs.keys();
120         while (iter.hasMoreElements()) {
121             String JavaDoc name = (String JavaDoc) iter.nextElement();
122             Object JavaDoc value = optionalAttrs.get(name);
123             cmdl.createArgument()
124                 .setValue("-" + name + ":" + value.toString());
125         }
126
127         if (targetFile == null || !targetFile.isFile()) {
128             throw new BuildException("Invalid target: " + targetFile);
129         }
130
131         if (outputFile != null) {
132             cmdl.createArgument() .setValue("-" + OUTPUT_FILE + ":"
133                                             + outputFile.replace('\\', '/'));
134         }
135
136         // use the directory containing the target as the output directory
137
File JavaDoc javaFile = new File JavaDoc(createOutputFileName(targetFile, outputFile,
138                                                       plainText));
139
140         if (javaFile.exists()
141              && targetFile.lastModified() < javaFile.lastModified()) {
142             log("Target is already built - skipping (" + targetFile + ")",
143                 Project.MSG_VERBOSE);
144             return;
145         }
146
147         cmdl.createArgument().setValue(targetFile.getAbsolutePath());
148
149         final Path classpath = cmdl.createClasspath(getProject());
150         final File JavaDoc javaccJar = JavaCC.getArchiveFile(javaccHome);
151         classpath.createPathElement().setPath(javaccJar.getAbsolutePath());
152         classpath.addJavaRuntime();
153
154         cmdl.setClassname(JavaCC.getMainClass(classpath,
155                                               JavaCC.TASKDEF_TYPE_JJDOC));
156
157         final Commandline.Argument arg = cmdl.createVmArgument();
158         arg.setValue("-mx140M");
159         arg.setValue("-Dinstall.root=" + javaccHome.getAbsolutePath());
160
161         final Execute process =
162             new Execute(new LogStreamHandler(this,
163                                              Project.MSG_INFO,
164                                              Project.MSG_INFO),
165                         null);
166         log(cmdl.describeCommand(), Project.MSG_VERBOSE);
167         process.setCommandline(cmdl.getCommandline());
168
169         try {
170             if (process.execute() != 0) {
171                 throw new BuildException("JJDoc failed.");
172             }
173         } catch (IOException JavaDoc e) {
174             throw new BuildException("Failed to launch JJDoc", e);
175         }
176     }
177
178     private String JavaDoc createOutputFileName(File JavaDoc destFile, String JavaDoc optionalOutputFile,
179                                         boolean plain) {
180         String JavaDoc suffix = DEFAULT_SUFFIX_HTML;
181         String JavaDoc javaccFile = destFile.getAbsolutePath().replace('\\', '/');
182
183         if (plain) {
184             suffix = DEFAULT_SUFFIX_TEXT;
185         }
186
187         if ((optionalOutputFile == null) || optionalOutputFile.equals("")) {
188             int filePos = javaccFile.lastIndexOf("/");
189
190             if (filePos >= 0) {
191                 javaccFile = javaccFile.substring(filePos + 1);
192             }
193
194             int suffixPos = javaccFile.lastIndexOf('.');
195
196             if (suffixPos == -1) {
197                 optionalOutputFile = javaccFile + suffix;
198             } else {
199                 String JavaDoc currentSuffix = javaccFile.substring(suffixPos);
200
201                 if (currentSuffix.equals(suffix)) {
202                     optionalOutputFile = javaccFile + suffix;
203                 } else {
204                     optionalOutputFile = javaccFile.substring(0, suffixPos)
205                         + suffix;
206                 }
207             }
208         } else {
209             optionalOutputFile = optionalOutputFile.replace('\\', '/');
210         }
211
212         return (getProject().getBaseDir() + "/" + optionalOutputFile)
213             .replace('\\', '/');
214     }
215 }
216
Popular Tags