List of usage examples for javax.xml.transform Transformer setErrorListener
public abstract void setErrorListener(ErrorListener listener) throws IllegalArgumentException;
From source file:it.cnr.icar.eric.server.cms.CanonicalXMLFilteringService.java
private static Transformer initTransformer(TransformerFactory tFactory, StreamSource xslt) throws TransformerConfigurationException { Transformer transformer = tFactory.newTransformer(xslt); transformer.setErrorListener(new ErrorListener() { public void error(TransformerException exception) throws TransformerException { log.info(exception);/* w ww . ja v a 2 s. com*/ } public void fatalError(TransformerException exception) throws TransformerException { log.error(exception); throw exception; } public void warning(TransformerException exception) throws TransformerException { log.info(exception); } }); return transformer; }
From source file:SimpleXMLTransform.java
public void transform(String inXML, String inXSL, String outTXT) throws TransformerConfigurationException, TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); StreamSource xslStream = new StreamSource(inXSL); Transformer transformer = factory.newTransformer(xslStream); transformer.setErrorListener(new MyErrorListener()); StreamSource in = new StreamSource(inXML); StreamResult out = new StreamResult(outTXT); transformer.transform(in, out);//from w w w. j a v a 2 s .c o m }
From source file:dk.statsbiblioteket.util.xml.XSLT.java
/** * Creates a new transformer based on the given XSLTLocation. * Useful for e.g. using Saxon instead of the default Xalan. * * @param factory the factory to use for creating the transformer. * @param xslt the location of the XSLT. * @return a Transformer based on the given XSLT. * @throws javax.xml.transform.TransformerException thrown if for some * reason a Transformer could not be instantiated. * This is normally due to problems with the {@code xslt} URL * @see #getLocalTransformer for reusing Transformers. *//*from ww w . j a v a 2s . c o m*/ public static Transformer createTransformer(TransformerFactory factory, URL xslt) throws TransformerException { log.trace("createTransformer: Requesting and compiling XSLT from '" + xslt + "'"); final long startTime = System.nanoTime(); InputStream in = null; Transformer transformer; try { if (xslt == null) { throw new NullPointerException("xslt URL is null"); } in = xslt.openStream(); transformer = factory.newTransformer(new StreamSource(in, xslt.toString())); transformer.setErrorListener(getErrorListener()); } catch (TransformerException e) { throw new TransformerException(String.format( "Unable to instantiate Transformer, a system configuration error for XSLT at '%s'", xslt), e); } catch (MalformedURLException e) { throw new TransformerException(String.format("The URL to the XSLT is not a valid URL: '%s'", xslt), e); } catch (IOException e) { throw new TransformerException( String.format("Unable to open the XSLT resource due to IOException '%s'", xslt), e); } catch (Exception e) { throw new TransformerException(String.format("Unable to open the XSLT resource '%s'", xslt), e); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { log.warn("Non-fatal IOException while closing stream to '" + xslt + "'"); } } log.debug("createTransformer: Requested and compiled XSLT from '" + xslt + "' in " + (System.nanoTime() - startTime) / 1000000 + "ms"); return transformer; }
From source file:com.swordlord.gozer.renderer.fop.FopFactoryHelper.java
public void transform(String strSource, Result result) throws TransformerConfigurationException, TransformerException { StringReader sr = new StringReader(strSource); // Setup input stream Source src = new StreamSource(sr); try {//from w w w . j av a 2 s. com // Setup JAXP using identity transformer TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setErrorListener(new FopTransformerErrorListener()); String strEncoding = getCharset(); transformer.setOutputProperty(OutputKeys.ENCODING, strEncoding); //System.setProperty("java.awt.headless", "true"); //LOG.info("Headless mode before FOPing: " + GraphicsEnvironment.isHeadless()); // Start XSLT transformation and FOP processing transformer.transform(src, result); } catch (TransformerConfigurationException e) { LOG.error( MessageFormat.format("FOP transformation finalisation crashed: {0}", e.getLocalizedMessage())); throw (e); } catch (TransformerException e) { LOG.error( MessageFormat.format("FOP transformation finalisation crashed: {0}", e.getLocalizedMessage())); throw (e); } }
From source file:com.predic8.membrane.core.interceptor.schemavalidation.SchematronValidator.java
public SchematronValidator(ResolverMap resourceResolver, String schematron, ValidatorInterceptor.FailureHandler failureHandler, Router router, BeanFactory beanFactory) throws Exception { this.failureHandler = failureHandler; //works as standalone "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl" TransformerFactory fac;/*from w w w .j ava2s . c o m*/ try { fac = beanFactory.getBean("transformerFactory", TransformerFactory.class); } catch (NoSuchBeanDefinitionException e) { throw new RuntimeException( "Please define a bean called 'transformerFactory' in monitor-beans.xml, e.g. with " + "<spring:bean id=\"transformerFactory\" class=\"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl\" />", e); } fac.setURIResolver(new URIResolver() { @Override public Source resolve(String href, String base) throws TransformerException { return new StreamSource(SchematronValidator.class.getResourceAsStream(href)); } }); Transformer t = fac.newTransformer( new StreamSource(SchematronValidator.class.getResourceAsStream("conformance1-5.xsl"))); // transform schematron-XML into XSLT DOMResult r = new DOMResult(); t.transform(new StreamSource(router.getResolverMap().resolve(schematron)), r); // build XSLT transformers fac.setURIResolver(null); int concurrency = Runtime.getRuntime().availableProcessors() * 2; transformers = new ArrayBlockingQueue<Transformer>(concurrency); for (int i = 0; i < concurrency; i++) { Transformer transformer = fac.newTransformer(new DOMSource(r.getNode())); transformer.setErrorListener(new NullErrorListener()); // silence console logging transformers.put(transformer); } xmlInputFactory = XMLInputFactory.newInstance(); xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false); xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); }
From source file:eionet.gdem.conversion.converters.ConvertStrategy.java
/** * Method transforms XML source using XSL stream. * @param in InputStream containing source XML. * @param xslStream InputStream containing XSL content. * @param out OutputStream for conversion result. * @throws GDEMException In case of unexpected XML or XSL errors. *//*from w w w . j a va2s .co m*/ protected void runXslTransformation(InputStream in, InputStream xslStream, OutputStream out) throws GDEMException { try { TransformerFactory tFactory = transform.getTransformerFactoryInstance(); TransformerErrorListener errors = new TransformerErrorListener(); tFactory.setErrorListener(errors); StreamSource transformerSource = new StreamSource(xslStream); if (getXslPath() != null) { transformerSource.setSystemId(getXslPath()); } Transformer transformer = tFactory.newTransformer(transformerSource); transformer.setErrorListener(errors); transformer.setParameter(DD_DOMAIN_PARAM, Properties.ddURL); setTransformerParameters(transformer); long l = System.currentTimeMillis(); transformer.transform(new StreamSource(in), new StreamResult(out)); if (LOGGER.isDebugEnabled()) { LOGGER.debug((new StringBuilder()).append("generate: transformation needed ") .append(System.currentTimeMillis() - l).append(" ms").toString()); } } catch (TransformerConfigurationException tce) { throw new GDEMException("Error transforming XML - incorrect stylesheet file: " + tce.toString(), tce); } catch (TransformerException tfe) { throw new GDEMException( "Error transforming XML - it's not probably well-formed xml file: " + tfe.toString(), tfe); } catch (Throwable th) { LOGGER.error("Error " + th.toString(), th); th.printStackTrace(System.out); throw new GDEMException("Error transforming XML: " + th.toString()); } }
From source file:it.cnr.icar.eric.server.cms.CanonicalXMLCatalogingService.java
private Transformer initTransformer(TransformerFactory tFactory, StreamSource invocationControlFileSrc) throws TransformerConfigurationException { Transformer transformer = tFactory.newTransformer(invocationControlFileSrc); transformer.setErrorListener(new ErrorListener() { public void error(TransformerException exception) throws TransformerException { log.info(exception);/*from w w w . ja va 2 s .co m*/ } public void fatalError(TransformerException exception) throws TransformerException { log.error(exception); throw exception; } public void warning(TransformerException exception) throws TransformerException { log.info(exception); } }); return transformer; }
From source file:eionet.gdem.conversion.converters.ConvertStrategy.java
/** * Method transforms XML source to PDF using XSL-FO stream. * @param in InputStream containing source XML. * @param xsl InputStream containing XSL-FO content. * @param out OutputStream for conversion result. * @throws GDEMException In case of unexpected XML or XSL errors. *//*from w w w . j a v a 2s . c om*/ protected void runFOPTransformation(InputStream in, InputStream xsl, OutputStream out) throws GDEMException { try { Driver driver = new Driver(); driver.setRenderer(Driver.RENDER_PDF); driver.setOutputStream(out); Result res = new SAXResult(driver.getContentHandler()); Source src = new StreamSource(in); TransformerFactory transformerFactory = transform.getTransformerFactoryInstance(); TransformerErrorListener errors = new TransformerErrorListener(); transformerFactory.setErrorListener(errors); StreamSource transformerSource = new StreamSource(xsl); if (getXslPath() != null) { transformerSource.setSystemId(getXslPath()); } Transformer transformer = transformerFactory.newTransformer(transformerSource); setTransformerParameters(transformer); transformer.setErrorListener(errors); long l = System.currentTimeMillis(); transformer.transform(src, res); if (LOGGER.isDebugEnabled()) { LOGGER.debug((new StringBuilder()).append("generate: transformation needed ") .append(System.currentTimeMillis() - l).append(" ms").toString()); } } catch (TransformerConfigurationException tce) { throw new GDEMException("Error transforming XML to PDF - incorrect stylesheet file: " + tce.toString(), tce); } catch (TransformerException tfe) { throw new GDEMException( "Error transforming XML to PDF - it's not probably well-formed xml file: " + tfe.toString(), tfe); } catch (Throwable e) { LOGGER.error("Error " + e.toString(), e); throw new GDEMException("Error transforming XML to PDF " + e.toString()); } }
From source file:it.cnr.icar.eric.server.cms.CanonicalXMLValidationService.java
private void configureTransformer(Transformer transformer) { transformer.setURIResolver(rm.getURIResolver()); transformer.setErrorListener(new ErrorListener() { public void error(TransformerException exception) throws TransformerException { log.info(exception);/* w w w. j ava 2 s. com*/ } public void fatalError(TransformerException exception) throws TransformerException { log.error(exception); throw exception; } public void warning(TransformerException exception) throws TransformerException { log.info(exception); } }); }
From source file:it.cnr.icar.eric.server.event.EmailNotifier.java
private String transformContent(ServerRequestContext context, String xsltId, String xmlNotif, String action, String user) throws RegistryException { try {/*w w w .j a va 2s . c o m*/ RepositoryItem repositoryItem = RepositoryManagerFactory.getInstance().getRepositoryManager() .getRepositoryItem(xsltId); StreamSource xsltIn = new StreamSource(repositoryItem.getDataHandler().getInputStream()); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(xsltIn); //transformer.setURIResolver(rm.getURIResolver()); transformer.setErrorListener(new ErrorListener() { public void error(TransformerException exception) throws TransformerException { log.info(ServerResourceBundle.getInstance().getString("xsltError"), exception); } public void fatalError(TransformerException exception) throws TransformerException { log.error(ServerResourceBundle.getInstance().getString("xsltFatalError"), exception); throw exception; } public void warning(TransformerException exception) throws TransformerException { log.info(ServerResourceBundle.getInstance().getString("xsltWarning"), exception); } }); //Set parameters transformer.setParameter("action", action); transformer.setParameter("user", user); transformer.setParameter("registryBaseURL", RegistryProperties.getInstance().getProperty("eric.registry.baseurl")); ByteArrayInputStream bais = new ByteArrayInputStream(xmlNotif.getBytes("utf-8")); StreamSource inputSrc = new StreamSource(bais); //TODO: use file in case we have a large amount of data to transform? ByteArrayOutputStream baos = new ByteArrayOutputStream(); StreamResult streamResult = new StreamResult(baos); transformer.transform(inputSrc, streamResult); return baos.toString("utf-8"); } catch (Exception e) { log.error(ServerResourceBundle.getInstance().getString("message.prettyPrintNotificationFailure"), e); throw new RegistryException(e); } }