List of usage examples for javax.xml.transform.stream StreamResult setOutputStream
public void setOutputStream(OutputStream outputStream)
From source file:Main.java
public static void writeXml(OutputStream os, Node node, String encoding) throws TransformerException { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); DOMSource source = new DOMSource(); source.setNode(node);/*from w w w . jav a 2s . co m*/ StreamResult result = new StreamResult(); result.setOutputStream(os); transformer.transform(source, result); }
From source file:Main.java
/** * Creae a stream result based on a File. * Other than the default implementation not only the File is used to * initialize the result, but also a Stream is opened and initialized. * This works around an issue with whitespaces in the pathname which * otherwise would lead to a file not found exception * <pre>//from w w w .jav a 2s . com * Error during transformation: javax.xml.transform.TransformerException: java.io.FileNotFoundException: ...%20... * at org.apache.xalan.transformer.TransformerImpl.createSerializationHandler(TransformerImpl.java:1218) * at org.apache.xalan.transformer.TransformerImpl.createSerializationHandler(TransformerImpl.java:1060) * at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1268) * at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1251) * .... * </pre>. * The caller must ensure that the created outputstream is closed. * @param outFile the file to be used in the stream result. * @return a new StreamResult piontint to the given File. * @throws IOException in case of an issue while creating the stream. */ public static StreamResult createStreamResult(File outFile) throws IOException { final StreamResult result = new StreamResult(outFile); // set the stream directly to avoid issues with blanks in the // filename. result.setOutputStream(new FileOutputStream(outFile)); return result; }
From source file:Main.java
public static void saveDocument(Document dom, String file) throws TransformerException, IOException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, dom.getDoctype().getPublicId()); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dom.getDoctype().getSystemId()); DOMSource source = new DOMSource(dom); StreamResult result = new StreamResult(); FileOutputStream outputStream = null; try {/*from w w w .j ava 2s. co m*/ outputStream = new FileOutputStream(file); result.setOutputStream(outputStream); transformer.transform(source, result); outputStream.flush(); } finally { if (outputStream != null) { outputStream.close(); } } }
From source file:fr.ece.epp.tools.Utils.java
public static void output(Node node, String filename) { TransformerFactory transFactory = TransformerFactory.newInstance(); try {/* ww w . j av a 2 s. c o m*/ Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("encoding", "utf8"); transformer.setOutputProperty("indent", "yes"); DOMSource source = new DOMSource(); source.setNode(node); StreamResult result = new StreamResult(); if (filename == null) { result.setOutputStream(System.out); } else { result.setOutputStream(new FileOutputStream(filename)); } transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
From source file:Main.java
public static String generateXml() { DocumentBuilder documentBuilder = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {//w ww .j a v a 2 s .c o m documentBuilder = docFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); DOMSource docSource = new DOMSource(document); StreamResult result = new StreamResult(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); result.setOutputStream(baos); transformer.transform(docSource, result); } catch (Exception e) { e.printStackTrace(); } return baos.toString(); }
From source file:cz.cas.lib.proarc.common.export.mets.MetsUtils.java
/** * * Validates given document agains an XSD schema * * @param document// w ww .j a v a 2 s . c o m * @param xsd * @return */ public static List<String> validateAgainstXSD(Document document, InputStream xsd) throws Exception { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); factory.setResourceResolver(MetsLSResolver.getInstance()); Schema schema = factory.newSchema(new StreamSource(xsd)); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource domSource = new DOMSource(document); StreamResult sResult = new StreamResult(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); sResult.setOutputStream(bos); transformer.transform(domSource, sResult); InputStream is = new ByteArrayInputStream(bos.toByteArray()); DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); dbfactory.setValidating(false); dbfactory.setNamespaceAware(true); dbfactory.setSchema(schema); DocumentBuilder documentBuilder = dbfactory.newDocumentBuilder(); ValidationErrorHandler errorHandler = new ValidationErrorHandler(); documentBuilder.setErrorHandler(errorHandler); documentBuilder.parse(is); return errorHandler.getValidationErrors(); }
From source file:org.opencds.vmr.v1_0.mappings.out.MarshalVMR2VMRSchemaPayload.java
public synchronized String mappingOutbound(Map<String, List<?>> results, DSSRequestKMItem dssRequestKMItem) throws InvalidDriDataFormatExceptionFault, UnrecognizedLanguageExceptionFault, RequiredDataNotProvidedExceptionFault, UnsupportedLanguageExceptionFault, UnrecognizedScopedEntityExceptionFault, EvaluationExceptionFault, InvalidTimeZoneOffsetExceptionFault, DSSRuntimeExceptionFault {/*from w w w . j a va2 s .c o m*/ String interactionId = dssRequestKMItem.getDssRequestDataItem().getInteractionId(); String requestedKmId = dssRequestKMItem.getRequestedKmId(); String streamResultString = ""; log.debug("II: " + interactionId + " KMId: " + requestedKmId + " begin buildVMRSchemaResultSet"); BuildVMRSchemaResultSet buildVMRSchemaResultSet = BuildVMRSchemaResultSet.getInstance(); org.opencds.vmr.v1_0.schema.CDSOutput cdsXMLOutput = buildVMRSchemaResultSet.buildResultSet(results, dssRequestKMItem); log.debug("II: " + interactionId + " KMId: " + requestedKmId + " finish buildVMRSchemaResultSet"); dssRequestKMItem.getKmTimingData().setFinishBuildOutputTime(new AtomicLong(System.nanoTime())); JAXBElement<org.opencds.vmr.v1_0.schema.CDSOutput> jaxbCDSOutput; try { log.debug("II: " + interactionId + " KMId: " + requestedKmId + " begin factory.createCdsOutput"); org.opencds.vmr.v1_0.schema.ObjectFactory factory = new org.opencds.vmr.v1_0.schema.ObjectFactory(); jaxbCDSOutput = factory.createCdsOutput(cdsXMLOutput); log.debug("II: " + interactionId + " KMId: " + requestedKmId + " finish factory.createCdsOutput"); } catch (RuntimeException e) { e.printStackTrace(); throw new DSSRuntimeExceptionFault("RuntimeException in mappingOutbound ObjectFactory cdsOutput: " + e.getMessage() + ", vmrOutput=" + cdsXMLOutput.getVmrOutput().toString(), e); } if ((null == cdsXMLOutput) || ((null == cdsXMLOutput.getSimpleOutput()) && (null == cdsXMLOutput.getVmrOutput()))) { // show the tags, but empty payload streamResultString = ""; //FIXME throw new DSSRuntimeExceptionFault("Marshalled Rules Engine results are empty."); } else { log.debug("II: " + interactionId + " KMId: " + requestedKmId + " begin marshalling results to external VMR: " + cdsXMLOutput.getVmrOutput().getTemplateId().toString()); ByteArrayOutputStream output = new ByteArrayOutputStream(); StreamResult streamResult = new StreamResult(); streamResult.setOutputStream(output); try { JAXBContext jaxbContext = SimpleKnowledgeRepository .getRequiredJAXBContextForMarshallerClassCache(this.getClass().getName()); Marshaller marshaller = SimpleKnowledgeRepository .getRequiredMarshallerInstanceForMarshallerClassCache(this.getClass().getName(), jaxbContext); marshaller.marshal(jaxbCDSOutput, streamResult); } catch (JAXBException e) { e.printStackTrace(); throw new EvaluationExceptionFault( "JAXBException in mappingOutbound marshalling cdsOutput: " + e.getMessage(), e); } catch (RuntimeException e) { e.printStackTrace(); throw new DSSRuntimeExceptionFault( "RuntimeException in mappingOutbound marshalling cdsOutput: " + e.getMessage(), e); } log.debug("II: " + interactionId + " KMId: " + requestedKmId + " finished marshalling results to external VMR: " + cdsXMLOutput.getVmrOutput().getTemplateId().toString()); OutputStream outputStream = streamResult.getOutputStream(); streamResultString = outputStream.toString(); } dssRequestKMItem.getKmTimingData().setFinishMarshalTime(new AtomicLong(System.nanoTime())); return streamResultString; }
From source file:org.xchain.namespaces.sax.ResultCommand.java
/** * <p>Returns the Result object for the system-id attribute.</p> * @param context the JXPathContext to evaluate against. * @return a stream result for the system id. *///from ww w . j a va 2 s . c o m public Result createResultForSystemId(JXPathContext context) throws Exception { // set the system id. //String systemId = getSystemId(context); String systemId = null; if (hasSystemId()) { systemId = getSystemId(context); } else { systemId = getSystemIdDepricated(context); } // create a result object for the system id. URL url = UrlFactory.getInstance().newUrl(systemId); // create an output stream for this url. OutputStream out = url.openConnection().getOutputStream(); // create a stream result for the output stream. StreamResult streamResult = new StreamResult(); streamResult.setSystemId(systemId); streamResult.setOutputStream(out); return streamResult; }
From source file:org.xchain.namespaces.sax.ResultCommand.java
/** * <p>Returns the Result object for the path attribute.</p> * @param context the JXPathContext to evaluate against. * @return a stream result for the path specified. *///from ww w. ja v a 2 s.c o m public Result createResultForPath(JXPathContext context) throws Exception { // set the system id. String path = getPath(context); // get the file object for the path. File file = new File(path); // create the directories leading up to the path. File parentFile = file.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } // make sure that the file also exists. file.createNewFile(); // create an output stream for this url. OutputStream out = new FileOutputStream(file); // create a stream result for the output stream. StreamResult streamResult = new StreamResult(); streamResult.setSystemId(file.toURL().toExternalForm()); streamResult.setOutputStream(out); return streamResult; }