/** * Template method for handling {@code StreamResult}s. * <p>This implementation delegates to {@code marshalOutputStream} or {@code marshalWriter}, * depending on what is contained in the {@code StreamResult} * @param graph the root of the object graph to marshal * @param streamResult the {@code StreamResult} * @throws IOException if an I/O Exception occurs * @throws XmlMappingException if the given object cannot be marshalled to the result * @throws IllegalArgumentException if {@code streamResult} does neither * contain an {@code OutputStream} nor a {@code Writer} */ protected void marshalStreamResult(Object graph, StreamResult streamResult) throws XmlMappingException, IOException { if (streamResult.getOutputStream() != null) { marshalOutputStream(graph, streamResult.getOutputStream()); } else if (streamResult.getWriter() != null) { marshalWriter(graph, streamResult.getWriter()); } else { throw new IllegalArgumentException("StreamResult contains neither OutputStream nor Writer"); } }
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; } }
private static String prettyFormatXml(String xml) { try { TransformerFactory factory = SAXTransformerFactory.newInstance(); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); Transformer serializer = factory.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", DEFAULT_INDENT); Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray(), StandardCharsets.UTF_8); } catch (TransformerException ignored) { // Ignore, raw XML will be returned } return xml; }
StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); return new String(((ByteArrayOutputStream)res.getOutputStream()).toByteArray()); }catch(Exception e){
/** * Generate schema. */ public void doSchema(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException { setHeaders(rsp); rsp.setContentType("application/xml"); StreamResult r = new StreamResult(rsp.getOutputStream()); new SchemaGenerator(new ModelBuilder().get(bean.getClass())).generateSchema(r); r.getOutputStream().close(); }
e.getKey().writeTo( result, systemIds ); if(result instanceof StreamResult) { OutputStream outputStream = ((StreamResult)result).getOutputStream(); if(outputStream != null) {
else if (null != sresult.getOutputStream()) serializer.setOutputStream(sresult.getOutputStream()); else if (null != sresult.getSystemId())
else if (null != sresult.getOutputStream()) serializer.setOutputStream(sresult.getOutputStream()); else if (null != sresult.getSystemId())
else if (null != sresult.getOutputStream()) serializer.setOutputStream(sresult.getOutputStream()); else if (null != sresult.getSystemId())
else if (sr.getOutputStream() != null) return createWriter(sr.getOutputStream()); else if (sr.getSystemId() != null) { String fileURL = sr.getSystemId();
java.io.OutputStream out = strResult.getOutputStream(); java.io.Writer writer = strResult.getWriter(); try
final OutputStream ostream = target.getOutputStream(); if (ostream != null) { _tohFactory.setOutputStream(ostream);
else if (null != sresult.getOutputStream()) serializer.setOutputStream(sresult.getOutputStream()); else if (null != sresult.getSystemId())
public static String saxonTransform(String source, String xslt) throws TransformerException, FileNotFoundException { TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl(); f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE); StreamSource xsrc = new StreamSource(new FileInputStream(xslt)); Transformer t = f.newTransformer(xsrc); StreamSource src = new StreamSource(new FileInputStream(source)); StreamResult res = new StreamResult(new ByteArrayOutputStream()); t.transform(src, res); return res.getOutputStream().toString(); }
else if (null != sresult.getOutputStream()) serializer.setOutputStream(sresult.getOutputStream()); else if (null != sresult.getSystemId())
public XMLStreamWriter createXMLStreamWriter(Result result) throws XMLStreamException { // Can only support simplest of Result impls: if (result instanceof StreamResult) { StreamResult sr = (StreamResult) result; OutputStream out = sr.getOutputStream(); if (out != null) { return createXMLStreamWriter(out); } Writer w = sr.getWriter(); if (w != null) { return createXMLStreamWriter(w); } throw new UnsupportedOperationException("Only those javax.xml.transform.stream.StreamResult instances supported that have an OutputStream or Writer"); } throw new UnsupportedOperationException("Only javax.xml.transform.stream.StreamResult type supported"); }
/** * Parses an instance document defined by a transformer source. * <p> * Note: Currently this method reads the entire source into memory in order to validate * it. If large documents must be parsed one of {@link # * </p> * @param source THe source of the instance document. * * @return @return The object representation of the root element of the document. * * @throws IOException * @throws SAXException * @throws ParserConfigurationException * @throws TransformerException * * @since 2.6 */ public Object parse(Source source) throws IOException, SAXException, ParserConfigurationException, TransformerException { // TODO: use SAXResult to stream, need to figure out how to enable // validation with transformer api // SAXResult result = new SAXResult( handler ); StreamResult result = new StreamResult(new ByteArrayOutputStream()); TransformerFactory tf = TransformerFactory.newInstance(); Transformer tx = tf.newTransformer(); tx.transform(source, result); return parse( new ByteArrayInputStream( ((ByteArrayOutputStream) result.getOutputStream()).toByteArray())); }
public void marshal(Object graph, Result result) throws IOException, XmlMappingException { String payload = null; if (graph instanceof Message<?>) { payload = (String) ((Message<?>) graph).getPayload(); } else { payload = (String) graph; } ((StreamResult) result).getOutputStream().write(payload.getBytes()); }
e.getKey().writeTo( result, systemIds ); if(result instanceof StreamResult) { OutputStream outputStream = ((StreamResult)result).getOutputStream(); if(outputStream != null) {
else if (sr.getOutputStream() != null) return createWriter(sr.getOutputStream()); else if (sr.getSystemId() != null) { String fileURL = sr.getSystemId();