Java Code Examples for javax.xml.transform.TransformerFactory#setErrorListener()
The following examples show how to use
javax.xml.transform.TransformerFactory#setErrorListener() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XMLTransformerFactory.java From ph-commons with Apache License 2.0 | 6 votes |
@Nonnull public static TransformerFactory createTransformerFactory (@Nullable final ErrorListener aErrorListener, @Nullable final URIResolver aURIResolver) { try { final TransformerFactory aFactory = TransformerFactory.newInstance (); if (aErrorListener != null) aFactory.setErrorListener (aErrorListener); if (aURIResolver != null) aFactory.setURIResolver (aURIResolver); return aFactory; } catch (final TransformerFactoryConfigurationError ex) { throw new InitializationException ("Failed to create XML TransformerFactory", ex); } }
Example 2
Source File: XmlUtils.java From iaf with Apache License 2.0 | 6 votes |
public static TransformerFactory getTransformerFactory(int xsltVersion) { TransformerFactory factory; switch (xsltVersion) { case 2: factory = new net.sf.saxon.TransformerFactoryImpl(); // Use ErrorListener to prevent warning "Stylesheet module ....xsl // is included or imported more than once. This is permitted, but // may lead to errors or unexpected behavior" written to System.err // (https://stackoverflow.com/questions/10096086/how-to-handle-duplicate-imports-in-xslt) factory.setErrorListener(new TransformerErrorListener()); return factory; default: factory=new org.apache.xalan.processor.TransformerFactoryImpl(); factory.setErrorListener(new TransformerErrorListener()); if (isXsltStreamingByDefault()) { factory.setAttribute(org.apache.xalan.processor.TransformerFactoryImpl.FEATURE_INCREMENTAL, Boolean.TRUE); } return factory; } }
Example 3
Source File: CamelXsltRecorder.java From camel-quarkus with Apache License 2.0 | 5 votes |
@Override public void configure(TransformerFactory tf, XsltEndpoint endpoint) { final String className = uriResolver.getTransletClassName(endpoint.getResourceUri()); tf.setAttribute("use-classpath", true); tf.setAttribute("translet-name", className); tf.setAttribute("package-name", packageName); tf.setErrorListener(new CamelXsltErrorListener()); endpoint.setTransformerFactory(tf); }
Example 4
Source File: ErrorListenerTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Expect a TransformerConfigurationException when transforming a file * invalid.xsl that has some well-formedness error. */ @Test public void errorListener01() { ErrorListenerTest listener = new ErrorListenerTest(); try { TransformerFactory tfactory = TransformerFactory.newInstance(); tfactory.setErrorListener (listener); tfactory.newTransformer(new StreamSource( new File(XML_DIR + "invalid.xsl"))); fail("Expect TransformerConfigurationException here"); } catch (TransformerConfigurationException ex) { assertEquals(listener.status, ListenerStatus.FATAL); } }
Example 5
Source File: MCRXSLTransformer.java From mycore with GNU General Public License v3.0 | 5 votes |
public synchronized void setTransformerFactory(String factoryClass) throws TransformerFactoryConfigurationError { TransformerFactory transformerFactory = Optional.ofNullable(factoryClass) .map(c -> TransformerFactory.newInstance(c, MCRClassTools.getClassLoader())) .orElseGet(TransformerFactory::newInstance); LOGGER.debug("Transformerfactory: {}", transformerFactory.getClass().getName()); transformerFactory.setURIResolver(URI_RESOLVER); transformerFactory.setErrorListener(MCRErrorListener.getInstance()); if (transformerFactory.getFeature(SAXSource.FEATURE) && transformerFactory.getFeature(SAXResult.FEATURE)) { this.tFactory = (SAXTransformerFactory) transformerFactory; } else { throw new MCRConfigurationException("Transformer Factory " + transformerFactory.getClass().getName() + " does not implement SAXTransformerFactory"); } }
Example 6
Source File: TransformerProvider.java From lucene-solr with Apache License 2.0 | 5 votes |
/** Return a Templates object for the given filename */ private Templates getTemplates(ResourceLoader loader, String filename,int cacheLifetimeSeconds) throws IOException { Templates result = null; lastFilename = null; try { if(log.isDebugEnabled()) { log.debug("compiling XSLT templates:{}", filename); } final String fn = "xslt/" + filename; final TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setURIResolver(new SystemIdResolver(loader).asURIResolver()); tFactory.setErrorListener(xmllog); final StreamSource src = new StreamSource(loader.openResource(fn), SystemIdResolver.createSystemIdFromResourceName(fn)); try { result = tFactory.newTemplates(src); } finally { // some XML parsers are broken and don't close the byte stream (but they should according to spec) IOUtils.closeQuietly(src.getInputStream()); } } catch (Exception e) { log.error(getClass().getName(), "newTemplates", e); throw new IOException("Unable to initialize Templates '" + filename + "'", e); } lastFilename = filename; lastTemplates = result; cacheExpiresTimeout = new TimeOut(cacheLifetimeSeconds, TimeUnit.SECONDS, TimeSource.NANO_TIME); return result; }
Example 7
Source File: TransformFilter.java From tcases with MIT License | 5 votes |
/** * Initializes the filter. */ protected void initializeFilter( InputStream filterInput, OutputStream filterOutput) { try { // Create Transformer object. Source transform = getTransform(); if( transform == null) { throw new IllegalStateException( "No XSLT transform specified"); } TransformerFactory factory = TransformerFactory.newInstance(); factory.setErrorListener( this); transformer_ = factory.newTransformer( transform); transformer_.setErrorListener( this); if( transformParams_ != null) { for( String paramName : transformParams_.keySet()) { transformer_.setParameter( paramName, transformParams_.get( paramName)); } } transformSource_ = new StreamSource( filterInput); transformResult_ = new StreamResult( filterOutput); } catch( Exception e) { throw new RuntimeException( "Can't initialize filter", e); } }
Example 8
Source File: TransToHtmlTest.java From JVoiceXML with GNU Lesser General Public License v2.1 | 5 votes |
private void transfer(final OutputStream out, final InputStream dataStream, final InputStream styleStream) throws IOException, TransformerException { Source data = new StreamSource(dataStream); Source style = new StreamSource(styleStream); Result output = new StreamResult(out); TransformerFactory factory = TransformerFactory.newInstance(); factory.setErrorListener(new MyListener()); LOGGER.debug(factory.getClass().getName()); Transformer xslt = factory.newTransformer(style); xslt.transform(data, output); }
Example 9
Source File: SCHTransformerCustomizer.java From ph-schematron with Apache License 2.0 | 5 votes |
public void customize (@Nonnull final TransformerFactory aTransformer) { // Ensure an error listener is present if (m_aCustomErrorListener != null) aTransformer.setErrorListener (m_aCustomErrorListener); else aTransformer.setErrorListener (new LoggingTransformErrorListener (Locale.US)); // Set the optional URI Resolver if (m_aCustomURIResolver != null) aTransformer.setURIResolver (m_aCustomURIResolver); }
Example 10
Source File: DefaultXMLService.java From scifio with BSD 2-Clause "Simplified" License | 5 votes |
@Override public String getXML(final Document doc) throws TransformerException { final Source source = new DOMSource(doc); final StringWriter stringWriter = new StringWriter(); final Result result = new StreamResult(stringWriter); // Java XML factories are not declared to be thread safe final TransformerFactory factory = TransformerFactory.newInstance(); factory.setErrorListener(new XMLListener()); final Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); }
Example 11
Source File: MCRFoFormatterFOP.java From mycore with GNU General Public License v3.0 | 4 votes |
private static TransformerFactory getTransformerFactory() throws TransformerFactoryConfigurationError { TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setURIResolver(MCRURIResolver.instance()); transformerFactory.setErrorListener(MCRErrorListener.getInstance()); return transformerFactory; }
Example 12
Source File: DomUtils.java From dss with GNU Lesser General Public License v2.1 | 4 votes |
/** * This method returns a new instance of TransformerFactory with secured features enabled * * @return an instance of TransformerFactory with enabled secure features */ public static TransformerFactory getSecureTransformerFactory() { TransformerFactory transformerFactory = XmlDefinerUtils.getInstance().getSecureTransformerFactory(); transformerFactory.setErrorListener(new DSSXmlErrorListener()); return transformerFactory; }