in java/src/org/apache/qetest/trax/sax/SAXResultAPITest.java [369:534]
public boolean testCase3()
{
reporter.testCaseInit("Detailed functionality of SAXResults: setLexicalHandler");
String xslURI = QetestUtils.filenameToURL(dtdFileInfo.inputName);
String xmlURI = QetestUtils.filenameToURL(dtdFileInfo.xmlName);
TransformerFactory factory = null;
SAXTransformerFactory saxFactory = null;
Templates streamTemplates;
try
{
factory = TransformerFactory.newInstance();
factory.setErrorListener(new DefaultErrorHandler());
saxFactory = (SAXTransformerFactory)factory;
// Process a simple stylesheet for use later
reporter.logTraceMsg("factory.newTemplates(new StreamSource(" + xslURI + "))");
streamTemplates = factory.newTemplates(new StreamSource(xslURI));
}
catch (Throwable t)
{
reporter.checkFail("Problem creating factory; can't continue testcase");
reporter.logThrowable(reporter.ERRORMSG, t,"Problem creating factory; can't continue testcase");
return true;
}
try
{
// Validate a StreamSource to a logging SAXResult,
// where we validate the actual events passed
// through the SAXResult's ContentHandler and
// LexicalHandler
// Have an actual handler that does the physical output
reporter.logInfoMsg("TransformerHandler.setResult(StreamResult)");
TransformerHandler tHandler = saxFactory.newTransformerHandler();
FileOutputStream fos = new FileOutputStream(outNames.nextName());
Result realResult = new StreamResult(fos);
tHandler.setResult(realResult);
SAXResult saxResult = new SAXResult();
// Add a contentHandler that logs out info about the
// transform, and that passes-through calls back
// to the original tHandler
reporter.logInfoMsg("loggingSaxResult.setHandler(loggingContentHandler)");
LoggingContentHandler lch = new LoggingContentHandler((Logger)reporter);
lch.setDefaultHandler(tHandler);
saxResult.setHandler(lch);
// Add a lexicalHandler that logs out info about the
// transform, and that passes-through calls back
// to the original tHandler
reporter.logInfoMsg("loggingSaxResult.setLexicalHandler(loggingLexicalHandler)");
LoggingLexicalHandler llh = new LoggingLexicalHandler((Logger)reporter);
llh.setDefaultHandler(tHandler);
saxResult.setLexicalHandler(llh);
// Just do a normal transform to this result
Transformer transformer = streamTemplates.newTransformer();
transformer.setErrorListener(new DefaultErrorHandler());
reporter.logTraceMsg("transform(new StreamSource(" + xmlURI + "), loggingSaxResult)");
transformer.transform(new StreamSource(xmlURI), saxResult);
fos.close(); // must close ostreams we own
reporter.logStatusMsg("Closed result stream from loggingSaxResult, about to check result");
fileChecker.check(reporter,
new File(outNames.currentName()),
new File(dtdFileInfo.goldName),
"transform loggingSaxResult into: " + outNames.currentName());
reporter.logWarningMsg("//@todo validate that llh got lexical events: Bugzilla#888");
reporter.logWarningMsg("//@todo validate that lch got content events");
}
catch (Throwable t)
{
reporter.checkFail("Basic functionality1 of SAXResults threw: " + t.toString());
reporter.logThrowable(reporter.ERRORMSG, t, "Basic functionality1 of SAXResults");
}
try
{
// Same as above, with identityTransformer
reporter.logInfoMsg("TransformerHandler.setResult(StreamResult)");
TransformerHandler tHandler = saxFactory.newTransformerHandler();
FileOutputStream fos = new FileOutputStream(outNames.nextName());
Result realResult = new StreamResult(fos);
tHandler.setResult(realResult);
SAXResult saxResult = new SAXResult();
reporter.logInfoMsg("loggingSaxResult.setHandler(loggingContentHandler)");
LoggingContentHandler lch = new LoggingContentHandler((Logger)reporter);
lch.setDefaultHandler(tHandler);
saxResult.setHandler(lch);
reporter.logInfoMsg("loggingSaxResult.setLexicalHandler(loggingLexicalHandler)");
LoggingLexicalHandler llh = new LoggingLexicalHandler((Logger)reporter);
llh.setDefaultHandler(tHandler);
saxResult.setLexicalHandler(llh);
// Do an identityTransform to this result
Transformer identityTransformer = TransformerFactory.newInstance().newTransformer();
identityTransformer.setErrorListener(new DefaultErrorHandler());
reporter.logTraceMsg("identityTransform(new StreamSource(" + xmlURI + "), loggingSaxResult)");
identityTransformer.transform(new StreamSource(xmlURI), saxResult);
fos.close(); // must close ostreams we own
reporter.logStatusMsg("Closed result stream from loggingSaxResult, about to check result");
fileChecker.check(reporter,
new File(outNames.currentName()),
new File(dtdFileInfo.xmlName),
"identity transform loggingSaxResult into: " + outNames.currentName());
reporter.logWarningMsg("//@todo validate that llh got lexical events: Bugzilla#888");
reporter.logWarningMsg("//@todo validate that lch got content events");
}
catch (Throwable t)
{
reporter.checkFail("Basic functionality2 of SAXResults threw: " + t.toString());
reporter.logThrowable(reporter.ERRORMSG, t, "Basic functionality2 of SAXResults");
}
try
{
// Validate a DOMSource to a logging SAXResult, as above
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
reporter.logTraceMsg("docBuilder.parse(" + xmlURI + ")");
Node xmlNode = docBuilder.parse(new InputSource(xmlURI));
// Have an actual handler that does the physical output
TransformerHandler tHandler = saxFactory.newTransformerHandler();
FileOutputStream fos = new FileOutputStream(outNames.nextName());
Result realResult = new StreamResult(fos);
tHandler.setResult(realResult);
SAXResult saxResult = new SAXResult();
// Add a contentHandler that logs out info about the
// transform, and that passes-through calls back
// to the original tHandler
LoggingContentHandler lch = new LoggingContentHandler((Logger)reporter);
lch.setDefaultHandler(tHandler);
saxResult.setHandler(lch);
// Add a lexicalHandler that logs out info about the
// transform, and that passes-through calls back
// to the original tHandler
LoggingLexicalHandler llh = new LoggingLexicalHandler((Logger)reporter);
llh.setDefaultHandler(tHandler);
saxResult.setLexicalHandler(llh);
// Just do a normal transform to this result
Transformer transformer = streamTemplates.newTransformer();
transformer.setErrorListener(new DefaultErrorHandler());
reporter.logTraceMsg("transform(new DOMSource(" + xmlURI + "), loggingSaxResult)");
transformer.transform(new DOMSource(xmlNode), saxResult);
fos.close(); // must close ostreams we own
reporter.logStatusMsg("Closed result stream from loggingSaxResult, about to check result");
fileChecker.check(reporter,
new File(outNames.currentName()),
new File(dtdFileInfo.goldName),
"transform DOM-loggingSaxResult into: " + outNames.currentName());
reporter.logWarningMsg("//@todo validate that llh got lexical events: Bugzilla#888");
reporter.logWarningMsg("//@todo validate that lch got content events");
}
catch (Throwable t)
{
reporter.checkFail("Basic functionality3 of SAXResults threw: " + t.toString());
reporter.logThrowable(reporter.ERRORMSG, t, "Basic functionality3 of SAXResults");
}
reporter.testCaseClose();
return true;
}