* The default diagnostic writer...
*/
java.io.PrintWriter diagnosticsWriter = new PrintWriter(System.err, true);
java.io.PrintWriter dumpWriter = diagnosticsWriter;
XSLTErrorResources resbundle = (XSLTErrorResources)(XSLMessages.loadResourceBundle(Constants.ERROR_RESOURCES));
//XSLTErrorResources resbundle = (XSLTErrorResources)(ResourceBundle.getBundle (Constants.ERROR_RESOURCES , Locale.getDefault()));
if(argv.length < 1)
{
printArgOptions(resbundle);
}
else
{
XMLParserLiaison xmlProcessorLiaison;
String parserLiaisonClassName = Constants.LIAISON_CLASS;
try
{
boolean usingDefault = true;
for (int i = 0; i < argv.length; i ++)
{
if("-PARSER".equalsIgnoreCase(argv[i]))
{
i++;
parserLiaisonClassName = argv[i];
usingDefault = false;
}
}
Class parserLiaisonClass = Class.forName(parserLiaisonClassName);
Constructor parserLiaisonCtor = parserLiaisonClass.getConstructor(null);
xmlProcessorLiaison
= (XMLParserLiaison)parserLiaisonCtor.newInstance(null);
}
catch(Exception e)
{
System.err.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0025, new Object[] {parserLiaisonClassName})); //"Could not create XML Processor Liaison: "+parserLiaisonClassName);
return;
}
XSLTProcessor processor = XSLTProcessorFactory.getProcessor(xmlProcessorLiaison);
boolean formatOutput = false;
QName mode = null;
String inFileName = null;
String outFileName = null;
String dumpFileName = null;
String xslFileName = null;
String compiledStylesheetFileNameOut = null;
String compiledStylesheetFileNameIn = null;
String treedumpFileName = null;
boolean didSetCR = false;
boolean didSetLF = false;
boolean stripCData = false;
boolean escapeCData = false;
PrintTraceListener tracer = null;
FileOutputStream compiledStylesheetOutputStream = null;
ObjectOutputStream compiledStylesheetOutput = null;
int outputType = -1;
for (int i = 0; i < argv.length; i ++)
{
if("-TT".equalsIgnoreCase(argv[i]))
{
if(null == tracer)
tracer = new PrintTraceListener(diagnosticsWriter);
tracer.m_traceTemplates = true;
// processor.setTraceTemplates(true);
}
else if("-TG".equalsIgnoreCase(argv[i]))
{
if(null == tracer)
tracer = new PrintTraceListener(diagnosticsWriter);
tracer.m_traceGeneration = true;
// processor.setTraceSelect(true);
}
if("-TS".equalsIgnoreCase(argv[i]))
{
if(null == tracer)
tracer = new PrintTraceListener(diagnosticsWriter);
tracer.m_traceSelection = true;
// processor.setTraceTemplates(true);
}
else if("-TTC".equalsIgnoreCase(argv[i]))
{
if(null == tracer)
tracer = new PrintTraceListener(diagnosticsWriter);
tracer.m_traceElements = true;
// processor.setTraceTemplateChildren(true);
}
if ("-TCLASS".equalsIgnoreCase(argv[i]))
{
String className=argv[++i];
try
{
Class traceClass = Class.forName(className);
Constructor traceCtor = traceClass.getConstructor(null);
TraceListener traceL = (TraceListener)traceCtor.newInstance(null);
processor.addTraceListener(traceL);
}
catch(Exception e)
{
System.err.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0029, new Object[] {className})); //"Could not create TraceListener: "+className);
}
}
else if ("-ESCAPE".equalsIgnoreCase(argv[i]))
{
xmlProcessorLiaison.setSpecialCharacters(argv[++i]);
}
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;
}
xmlProcessorLiaison.setIndent(indentAmount);
}
else if ("-IN".equalsIgnoreCase(argv[i]))
{
inFileName = argv[++i];
}
else if ("-OUT".equalsIgnoreCase(argv[i]))
{
outFileName = argv[++i];
}
else if ("-XSL".equalsIgnoreCase(argv[i]))
{
xslFileName = argv[++i];
}
else if("-LXCIN".equalsIgnoreCase(argv[i]))
{
compiledStylesheetFileNameIn = argv[++i];
}
else if("-LXCOUT".equalsIgnoreCase(argv[i]))
{
compiledStylesheetFileNameOut = argv[++i];
}
else if ("-PARAM".equalsIgnoreCase(argv[i]))
{
String name = argv[++i];
String expression = argv[++i];
processor.setStylesheetParam(name, expression);
}
else if ("-treedump".equalsIgnoreCase(argv[i]))
{
treedumpFileName = argv[++i];
}
else if("-F".equalsIgnoreCase(argv[i]))
{
formatOutput = true;
}
else if("-E".equalsIgnoreCase(argv[i]))
{
xmlProcessorLiaison.SetShouldExpandEntityRefs(false);
}
else if("-V".equalsIgnoreCase(argv[i]))
{
diagnosticsWriter.println(resbundle.getString("version") //">>>>>>> XSLT4J Version "
+XSLProcessorVersion.S_VERSION+", "+
xmlProcessorLiaison.getParserDescription()+
resbundle.getString("version2"));// "<<<<<<<");
}
else if("-QC".equalsIgnoreCase(argv[i]))
{
processor.setQuietConflictWarnings(true);
}
else if("-Q".equalsIgnoreCase(argv[i]))
{
setQuietMode = true;
}
else if("-VALIDATE".equalsIgnoreCase(argv[i]))
{
String shouldValidate;
if(((i+1) < argv.length) && (argv[i+1].charAt(0) != '-'))
{
shouldValidate = argv[++i];
}
else
{
shouldValidate = "yes";
}
xmlProcessorLiaison.setUseValidation(shouldValidate.equalsIgnoreCase("yes"));
}
else if("-PARSER".equalsIgnoreCase(argv[i]))
{
i++;
// Handled above
}
else if("-XML".equalsIgnoreCase(argv[i]))
{
outputType = Formatter.OUTPUT_METH_XML;
}
else if("-TEXT".equalsIgnoreCase(argv[i]))
{
outputType = Formatter.OUTPUT_METH_TEXT;
}
else if("-HTML".equalsIgnoreCase(argv[i]))
{
outputType = Formatter.OUTPUT_METH_HTML;
}
else if("-STRIPCDATA".equalsIgnoreCase(argv[i]))
{
stripCData = true;
}
else if("-ESCAPECDATA".equalsIgnoreCase(argv[i]))
{
escapeCData = true;
}
else if("-EDUMP".equalsIgnoreCase(argv[i]))
{
doStackDumpOnError = true;
if(((i+1) < argv.length) && (argv[i+1].charAt(0) != '-'))
{
dumpFileName = argv[++i];
}
}
}
// The main XSL transformation occurs here!
try
{
processor.setDiagnosticsOutput( setQuietMode ? null : diagnosticsWriter );
// processor.pushTime(processor);
if(null != dumpFileName)
{
dumpWriter = new PrintWriter( new FileWriter(dumpFileName) );
}
StylesheetRoot stylesheet = null;
if(null != compiledStylesheetFileNameIn)
{
try
{
FileInputStream fileInputStream
= new FileInputStream(compiledStylesheetFileNameIn);
ObjectInputStream objectInput
= new ObjectInputStream(fileInputStream);
stylesheet = (StylesheetRoot)objectInput.readObject();
objectInput.close();
}
catch(java.io.UnsupportedEncodingException uee)
{
stylesheet = null;
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0028, new Object[] {stylesheet.m_encoding})); //"Encoding not supported: "+stylesheet.m_encoding);
throw new XSLProcessorException(XSLMessages.createMessage(XSLTErrorResources.ERROR0028, new Object[] {stylesheet.m_encoding})); //"Encoding not supported: "+stylesheet.m_encoding);
}
}
else if(null != xslFileName)
{
stylesheet = processor.processStylesheet(xslFileName);
}
PrintWriter resultWriter;
String mimeEncoding = null;
String encoding = null;
if(null != stylesheet)
{
mimeEncoding = stylesheet.getOutputEncoding();
encoding = stylesheet.getJavaOutputEncoding();
}
if(null == encoding)
{
if(null != stylesheet)
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0028, new Object[] {mimeEncoding})); //"Encoding not supported: "+mimeEncoding);
mimeEncoding = "UTF-8";
encoding = FormatterToXML.convertMime2JavaEncoding(mimeEncoding);
}
OutputStream outputStream = System.out;
if(null != outFileName)
{
outputStream = new FileOutputStream(outFileName);
// processor.setOutputFileName(outFileName);
}
FormatterListener formatter = null;
if(null != stylesheet)
{
if(Formatter.OUTPUT_METH_XML == outputType)
{
try
{
// resultWriter = new PrintWriter( new BufferedWriter(new FileWriter(outFileName)) );
OutputStreamWriter osw = new OutputStreamWriter(outputStream, encoding);
resultWriter = new PrintWriter( new BufferedWriter(osw) );
}
catch(java.io.UnsupportedEncodingException uee)
{
mimeEncoding = "UTF-8";
encoding = FormatterToXML.convertMime2JavaEncoding(mimeEncoding);
OutputStreamWriter osw = new OutputStreamWriter(outputStream, encoding);
resultWriter = new PrintWriter( new BufferedWriter(osw) );
}
FormatterToXML fToXML = new FormatterToXML(resultWriter,
stylesheet.m_version,
stylesheet.m_indentResult,
xmlProcessorLiaison.getIndent(),
mimeEncoding,
stylesheet.m_mediatype,
stylesheet.m_doctypeSystem,
stylesheet.m_doctypePublic,
stylesheet.m_xmlDecl,
stylesheet.m_standalone,
stylesheet.m_cdataSectionElems);
fToXML.m_shouldWriteXMLHeader = true;
fToXML.m_attrSpecialChars = xmlProcessorLiaison.getSpecialCharacters();
fToXML.m_stripCData = stripCData;
fToXML.m_escapeCData = escapeCData;
formatter = fToXML;
}
else if(Formatter.OUTPUT_METH_TEXT == outputType)
{
try
{
// resultWriter = new PrintWriter( new BufferedWriter(new FileWriter(outFileName)) );
OutputStreamWriter osw = new OutputStreamWriter(outputStream, encoding);
resultWriter = new PrintWriter( new BufferedWriter(osw) );
}
catch(java.io.UnsupportedEncodingException uee)
{
mimeEncoding = "UTF-8";
encoding = FormatterToXML.convertMime2JavaEncoding(mimeEncoding);
OutputStreamWriter osw = new OutputStreamWriter(outputStream, encoding);
resultWriter = new PrintWriter( new BufferedWriter(osw) );
}
FormatterToText fToText = new FormatterToText(resultWriter);
formatter = fToText;
}
else if(Formatter.OUTPUT_METH_HTML == outputType)
{
try
{
// resultWriter = new PrintWriter( new BufferedWriter(new FileWriter(outFileName)) );
OutputStreamWriter osw = new OutputStreamWriter(outputStream, encoding);
resultWriter = new PrintWriter( new BufferedWriter(osw) );
}
catch(java.io.UnsupportedEncodingException uee)
{
mimeEncoding = "UTF-8";
encoding = FormatterToXML.convertMime2JavaEncoding(mimeEncoding);
OutputStreamWriter osw = new OutputStreamWriter(outputStream, encoding);
resultWriter = new PrintWriter( new BufferedWriter(osw) );
}
FormatterToHTML fToHTML
= new FormatterToHTML(resultWriter,
stylesheet.m_version,
stylesheet.m_indentResult,
xmlProcessorLiaison.getIndent(),
mimeEncoding,
stylesheet.m_mediatype,
stylesheet.m_doctypeSystem,
stylesheet.m_doctypePublic,
stylesheet.m_xmlDecl,
stylesheet.m_standalone,
stylesheet.m_cdataSectionElems);
fToHTML.m_attrSpecialChars = xmlProcessorLiaison.getSpecialCharacters();
fToHTML.m_stripCData = stripCData;
formatter = fToHTML;
}
}
if(null != tracer)
processor.addTraceListener(tracer);
if(null == formatter)
{
if(null != inFileName)
{
if(null != stylesheet)
{
Node sourceTree = processor.getSourceTreeFromInput(new XSLTInputSource(inFileName));
stylesheet.process(processor, sourceTree, new XSLTResultTarget(outputStream));
}
else
{
processor.process(new XSLTInputSource(inFileName),
(XSLTInputSource)null,
new XSLTResultTarget(outputStream));
}
}
else
{
if(null == compiledStylesheetFileNameOut)
{
Document dummyDoc = xmlProcessorLiaison.createDocument();
stylesheet.process(processor, dummyDoc, new XSLTResultTarget(outputStream));
}
}
}
else
{
if(null != inFileName)
{
XSLTResultTarget rTreeTarget = new XSLTResultTarget();
rTreeTarget.setFormatterListener(formatter);
if(null != stylesheet)
{
Node sourceTree = processor.getSourceTreeFromInput(new XSLTInputSource(inFileName));
stylesheet.process(processor, sourceTree, rTreeTarget);
}
else
{
processor.process(new XSLTInputSource(inFileName),
(XSLTInputSource)null,
rTreeTarget);
}
}
else
{
if(null == compiledStylesheetFileNameOut)
{
Document dummyDoc = xmlProcessorLiaison.createDocument();
XSLTResultTarget resultTarget = new XSLTResultTarget();
resultTarget.setFormatterListener(formatter);
stylesheet.process(processor, dummyDoc, resultTarget);
}
}
}
if(null != compiledStylesheetFileNameOut)
{
compiledStylesheetOutputStream
= new FileOutputStream(compiledStylesheetFileNameOut);
compiledStylesheetOutput
= new ObjectOutputStream(compiledStylesheetOutputStream);
compiledStylesheetOutput.writeObject(stylesheet);
}
}
catch(TooManyListenersException tmle)
{
if(doStackDumpOnError)
tmle.printStackTrace(dumpWriter);
// else
// System.out.println("Error! "+se.getMessage());
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0027, null)); //"XSL Process was not successful.");
}
catch(SAXException se)
{
Exception containedException = se.getException();
if(null != containedException)
{
if(doStackDumpOnError)
containedException.printStackTrace(dumpWriter);
// else
// System.out.println("Error! "+se.getMessage());
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0027, null)); //"XSL Process was not successful.");
}
else
{
if(doStackDumpOnError)
se.printStackTrace(dumpWriter);
// else
// System.out.println("Error! "+se.getMessage());
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0027, null) ); //"XSL Process was not successful.");
}
}
catch(XPathException xpe)
{
if(doStackDumpOnError)
xpe.printStackTrace(dumpWriter);
if((null != xpe.m_styleNode)
&& xpe.m_styleNode instanceof ElemTemplateElement)
{
ElemTemplateElement elem = (ElemTemplateElement)xpe.m_styleNode;
diagnosticsWriter.println(elem.m_stylesheet.m_baseIdent+"; " + resbundle.getString("line")+
elem.m_lineNumber+"; " + resbundle.getString("column")+
elem.m_columnNumber+"; "+xpe.getMessage());
}
else
{
xpe.getMessage();
}
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0026, null)); //"XPATH: XSL Process was not successful.");
}
catch(XSLProcessorException xslpe)
{
if(doStackDumpOnError)
{
xslpe.printStackTrace(dumpWriter);
}
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0027, null)); //"XSL Process was not successful.");
}
catch(MalformedURLException mue)
{
if(doStackDumpOnError)
mue.printStackTrace(dumpWriter);
else
System.out.println("Error! "+mue.getMessage());
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0027, null)); //"XSL Process was not successful.");
}
catch(ClassNotFoundException cnfe)
{
if(doStackDumpOnError)
cnfe.printStackTrace(dumpWriter);
else
System.out.println("Error! "+cnfe.getMessage());
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0027, null)); //"XSL Process was not successful.");
}
catch(FileNotFoundException fne)
{
if(doStackDumpOnError)
fne.printStackTrace(dumpWriter);
else
System.out.println("Error! "+fne.getMessage());
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0027, null)); //"XSL Process was not successful.");
}
catch(IOException ioe)
{
if(doStackDumpOnError)
ioe.printStackTrace(dumpWriter);
else
System.out.println("Error! "+ioe.getMessage());
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0027, null)); //"XSL Process was not successful.");
}
catch(java.lang.NoClassDefFoundError ncdfe)
{
String msg = ncdfe.getMessage();
if(msg.indexOf("com/ibm/xml/parser/Parser") >= 0)
{
diagnosticsWriter.println("==========================================");
diagnosticsWriter.println(resbundle.getString("noParsermsg1")); //"XSL Process was not successful.");
diagnosticsWriter.println(resbundle.getString("noParsermsg2")); //"** Could not find parser **");
diagnosticsWriter.println(resbundle.getString("noParsermsg3")); //"Please check your classpath.");
diagnosticsWriter.println(resbundle.getString("noParsermsg4")); //"If you don't have IBM's XML Parser for Java, you can download it from");
diagnosticsWriter.println(resbundle.getString("noParsermsg5")); //"IBM's AlphaWorks: http://www.alphaworks.ibm.com/formula/xml");
diagnosticsWriter.println("==========================================");
}
else
{
if(doStackDumpOnError)
ncdfe.printStackTrace(dumpWriter);
else
System.out.println("Error! "+ncdfe.getMessage());
diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ERROR0027, null)); //"XSL Process was not successful.");
}
}
finally
{
try
{
if(null != compiledStylesheetOutput)
{
compiledStylesheetOutput.flush();
compiledStylesheetOutput.close();
}
if(null != compiledStylesheetOutputStream)
{
compiledStylesheetOutputStream.flush();
compiledStylesheetOutputStream.close();
}
}
catch(Exception e)
{
}
}
if(null != dumpFileName)
{
dumpWriter.close();
}
if(null != diagnosticsWriter)
{
// diagnosticsWriter.close();
}
diagnosticsWriter.println(resbundle.getString("xsldone")); //"XSLT4J: done");
}
}