{
useXSLTC = true;
}
}
TransformerFactory tfactory;
if (useXSLTC)
{
String key = "javax.xml.transform.TransformerFactory";
String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl";
Properties props = System.getProperties();
props.put(key, value);
System.setProperties(props);
}
try
{
tfactory = TransformerFactory.newInstance();
}
catch (TransformerFactoryConfigurationError pfe)
{
pfe.printStackTrace(dumpWriter);
diagnosticsWriter.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_NOT_SUCCESSFUL, null)); //"XSL Process was not successful.");
tfactory = null; // shut up compiler
doExit(-1);
}
boolean formatOutput = false;
boolean useSourceLocation = false;
String inFileName = null;
String outFileName = null;
String dumpFileName = null;
String xslFileName = null;
String treedumpFileName = null;
PrintTraceListener tracer = null;
String outputType = null;
String media = null;
Vector params = new Vector();
boolean quietConflictWarnings = false;
URIResolver uriResolver = null;
EntityResolver entityResolver = null;
ContentHandler contentHandler = null;
int recursionLimit=-1;
for (int i = 0; i < argv.length; i++)
{
if ("-XSLTC".equalsIgnoreCase(argv[i]))
{
// The -XSLTC option has been processed.
}
else if ("-TT".equalsIgnoreCase(argv[i]))
{
if (!useXSLTC)
{
if (null == tracer)
tracer = new PrintTraceListener(diagnosticsWriter);
tracer.m_traceTemplates = true;
}
else
printInvalidXSLTCOption("-TT");
// tfactory.setTraceTemplates(true);
}
else if ("-TG".equalsIgnoreCase(argv[i]))
{
if (!useXSLTC)
{
if (null == tracer)
tracer = new PrintTraceListener(diagnosticsWriter);
tracer.m_traceGeneration = true;
}
else
printInvalidXSLTCOption("-TG");
// tfactory.setTraceSelect(true);
}
else if ("-TS".equalsIgnoreCase(argv[i]))
{
if (!useXSLTC)
{
if (null == tracer)
tracer = new PrintTraceListener(diagnosticsWriter);
tracer.m_traceSelection = true;
}
else
printInvalidXSLTCOption("-TS");
// tfactory.setTraceTemplates(true);
}
else if ("-TTC".equalsIgnoreCase(argv[i]))
{
if (!useXSLTC)
{
if (null == tracer)
tracer = new PrintTraceListener(diagnosticsWriter);
tracer.m_traceElements = true;
}
else
printInvalidXSLTCOption("-TTC");
// tfactory.setTraceTemplateChildren(true);
}
else if ("-INDENT".equalsIgnoreCase(argv[i]))
{
int indentAmount;
if (((i + 1) < argv.length) && (argv[i + 1].charAt(0) != '-'))
{
indentAmount = Integer.parseInt(argv[++i]);
}
else
{
indentAmount = 0;
}
// TBD:
// xmlProcessorLiaison.setIndent(indentAmount);
}
else if ("-IN".equalsIgnoreCase(argv[i]))
{
if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
inFileName = argv[++i];
else
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
new Object[]{ "-IN" })); //"Missing argument for);
}
else if ("-MEDIA".equalsIgnoreCase(argv[i]))
{
if (i + 1 < argv.length)
media = argv[++i];
else
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
new Object[]{ "-MEDIA" })); //"Missing argument for);
}
else if ("-OUT".equalsIgnoreCase(argv[i]))
{
if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
outFileName = argv[++i];
else
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
new Object[]{ "-OUT" })); //"Missing argument for);
}
else if ("-XSL".equalsIgnoreCase(argv[i]))
{
if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
xslFileName = argv[++i];
else
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
new Object[]{ "-XSL" })); //"Missing argument for);
}
else if ("-FLAVOR".equalsIgnoreCase(argv[i]))
{
if (i + 1 < argv.length)
{
flavor = argv[++i];
}
else
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
new Object[]{ "-FLAVOR" })); //"Missing argument for);
}
else if ("-PARAM".equalsIgnoreCase(argv[i]))
{
if (i + 2 < argv.length)
{
String name = argv[++i];
params.addElement(name);
String expression = argv[++i];
params.addElement(expression);
}
else
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
new Object[]{ "-PARAM" })); //"Missing argument for);
}
else if ("-E".equalsIgnoreCase(argv[i]))
{
// TBD:
// xmlProcessorLiaison.setShouldExpandEntityRefs(false);
}
else if ("-V".equalsIgnoreCase(argv[i]))
{
diagnosticsWriter.println(resbundle.getString("version") //">>>>>>> Xalan Version "
+ XSLProcessorVersion.S_VERSION + ", " +
/* xmlProcessorLiaison.getParserDescription()+ */
resbundle.getString("version2")); // "<<<<<<<");
}
else if ("-QC".equalsIgnoreCase(argv[i]))
{
if (!useXSLTC)
quietConflictWarnings = true;
else
printInvalidXSLTCOption("-QC");
}
else if ("-Q".equalsIgnoreCase(argv[i]))
{
setQuietMode = true;
}
else if ("-DIAG".equalsIgnoreCase(argv[i]))
{
doDiag = true;
}
else if ("-XML".equalsIgnoreCase(argv[i]))
{
outputType = "xml";
}
else if ("-TEXT".equalsIgnoreCase(argv[i]))
{
outputType = "text";
}
else if ("-HTML".equalsIgnoreCase(argv[i]))
{
outputType = "html";
}
else if ("-EDUMP".equalsIgnoreCase(argv[i]))
{
doStackDumpOnError = true;
if (((i + 1) < argv.length) && (argv[i + 1].charAt(0) != '-'))
{
dumpFileName = argv[++i];
}
}
else if ("-URIRESOLVER".equalsIgnoreCase(argv[i]))
{
if (i + 1 < argv.length)
{
try
{
uriResolver =
(URIResolver) Class.forName(argv[++i]).newInstance();
tfactory.setURIResolver(uriResolver);
}
catch (Exception cnfe)
{
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION,
new Object[]{ "-URIResolver" }));
doExit(-1);
}
}
else
{
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
new Object[]{ "-URIResolver" })); //"Missing argument for);
doExit(-1);
}
}
else if ("-ENTITYRESOLVER".equalsIgnoreCase(argv[i]))
{
if (i + 1 < argv.length)
{
try
{
entityResolver =
(EntityResolver) Class.forName(argv[++i]).newInstance();
}
catch (Exception cnfe)
{
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION,
new Object[]{ "-EntityResolver" }));
doExit(-1);
}
}
else
{
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
new Object[]{ "-EntityResolver" })); //"Missing argument for);
doExit(-1);
}
}
else if ("-CONTENTHANDLER".equalsIgnoreCase(argv[i]))
{
if (i + 1 < argv.length)
{
try
{
contentHandler =
(ContentHandler) Class.forName(argv[++i]).newInstance();
}
catch (Exception cnfe)
{
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION,
new Object[]{ "-ContentHandler" }));
doExit(-1);
}
}
else
{
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
new Object[]{ "-ContentHandler" })); //"Missing argument for);
doExit(-1);
}
}
else if ("-L".equalsIgnoreCase(argv[i]))
{
if (!useXSLTC)
useSourceLocation = true;
else
printInvalidXSLTCOption("-L");
}
else if ("-INCREMENTAL".equalsIgnoreCase(argv[i]))
{
if (!useXSLTC)
tfactory.setAttribute
("http://xml.apache.org/xalan/features/incremental",
java.lang.Boolean.TRUE);
else
printInvalidXSLTCOption("-INCREMENTAL");
}
else if ("-NOOPTIMIZE".equalsIgnoreCase(argv[i]))
{
// Default is true.
//
// %REVIEW% We should have a generalized syntax for negative
// switches... and probably should accept the inverse even
// if it is the default.
if (!useXSLTC)
tfactory.setAttribute
("http://xml.apache.org/xalan/features/optimize",
java.lang.Boolean.FALSE);
else
printInvalidXSLTCOption("-NOOPTIMIZE");
}
else if ("-RL".equalsIgnoreCase(argv[i]))
{
if (!useXSLTC)
{
if (i + 1 < argv.length)
recursionLimit = Integer.parseInt(argv[++i]);
else
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
new Object[]{ "-rl" })); //"Missing argument for);
}
else
{
if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
i++;
printInvalidXSLTCOption("-RL");
}
}
// Generate the translet class and optionally specify the name
// of the translet class.
else if ("-XO".equalsIgnoreCase(argv[i]))
{
if (useXSLTC)
{
if (i + 1 < argv.length && argv[i+1].charAt(0) != '-')
{
tfactory.setAttribute("generate-translet", "true");
tfactory.setAttribute("translet-name", argv[++i]);
}
else
tfactory.setAttribute("generate-translet", "true");
}
else
{
if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
i++;
printInvalidXalanOption("-XO");
}
}
// Specify the destination directory for the translet classes.
else if ("-XD".equalsIgnoreCase(argv[i]))
{
if (useXSLTC)
{
if (i + 1 < argv.length && argv[i+1].charAt(0) != '-')
tfactory.setAttribute("destination-directory", argv[++i]);
else
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
new Object[]{ "-XD" })); //"Missing argument for);
}
else
{
if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
i++;
printInvalidXalanOption("-XD");
}
}
// Specify the jar file name which the translet classes are packaged into.
else if ("-XJ".equalsIgnoreCase(argv[i]))
{
if (useXSLTC)
{
if (i + 1 < argv.length && argv[i+1].charAt(0) != '-')
{
tfactory.setAttribute("generate-translet", "true");
tfactory.setAttribute("jar-name", argv[++i]);
}
else
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
new Object[]{ "-XJ" })); //"Missing argument for);
}
else
{
if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
i++;
printInvalidXalanOption("-XJ");
}
}
// Specify the package name prefix for the generated translet classes.
else if ("-XP".equalsIgnoreCase(argv[i]))
{
if (useXSLTC)
{
if (i + 1 < argv.length && argv[i+1].charAt(0) != '-')
tfactory.setAttribute("package-name", argv[++i]);
else
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
new Object[]{ "-XP" })); //"Missing argument for);
}
else
{
if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
i++;
printInvalidXalanOption("-XP");
}
}
// Enable template inlining.
else if ("-XN".equalsIgnoreCase(argv[i]))
{
if (useXSLTC)
{
tfactory.setAttribute("enable-inlining", "true");
}
else
printInvalidXalanOption("-XN");
}
// Turns on additional debugging message output
else if ("-XX".equalsIgnoreCase(argv[i]))
{
if (useXSLTC)
{
tfactory.setAttribute("debug", "true");
}
else
printInvalidXalanOption("-XX");
}
// Create the Transformer from the translet if the translet class is newer
// than the stylesheet.
else if ("-XT".equalsIgnoreCase(argv[i]))
{
if (useXSLTC)
{
tfactory.setAttribute("auto-translet", "true");
}
else
printInvalidXalanOption("-XT");
}
else
System.err.println(
XSLMessages.createMessage(
XSLTErrorResources.ER_INVALID_OPTION, new Object[]{ argv[i] })); //"Invalid argument:);
}
// Print usage instructions if no xml and xsl file is specified in the command line
if (inFileName == null && xslFileName == null)
{
System.err.println(resbundle.getString("xslProc_no_input"));
doExit(-1);
}
// Note that there are usage cases for calling us without a -IN arg
// The main XSL transformation occurs here!
try
{
long start = System.currentTimeMillis();
if (null != dumpFileName)
{
dumpWriter = new PrintWriter(new FileWriter(dumpFileName));
}
Templates stylesheet = null;
if (null != xslFileName)
{
if (flavor.equals("d2d"))
{
// Parse in the xml data into a DOM
DocumentBuilderFactory dfactory =
DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
Node xslDOM = docBuilder.parse(new InputSource(xslFileName));
stylesheet = tfactory.newTemplates(new DOMSource(xslDOM,
xslFileName));
}
else
{
// System.out.println("Calling newTemplates: "+xslFileName);
stylesheet = tfactory.newTemplates(new StreamSource(xslFileName));
// System.out.println("Done calling newTemplates: "+xslFileName);
}
}
PrintWriter resultWriter;
StreamResult strResult;
if (null != outFileName)
{
strResult = new StreamResult(new FileOutputStream(outFileName));
// One possible improvement might be to ensure this is
// a valid URI before setting the systemId, but that
// might have subtle changes that pre-existing users
// might notice; we can think about that later -sc r1.46
strResult.setSystemId(outFileName);
}
else
{
strResult = new StreamResult(System.out);
// We used to default to incremental mode in this case.
// We've since decided that since the -INCREMENTAL switch is
// available, that default is probably not necessary nor
// necessarily a good idea.
}
SAXTransformerFactory stf = (SAXTransformerFactory) tfactory;
// This is currently controlled via TransformerFactoryImpl.
if (!useXSLTC && useSourceLocation)
stf.setAttribute(XalanProperties.SOURCE_LOCATION, Boolean.TRUE);
// Did they pass in a stylesheet, or should we get it from the
// document?
if (null == stylesheet)
{
Source source =
stf.getAssociatedStylesheet(new StreamSource(inFileName), media,
null, null);
if (null != source)
stylesheet = tfactory.newTemplates(source);
else
{
if (null != media)
throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_STYLESHEET_IN_MEDIA, new Object[]{inFileName, media})); //"No stylesheet found in: "
// + inFileName + ", media="