javax.xml.transform.TransformerFactory#setErrorListener ( )源码实例Demo

下面列出了javax.xml.transform.TransformerFactory#setErrorListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: ph-commons   文件: XMLTransformerFactory.java
@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);
  }
}
 
源代码2 项目: iaf   文件: XmlUtils.java
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;
	}
}
 
源代码3 项目: camel-quarkus   文件: CamelXsltRecorder.java
@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);
}
 
源代码4 项目: openjdk-jdk9   文件: ErrorListenerTest.java
/**
 * 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);
    }
}
 
源代码5 项目: mycore   文件: MCRXSLTransformer.java
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");
    }
}
 
源代码6 项目: lucene-solr   文件: TransformerProvider.java
/** 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;
}
 
源代码7 项目: tcases   文件: TransformFilter.java
/**
 * 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);
    } 
  }
 
源代码8 项目: JVoiceXML   文件: TransToHtmlTest.java
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);
}
 
源代码9 项目: ph-schematron   文件: SCHTransformerCustomizer.java
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);
}
 
源代码10 项目: scifio   文件: DefaultXMLService.java
@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();
}
 
源代码11 项目: mycore   文件: MCRFoFormatterFOP.java
private static TransformerFactory getTransformerFactory() throws TransformerFactoryConfigurationError {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setURIResolver(MCRURIResolver.instance());
    transformerFactory.setErrorListener(MCRErrorListener.getInstance());
    return transformerFactory;
}
 
源代码12 项目: dss   文件: DomUtils.java
/**
 * 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;
}