8130238: Remove com.sun.org.apache.xalan.internal.xsltc.cmdline
Reviewed-by: lancea, joehw
This commit is contained in:
parent
52cdae26ef
commit
18b9b116dc
@ -1,171 +0,0 @@
|
||||
/*
|
||||
* reserved comment block
|
||||
* DO NOT REMOVE OR ALTER!
|
||||
*/
|
||||
/*
|
||||
* Copyright 2001-2004 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* $Id: Compile.java,v 1.2.4.1 2005/08/31 11:24:13 pvedula Exp $
|
||||
*/
|
||||
|
||||
package com.sun.org.apache.xalan.internal.xsltc.cmdline;
|
||||
|
||||
import com.sun.org.apache.xalan.internal.utils.FeatureManager;
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.Vector;
|
||||
|
||||
import com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt.GetOpt;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt.GetOptsException;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
|
||||
|
||||
/**
|
||||
* @author Jacek Ambroziak
|
||||
* @author Santiago Pericas-Geertsen
|
||||
* @author G. Todd Miller
|
||||
* @author Morten Jorgensen
|
||||
*/
|
||||
public final class Compile {
|
||||
|
||||
// Versioning numbers for the compiler -v option output
|
||||
private static int VERSION_MAJOR = 1;
|
||||
private static int VERSION_MINOR = 4;
|
||||
private static int VERSION_DELTA = 0;
|
||||
|
||||
|
||||
|
||||
// This variable should be set to false to prevent any methods in this
|
||||
// class from calling System.exit(). As this is a command-line tool,
|
||||
// calling System.exit() is normally OK, but we also want to allow for
|
||||
// this class being used in other ways as well.
|
||||
private static boolean _allowExit = true;
|
||||
|
||||
|
||||
public static void printUsage() {
|
||||
System.err.println("XSLTC version " +
|
||||
VERSION_MAJOR + "." + VERSION_MINOR +
|
||||
((VERSION_DELTA > 0) ? ("." + VERSION_DELTA) : ("")) + "\n" +
|
||||
new ErrorMsg(ErrorMsg.COMPILE_USAGE_STR));
|
||||
if (_allowExit) System.exit(-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method implements the command line compiler. See the USAGE_STRING
|
||||
* constant for a description. It may make sense to move the command-line
|
||||
* handling to a separate package (ie. make one xsltc.cmdline.Compiler
|
||||
* class that contains this main() method and one xsltc.cmdline.Transform
|
||||
* class that contains the DefaultRun stuff).
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
boolean inputIsURL = false;
|
||||
boolean useStdIn = false;
|
||||
boolean classNameSet = false;
|
||||
final GetOpt getopt = new GetOpt(args, "o:d:j:p:uxhsinv");
|
||||
if (args.length < 1) printUsage();
|
||||
|
||||
final XSLTC xsltc = new XSLTC(true, new FeatureManager());
|
||||
xsltc.init();
|
||||
|
||||
int c;
|
||||
while ((c = getopt.getNextOption()) != -1) {
|
||||
switch(c) {
|
||||
case 'i':
|
||||
useStdIn = true;
|
||||
break;
|
||||
case 'o':
|
||||
xsltc.setClassName(getopt.getOptionArg());
|
||||
classNameSet = true;
|
||||
break;
|
||||
case 'd':
|
||||
xsltc.setDestDirectory(getopt.getOptionArg());
|
||||
break;
|
||||
case 'p':
|
||||
xsltc.setPackageName(getopt.getOptionArg());
|
||||
break;
|
||||
case 'j':
|
||||
xsltc.setJarFileName(getopt.getOptionArg());
|
||||
break;
|
||||
case 'x':
|
||||
xsltc.setDebug(true);
|
||||
break;
|
||||
case 'u':
|
||||
inputIsURL = true;
|
||||
break;
|
||||
case 's':
|
||||
_allowExit = false;
|
||||
break;
|
||||
case 'n':
|
||||
xsltc.setTemplateInlining(true); // used to be 'false'
|
||||
break;
|
||||
case 'v':
|
||||
// fall through to case h
|
||||
case 'h':
|
||||
default:
|
||||
printUsage();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
boolean compileOK;
|
||||
|
||||
if (useStdIn) {
|
||||
if (!classNameSet) {
|
||||
System.err.println(new ErrorMsg(ErrorMsg.COMPILE_STDIN_ERR));
|
||||
if (_allowExit) System.exit(-1);
|
||||
}
|
||||
compileOK = xsltc.compile(System.in, xsltc.getClassName());
|
||||
}
|
||||
else {
|
||||
// Generate a vector containg URLs for all stylesheets specified
|
||||
final String[] stylesheetNames = getopt.getCmdArgs();
|
||||
final Vector stylesheetVector = new Vector();
|
||||
for (int i = 0; i < stylesheetNames.length; i++) {
|
||||
final String name = stylesheetNames[i];
|
||||
URL url;
|
||||
if (inputIsURL)
|
||||
url = new URL(name);
|
||||
else
|
||||
url = (new File(name)).toURI().toURL();
|
||||
stylesheetVector.addElement(url);
|
||||
}
|
||||
compileOK = xsltc.compile(stylesheetVector);
|
||||
}
|
||||
|
||||
// Compile the stylesheet and output class/jar file(s)
|
||||
if (compileOK) {
|
||||
xsltc.printWarnings();
|
||||
if (xsltc.getJarFileName() != null) xsltc.outputToJar();
|
||||
if (_allowExit) System.exit(0);
|
||||
}
|
||||
else {
|
||||
xsltc.printWarnings();
|
||||
xsltc.printErrors();
|
||||
if (_allowExit) System.exit(-1);
|
||||
}
|
||||
}
|
||||
catch (GetOptsException ex) {
|
||||
System.err.println(ex);
|
||||
printUsage(); // exits with code '-1'
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
if (_allowExit) System.exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,292 +0,0 @@
|
||||
/*
|
||||
* reserved comment block
|
||||
* DO NOT REMOVE OR ALTER!
|
||||
*/
|
||||
/*
|
||||
* Copyright 2001-2004 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* $Id: Transform.java,v 1.2.4.1 2005/09/12 09:07:33 pvedula Exp $
|
||||
*/
|
||||
|
||||
package com.sun.org.apache.xalan.internal.xsltc.cmdline;
|
||||
|
||||
import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.StripFilter;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.TransletException;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.dom.DOMWSFilter;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.runtime.Constants;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.runtime.Parameter;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.runtime.output.TransletOutputHandlerFactory;
|
||||
import com.sun.org.apache.xml.internal.dtm.DTMWSFilter;
|
||||
import com.sun.org.apache.xml.internal.serializer.SerializationHandler;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Vector;
|
||||
import javax.xml.parsers.SAXParser;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
/**
|
||||
* @author Jacek Ambroziak
|
||||
* @author Santiago Pericas-Geertsen
|
||||
* @author G. Todd Miller
|
||||
* @author Morten Jorgensen
|
||||
*/
|
||||
final public class Transform {
|
||||
|
||||
private SerializationHandler _handler;
|
||||
|
||||
private String _fileName;
|
||||
private String _className;
|
||||
private String _jarFileSrc;
|
||||
private boolean _isJarFileSpecified = false;
|
||||
private Vector _params = null;
|
||||
private boolean _uri, _debug;
|
||||
private int _iterations;
|
||||
|
||||
public Transform(String className, String fileName,
|
||||
boolean uri, boolean debug, int iterations) {
|
||||
_fileName = fileName;
|
||||
_className = className;
|
||||
_uri = uri;
|
||||
_debug = debug;
|
||||
_iterations = iterations;
|
||||
}
|
||||
|
||||
public String getFileName(){return _fileName;}
|
||||
public String getClassName(){return _className;}
|
||||
|
||||
public void setParameters(Vector params) {
|
||||
_params = params;
|
||||
}
|
||||
|
||||
private void setJarFileInputSrc(boolean flag, String jarFile) {
|
||||
// TODO: at this time we do not do anything with this
|
||||
// information, attempts to add the jarfile to the CLASSPATH
|
||||
// were successful via System.setProperty, but the effects
|
||||
// were not visible to the running JVM. For now we add jarfile
|
||||
// to CLASSPATH in the wrapper script that calls this program.
|
||||
_isJarFileSpecified = flag;
|
||||
// TODO verify jarFile exists...
|
||||
_jarFileSrc = jarFile;
|
||||
}
|
||||
|
||||
private void doTransform() {
|
||||
try {
|
||||
final Class clazz = ObjectFactory.findProviderClass(_className, true);
|
||||
final AbstractTranslet translet = (AbstractTranslet)clazz.newInstance();
|
||||
translet.postInitialization();
|
||||
|
||||
// Create a SAX parser and get the XMLReader object it uses
|
||||
final SAXParserFactory factory = SAXParserFactory.newInstance();
|
||||
try {
|
||||
factory.setFeature(Constants.NAMESPACE_FEATURE,true);
|
||||
}
|
||||
catch (Exception e) {
|
||||
factory.setNamespaceAware(true);
|
||||
}
|
||||
final SAXParser parser = factory.newSAXParser();
|
||||
final XMLReader reader = parser.getXMLReader();
|
||||
|
||||
// Set the DOM's DOM builder as the XMLReader's SAX2 content handler
|
||||
XSLTCDTMManager dtmManager =
|
||||
XSLTCDTMManager.createNewDTMManagerInstance();
|
||||
|
||||
DTMWSFilter wsfilter;
|
||||
if (translet != null && translet instanceof StripFilter) {
|
||||
wsfilter = new DOMWSFilter(translet);
|
||||
} else {
|
||||
wsfilter = null;
|
||||
}
|
||||
|
||||
final DOMEnhancedForDTM dom =
|
||||
(DOMEnhancedForDTM)dtmManager.getDTM(
|
||||
new SAXSource(reader, new InputSource(_fileName)),
|
||||
false, wsfilter, true, false, translet.hasIdCall());
|
||||
|
||||
dom.setDocumentURI(_fileName);
|
||||
translet.prepassDocument(dom);
|
||||
|
||||
// Pass global parameters
|
||||
int n = _params.size();
|
||||
for (int i = 0; i < n; i++) {
|
||||
Parameter param = (Parameter) _params.elementAt(i);
|
||||
translet.addParameter(param._name, param._value);
|
||||
}
|
||||
|
||||
// Transform the document
|
||||
TransletOutputHandlerFactory tohFactory =
|
||||
TransletOutputHandlerFactory.newInstance();
|
||||
tohFactory.setOutputType(TransletOutputHandlerFactory.STREAM);
|
||||
tohFactory.setEncoding(translet._encoding);
|
||||
tohFactory.setOutputMethod(translet._method);
|
||||
|
||||
if (_iterations == -1) {
|
||||
translet.transform(dom, tohFactory.getSerializationHandler());
|
||||
}
|
||||
else if (_iterations > 0) {
|
||||
long mm = System.currentTimeMillis();
|
||||
for (int i = 0; i < _iterations; i++) {
|
||||
translet.transform(dom,
|
||||
tohFactory.getSerializationHandler());
|
||||
}
|
||||
mm = System.currentTimeMillis() - mm;
|
||||
|
||||
System.err.println("\n<!--");
|
||||
System.err.println(" transform = "
|
||||
+ (((double) mm) / ((double) _iterations))
|
||||
+ " ms");
|
||||
System.err.println(" throughput = "
|
||||
+ (1000.0 / (((double) mm)
|
||||
/ ((double) _iterations)))
|
||||
+ " tps");
|
||||
System.err.println("-->");
|
||||
}
|
||||
}
|
||||
catch (TransletException e) {
|
||||
if (_debug) e.printStackTrace();
|
||||
System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
|
||||
e.getMessage());
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
if (_debug) e.printStackTrace();
|
||||
System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
|
||||
e.getMessage());
|
||||
}
|
||||
catch (FileNotFoundException e) {
|
||||
if (_debug) e.printStackTrace();
|
||||
ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_NOT_FOUND_ERR, _fileName);
|
||||
System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
|
||||
err.toString());
|
||||
}
|
||||
catch (MalformedURLException e) {
|
||||
if (_debug) e.printStackTrace();
|
||||
ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_URI_ERR, _fileName);
|
||||
System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
|
||||
err.toString());
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
if (_debug) e.printStackTrace();
|
||||
ErrorMsg err= new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR,_className);
|
||||
System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
|
||||
err.toString());
|
||||
}
|
||||
catch (UnknownHostException e) {
|
||||
if (_debug) e.printStackTrace();
|
||||
ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_URI_ERR, _fileName);
|
||||
System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
|
||||
err.toString());
|
||||
}
|
||||
catch (SAXException e) {
|
||||
Exception ex = e.getException();
|
||||
if (_debug) {
|
||||
if (ex != null) ex.printStackTrace();
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.err.print(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY));
|
||||
if (ex != null)
|
||||
System.err.println(ex.getMessage());
|
||||
else
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (_debug) e.printStackTrace();
|
||||
System.err.println(new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)+
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void printUsage() {
|
||||
System.err.println(new ErrorMsg(ErrorMsg.TRANSFORM_USAGE_STR));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
if (args.length > 0) {
|
||||
int i;
|
||||
int iterations = -1;
|
||||
boolean uri = false, debug = false;
|
||||
boolean isJarFileSpecified = false;
|
||||
String jarFile = null;
|
||||
|
||||
// Parse options starting with '-'
|
||||
for (i = 0; i < args.length && args[i].charAt(0) == '-'; i++) {
|
||||
if (args[i].equals("-u")) {
|
||||
uri = true;
|
||||
}
|
||||
else if (args[i].equals("-x")) {
|
||||
debug = true;
|
||||
}
|
||||
else if (args[i].equals("-j")) {
|
||||
isJarFileSpecified = true;
|
||||
jarFile = args[++i];
|
||||
}
|
||||
else if (args[i].equals("-n")) {
|
||||
try {
|
||||
iterations = Integer.parseInt(args[++i]);
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
else {
|
||||
printUsage();
|
||||
}
|
||||
}
|
||||
|
||||
// Enough arguments left ?
|
||||
if (args.length - i < 2) printUsage();
|
||||
|
||||
// Get document file and class name
|
||||
Transform handler = new Transform(args[i+1], args[i], uri,
|
||||
debug, iterations);
|
||||
handler.setJarFileInputSrc(isJarFileSpecified, jarFile);
|
||||
|
||||
// Parse stylesheet parameters
|
||||
Vector params = new Vector();
|
||||
for (i += 2; i < args.length; i++) {
|
||||
final int equal = args[i].indexOf('=');
|
||||
if (equal > 0) {
|
||||
final String name = args[i].substring(0, equal);
|
||||
final String value = args[i].substring(equal+1);
|
||||
params.addElement(new Parameter(name, value));
|
||||
}
|
||||
else {
|
||||
printUsage();
|
||||
}
|
||||
}
|
||||
|
||||
if (i == args.length) {
|
||||
handler.setParameters(params);
|
||||
handler.doTransform();
|
||||
}
|
||||
} else {
|
||||
printUsage();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,258 +0,0 @@
|
||||
/*
|
||||
* reserved comment block
|
||||
* DO NOT REMOVE OR ALTER!
|
||||
*/
|
||||
/*
|
||||
* Copyright 2001-2004 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* $Id: GetOpt.java,v 1.2.4.1 2005/08/31 11:46:04 pvedula Exp $
|
||||
*/
|
||||
|
||||
package com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
|
||||
|
||||
|
||||
/**
|
||||
* GetOpt is a Java equivalent to the C getopt() library function
|
||||
* discussed in man page getopt(3C). It provides command line
|
||||
* parsing for Java applications. It supports the most rules of the
|
||||
* command line standard (see man page intro(1)) including stacked
|
||||
* options such as '-sxm' (which is equivalent to -s -x -m); it
|
||||
* handles special '--' option that signifies the end of options.
|
||||
* Additionally this implementation of getopt will check for
|
||||
* mandatory arguments to options such as in the case of
|
||||
* '-d <file>' it will throw a MissingOptArgException if the
|
||||
* option argument '<file>' is not included on the commandline.
|
||||
* getopt(3C) does not check for this.
|
||||
* @author G Todd Miller
|
||||
*/
|
||||
public class GetOpt{
|
||||
public GetOpt(String[] args, String optString){
|
||||
theOptions = new ArrayList();
|
||||
int currOptIndex = 0;
|
||||
theCmdArgs = new ArrayList();
|
||||
theOptionMatcher = new OptionMatcher(optString);
|
||||
// fill in the options list
|
||||
for(int i=0; i<args.length; i++){
|
||||
String token = args[i];
|
||||
int tokenLength = token.length();
|
||||
if(token.equals("--")){ // end of opts
|
||||
currOptIndex = i+1; // set index of first operand
|
||||
break; // end of options
|
||||
}
|
||||
else if(token.startsWith("-") && tokenLength == 2){
|
||||
// simple option token such as '-s' found
|
||||
theOptions.add(new Option(token.charAt(1)));
|
||||
}
|
||||
else if(token.startsWith("-") && tokenLength > 2){
|
||||
// stacked options found, such as '-shm'
|
||||
// iterate thru the tokens after the dash and
|
||||
// add them to theOptions list
|
||||
for(int j=1; j<tokenLength; j++){
|
||||
theOptions.add(new Option(token.charAt(j)));
|
||||
}
|
||||
}
|
||||
else if(!token.startsWith("-")){
|
||||
// case 1- there are not options stored yet therefore
|
||||
// this must be an command argument, not an option argument
|
||||
if(theOptions.size() == 0){
|
||||
currOptIndex = i;
|
||||
break; // stop processing options
|
||||
}
|
||||
else {
|
||||
// case 2-
|
||||
// there are options stored, check to see if
|
||||
// this arg belong to the last arg stored
|
||||
int indexoflast=0;
|
||||
indexoflast = theOptions.size()-1;
|
||||
Option op = (Option)theOptions.get(indexoflast);
|
||||
char opLetter = op.getArgLetter();
|
||||
if(!op.hasArg() && theOptionMatcher.hasArg(opLetter)){
|
||||
op.setArg(token);
|
||||
}
|
||||
else{
|
||||
// case 3 -
|
||||
// the last option stored does not take
|
||||
// an argument, so again, this argument
|
||||
// must be a command argument, not
|
||||
// an option argument
|
||||
currOptIndex = i;
|
||||
break; // end of options
|
||||
}
|
||||
}
|
||||
}// end option does not start with "-"
|
||||
} // end for args loop
|
||||
|
||||
// attach an iterator to list of options
|
||||
theOptionsIterator = theOptions.listIterator();
|
||||
|
||||
// options are done, now fill out cmd arg list with remaining args
|
||||
for(int i=currOptIndex; i<args.length; i++){
|
||||
String token = args[i];
|
||||
theCmdArgs.add(token);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* debugging routine to print out all options collected
|
||||
*/
|
||||
public void printOptions(){
|
||||
for(ListIterator it=theOptions.listIterator(); it.hasNext();){
|
||||
Option opt = (Option)it.next();
|
||||
System.out.print("OPT =" + opt.getArgLetter());
|
||||
String arg = opt.getArgument();
|
||||
if(arg != null){
|
||||
System.out.print(" " + arg);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* gets the next option found in the commandline. Distinguishes
|
||||
* between two bad cases, one case is when an illegal option
|
||||
* is found, and then other case is when an option takes an
|
||||
* argument but no argument was found for that option.
|
||||
* If the option found was not declared in the optString, then
|
||||
* an IllegalArgumentException will be thrown (case 1).
|
||||
* If the next option found has been declared to take an argument,
|
||||
* and no such argument exists, then a MissingOptArgException
|
||||
* is thrown (case 2).
|
||||
* @param none
|
||||
* @return int - the next option found.
|
||||
* @throws IllegalArgumentException, MissingOptArgException.
|
||||
*/
|
||||
public int getNextOption() throws IllegalArgumentException,
|
||||
MissingOptArgException
|
||||
{
|
||||
int retval = -1;
|
||||
if(theOptionsIterator.hasNext()){
|
||||
theCurrentOption = (Option)theOptionsIterator.next();
|
||||
char c = theCurrentOption.getArgLetter();
|
||||
boolean shouldHaveArg = theOptionMatcher.hasArg(c);
|
||||
String arg = theCurrentOption.getArgument();
|
||||
if(!theOptionMatcher.match(c)) {
|
||||
ErrorMsg msg = new ErrorMsg(ErrorMsg.ILLEGAL_CMDLINE_OPTION_ERR,
|
||||
new Character(c));
|
||||
throw (new IllegalArgumentException(msg.toString()));
|
||||
}
|
||||
else if(shouldHaveArg && (arg == null)) {
|
||||
ErrorMsg msg = new ErrorMsg(ErrorMsg.CMDLINE_OPT_MISSING_ARG_ERR,
|
||||
new Character(c));
|
||||
throw (new MissingOptArgException(msg.toString()));
|
||||
}
|
||||
retval = c;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets the argument for the current parsed option. For example,
|
||||
* in case of '-d <file>', if current option parsed is 'd' then
|
||||
* getOptionArg() would return '<file>'.
|
||||
* @return String - argument for current parsed option.
|
||||
* @param none
|
||||
*/
|
||||
public String getOptionArg(){
|
||||
String retval = null;
|
||||
String tmp = theCurrentOption.getArgument();
|
||||
char c = theCurrentOption.getArgLetter();
|
||||
if(theOptionMatcher.hasArg(c)){
|
||||
retval = tmp;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets list of the commandline arguments. For example, in command
|
||||
* such as 'cmd -s -d file file2 file3 file4' with the usage
|
||||
* 'cmd [-s] [-d <file>] <file>...', getCmdArgs() would return
|
||||
* the list {file2, file3, file4}.
|
||||
* @return String[] - list of command arguments that may appear
|
||||
* after options and option arguments.
|
||||
* @params none
|
||||
*/
|
||||
public String[] getCmdArgs(){
|
||||
String[] retval = new String[theCmdArgs.size()];
|
||||
int i=0;
|
||||
for(ListIterator it=theCmdArgs.listIterator(); it.hasNext();){
|
||||
retval[i++] = (String)it.next();
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
private Option theCurrentOption = null;
|
||||
private ListIterator theOptionsIterator;
|
||||
private List theOptions = null;
|
||||
private List theCmdArgs = null;
|
||||
private OptionMatcher theOptionMatcher = null;
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
//
|
||||
// Inner Classes
|
||||
//
|
||||
///////////////////////////////////////////////////////////
|
||||
|
||||
// inner class to model an option
|
||||
class Option{
|
||||
private char theArgLetter;
|
||||
private String theArgument = null;
|
||||
public Option(char argLetter) { theArgLetter = argLetter; }
|
||||
public void setArg(String arg) {
|
||||
theArgument = arg;
|
||||
}
|
||||
public boolean hasArg() { return (theArgument != null); }
|
||||
public char getArgLetter() { return theArgLetter; }
|
||||
public String getArgument() { return theArgument; }
|
||||
} // end class Option
|
||||
|
||||
|
||||
// inner class to query optString for a possible option match,
|
||||
// and whether or not a given legal option takes an argument.
|
||||
//
|
||||
class OptionMatcher{
|
||||
public OptionMatcher(String optString){
|
||||
theOptString = optString;
|
||||
}
|
||||
public boolean match(char c){
|
||||
boolean retval = false;
|
||||
if(theOptString.indexOf(c) != -1){
|
||||
retval = true;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
public boolean hasArg(char c){
|
||||
boolean retval = false;
|
||||
int index = theOptString.indexOf(c)+1;
|
||||
if (index == theOptString.length()){
|
||||
// reached end of theOptString
|
||||
retval = false;
|
||||
}
|
||||
else if(theOptString.charAt(index) == ':'){
|
||||
retval = true;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
private String theOptString = null;
|
||||
} // end class OptionMatcher
|
||||
}// end class GetOpt
|
@ -1,34 +0,0 @@
|
||||
/*
|
||||
* reserved comment block
|
||||
* DO NOT REMOVE OR ALTER!
|
||||
*/
|
||||
/*
|
||||
* Copyright 2001-2004 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* $Id: GetOptsException.java,v 1.2.4.1 2005/08/31 11:47:06 pvedula Exp $
|
||||
*/
|
||||
|
||||
package com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt;
|
||||
|
||||
/**
|
||||
* @author G Todd Miller
|
||||
*/
|
||||
public class GetOptsException extends Exception{
|
||||
static final long serialVersionUID = 8736874967183039804L;
|
||||
public GetOptsException(String msg){
|
||||
super(msg);
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
/*
|
||||
* reserved comment block
|
||||
* DO NOT REMOVE OR ALTER!
|
||||
*/
|
||||
/*
|
||||
* Copyright 2001-2004 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* $Id: IllegalArgumentException.java,v 1.2.4.1 2005/08/31 11:47:56 pvedula Exp $
|
||||
*/
|
||||
|
||||
package com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt;
|
||||
|
||||
|
||||
class IllegalArgumentException extends GetOptsException{
|
||||
static final long serialVersionUID = 8642122427294793651L;
|
||||
public IllegalArgumentException(String msg){
|
||||
super(msg);
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
/*
|
||||
* reserved comment block
|
||||
* DO NOT REMOVE OR ALTER!
|
||||
*/
|
||||
/*
|
||||
* Copyright 2001-2004 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* $Id: MissingOptArgException.java,v 1.2.4.1 2005/08/31 11:49:21 pvedula Exp $
|
||||
*/
|
||||
|
||||
package com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt;
|
||||
|
||||
|
||||
/**
|
||||
* @author G Todd Miller
|
||||
*/
|
||||
class MissingOptArgException extends GetOptsException{
|
||||
static final long serialVersionUID = -1972471465394544822L;
|
||||
public MissingOptArgException(String msg){
|
||||
super(msg);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user