001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.build;
028:
029: import java.io.BufferedReader;
030: import java.io.DataInputStream;
031: import java.io.FileInputStream;
032: import java.io.FileNotFoundException;
033: import java.io.InputStream;
034: import java.io.InputStreamReader;
035: import java.lang.reflect.Method;
036: import java.util.ArrayList;
037: import java.util.List;
038: import java.util.StringTokenizer;
039:
040: /** A tool for executing various COUGAAR code generators as specified by
041: * .def files. This supports the ANT build method.
042: **/
043: public class DefRunner {
044: /**
045: * Run the code generator against the given file and a command line
046: **/
047: public static void runDef(String defile, String argline) {
048: List<String> argx = explode(argline, " ");
049:
050: String classname = (String) argx.get(0);
051:
052: List<String> nargs = new ArrayList<String>();
053: for (String a : argx) {
054: nargs.add(a);
055: }
056: nargs.add(defile); // last argument is always the def file
057:
058: String[] args = (String[]) nargs.toArray(new String[nargs
059: .size()]);
060:
061: // prepend the standard build prefix when it is probably wrong.
062: if (classname.indexOf('.') == -1) {
063: classname = "org.cougaar.tools.build." + classname;
064: }
065:
066: // cribbed from Bootstrapper
067: try {
068: ClassLoader cl = DefRunner.class.getClassLoader();
069: Class realnode = cl.loadClass(classname);
070: Class argl[] = new Class[] { String[].class };
071:
072: Method main = realnode.getMethod("main", argl);
073: Object[] argv = new Object[] { args };
074: main.invoke(null, argv);
075: } catch (Exception e) {
076: System.err.println("Failed to RunDef " + classname + ": ");
077: e.printStackTrace();
078: }
079: }
080:
081: private static final List<String> explode(String s, String seps) {
082: StringTokenizer tokens = new StringTokenizer(s, seps);
083: List<String> v = new ArrayList<String>();
084: while (tokens.hasMoreTokens()) {
085: v.add(tokens.nextToken());
086: }
087: return v;
088: }
089:
090: /**
091: * Read the first line of a file and construct and run the command
092: * to generate code from that file.
093: **/
094: public static void parse(String filename, String options) {
095: InputStream in;
096: try {
097: if (filename.equals("-")) {
098: in = new DataInputStream(System.in);
099: } else {
100: try {
101: in = new FileInputStream(filename);
102: } catch (FileNotFoundException fe) {
103: in = DefRunner.class.getClassLoader()
104: .getResourceAsStream(filename);
105: }
106: }
107: if (in == null) {
108: System.err.println("File " + filename
109: + " could not be opened.");
110: }
111: InputStreamReader isr = new InputStreamReader(in);
112: BufferedReader br = new BufferedReader(isr);
113:
114: // read the first line - it'll tell us what to do next
115: String line = br.readLine();
116: if (line != null) {
117: int i;
118:
119: // check for old syntax
120: if ((i = line.indexOf("!generate:")) != -1) {
121: String args = line.substring(i + 10).trim();
122: if (options != null) {
123: args += " " + options;
124: }
125: runDef(filename, args);
126: } else if ((i = line.indexOf("!")) != -1) {
127: String args = line.substring(i + 1).trim();
128: if (options != null) {
129: args += " " + options;
130: }
131: runDef(filename, args);
132: } else {
133: System.err.println("File \"" + filename
134: + "\" is a broken def file!");
135: }
136: }
137: br.close();
138: } catch (Exception e) {
139: e.printStackTrace();
140: }
141: }
142:
143: public static void main(String args[]) {
144: String options = null;
145: for (int i = 0; i < args.length; i++) {
146: String arg = args[i];
147: if (arg.startsWith("-o")) {
148: options = args[++i];
149: } else {
150: parse(arg, options);
151: }
152: }
153: }
154: }
|