e.g. Calendar Search Help
You must enter a value before pressing Search
ant

Class: org.apache.tools.ant.taskdefs.optional.javacc.JJDoc   ©

 OK to copy?
001 /*
002  * Copyright  2003-2004 The Apache Software Foundation
003  *
004  *  Licensed under the Apache License, Version 2.0 (the "License");
005  *  you may not use this file except in compliance with the License.
006  *  You may obtain a copy of the License at
007  *
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  *
010  *  Unless required by applicable law or agreed to in writing, software
011  *  distributed under the License is distributed on an "AS IS" BASIS,
012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  *  See the License for the specific language governing permissions and
014  *  limitations under the License.
015  *
016  */
017 
018 package org.apache.tools.ant.taskdefs.optional.javacc;
019 
020 import java.io.File;
021 import java.io.IOException;
022 import java.util.Enumeration;
023 import java.util.Hashtable;
024 import org.apache.tools.ant.BuildException;
025 import org.apache.tools.ant.Project;
026 import org.apache.tools.ant.Task;
027 import org.apache.tools.ant.taskdefs.Execute;
028 import org.apache.tools.ant.taskdefs.LogStreamHandler;
029 import org.apache.tools.ant.types.Commandline;
030 import org.apache.tools.ant.types.CommandlineJava;
031 import org.apache.tools.ant.types.Path;
032 import org.apache.tools.ant.util.JavaEnvUtils;
033 
034 /**
035  * Runs the JJDoc compiler compiler.
036  *
037  */
038 public class JJDoc extends Task {
039 
040     // keys to optional attributes
041     private static final String OUTPUT_FILE       = "OUTPUT_FILE";
042     private static final String TEXT              = "TEXT";
043     private static final String ONE_TABLE         = "ONE_TABLE";
044 
045     private final Hashtable optionalAttrs = new Hashtable();
046 
047     private String outputFile = null;
048     private boolean plainText = false;
049 
050     private static final String DEFAULT_SUFFIX_HTML = ".html";
051     private static final String DEFAULT_SUFFIX_TEXT = ".txt";
052 
053     // required attributes
054     private File target          = null;
055     private File javaccHome      = null;
056 
057     private CommandlineJava cmdl = new CommandlineJava();
058 
059 
060     /**
061      * Sets the TEXT BNF documentation option.
062      */
063     public void setText(boolean plainText) {
064         optionalAttrs.put(TEXT, new Boolean(plainText));
065         this.plainText = plainText;
066     }
067 
068     /**
069      * Sets the ONE_TABLE documentation option.
070      */
071     public void setOnetable(boolean oneTable) {
072         optionalAttrs.put(ONE_TABLE, new Boolean(oneTable));
073     }
074 
075     /**
076      * The outputfile to write the generated BNF documentation file to.
077      * If not set, the file is written with the same name as
078      * the JavaCC grammar file with a suffix .html or .txt.
079      */
080     public void setOutputfile(String outputFile) {
081         this.outputFile = outputFile;
082     }
083 
084     /**
085      * The javacc grammar file to process.
086      */
087     public void setTarget(File target) {
088         this.target = target;
089     }
090 
091     /**
092      * The directory containing the JavaCC distribution.
093      */
094     public void setJavacchome(File javaccHome) {
095         this.javaccHome = javaccHome;
096     }
097 
098     public JJDoc() {
099         cmdl.setVm(JavaEnvUtils.getJreExecutable("java"));
100     }
101 
102     public void execute() throws BuildException {
103 
104         // load command line with optional attributes
105         Enumeration iter = optionalAttrs.keys();
106         while (iter.hasMoreElements()) {
107             String name  = (String) iter.nextElement();
108             Object value = optionalAttrs.get(name);
109             cmdl.createArgument()
110                 .setValue("-" + name + ":" + value.toString());
111         }
112 
Rate113         if (target == null || !target.isFile()) {
114             throw new BuildException("Invalid target: " + target);
115         }
116 
117         if (outputFile != null) {
118             cmdl.createArgument() .setValue("-" + OUTPUT_FILE + ":"
119                                             + outputFile.replace('\\', '/'));
120         }
121 
122         // use the directory containing the target as the output directory
123         File javaFile = new File(createOutputFileName(target, outputFile,
124                                                       plainText));
125 
126         if (javaFile.exists()
127              && target.lastModified() < javaFile.lastModified()) {
128             log("Target is already built - skipping (" + target + ")",
129                 Project.MSG_VERBOSE);
130             return;
131         }
132 
133         cmdl.createArgument().setValue(target.getAbsolutePath());
134 
135         cmdl.setClassname(JavaCC.getMainClass(javaccHome,
136                                               JavaCC.TASKDEF_TYPE_JJDOC));
137 
138         final Path classpath = cmdl.createClasspath(getProject());
139         final File javaccJar = JavaCC.getArchiveFile(javaccHome);
140         classpath.createPathElement().setPath(javaccJar.getAbsolutePath());
141         classpath.addJavaRuntime();
142 
143         final Commandline.Argument arg = cmdl.createVmArgument();
144         arg.setValue("-mx140M");
145         arg.setValue("-Dinstall.root=" + javaccHome.getAbsolutePath());
146 
147         final Execute process =
148             new Execute(new LogStreamHandler(this,
149                                              Project.MSG_INFO,
150                                              Project.MSG_INFO),
151                         null);
152         log(cmdl.describeCommand(), Project.MSG_VERBOSE);
153         process.setCommandline(cmdl.getCommandline());
154 
155         try {
156             if (process.execute() != 0) {
157                 throw new BuildException("JJDoc failed.");
158             }
159         } catch (IOException e) {
160             throw new BuildException("Failed to launch JJDoc", e);
161         }
162     }
163 
164     private String createOutputFileName(File target, String optionalOutputFile,
165                                         boolean plainText) {
166         String suffix = DEFAULT_SUFFIX_HTML;
167         String javaccFile = target.getAbsolutePath().replace('\\', '/');
168 
169         if (plainText) {
170             suffix = DEFAULT_SUFFIX_TEXT;
171         }
172 
173         if ((optionalOutputFile == null) || optionalOutputFile.equals("")) {
174             int filePos = javaccFile.lastIndexOf("/");
175 
176             if (filePos >= 0) {
177                 javaccFile = javaccFile.substring(filePos + 1);
178             }
179 
180             int suffixPos = javaccFile.lastIndexOf('.');
181 
182             if (suffixPos == -1) {
183                 optionalOutputFile = javaccFile + suffix;
184             } else {
185                 String currentSuffix = javaccFile.substring(suffixPos);
186 
187                 if (currentSuffix.equals(suffix)) {
188                     optionalOutputFile = javaccFile + suffix;
189                 } else {
190                     optionalOutputFile = javaccFile.substring(0, suffixPos)
191                         + suffix;
192                 }
193             }
194         } else {
195             optionalOutputFile = optionalOutputFile.replace('\\', '/');
196         }
197 
198         return (getProject().getBaseDir() + "/" + optionalOutputFile)
199             .replace('\\', '/');
200     }
201 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
113 0% of 0