本文整理汇总了Java中javax.xml.transform.Transformer.setErrorListener方法的典型用法代码示例。如果您正苦于以下问题:Java Transformer.setErrorListener方法的具体用法?Java Transformer.setErrorListener怎么用?Java Transformer.setErrorListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.transform.Transformer
的用法示例。
在下文中一共展示了Transformer.setErrorListener方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createTransformerHandler
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
public static TransformerHandler createTransformerHandler(final ErrorListener errorListener,
final boolean indentOutput) throws TransformerConfigurationException {
final SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactoryFactory.newInstance();
if (errorListener != null) {
stf.setErrorListener(errorListener);
}
final TransformerHandler th = stf.newTransformerHandler();
final Transformer transformer = th.getTransformer();
setCommonOutputProperties(transformer, indentOutput);
if (errorListener != null) {
transformer.setErrorListener(errorListener);
}
return th;
}
示例2: generateSimpleReport
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
public String generateSimpleReport(String simpleReport) {
Writer writer = new StringWriter();
try {
Transformer transformer = templateSimpleReport.newTransformer();
transformer.setErrorListener(new DSSXmlErrorListener());
transformer.transform(new StreamSource(new StringReader(simpleReport)), new StreamResult(writer));
} catch (Exception e) {
logger.error("Error while generating simple report : " + e.getMessage(), e);
}
return writer.toString();
}
示例3: generateDetailedReport
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
public String generateDetailedReport(String detailedReport) {
Writer writer = new StringWriter();
try {
Transformer transformer = templateDetailedReport.newTransformer();
transformer.setErrorListener(new DSSXmlErrorListener());
transformer.transform(new StreamSource(new StringReader(detailedReport)), new StreamResult(writer));
} catch (Exception e) {
logger.error("Error while generating detailed report : " + e.getMessage(), e);
}
return writer.toString();
}
示例4: generateSimpleReport
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
public void generateSimpleReport(String simpleReport, OutputStream os) throws Exception {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, os);
Result res = new SAXResult(fop.getDefaultHandler());
Transformer transformer = templateSimpleReport.newTransformer();
transformer.setErrorListener(new DSSXmlErrorListener());
transformer.transform(new StreamSource(new StringReader(simpleReport)), res);
}
示例5: generateDetailedReport
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
public void generateDetailedReport(String detailedReport, OutputStream os) throws Exception {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, os);
Result res = new SAXResult(fop.getDefaultHandler());
Transformer transformer = templateDetailedReport.newTransformer();
transformer.setErrorListener(new DSSXmlErrorListener());
transformer.transform(new StreamSource(new StringReader(detailedReport)), res);
}
示例6: process
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
@Override
public void process(ProcessingContext<Corpus> ctx, Corpus corpus) throws ModuleException {
try {
Logger logger = getLogger(ctx);
TransformerFactory factory = TransformerFactory.newInstance();
InputStream is = xslTransform.getInputStream();
Source transformerSource = new StreamSource(is);
Transformer transformer = factory.newTransformer(transformerSource);
is.close();
transformer.setErrorListener(new ListingErrorHandler());
libraryResolver = getLibraryResolver(ctx);
Evaluator roots = libraryResolver.resolveNullable(this.roots);
Evaluator fileName = this.fileName.resolveExpressions(libraryResolver);
EvaluationContext evalCtx = new EvaluationContext(logger);
for (Element root : Iterators.loop(getRoots(roots, evalCtx, corpus))) {
transformer.reset();
Document doc = XMLUtils.docBuilder.newDocument();
doc.setUserData(ELEMENT_USER_DATA, root, null);
// org.w3c.dom.Element elt = getElementProxy(doc, root);
// doc.appendChild(elt);
Source source = new DOMSource(doc);
String fileNameString = fileName.evaluateString(evalCtx, root);
// System.out.println("creating file: " + fileNameString);
File outFile = new File(outDir, fileNameString);
Result result = new StreamResult(outFile);
transformer.transform(source, result);
}
} catch (DOMException | TransformerException | IOException e) {
rethrow(e);
}
}
示例7: getTransformer
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
@TimeThis(task="read-xslt", category=TimerCategory.LOAD_RESOURCE)
protected Transformer getTransformer(@SuppressWarnings("unused") ProcessingContext<Corpus> ctx) throws IOException, TransformerConfigurationException {
TransformerFactory factory = TransformerFactory.newInstance();
try (InputStream is = xslTransform.getInputStream()) {
Source transformerSource = new StreamSource(is);
Transformer result = factory.newTransformer(transformerSource);
result.setErrorListener(new ListingErrorHandler());
if (indent) {
result.setOutputProperty(OutputKeys.INDENT, "yes");
result.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
}
return result;
}
}
示例8: transformer04
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* This tests set/get ErrorListener methods of Transformer.
*
* @throws Exception If any errors occur.
*/
@Test
public void transformer04() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(TEST_XSL));
DOMSource domSource = new DOMSource(document);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(domSource);
transformer.setErrorListener(new MyErrorListener());
assertNotNull(transformer.getErrorListener());
assertTrue(transformer.getErrorListener() instanceof MyErrorListener);
}
示例9: testDocument
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* bug 8062518
* Verifies that a reference to the DTM created by XSLT document function is
* actually read from the DTM by an extension function.
* @param xml Content of xml file to process
* @param xsl stylesheet content that loads external document {@code externalDoc}
* with XSLT 'document' function and then reads it with
* DocumentExtFunc.test() function
* @param externalDoc Content of the external xml document
* @param expectedResult Expected transformation result
**/
@Test(dataProvider = "document")
public void testDocument(final String xml, final String xsl,
final String externalDoc, final String expectedResult) throws Exception {
// Prepare sources for transormation
Source src = new StreamSource(new StringReader(xml));
Source xslsrc = new StreamSource(new StringReader(xsl));
// Create factory and transformer
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer( xslsrc );
t.setErrorListener(tf.getErrorListener());
// Set URI Resolver to return the newly constructed xml
// stream source object from xml test string
t.setURIResolver(new URIResolver() {
@Override
public Source resolve(String href, String base)
throws TransformerException {
if (href.contains("externalDoc")) {
return new StreamSource(new StringReader(externalDoc));
} else {
return new StreamSource(new StringReader(xml));
}
}
});
// Prepare output stream
StringWriter xmlResultString = new StringWriter();
StreamResult xmlResultStream = new StreamResult(xmlResultString);
//Transform the xml
t.transform(src, xmlResultStream);
// If the document can't be accessed and the bug is in place then
// reported exception will be thrown during transformation
System.out.println("Transformation result:"+xmlResultString.toString().trim());
// Check the result - it should contain two (node name, node values) entries -
// one for original document, another for a document created with
// call to 'document' function
assertEquals(xmlResultString.toString().trim(), expectedResult);
}
示例10: testDocument
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* @bug 8062518 8153082
* Verifies that a reference to the DTM created by XSLT document function is
* actually read from the DTM by an extension function.
* @param xml Content of xml file to process
* @param xsl stylesheet content that loads external document {@code externalDoc}
* with XSLT 'document' function and then reads it with
* DocumentExtFunc.test() function
* @param externalDoc Content of the external xml document
* @param expectedResult Expected transformation result
**/
@Test(dataProvider = "document")
public void testDocument(final String xml, final String xsl,
final String externalDoc, final String expectedResult) throws Exception {
// Prepare sources for transormation
Source src = new StreamSource(new StringReader(xml));
Source xslsrc = new StreamSource(new StringReader(xsl));
// Create factory and transformer
TransformerFactory tf = TransformerFactory.newInstance();
tf.setFeature(ORACLE_ENABLE_EXTENSION_FUNCTION, true);
tf.setAttribute(EXTENSION_CLASS_LOADER,
runWithAllPerm(() -> Thread.currentThread().getContextClassLoader()));
Transformer t = tf.newTransformer( xslsrc );
t.setErrorListener(tf.getErrorListener());
// Set URI Resolver to return the newly constructed xml
// stream source object from xml test string
t.setURIResolver(new URIResolver() {
@Override
public Source resolve(String href, String base)
throws TransformerException {
if (href.contains("externalDoc")) {
return new StreamSource(new StringReader(externalDoc));
} else {
return new StreamSource(new StringReader(xml));
}
}
});
// Prepare output stream
StringWriter xmlResultString = new StringWriter();
StreamResult xmlResultStream = new StreamResult(xmlResultString);
//Transform the xml
t.transform(src, xmlResultStream);
// If the document can't be accessed and the bug is in place then
// reported exception will be thrown during transformation
System.out.println("Transformation result:"+xmlResultString.toString().trim());
// Check the result - it should contain two (node name, node values) entries -
// one for original document, another for a document created with
// call to 'document' function
assertEquals(xmlResultString.toString().trim(), expectedResult);
}