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:biz.c24.io.spring.oxm.C24Marshaller.java

/**
 * Returns a C24 {@link XMLSink} for the given {@link StreamResult}. Will prefer the result's {@link Writer} over its
 * {@link OutputStream}./*from   w  ww.j a v  a 2 s  . co m*/
 * 
 * @param result
 * @return
 */
private XMLSink getXmlSinkFrom(StreamResult result) {

    Writer writer = result.getWriter();
    return writer != null ? new XMLSink(writer) : new XMLSink(result.getOutputStream());
}

From source file:org.springframework.integration.samples.rest.RestHttpClientTest.java

/**
 *
 * @throws Exception/*from  w  w  w .j a v  a  2 s.  c  om*/
 */
@Test
public void testGetEmployeeAsXml() throws Exception {

    Map<String, Object> employeeSearchMap = getEmployeeSearchMap("0");

    final String fullUrl = "http://localhost:8080/rest-http/services/employee/{id}/search";

    EmployeeList employeeList = restTemplate.execute(fullUrl, HttpMethod.GET, new RequestCallback() {
        @Override
        public void doWithRequest(ClientHttpRequest request) throws IOException {
            HttpHeaders headers = getHttpHeadersWithUserCredentials(request);
            headers.add("Accept", "application/xml");
        }
    }, responseExtractor, employeeSearchMap);

    logger.info("The employee list size :" + employeeList.getEmployee().size());

    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);

    marshaller.marshal(employeeList, sr);
    logger.info(sr.getWriter().toString());
    assertTrue(employeeList.getEmployee().size() > 0);
}

From source file:com.mockey.storage.xml.MockeyXmlFactory.java

private String getDocumentAsString(Document document) throws IOException, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, HTTP.UTF_8);
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(document);
    transformer.transform(source, result);
    return result.getWriter().toString();
}

From source file:info.ajaxplorer.client.http.XMLDocEntity.java

public void toLogger() {
    if (true) {/*www .j a  v a2s.co m*/
        return;
    }
    try {
        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(doc);
        transformer.transform(source, result);

        String xmlString = result.getWriter().toString();
        System.out.println(xmlString);
    } catch (Exception e) {
    }
}

From source file:com.github.caldav4j.methods.NewPropFindTest.java

private String ElementoString(Element node) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(node);
    transformer.transform(source, result);

    String xmlString = result.getWriter().toString();
    log.info(xmlString);//w  w w.  j a  v  a2  s .c om
    return xmlString;
}

From source file:com.github.caldav4j.methods.CalDAVReportTest.java

private String ElementoString(Element node) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(node);
    transformer.transform(source, result);

    String xmlString = result.getWriter().toString();
    log.info(xmlString);/*from   w w  w .  ja  va2  s  . c  o  m*/
    return xmlString;
}

From source file:com.github.caldav4j.methods.PropFindTest.java

private String ElementoString(Element node) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(node);
    transformer.transform(source, result);

    String xmlString = result.getWriter().toString();
    log.info(xmlString);//from w w w .  j a va 2 s. com
    return xmlString;
}

From source file:com.sangupta.clitools.file.Format.java

private void formatXML(File file) {
    try {//from  ww  w.j  a  v a2 s .  c o  m
        Source xmlInput = new StreamSource(file);
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 4);

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        String out = xmlOutput.getWriter().toString();

        // add a new line after "?>< for next tag starting
        int index = out.indexOf("\"?><");
        if (index > 0) {
            out = out.substring(0, index + 3) + "\n" + out.substring(index + 3);
        }

        if (!this.overwrite) {
            System.out.println(out);
            return;
        }

        org.apache.commons.io.FileUtils.writeStringToFile(file, out);
    } catch (Exception e) {
        System.out.println("Unable to format file!");
        e.printStackTrace();
    }
}

From source file:eu.esdihumboldt.cst.doc.functions.internal.content.FunctionReferenceContent.java

private InputStream getFunctionContent(String func_id) throws Exception {
    // maps "function" to the real function ID (used by the template)
    final FunctionDefinition<?> function = FunctionUtil.getFunction(func_id, null);

    if (function == null) {
        log.warn("Unknown function " + func_id);
        return null;
    }//w ww.ja va  2s  .c  om

    Callable<VelocityContext> contextFactory = new Callable<VelocityContext>() {

        @Override
        public VelocityContext call() throws Exception {
            VelocityContext context = new VelocityContext();

            context.put("showImage", imageContentMethod != null);

            context.put("function", function);

            // Map<paramDisplayName, sampleDataStringRepresentation>
            Map<String, String> parameterDocu = new HashMap<String, String>();
            for (FunctionParameterDefinition param : function.getDefinedParameters()) {
                if (param.getValueDescriptor() != null && param.getValueDescriptor().getSampleData() != null) {
                    Value sample = param.getValueDescriptor().getSampleData();
                    if (sample.isRepresentedAsDOM()) {
                        // get DOM Element as String
                        Element ele = sample.getDOMRepresentation();
                        StringWriter writer = new StringWriter();
                        StreamResult formattedXmlString = new StreamResult(writer);
                        XmlUtil.prettyPrint(new DOMSource(ele), formattedXmlString);
                        // escape special chars to display xml code on html
                        String xmlString = formattedXmlString.getWriter().toString();
                        xmlString = StringEscapeUtils.escapeXml(xmlString);

                        parameterDocu.put(param.getDisplayName(), xmlString);
                    } else {
                        parameterDocu.put(param.getDisplayName(), sample.getStringRepresentation());
                    }
                }
            }
            context.put("parameterDocu", parameterDocu);

            if (function.getCategoryId() != null) {
                String categoryId = function.getCategoryId();

                Category category = (CategoryExtension.getInstance().get(categoryId));

                // String category = categoryId.substring(categoryId
                // .lastIndexOf(".") + 1);
                //
                // category = capitalize(category);
                context.put("category", category);
            }

            // creating path for the file to be included
            URL help_url = function.getHelpURL();
            if (help_url != null) {
                String help_path = help_url.getPath();
                String bundle = function.getDefiningBundle();

                StringBuffer sb_include = new StringBuffer();
                sb_include.append(bundle);
                sb_include.append(help_path);
                sb_include.append("/help");

                String final_help_url = sb_include.toString();

                context.put("include", final_help_url);
            }

            return context;
        }
    };

    return getContentFromTemplate(func_id, "function", contextFactory);
}

From source file:dk.defxws.fedoragsearch.server.GTransformer.java

public StringBuffer transform(String xsltName, Source sourceStream, URIResolver uriResolver,
        HashMap<String, String> params, boolean checkInHome) throws Exception {
    logger.fine("xsltName=" + xsltName);
    Transformer transformer = getTransformer(xsltName, uriResolver, checkInHome);
    //logger.info(params);
    Iterator it = params.keySet().iterator();
    String key = "";
    String value = "";
    while (it.hasNext()) {
        key = (String) it.next();
        value = params.get(key);/*from  ww w .j  ava2 s  . c  om*/
        if (value == null) {
            value = "";
        }
        transformer.setParameter(key, value);

    }
    transformer.setParameter("DATETIME", new Date());
    StreamResult destStream = new StreamResult(new StringWriter());
    try {
        transformer.transform(sourceStream, destStream);
    } catch (TransformerException e) {
        logger.log(Level.SEVERE, "transform " + xsltName + ":\n", e);
        throw new Exception("transform " + xsltName + ":\n", e);
    }
    StringWriter sw = (StringWriter) destStream.getWriter();
    //      if (logger.isDebugEnabled())
    //      logger.debug("sw="+sw.getBuffer().toString());
    return sw.getBuffer();
}