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

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

Introduction

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

Prototype

public OutputStream getOutputStream() 

Source Link

Document

Get the byte stream that was set with setOutputStream.

Usage

From source file:org.toobsframework.transformpipeline.domain.TransletTransformer.java

@SuppressWarnings("unchecked")
public List transform(List inputXSLTranslets, List inputXMLs, Map inputParams,
        IXMLTransformerHelper transformerHelper) throws XMLTransformerException {

    if (log.isDebugEnabled()) {
        log.debug("TRANSFORM XML STARTED");
        log.debug("Get input XMLs");
    }/*from   w  w  w.  j a v a  2 s  .  c  o m*/
    String xslClass = null;
    ArrayList resultingXMLs = null;
    ByteArrayInputStream xmlInputStream = null;
    ByteArrayOutputStream xmlOutputStream = null;
    StreamSource xmlSource = null;
    StreamResult xmlResult = null;
    for (int xt = 0; xt < inputXSLTranslets.size(); xt++) {

        xslClass = (String) inputXSLTranslets.get(xt);

        resultingXMLs = new ArrayList();

        xmlInputStream = null;
        xmlOutputStream = null;
        xmlSource = null;
        xmlResult = null;
        for (int x = 0; x < inputXMLs.size(); x++) {
            if (log.isDebugEnabled()) {
                log.debug("Input XML for " + xslClass + " : " + (String) inputXMLs.get(x));
            }
            try {
                xmlInputStream = new ByteArrayInputStream(((String) inputXMLs.get(x)).getBytes("UTF-8"));
                xmlOutputStream = new ByteArrayOutputStream();

                xmlSource = new StreamSource(xmlInputStream);
                xmlResult = new StreamResult(xmlOutputStream);

                doTransform(xslClass, xmlSource, inputParams, transformerHelper, xmlResult);

                resultingXMLs.add(((ByteArrayOutputStream) xmlResult.getOutputStream()).toString("UTF-8"));
            } catch (UnsupportedEncodingException uee) {
                log.error("Error creating output string", uee);
                throw new XMLTransformerException(uee);
            } finally {
                try {
                    if (xmlInputStream != null) {
                        xmlInputStream.close();
                        xmlInputStream = null;
                    }
                    if (xmlOutputStream != null) {
                        xmlOutputStream.close();
                        xmlOutputStream = null;
                    }
                } catch (IOException ex) {
                }
            }
        }
        inputXMLs = resultingXMLs;
    }

    return inputXMLs;
}

From source file:revaligner.service.FileAligner.java

private String createglpfile(String prjid) throws Exception {
    this.tempfolder = (this.prjfolder + File.separator + "temp");
    File temp = new File(this.tempfolder);
    if (temp.exists()) {
        FileUtils.cleanDirectory(temp);//from  w  w  w  .  j a  v  a  2s.  c o  m
    } else {
        temp.mkdirs();
    }
    InputStream contentfile = FileAligner.class.getClassLoader().getResourceAsStream("content.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
    org.w3c.dom.Document document = builder.parse(contentfile);
    org.w3c.dom.Element root = document.getDocumentElement();
    org.w3c.dom.Node sourcelang = root.getElementsByTagName("sourceLanguage").item(0);
    sourcelang.getAttributes().getNamedItem("localeCode").setTextContent(this.sourcelanguage);
    org.w3c.dom.Node targetlang = root.getElementsByTagName("targetLanguage").item(0);
    targetlang.getAttributes().getNamedItem("localeCode").setTextContent(this.targetlanguage);
    org.w3c.dom.Node xliffpath = root.getElementsByTagName("__xliffFile").item(0);
    xliffpath.getAttributes().getNamedItem("archivePath")
            .setTextContent(this.targetlanguage + "/txlf/" + new File(this.populatedsourcetxlf).getName());
    org.w3c.dom.Node sourcepath = root.getElementsByTagName("__storable").item(0);
    sourcepath.getAttributes().getNamedItem("archivePath")
            .setTextContent("source/" + new File(this.backupsourcefile).getName());
    org.w3c.dom.Node projectname = root.getElementsByTagName("projectName").item(0);
    projectname.setTextContent(prjid);

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer m = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    File dest = new File(temp + File.separator + "content.xml");
    StreamResult result = new StreamResult(new FileOutputStream(dest));
    m.transform(source, result);

    File sourcefolder = new File(temp + File.separator + "source");
    sourcefolder.mkdir();
    FileUtils.copyFileToDirectory(new File(this.backupsourcefile), sourcefolder);

    File targetlangfolder = new File(temp + File.separator + this.targetlanguage);
    targetlangfolder.mkdir();
    File txlffolder = new File(targetlangfolder + File.separator + "txlf");
    txlffolder.mkdir();
    FileUtils.copyFileToDirectory(new File(this.populatedsourcetxlf), txlffolder);

    ZipFile zf = new ZipFile();
    String glpfile = this.tempfolder + File.separator + prjid + ".glp";
    zf.ZipIt(glpfile, this.tempfolder);

    contentfile.close();
    result.getOutputStream().close();

    return glpfile;
}