Search in sources :

Example 36 with Result

use of javax.xml.transform.Result in project bnd by bndtools.

the class SpringComponent method analyze.

public static Set<CharSequence> analyze(InputStream in) throws Exception {
    if (transformer == null) {
        TransformerFactory tf = TransformerFactory.newInstance();
        Source source = new StreamSource(SpringComponent.class.getResourceAsStream("extract.xsl"));
        transformer = tf.newTransformer(source);
    }
    Set<CharSequence> refers = new HashSet<CharSequence>();
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Result r = new StreamResult(bout);
    Source s = new StreamSource(in);
    transformer.transform(s, r);
    try (BufferedReader br = IO.reader(bout.toString("UTF-8"))) {
        String line = br.readLine();
        while (line != null) {
            line = line.trim();
            if (line.length() > 0) {
                String[] parts = line.split("\\s*,\\s*");
                for (int i = 0; i < parts.length; i++) {
                    int n = parts[i].lastIndexOf('.');
                    if (n > 0) {
                        refers.add(parts[i].subSequence(0, n));
                    }
                }
            }
            line = br.readLine();
        }
    }
    return refers;
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) BufferedReader(java.io.BufferedReader) HashSet(java.util.HashSet)

Example 37 with Result

use of javax.xml.transform.Result in project Gargoyle by callakrsos.

the class DesignerFx method saveSettings.

private void saveSettings() {
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        Element settingsElement = document.createElement("settings");
        document.appendChild(settingsElement);
        Element codeElement = document.createElement("code");
        settingsElement.appendChild(codeElement);
        codeElement.setAttribute("language-version", getLanguageVersion().getTerseName());
        codeElement.appendChild(document.createCDATASection(codeEditorPane.getText()));
        Element xpathElement = document.createElement("xpath");
        settingsElement.appendChild(xpathElement);
        Object userData = xpathVersionButtonGroup.getSelectedToggle().getUserData();
        xpathElement.setAttribute("version", userData.toString());
        //			xpathElement.setAttribute("version", xpathVersionButtonGroup.getSelection().getActionCommand());
        xpathElement.appendChild(document.createCDATASection(xpathQueryArea.getText()));
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        // This is as close to pretty printing as we'll get using standard Java APIs.
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        Source source = new DOMSource(document);
        Result result = new StreamResult(new FileWriter(new File(SETTINGS_FILE_NAME)));
        transformer.transform(source, result);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) FileWriter(java.io.FileWriter) IOException(java.io.IOException) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 38 with Result

use of javax.xml.transform.Result in project linuxtools by eclipse.

the class AbstractDataAdapter method getInputStream.

/**
 * @return an InputStream to the newly created XML data.
 */
public InputStream getInputStream() {
    InputStream inp = null;
    Source source = new DOMSource(getDocument());
    StringWriter stw = new StringWriter();
    Result result = new StreamResult(stw);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer xformer;
    try {
        xformer = factory.newTransformer();
        // $NON-NLS-1$ //$NON-NLS-2$
        xformer.setOutputProperty("indent", "yes");
        xformer.transform(source, result);
        inp = new ByteArrayInputStream(stw.toString().getBytes(StandardCharsets.UTF_8));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return inp;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 39 with Result

use of javax.xml.transform.Result in project linuxtools by eclipse.

the class SessionManager method writeToFile.

/**
 * Write the contents of the given Document to a file.
 */
private void writeToFile(String filePath) {
    Source source = new DOMSource(doc);
    Result result = new StreamResult(new File(filePath));
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer xformer;
    try {
        xformer = factory.newTransformer();
        xformer.transform(source, result);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) File(java.io.File) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 40 with Result

use of javax.xml.transform.Result in project cxf by apache.

the class XSLTJaxbProvider method marshalToOutputStream.

@Override
protected void marshalToOutputStream(Marshaller ms, Object obj, OutputStream os, Annotation[] anns, MediaType mt) throws Exception {
    Templates t = createTemplates(getOutTemplates(anns, mt), outParamsMap, outProperties);
    if (t == null && supportJaxbOnly) {
        super.marshalToOutputStream(ms, obj, os, anns, mt);
        return;
    }
    org.apache.cxf.common.jaxb.JAXBUtils.setMinimumEscapeHandler(ms);
    TransformerHandler th = null;
    try {
        th = factory.newTransformerHandler(t);
    } catch (TransformerConfigurationException ex) {
        TemplatesImpl ti = (TemplatesImpl) t;
        th = factory.newTransformerHandler(ti.getTemplates());
        this.trySettingProperties(th, ti);
    }
    Result result = new StreamResult(os);
    if (systemId != null) {
        result.setSystemId(systemId);
    }
    th.setResult(result);
    if (getContext() == null) {
        th.startDocument();
    }
    ms.marshal(obj, th);
    if (getContext() == null) {
        th.endDocument();
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamResult(javax.xml.transform.stream.StreamResult) Templates(javax.xml.transform.Templates) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DOMResult(javax.xml.transform.dom.DOMResult)

Aggregations

Result (javax.xml.transform.Result)80 StreamResult (javax.xml.transform.stream.StreamResult)72 Source (javax.xml.transform.Source)52 Transformer (javax.xml.transform.Transformer)52 DOMSource (javax.xml.transform.dom.DOMSource)42 TransformerFactory (javax.xml.transform.TransformerFactory)33 StringWriter (java.io.StringWriter)24 TransformerException (javax.xml.transform.TransformerException)21 IOException (java.io.IOException)20 StreamSource (javax.xml.transform.stream.StreamSource)20 SAXResult (javax.xml.transform.sax.SAXResult)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 File (java.io.File)15 Document (org.w3c.dom.Document)14 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)11 DOMResult (javax.xml.transform.dom.DOMResult)11 InputSource (org.xml.sax.InputSource)11 SAXException (org.xml.sax.SAXException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9