List of usage examples for javax.xml.transform TransformerFactory getErrorListener
public abstract ErrorListener getErrorListener();
From source file:org.toobsframework.transformpipeline.domain.ChainedXSLTransformer.java
private String transform(List inputXSLs, String inputXML, Map inputParams, IXMLTransformerHelper transformerHelper) throws XMLTransformerException { String outputXML = null;//ww w . java 2s.c om ByteArrayInputStream xmlInputStream = null; ByteArrayOutputStream xmlOutputStream = null; try { TransformerFactory tFactory = TransformerFactory.newInstance(); setFactoryResolver(tFactory); if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) { // Cast the TransformerFactory to SAXTransformerFactory. SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory); // Create a TransformerHandler for each stylesheet. ArrayList tHandlers = new ArrayList(); TransformerHandler tHandler = null; // Create an XMLReader. XMLReader reader = new SAXParser(); // transformer3 outputs SAX events to the serializer. if (outputProperties == null) { outputProperties = OutputPropertiesFactory.getDefaultMethodProperties("html"); } Serializer serializer = SerializerFactory.getSerializer(outputProperties); String xslFile = null; for (int it = 0; it < inputXSLs.size(); it++) { Object source = inputXSLs.get(it); if (source instanceof StreamSource) { tHandler = saxTFactory.newTransformerHandler((StreamSource) source); if (xslFile == null) xslFile = ((StreamSource) source).getSystemId(); } else { //tHandler = saxTFactory.newTransformerHandler(new StreamSource(getXSLFile((String) source))); tHandler = saxTFactory .newTransformerHandler(uriResolver.resolve((String) source + ".xsl", "")); if (xslFile == null) xslFile = (String) source; } Transformer transformer = tHandler.getTransformer(); transformer.setOutputProperty("encoding", "UTF-8"); transformer.setErrorListener(tFactory.getErrorListener()); if (inputParams != null) { Iterator paramIt = inputParams.entrySet().iterator(); while (paramIt.hasNext()) { Map.Entry thisParam = (Map.Entry) paramIt.next(); transformer.setParameter((String) thisParam.getKey(), (String) thisParam.getValue()); } } if (transformerHelper != null) { transformer.setParameter(TRANSFORMER_HELPER, transformerHelper); } tHandlers.add(tHandler); } tHandler = null; for (int th = 0; th < tHandlers.size(); th++) { tHandler = (TransformerHandler) tHandlers.get(th); if (th == 0) { reader.setContentHandler(tHandler); reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler); } else { ((TransformerHandler) tHandlers.get(th - 1)).setResult(new SAXResult(tHandler)); } } // Parse the XML input document. The input ContentHandler and output ContentHandler // work in separate threads to optimize performance. InputSource xmlSource = null; xmlInputStream = new ByteArrayInputStream((inputXML).getBytes("UTF-8")); if (log.isTraceEnabled()) { log.trace("Input XML:\n" + inputXML); } xmlSource = new InputSource(xmlInputStream); xmlOutputStream = new ByteArrayOutputStream(); serializer.setOutputStream(xmlOutputStream); ((TransformerHandler) tHandlers.get(tHandlers.size() - 1)) .setResult(new SAXResult(serializer.asContentHandler())); Date timer = new Date(); reader.parse(xmlSource); Date timer2 = new Date(); outputXML = xmlOutputStream.toString("UTF-8"); if (log.isDebugEnabled()) { long diff = timer2.getTime() - timer.getTime(); log.debug("Time to transform: " + diff + " mS XSL: " + xslFile); if (log.isTraceEnabled()) { log.trace("Output XML:\n" + outputXML); } } } } catch (IOException ex) { throw new XMLTransformerException(ex); } catch (IllegalArgumentException ex) { throw new XMLTransformerException(ex); } catch (SAXException ex) { throw new XMLTransformerException(ex); } catch (TransformerConfigurationException ex) { throw new XMLTransformerException(ex); } catch (TransformerFactoryConfigurationError ex) { throw new XMLTransformerException(ex); } catch (TransformerException ex) { throw new XMLTransformerException(ex); } finally { try { if (xmlInputStream != null) { xmlInputStream.close(); xmlInputStream = null; } if (xmlOutputStream != null) { xmlOutputStream.close(); xmlOutputStream = null; } } catch (IOException ex) { } } return outputXML; }
From source file:org.toobsframework.transformpipeline.domain.ChainedXSLTransletTransformer.java
public String transform(List inputXSLs, String inputXML, Map inputParams, IXMLTransformerHelper transformerHelper) throws XMLTransformerException { String outputXML = null;/*from w w w.java2 s.co m*/ ByteArrayInputStream xmlInputStream = null; ByteArrayOutputStream xmlOutputStream = null; try { TransformerFactory tFactory = new org.apache.xalan.xsltc.trax.TransformerFactoryImpl(); try { //tFactory.setAttribute("use-classpath", Boolean.TRUE); tFactory.setAttribute("auto-translet", Boolean.TRUE); } catch (IllegalArgumentException iae) { log.error("Error setting XSLTC specific attribute", iae); throw new XMLTransformerException(iae); } setFactoryResolver(tFactory); TransformerFactoryImpl traxFactory = (TransformerFactoryImpl) tFactory; // Create a TransformerHandler for each stylesheet. ArrayList tHandlers = new ArrayList(); TransformerHandler tHandler = null; // Create a SAX XMLReader. XMLReader reader = new org.apache.xerces.parsers.SAXParser(); // transformer3 outputs SAX events to the serializer. if (outputProperties == null) { outputProperties = OutputPropertiesFactory.getDefaultMethodProperties("html"); } Serializer serializer = SerializerFactory.getSerializer(outputProperties); for (int it = 0; it < inputXSLs.size(); it++) { String xslTranslet = (String) inputXSLs.get(it); Source source = uriResolver.resolve(xslTranslet + ".xsl", ""); String tPkg = source.getSystemId().substring(0, source.getSystemId().lastIndexOf("/")) .replaceAll("/", ".").replaceAll("-", "_"); // Package name needs to be set for each TransformerHandler instance tFactory.setAttribute("package-name", tPkg); tHandler = traxFactory.newTransformerHandler(source); // Set parameters and output encoding on each handlers transformer Transformer transformer = tHandler.getTransformer(); transformer.setOutputProperty("encoding", "UTF-8"); transformer.setErrorListener(tFactory.getErrorListener()); if (inputParams != null) { Iterator paramIt = inputParams.entrySet().iterator(); while (paramIt.hasNext()) { Map.Entry thisParam = (Map.Entry) paramIt.next(); transformer.setParameter((String) thisParam.getKey(), (String) thisParam.getValue()); } } if (transformerHelper != null) { transformer.setParameter(TRANSFORMER_HELPER, transformerHelper); } tHandlers.add(tHandler); } tHandler = null; // Link the handlers to each other and to the reader for (int th = 0; th < tHandlers.size(); th++) { tHandler = (TransformerHandler) tHandlers.get(th); if (th == 0) { reader.setContentHandler(tHandler); reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler); } else { ((TransformerHandler) tHandlers.get(th - 1)).setResult(new SAXResult(tHandler)); } } // Parse the XML input document. The input ContentHandler and output ContentHandler // work in separate threads to optimize performance. InputSource xmlSource = null; xmlInputStream = new ByteArrayInputStream((inputXML).getBytes("UTF-8")); xmlSource = new InputSource(xmlInputStream); xmlOutputStream = new ByteArrayOutputStream(); serializer.setOutputStream(xmlOutputStream); // Link the last handler to the serializer ((TransformerHandler) tHandlers.get(tHandlers.size() - 1)) .setResult(new SAXResult(serializer.asContentHandler())); reader.parse(xmlSource); outputXML = xmlOutputStream.toString("UTF-8"); } catch (Exception ex) { log.error("Error performing chained transformation: " + ex.getMessage(), ex); throw new XMLTransformerException(ex); } finally { try { if (xmlInputStream != null) { xmlInputStream.close(); xmlInputStream = null; } if (xmlOutputStream != null) { xmlOutputStream.close(); xmlOutputStream = null; } } catch (IOException ex) { } } return outputXML; }
From source file:org.toobsframework.transformpipeline.domain.TransletTransformer.java
@SuppressWarnings("unchecked") protected void doTransform(String xslTranslet, StreamSource xmlSource, Map params, IXMLTransformerHelper transformerHelper, StreamResult xmlResult) throws XMLTransformerException { try {//from w ww .ja v a 2 s .c o m // Dont rely on the system property to get the right transformer TransformerFactory tFactory = new org.apache.xalan.xsltc.trax.TransformerFactoryImpl(); // set the URI Resolver for the transformer factory setFactoryResolver(tFactory); Source source = uriResolver.resolve(xslTranslet + ".xsl", ""); String tPkg = source.getSystemId().substring(0, source.getSystemId().lastIndexOf("/")) .replaceAll("/", ".").replaceAll("-", "_"); try { //tFactory.setAttribute("use-classpath", Boolean.TRUE); tFactory.setAttribute("auto-translet", Boolean.TRUE); tFactory.setAttribute("package-name", tPkg); } catch (IllegalArgumentException iae) { log.error("Error setting XSLTC specific attribute", iae); throw new XMLTransformerException(iae); } Transformer transformer = tFactory.newTransformer(source); // 2.2 Set character encoding for all transforms to UTF-8. transformer.setOutputProperty("encoding", "UTF-8"); transformer.setErrorListener(tFactory.getErrorListener()); // 2.5 Set Parameters necessary for transformation. if (params != null) { Iterator paramIt = params.entrySet().iterator(); while (paramIt.hasNext()) { Map.Entry thisParam = (Map.Entry) paramIt.next(); transformer.setParameter((String) thisParam.getKey(), (String) thisParam.getValue()); } } if (transformerHelper != null) { transformer.setParameter(TRANSFORMER_HELPER, transformerHelper); } // 3. Use the Transformer to transform an XML Source and send the // output to a Result object. transformer.transform(xmlSource, xmlResult); } catch (TransformerConfigurationException tce) { log.error(tce.toString(), tce); throw new XMLTransformerException(tce); } catch (TransformerException te) { log.error(te.toString(), te); throw new XMLTransformerException(te); } }