Example usage for javax.xml.transform.stream StreamResult getWriter

List of usage examples for javax.xml.transform.stream StreamResult getWriter

Introduction

In this page you can find the example usage for javax.xml.transform.stream StreamResult getWriter.

Prototype

public Writer getWriter() 

Source Link

Document

Get the character stream that was set with setWriter.

Usage

From source file:org.apache.camel.component.social.providers.twitter.AbstractTwitterPath.java

protected DefaultSocialData parseStatus(Node aNode) throws TransformerException {
    StreamResult streamResult = new StreamResult(new StringWriter());
    DOMSource domSource = new DOMSource(aNode);
    getTransformer().transform(domSource, streamResult);

    String xmlString = streamResult.getWriter().toString();
    String id = null;//from w  w  w .j a  v  a 2  s . c o m
    NodeList childs = aNode.getChildNodes();
    for (int j = 0; j < childs.getLength(); j++) {
        Node child = childs.item(j);
        if (child.getNodeName().equals("id")) {
            id = child.getTextContent();
            break;
        }
    }

    DefaultSocialData socialData = new DefaultSocialData(id, xmlString);
    return socialData;
}

From source file:com.ibm.bi.dml.conf.DMLConfig.java

/**
 * //from w  w w  . j a v a2 s  . c  o m
 * @return
 * @throws DMLRuntimeException
 */
public synchronized String serializeDMLConfig() throws DMLRuntimeException {
    String ret = null;
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        //transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(xml_root);
        transformer.transform(source, result);
        ret = result.getWriter().toString();
    } catch (Exception ex) {
        throw new DMLRuntimeException("Unable to serialize DML config.", ex);
    }

    return ret;
}

From source file:com.maxl.java.aips2xml.Aips2Xml.java

static String prettyFormat(String input) {
    try {//from  w  ww.  j a  v  a 2s  . co  m
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:com.summer.logger.LoggerPrinter.java

  /**
 * Formats the json content and print it
 *
 * @param xml the xml content//from  w w  w  .  ja  va2  s . c  om
 */
@Override public void xml(String xml) {
  if (TextUtils.isEmpty(xml)) {
    d("Empty/Null xml content");
    return;
  }
  try {
    Source xmlInput = new StreamSource(new StringReader(xml));
    StreamResult xmlOutput = new StreamResult(new StringWriter());
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.transform(xmlInput, xmlOutput);
    d(xmlOutput.getWriter().toString().replaceFirst(">", ">\n"));
  } catch (TransformerException e) {
    e(e.getCause().getMessage() + "\n" + xml);
  }
}

From source file:fr.arnaudguyon.xmltojsonlib.JsonToXml.java

/**
 *
 * @param indent size of the indent (number of spaces)
 * @return the formatted XML//from w  w  w . j  a  v  a 2s  . c  o m
 */
public String toFormattedString(@IntRange(from = 0) int indent) {
    String input = toString();
    try {
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "" + indent);
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // TODO: do my own
    }
}

From source file:com.genericworkflownodes.knime.workflowexporter.export.impl.GuseKnimeWorkflowExporter.java

private String formatXml(final String unformattedXml) throws TransformerException {
    final Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    // initialize StreamResult with File object to save to file
    final StreamResult result = new StreamResult(new StringWriter());
    final StreamSource source = new StreamSource(new StringReader(unformattedXml));
    transformer.transform(source, result);
    return result.getWriter().toString();
}

From source file:com.hp.hpl.jena.grddl.impl.GRDDL.java

private void endGrddlResult(Result result) {
    // TODO security issues here
    StreamResult sr = (StreamResult) result;
    StringWriter sw = (StringWriter) sr.getWriter();

    String html = sw.toString();/*  w  w w. j  a va2 s . c o m*/
    System.err.println(html);
    reader.read(model, new StringReader(html), input.retrievalIRI());

}

From source file:com.lmco.ddf.ui.Query.java

/**
 * /*from  w w  w . j  ava  2  s.c o  m*/
 * @param unformattedXml Unformatted xml.
 * @return Returns formatted xml.
 */
private String format(String unformattedXml) {
    Source xmlInput = new StreamSource(new StringReader(unformattedXml));
    StringWriter stringWriter = new StringWriter();
    StreamResult xmlOutput = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    Transformer transformer = null;
    String formattedXml = null;

    try {
        transformer = transformerFactory.newTransformer();
        LOGGER.debug("transformer class: " + transformer.getClass());
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(xmlInput, xmlOutput);
        formattedXml = xmlOutput.getWriter().toString();
    } catch (TransformerConfigurationException e)

    {
        String message = "Unable to transform xml:\n" + unformattedXml + "\nUsing unformatted xml.";
        LOGGER.error(message, e);
        formattedXml = unformattedXml;
    } catch (TransformerException e) {
        String message = "Unable to transform xml:\n" + unformattedXml + "\nUsing unformatted xml.";
        LOGGER.error(message, e);
        formattedXml = unformattedXml;
    }

    LOGGER.debug("Formatted xml:\n" + formattedXml);

    return formattedXml;
}

From source file:de.huberlin.wbi.hiway.am.galaxy.GalaxyApplicationMaster.java

/**
 * A (recursive) helper function for parsing the macros used by the XML files describing Galaxy's tools
 * //from w  w w .  ja  va2  s . co  m
 * @param macrosNd
 *            an XML node that specifies a set of macros
 * @param dir
 *            the directory in which the currently processed macros are located
 * @return processed macros accessible by their name
 */
private Map<String, String> processMacros(Node macrosNd, String dir) {
    Map<String, String> macrosByName = new HashMap<>();
    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Element macrosEl = (Element) macrosNd;

        // (1) if additional macro files are to be imported, open the files and recursively invoke this method
        NodeList importNds = macrosEl.getElementsByTagName("import");
        for (int j = 0; j < importNds.getLength(); j++) {
            Element importEl = (Element) importNds.item(j);
            String importFileName = importEl.getChildNodes().item(0).getNodeValue().trim();
            File file = new File(dir, importFileName);
            Document doc = builder.parse(file);
            macrosByName.putAll(processMacros(doc.getDocumentElement(), dir));
        }

        // (2) parse all macros in this set
        NodeList macroNds = macrosEl.getElementsByTagName("macro");
        for (int j = 0; j < macroNds.getLength(); j++) {
            Element macroEl = (Element) macroNds.item(j);
            String name = macroEl.getAttribute("name");

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            StreamResult result = new StreamResult(new StringWriter());
            DOMSource source = new DOMSource(macroEl);
            transformer.transform(source, result);
            String macro = result.getWriter().toString();
            macro = macro.substring(macro.indexOf('\n') + 1, macro.lastIndexOf('\n'));
            macrosByName.put(name, macro);
        }
    } catch (SAXException | IOException | TransformerException | ParserConfigurationException e) {
        e.printStackTrace();
        System.exit(-1);
    }
    return macrosByName;
}

From source file:edu.wpi.margrave.MCommunicator.java

protected static String transformXMLToString(Document theResponse) {
    try {//w  w w  .ja v  a2  s. c  o  m
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        //initialize StreamResult with File object to save to file
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(theResponse);

        // If this line causes a null pointer exception, there is an empty text element somewhere.
        // For some reason the transformer can't handle text elements with "" in them.
        transformer.transform(source, result);

        String xmlString = result.getWriter().toString();
        return xmlString;
    } catch (Exception e) {
        // Will hit this if theResponse is null.
        // don't do this. would go through System.err
        //e.printStackTrace();
        return (makeDetailedError(e.getLocalizedMessage()));
    }

}