Java Code Examples for javax.xml.transform.stream.StreamResult#setWriter()

The following examples show how to use javax.xml.transform.stream.StreamResult#setWriter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmiParser.java    From pentaho-metadata with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String generateXmi( Domain domain ) {
  if ( domain == null ) {
    logger.error( Messages.getErrorString( "XmiParser.ERROR_0001_DOMAIN_NULL" ) ); //$NON-NLS-1$
    return null;
  }

  try {
    StringWriter stringWriter = new StringWriter();
    StreamResult result = new StreamResult();
    result.setWriter( stringWriter );
    TransformerFactory factory = TransformerFactory.newInstance();
    Document doc = toXmiDocument( domain );
    if ( doc != null ) {
      factory.newTransformer().transform( new DOMSource( doc ), result );
      return stringWriter.getBuffer().toString();
    }
  } catch ( Exception e ) {
    logger.error( Messages.getErrorString( "XmiParser.ERROR_0002_TO_XML_FAILED" ), e ); //$NON-NLS-1$
  }
  return null;
}
 
Example 2
Source File: QueryXmlHelper.java    From pentaho-metadata with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String toXML( final Query query ) {
  if ( query == null ) {
    logger.error( Messages.getErrorString( "QueryXmlHelper.ERROR_0000_QUERY_MUST_NOT_BE_NULL" ) ); //$NON-NLS-1$
    return null;
  }

  try {
    StringWriter stringWriter = new StringWriter();
    StreamResult result = new StreamResult();
    result.setWriter( stringWriter );
    TransformerFactory factory = TransformerFactory.newInstance();
    Document doc = toDocument( query );
    if ( doc != null ) {
      factory.newTransformer().transform( new DOMSource( doc ), result );
      return stringWriter.getBuffer().toString();
    }
  } catch ( Exception e ) {
    logger.error( Messages.getErrorString( "QueryXmlHelper.ERROR_0001_TO_XML_FAILED" ), e ); //$NON-NLS-1$
  }
  return null;
}
 
Example 3
Source File: MQLQueryImpl.java    From pentaho-metadata with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String getXML() {
  try {
    StringWriter stringWriter = new StringWriter();
    StreamResult result = new StreamResult();
    result.setWriter( stringWriter );
    TransformerFactory factory = TransformerFactory.newInstance();
    Document doc = getDocument();
    if ( doc != null ) {
      factory.newTransformer().transform( new DOMSource( doc ), result );
      return stringWriter.getBuffer().toString();
    }
  } catch ( Exception e ) {
    logger.error( Messages.getErrorString( "MQLQuery.ERROR_0013_GET_XML_FAILED" ), e ); //$NON-NLS-1$
  }
  return null;
}
 
Example 4
Source File: AbstractSourcePayloadProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static String getSourceAsString(Source s) throws Exception {
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        try (Writer out = new StringWriter()) {
            StreamResult streamResult = new StreamResult();
            streamResult.setWriter(out);
            transformer.transform(s, streamResult);
            return streamResult.getWriter().toString();
        }
    } catch (TransformerException te) {
        if ("javax.xml.transform.stax.StAXSource".equals(s.getClass().getName())) {
            //on java6, we will get this class if "stax" is configured
            //for the preferred type. However, older xalans don't know about it
            //we'll manually do it
            XMLStreamReader r = (XMLStreamReader)s.getClass().getMethod("getXMLStreamReader").invoke(s);
            return StaxUtils.toString(StaxUtils.read(r).getDocumentElement());
        }
        throw te;
    }
}
 
Example 5
Source File: PreviewPanel.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
public static String formatMetadata(String input, int indent) {
    input = input.replace("> <", "><");
    try {
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        StringWriter sw = new StringWriter();
        xmlOutput.setWriter(sw);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "" + indent);
        transformer.transform(xmlInput, xmlOutput);

        return xmlOutput.getWriter().toString();
    } catch (IllegalArgumentException | TransformerException e) {
        return input;
    }
}
 
Example 6
Source File: XMLOutputFactoryImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 7
Source File: TestLogicalHandler.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static String getSourceAsString(Source s) throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    Writer out = new StringWriter();
    StreamResult streamResult = new StreamResult();
    streamResult.setWriter(out);
    transformer.transform(s, streamResult);
    return streamResult.getWriter().toString();
}
 
Example 8
Source File: XMLOutputFactoryImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 9
Source File: XMLOutputFactoryImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 10
Source File: XMLOutputFactoryImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 11
Source File: XMLOutputFactoryImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 12
Source File: XMLOutputFactoryImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId) {
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 13
Source File: XMLOutputFactoryImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 14
Source File: XMLOutputFactoryImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 15
Source File: StringSchemaOutputResolver.java    From aion-germany with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
	sw = new StringWriter();
	StreamResult sr = new StreamResult();

	// If it's not set - schemagen throws AssertionError
	sr.setSystemId(String.valueOf(System.currentTimeMillis()));

	sr.setWriter(sw);
	return sr;
}
 
Example 16
Source File: XMLOutputFactoryImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 17
Source File: DocumentUtil.java    From edireader with GNU General Public License v3.0 4 votes vote down vote up
public synchronized void writeXML(Document document, Writer writer) throws TransformerException {
    StreamResult streamResult = new StreamResult();
    streamResult.setWriter(writer);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(document), streamResult);
}