[14627] | 1 | /*
|
---|
| 2 | * Copyright 2003-2004 The Apache Software Foundation
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 5 | * you may not use this file except in compliance with the License.
|
---|
| 6 | * You may obtain a copy of the License at
|
---|
| 7 | *
|
---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 9 | *
|
---|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 13 | * See the License for the specific language governing permissions and
|
---|
| 14 | * limitations under the License.
|
---|
| 15 | *
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | package org.apache.tools.ant.taskdefs.optional.javacc;
|
---|
| 19 |
|
---|
| 20 | import java.io.File;
|
---|
| 21 | import java.io.IOException;
|
---|
| 22 | import java.util.Enumeration;
|
---|
| 23 | import java.util.Hashtable;
|
---|
| 24 | import org.apache.tools.ant.BuildException;
|
---|
| 25 | import org.apache.tools.ant.Project;
|
---|
| 26 | import org.apache.tools.ant.Task;
|
---|
| 27 | import org.apache.tools.ant.taskdefs.Execute;
|
---|
| 28 | import org.apache.tools.ant.taskdefs.LogStreamHandler;
|
---|
| 29 | import org.apache.tools.ant.types.Commandline;
|
---|
| 30 | import org.apache.tools.ant.types.CommandlineJava;
|
---|
| 31 | import org.apache.tools.ant.types.Path;
|
---|
| 32 | import org.apache.tools.ant.util.JavaEnvUtils;
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Runs the JJDoc compiler compiler.
|
---|
| 36 | *
|
---|
| 37 | */
|
---|
| 38 | public class JJDoc extends Task {
|
---|
| 39 |
|
---|
| 40 | // keys to optional attributes
|
---|
| 41 | private static final String OUTPUT_FILE = "OUTPUT_FILE";
|
---|
| 42 | private static final String TEXT = "TEXT";
|
---|
| 43 | private static final String ONE_TABLE = "ONE_TABLE";
|
---|
| 44 |
|
---|
| 45 | private final Hashtable optionalAttrs = new Hashtable();
|
---|
| 46 |
|
---|
| 47 | private String outputFile = null;
|
---|
| 48 | private boolean plainText = false;
|
---|
| 49 |
|
---|
| 50 | private static final String DEFAULT_SUFFIX_HTML = ".html";
|
---|
| 51 | private static final String DEFAULT_SUFFIX_TEXT = ".txt";
|
---|
| 52 |
|
---|
| 53 | // required attributes
|
---|
| 54 | private File target = null;
|
---|
| 55 | private File javaccHome = null;
|
---|
| 56 |
|
---|
| 57 | private CommandlineJava cmdl = new CommandlineJava();
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Sets the TEXT BNF documentation option.
|
---|
| 62 | */
|
---|
| 63 | public void setText(boolean plainText) {
|
---|
| 64 | optionalAttrs.put(TEXT, new Boolean(plainText));
|
---|
| 65 | this.plainText = plainText;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * Sets the ONE_TABLE documentation option.
|
---|
| 70 | */
|
---|
| 71 | public void setOnetable(boolean oneTable) {
|
---|
| 72 | optionalAttrs.put(ONE_TABLE, new Boolean(oneTable));
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * The outputfile to write the generated BNF documentation file to.
|
---|
| 77 | * If not set, the file is written with the same name as
|
---|
| 78 | * the JavaCC grammar file with a suffix .html or .txt.
|
---|
| 79 | */
|
---|
| 80 | public void setOutputfile(String outputFile) {
|
---|
| 81 | this.outputFile = outputFile;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * The javacc grammar file to process.
|
---|
| 86 | */
|
---|
| 87 | public void setTarget(File target) {
|
---|
| 88 | this.target = target;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * The directory containing the JavaCC distribution.
|
---|
| 93 | */
|
---|
| 94 | public void setJavacchome(File javaccHome) {
|
---|
| 95 | this.javaccHome = javaccHome;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | public JJDoc() {
|
---|
| 99 | 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 |
|
---|
| 113 | 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 | }
|
---|