/** * Returns a transformer handler that serializes incoming SAX events to * XHTML or HTML (depending the given method) using the given output encoding. * * @param encoding output encoding, or <code>null</code> for the platform default */ private static TransformerHandler getTransformerHandler(OutputStream out, String method, String encoding) throws TransformerConfigurationException { TransformerHandler transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler(); Transformer transformer = transformerHandler.getTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, method); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); if (encoding != null) { transformer.setOutputProperty(OutputKeys.ENCODING, encoding); } transformerHandler.setResult(new StreamResult(new PrintStream(out))); return transformerHandler; }
public static String formatXml(String xml) { try { Transformer serializer = SAXTransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); return new String(((ByteArrayOutputStream)res.getOutputStream()).toByteArray()); } catch (Exception e) { return xml; } }
SAXTransformerFactory saxTFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); String href = avt.evaluate(xctxt,xt, elem); String absURI = SystemIDResolver.getAbsoluteURI(href, sysId); Templates tmpl = saxTFactory.newTemplates(new StreamSource(absURI)); TransformerHandler tHandler = saxTFactory.newTransformerHandler(tmpl); Transformer trans = tHandler.getTransformer(); avt = ((ElemLiteralResult)paramNode).getLiteralResultAttribute("value"); String pValue = avt.evaluate(xctxt,xt, elem); trans.setParameter(pName, pValue);
/** * Get a TransformerHandler object that can process SAX ContentHandler * events based on a copy transformer. * Uses org.apache.xalan.processor.TransformerFactory. */ public TransformerHandler newTransformerHandler() throws TransformerConfigurationException { if (_xalanFactory == null) { createXalanTransformerFactory(); } if (_errorlistener != null) { _xalanFactory.setErrorListener(_errorlistener); } if (_uriresolver != null) { _xalanFactory.setURIResolver(_uriresolver); } return _xalanFactory.newTransformerHandler(); }
/** * Create a Transformer object that copies the input document to the * result. Uses the org.apache.xalan.processor.TransformerFactory. * @return A Transformer object. */ public Transformer newTransformer() throws TransformerConfigurationException { if (_xalanFactory == null) { createXalanTransformerFactory(); } if (_errorlistener != null) { _xalanFactory.setErrorListener(_errorlistener); } if (_uriresolver != null) { _xalanFactory.setURIResolver(_uriresolver); } _currFactory = _xalanFactory; return _currFactory.newTransformer(); }
/** * Create a Templates object that from the input stylesheet * Uses the org.apache.xalan.xsltc.trax.TransformerFactory. * @param source the stylesheet. * @return A Templates object. */ public Templates newTemplates(Source source) throws TransformerConfigurationException { if (_xsltcFactory == null) { createXSLTCTransformerFactory(); } if (_errorlistener != null) { _xsltcFactory.setErrorListener(_errorlistener); } if (_uriresolver != null) { _xsltcFactory.setURIResolver(_uriresolver); } _currFactory = _xsltcFactory; return _currFactory.newTemplates(source); }
SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance(); // These templates objects could be reused and obtained from elsewhere. Templates templates1 = stf.newTemplates(new StreamSource( getClass().getResourceAsStream("MyStylesheet1.xslt"))); Templates templates2 = stf.newTemplates(new StreamSource( getClass().getResourceAsStream("MyStylesheet1.xslt"))); TransformerHandler th1 = stf.newTransformerHandler(templates1); TransformerHandler th2 = stf.newTransformerHandler(templates2); th1.setResult(new SAXResult(th2)); th2.setResult(new StreamResult(System.out)); Transformer t = stf.newTransformer(); t.transform(new StreamSource(System.in), new SAXResult(th1)); // th1 feeds th2, which in turn feeds System.out.
(SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler xmls; try { xmls = txFactory.newTransformerHandler(); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); xmls.getTransformer().setOutputProperty(OutputKeys.METHOD, "XML"); xmls.getTransformer().setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "true"); xmls.getTransformer().setOutputProperty(OutputKeys.INDENT, "true"); xmls.getTransformer().transform(new DOMSource(named.getElement()), new StreamResult(writer));
argv[++i], ObjectFactory.findClassLoader(), true); tfactory.setURIResolver(uriResolver); strResult = new StreamResult(new FileOutputStream(outFileName)); strResult.setSystemId(outFileName); strResult = new StreamResult(System.out); stf.setAttribute(XalanProperties.SOURCE_LOCATION, Boolean.TRUE); stf.getAssociatedStylesheet(new StreamSource(inFileName), media, null, null); transformer.setErrorListener(new DefaultErrorHandler(false)); transformer.setOutputProperty(OutputKeys.METHOD, outputType); Transformer serializer = stf.newTransformer(); serializer.setErrorListener(new DefaultErrorHandler(false)); throw new org.xml.sax.SAXException(ex); stf.setAttribute(org.apache.xalan.processor.TransformerFactoryImpl.FEATURE_INCREMENTAL, Boolean.TRUE); TransformerHandler th = stf.newTransformerHandler(stylesheet);
(SAXTransformerFactory) SAXTransformerFactory.newInstance(); xmls = txFactory.newTransformerHandler(); } catch (TransformerConfigurationException e) { throw new IOException(e); xmls.getTransformer().setOutputProperties(outputProps); xmls.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml"); xmls.setResult(new StreamResult(out)); Transformer tx = TransformerFactory.newInstance().newTransformer(); tx.transform(new StreamSource(in), result); Document d = (Document) result.getNode(); return d;
SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance(); Templates templates1 = stf.newTemplates(new StreamSource( getClass().getResourceAsStream("MyStylesheet1.xslt"))); Templates templates2 = stf.newTemplates(new StreamSource( getClass().getResourceAsStream("MyStylesheet2.xslt"))); TransformerHandler th1 = stf.newTransformerHandler(templates1); TransformerHandler th2 = stf.newTransformerHandler(templates2); th2.setResult(new StreamResult(System.out)); // Note that indent, etc should be applied to the last transformer in chain: th2.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes"); th1.getTransformer().transform(new StreamSource(System.in), new SAXResult(th2));
SAXTransformerFactory transformFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); StreamSource source = new StreamSource(TRANSFORMER_PATH); StringWriter extractionWriter = new StringWriter(); TransformerHandler transformerHandler = null; try { transformerHandler = transformFactory.newTransformerHandler(source); transformerHandler.setResult(new StreamResult(extractionWriter)); } catch (TransformerConfigurationException e) { throw new SAXException("Unable to create transformerHandler due to transformer configuration exception."); } XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); reader.setContentHandler(transformerHandler); reader.parse(new InputSource(new FileReader(xml))); System.err.println(extractionWriter.toString());
/** * Create a processor that writes to the file named and may or may not * also output to the screen, as specified. * * @param out output stream to write * @throws IOException on any error */ public XmlEditsVisitor(OutputStream out) throws IOException { this.out = out; factory =(SAXTransformerFactory)SAXTransformerFactory.newInstance(); try { TransformerHandler handler = factory.newTransformerHandler(); handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml"); handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8"); handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes"); handler.getTransformer().setOutputProperty(XML_INDENTATION_PROP, XML_INDENTATION_NUM); handler.getTransformer().setOutputProperty(OutputKeys.STANDALONE, "yes"); handler.setResult(new StreamResult(out)); contentHandler = handler; contentHandler.startDocument(); contentHandler.startElement("", "", "EDITS", new AttributesImpl()); } catch (TransformerConfigurationException e) { throw new IOException("SAXTransformer error: " + e.getMessage()); } catch (SAXException e) { throw new IOException("SAX error: " + e.getMessage()); } }
StreamResult streamResult = new StreamResult(out); SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); hd = tf.newTransformerHandler(); } catch (TransformerConfigurationException e) { throw new AssertionError("The Transformer configuration must be valid!"); Transformer serializer = hd.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, CHARSET); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); hd.setResult(streamResult); hd.startDocument(); throw new IOException("Error during serialization: " + e.getMessage(), e);
/** * Creates a serializing content handler that writes to the given result. * * @param result serialization target * @return serializing content handler * @throws SAXException if the content handler could not be initialized */ public static DefaultHandler getSerializer(Result result) throws SAXException { try { TransformerHandler handler = FACTORY.newTransformerHandler(); handler.setResult(result); // Specify the output properties to avoid surprises especially in // character encoding or the output method (might be html for some // documents!) Transformer transformer = handler.getTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING); transformer.setOutputProperty(OutputKeys.INDENT, "no"); if (NEEDS_XMLNS_ATTRIBUTES) { // The serializer does not output xmlns declarations, // so we need to do it explicitly with this wrapper return new SerializingContentHandler(handler); } else { return new DefaultContentHandler(handler); } } catch (TransformerConfigurationException e) { throw new SAXException("Failed to initialize XML serializer", e); } }
(SAXTransformerFactory) SAXTransformerFactory.newInstance(); xmls = txFactory.newTransformerHandler(); } catch (TransformerConfigurationException e) { throw new IOException(e); xmls.getTransformer().setOutputProperties(outputProps); xmls.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml"); xmls.setResult(new StreamResult(out)); if (e.getException() != null && e.getCause() == null) { e.initCause(e.getException());
private TransformerHandler getTransformerHandler() throws Exception { SAXTransformerFactory tf = saxtf; if (tf == null) saxtf = tf = (SAXTransformerFactory) TransformerFactory .newInstance(); if (xsltFile == null) return tf.newTransformerHandler(); Templates tpls = xsltTpls; if (tpls == null); xsltTpls = tpls = tf.newTemplates(new StreamSource(xsltFile)); return tf.newTransformerHandler(tpls); }
public void transform() throws TransformerException { if (source == null) { return; } if (result == null) { return; } if (transformerFactory == null) { transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); } Transformer transformer = transformerFactory.newTransformer(); transformer.transform(source, result); }
public static ContentHandler newHandler(OutputStream out, String encoding, boolean indent) throws TransformerConfigurationException { SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler hd = tf.newTransformerHandler(); Transformer serializer = tf.newTransformer(); StreamResult stream = new StreamResult(out); serializer.setOutputProperty(OutputKeys.ENCODING, encoding); serializer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); hd.setResult(stream); return hd; }
try { SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler transformerHandler = transformerFactory.newTransformerHandler(); _stringWriter = new StringWriter(); transformerHandler.setResult(new StreamResult(_stringWriter)); _active = true; return (T) new SAXResult(transformerHandler); _stringWriter = new StringWriter(); _active = true; return (T) new StreamResult(_stringWriter); } else if (StAXResult.class.equals(resultClass)) { _stringWriter = new StringWriter();