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: package org.griphyn.vdl.toolkit;
016:
017: import org.griphyn.common.util.Currently;
018: import org.griphyn.common.util.Version;
019: import org.griphyn.vdl.parser.*;
020: import org.griphyn.vdl.classes.*;
021: import org.griphyn.vdl.util.Logging;
022: import org.griphyn.vdl.directive.VDLtConvert;
023:
024: import java.io.Reader;
025: import java.io.Writer;
026: import java.io.InputStreamReader;
027: import java.io.OutputStreamWriter;
028: import java.io.FileReader;
029: import java.io.FileWriter;
030: import java.io.BufferedReader;
031: import java.io.BufferedWriter;
032: import java.io.IOException;
033: import gnu.getopt.*;
034:
035: /**
036: * Test calls to parse the a given filename and produce XML from it.
037: *
038: * @see org.griphyn.vdl.parser.VDLtParser
039: */
040: public class VDLtConv extends Toolkit {
041: /**
042: * module local constructor for a toolkit application.
043: * @param appName is the name of the application to be displayed
044: */
045: VDLtConv(String appName) {
046: super (appName);
047: }
048:
049: public void showUsage() {
050: String linefeed = System.getProperty("line.separator", "\r\n");
051:
052: System.out
053: .println("$Id: VDLtConv.java 50 2007-05-19 00:48:32Z gmehta $"
054: + linefeed
055: + "VDS version "
056: + Version.instance().toString() + linefeed);
057:
058: System.out.println("Usage: " + this .m_application
059: + " [-n vdlns] [-v vdlvs] VDLt VDLx" + linefeed
060: + " or: " + this .m_application
061: + " [-n vdlns] [-v vdlvs] VDLt > VDLx" + linefeed
062: + " or: " + this .m_application
063: + " [-n vdlns] [-v vdlvs] < VDLt > VDLx");
064:
065: System.out
066: .println(linefeed
067: + "Generic options: "
068: + linefeed
069: + " -V|--version print version information and exit."
070: + linefeed
071: + " --verbose increases the verbosity level."
072: + linefeed
073: + " -n|--vdlns ns generates default namespace ns, default is none."
074: + linefeed
075: + " -v|--vdlvs vs geneartes default version vs, default is none."
076: + linefeed);
077: }
078:
079: /**
080: * Creates a set of long options.
081: */
082: protected LongOpt[] generateValidOptions() {
083: LongOpt[] lo = new LongOpt[7];
084:
085: lo[0] = new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'V');
086: lo[1] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
087: lo[2] = new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 1);
088:
089: lo[3] = new LongOpt("vdlvs", LongOpt.REQUIRED_ARGUMENT, null,
090: 'v');
091: lo[4] = new LongOpt("vdlns", LongOpt.REQUIRED_ARGUMENT, null,
092: 'n');
093: lo[5] = new LongOpt("namespace", LongOpt.REQUIRED_ARGUMENT,
094: null, 'n');
095: lo[6] = new LongOpt("ns", LongOpt.REQUIRED_ARGUMENT, null, 'n');
096:
097: return lo;
098: }
099:
100: public static void main(String args[]) {
101: String vdlns = null;
102: String vdlvs = null;
103: org.griphyn.vdl.toolkit.VDLtConv me = new org.griphyn.vdl.toolkit.VDLtConv(
104: "vdlt2vdlx");
105:
106: try {
107: // obtain commandline options first -- we may need the database stuff
108: Getopt opts = new Getopt(me.m_application, args, "hn:v:V",
109: me.generateValidOptions());
110: opts.setOpterr(false);
111: int option = 0;
112: while ((option = opts.getopt()) != -1) {
113: switch (option) {
114: case 1:
115: me.increaseVerbosity();
116: break;
117:
118: case 'V':
119: System.out
120: .println("$Id: VDLtConv.java 50 2007-05-19 00:48:32Z gmehta $");
121: System.out.println("VDS version "
122: + Version.instance().toString());
123: return;
124:
125: case 'n':
126: // default namespace option
127: vdlns = opts.getOptarg();
128: break;
129:
130: case 'v':
131: // default version option
132: vdlvs = opts.getOptarg();
133: break;
134:
135: case '?':
136: System.out.println("Invalid option '"
137: + (char) opts.getOptopt() + "'");
138: default:
139: case 'h':
140: me.showUsage();
141: return;
142: }
143: }
144:
145: Reader rd = null;
146: Writer wr = null;
147: int where = opts.getOptind();
148: switch (args.length - where) {
149: case 2:
150: wr = new BufferedWriter(new FileWriter(args[where + 1]));
151: rd = new BufferedReader(new FileReader(args[where + 0]));
152: break;
153: case 1:
154: wr = new OutputStreamWriter(System.out);
155: rd = new BufferedReader(new FileReader(args[where]));
156: break;
157: case 0:
158: System.err.println("# reminder: reading from stdin");
159: wr = new OutputStreamWriter(System.out);
160: rd = new InputStreamReader(System.in);
161: break;
162: default:
163: me.showUsage();
164: throw new RuntimeException(
165: "Illegal number of non-option arguments");
166: }
167:
168: VDLtConvert convert = new VDLtConvert();
169: convert.VDLt2VDLx(rd, wr, vdlns, vdlvs);
170: rd.close();
171: wr.flush();
172: wr.close();
173:
174: } catch (VDLtParserException e) {
175: me.m_logger.log("default", 0, "syntactical error");
176: System.err.println(e.getMessage());
177: System.exit(1);
178: } catch (VDLtScannerException e) {
179: me.m_logger.log("default", 0, "lexical error");
180: System.err.println(e.getMessage());
181: System.exit(1);
182: } catch (IOException e) {
183: me.m_logger.log("default", 0, "I/O error");
184: System.err.println(e.getMessage());
185: System.exit(1);
186: } catch (RuntimeException rte) {
187: me.m_logger.log("default", 0, "runtime error");
188: System.err.println(rte.getMessage());
189: System.exit(1);
190: } catch (Exception e) {
191: me.m_logger.log("default", 0, "unspecified error ");
192: System.err.println(e.getMessage());
193: e.printStackTrace(System.err);
194: System.exit(1);
195: }
196: }
197: }
|