001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015:
016: package org.griphyn.vdl.toolkit;
017:
018: import java.io.*;
019: import java.util.*;
020: import org.griphyn.common.util.Version;
021: import org.griphyn.vdl.dax.*;
022: import org.griphyn.vdl.parser.DAXParser;
023: import org.griphyn.vdl.util.Logging;
024: import org.griphyn.vdl.util.DAX2CoG;
025: import org.griphyn.vdl.util.ChimeraProperties;
026: import gnu.getopt.*;
027:
028: /**
029: * This class generates CoG XML from a DAX.
030: *
031: * @author Jens-S. Vöckler
032: * @author Yong Zhao
033: * @version $Revision: 50 $
034: *
035: */
036: public class VizDAX2 extends Toolkit {
037: /**
038: * ctor: Constructs a new instance object with the given application name.
039: */
040: public VizDAX2(String appName) {
041: super (appName);
042: }
043:
044: /**
045: * Implements printing the usage string onto stdout.
046: */
047: public void showUsage() {
048: String linefeed = System.getProperty("line.separator", "\r\n");
049:
050: System.out
051: .println("$Id: VizDAX2.java 50 2007-05-19 00:48:32Z gmehta $"
052: + linefeed
053: + "VDS version "
054: + Version.instance().toString() + linefeed);
055:
056: System.out.println("Usage: " + this .m_application
057: + " [general] [-o cog] [dax]");
058:
059: System.out
060: .println(linefeed
061: + " -V|--version print version information and exit."
062: + linefeed
063: + " -v|--verbose increases the verbosity level."
064: + linefeed
065: + " -o|--output cog put the output into the file cog, defaults to stdout."
066: + linefeed
067: + " dax reads the specified dax file; if absent, uses stdin."
068: + linefeed);
069: }
070:
071: /**
072: * Creates a set of options.
073: * @return the long option set.
074: */
075: protected LongOpt[] generateValidOptions() {
076: LongOpt[] lo = new LongOpt[4];
077:
078: lo[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
079: lo[1] = new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'V');
080: lo[2] = new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 'v');
081:
082: lo[3] = new LongOpt("output", LongOpt.REQUIRED_ARGUMENT, null,
083: 'o');
084:
085: return lo;
086: }
087:
088: /**
089: * Get the DAX and generate shell scripts in the designated directory.
090: */
091: public static void main(String[] args) {
092: int result = 0;
093:
094: try {
095: VizDAX2 me = new VizDAX2("dax2cog");
096: // get the commandline options
097: Getopt opts = new Getopt(me.m_application, args, "ho:V", me
098: .generateValidOptions());
099: opts.setOpterr(false);
100: String arg = null;
101: String dot = null;
102:
103: int option = 0;
104: while ((option = opts.getopt()) != -1) {
105: switch (option) {
106: case 'V':
107: System.out
108: .println("$Id: VizDAX2.java 50 2007-05-19 00:48:32Z gmehta $");
109: System.out.println("VDS version "
110: + Version.instance().toString());
111: return;
112:
113: case 'o':
114: arg = opts.getOptarg();
115: if (arg != null && arg.length() > 0)
116: dot = arg;
117: break;
118:
119: case 'v':
120: me.increaseVerbosity();
121: break;
122:
123: case 'h':
124: default:
125: me.showUsage();
126: return;
127: }
128: }
129:
130: // there is exactly one mandatory argument
131: String daxfn = (opts.getOptind() < args.length ? args[opts
132: .getOptind()] : null);
133:
134: BufferedInputStream input = null;
135: if (daxfn == null || daxfn.equals("-")) {
136: input = new BufferedInputStream(System.in);
137: System.err.println("# reminder: reading from stdin");
138: } else {
139: input = new BufferedInputStream(new FileInputStream(
140: daxfn));
141: }
142:
143: BufferedWriter output = new BufferedWriter(dot == null
144: || dot.equals("-") ? new OutputStreamWriter(
145: System.out) : // convert stream to writer
146: new FileWriter(dot));
147:
148: // start the DAX parser
149: me.m_logger.log("app", 2, "Initializing DAX parser");
150: DAXParser daxparser = new DAXParser(ChimeraProperties
151: .instance().getDAXSchemaLocation());
152:
153: // parse the file
154: me.m_logger.log("app", 1, "Parsing the DAX file " + daxfn);
155: ADAG adag = daxparser.parse(input);
156: if (adag == null) {
157: // unable to parse
158: throw new RuntimeException(
159: "Unable to parse the DAX file " + daxfn);
160: }
161:
162: me.m_logger.log("app", 1, "dumping XML output");
163: DAX2CoG.toString(output, adag);
164:
165: output.flush();
166: if (dot != null)
167: output.close();
168:
169: } catch (RuntimeException rte) {
170: System.err.println("ERROR: " + rte.getMessage());
171: result = 1;
172:
173: } catch (IOException ioe) {
174: System.err.println("ERROR: " + ioe.getMessage());
175: result = 1;
176:
177: } catch (Exception e) {
178: e.printStackTrace();
179: System.err.println("FATAL: " + e.getMessage());
180: result = 2;
181: }
182:
183: if (result != 0)
184: System.exit(result);
185: }
186: }
|