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 java.io.*;
018: import java.sql.SQLException;
019: import java.util.Set;
020: import java.util.TreeSet;
021: import org.griphyn.vdl.directive.*;
022: import org.griphyn.vdl.parser.*;
023: import org.griphyn.vdl.util.Logging;
024: import java.util.regex.*;
025:
026: /**
027: * This class encapsulates common helper functions for VDL parsing.
028: *
029: * @author Jens-S. Vöckler
030: * @author Yong Zhao
031: * @version $Revision: 50 $
032: *
033: * @see UpdateVDC
034: * @see VDLc
035: * @see org.griphyn.vdl.parser.VDLtParser
036: * @see org.griphyn.vdl.parser.VDLxParser
037: */
038: public abstract class VDLHelper extends Toolkit {
039: /**
040: * Pattern to match against when recognizing VDLx from VDLt.
041: * recognizes: "<?xml " "<definition " "<ns1:definition "
042: */
043: private static final String c_pattern = "^\\s*<(?:(?:\\w+:)?definitions|\\?\\s*xml)\\s+";
044:
045: /**
046: * Converter for VDLt to VDLx, is a singleton.
047: */
048: private static VDLtConvert c_t2x = null;
049:
050: /**
051: * ctor: Constructs a new instance object with the given application name.
052: *
053: * @param appName is the name of the shell wrapper with which to report.
054: */
055: public VDLHelper(String appName) {
056: super (appName);
057: }
058:
059: /**
060: * Default ctor: Constructs a new instance object with the given
061: * application name.
062: *
063: * @param appName is the name of the shell wrapper with which to report.
064: * @param verbosity sets the verbosity level of the "app" logging queue.
065: */
066: public VDLHelper(String appName, int verbosity) {
067: super (appName, verbosity);
068: }
069:
070: /**
071: * Determines if a file is VDLx or VDLt. It does so by
072: * expecting the ubiquitous XML preamble in the VDLx.
073: *
074: * @param filename is the name of the file
075: * @return true if the file is XML, false otherwise.
076: */
077: public boolean isXML(String filename) throws IOException {
078: LineNumberReader lnr = new LineNumberReader(new FileReader(
079: filename));
080: String line = null;
081: boolean result = false;
082: java.util.regex.Pattern p = java.util.regex.Pattern
083: .compile(VDLHelper.c_pattern);
084:
085: while ((line = lnr.readLine()) != null && !result
086: && lnr.getLineNumber() < 64) {
087: result = p.matcher(line).lookingAt();
088: }
089: lnr.close();
090: return result;
091: }
092:
093: /**
094: * Determines, if input file is VDLt or VDLx. Parses VDLt into a
095: * temporary VDLx file. Adds VDLx into the VDC.
096: *
097: * @param filename is the file to add, either VDLt or VDLx content.
098: * @param define is a VDC connector to add to the VDC
099: * @param reject is a writer to gather definitions that were reject from
100: * the VDC, depending on the overwrite mode. If <code>null</code>, no rejects
101: * will be gathered.
102: * @param overwrite determines insert or update mode.
103: * @return the result from adding the XML into the VDC
104: * @see org.griphyn.vdl.directive.Define#updateVDC( Reader, Writer, boolean )
105: */
106: public boolean addFileToVDC(String filename, Define define,
107: Writer reject, boolean overwrite)
108: throws VDLtParserException, VDLtScannerException,
109: IOException {
110: // instantiate singleton, if necessary
111: if (c_t2x == null)
112: c_t2x = new VDLtConvert();
113:
114: File tempfn = null;
115: if (isXML(filename)) {
116: // VDLx file, no conversions necessary
117: tempfn = new File(filename);
118: } else {
119: // VDLt file, convert to XML
120: Reader r = new BufferedReader(new FileReader(filename));
121: tempfn = File.createTempFile("vdlc-", ".xml", null);
122: tempfn.deleteOnExit();
123: Writer w = new BufferedWriter(new FileWriter(tempfn));
124: c_t2x.VDLt2VDLx(r, w);
125: r.close();
126: w.flush();
127: w.close();
128: }
129:
130: return define.updateVDC(new BufferedReader(new FileReader(
131: tempfn)), reject, overwrite);
132: }
133:
134: /**
135: * Determines, if input file is VDLt or VDLx. Parses VDLt into a
136: * temporary VDLx file. Adds VDLx into the VDC. Skips to next file on
137: * error.
138: *
139: * @param args is the argument vector of main
140: * @param start is the start of filenames in the argument vector.
141: * @param define is a VDC connector to add to the VDC
142: * @param reject is a writer to gather definitions that were reject from
143: * the VDC, depending on the overwrite mode. If <code>null</code>, no rejects
144: * will be gathered.
145: * @param overwrite determines insert or update mode.
146: * @return a set of filenames which reported errors.
147: * @see #addFileToVDC( String, Define, Writer, boolean )
148: */
149: public Set addFilesToVDC(String[] args, int start, Define define,
150: Writer reject, boolean overwrite) {
151: Set result = new TreeSet();
152:
153: for (int i = start; i < args.length; ++i) {
154: m_logger.log("app", 1, "parsing \"" + args[i] + "\"");
155:
156: try {
157: if (!addFileToVDC(args[i], define, reject, overwrite)) {
158: // unsuccessful parsing
159: m_logger.log("default", 0,
160: "XML parsing error, skipping to next file");
161: result.add(args[i]);
162: }
163: } catch (VDLtParserException e) {
164: m_logger.log("default", 0,
165: "syntactical error, skipping to next file");
166: System.err.println(e.getMessage());
167: result.add(args[i]);
168: } catch (VDLtScannerException e) {
169: m_logger.log("default", 0,
170: "lexical error, skipping to next file");
171: System.err.println(e.getMessage());
172: result.add(args[i]);
173: } catch (IOException e) {
174: m_logger.log("default", 0,
175: "I/O error, skipping to next file");
176: System.err.println(e.getMessage());
177: result.add(args[i]);
178: }
179: }
180:
181: return result;
182: }
183: }
|