Example usage for javax.xml.transform OutputKeys METHOD

List of usage examples for javax.xml.transform OutputKeys METHOD

Introduction

In this page you can find the example usage for javax.xml.transform OutputKeys METHOD.

Prototype

String METHOD

To view the source code for javax.xml.transform OutputKeys METHOD.

Click Source Link

Document

method = "xml" | "html" | "text" | expanded name.

Usage

From source file:com.ikanow.infinit.e.harvest.extraction.document.file.FileHarvester.java

private static TransformerHandler getTransformerHandler(String method, StringWriter sw) {
    try {//from w ww.  java2 s  .c o m
        SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        TransformerHandler handler = factory.newTransformerHandler();
        handler.getTransformer().setOutputProperty(OutputKeys.METHOD, method);
        handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
        handler.setResult(new StreamResult(sw));
        return handler;
    } catch (Exception e) {
        return null;
    }
}

From source file:org.keycloak.testsuite.adapter.servlet.SAMLServletAdapterTest.java

public static void printDocument(Source doc, OutputStream out) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(doc, new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

From source file:net.sourceforge.eclipsetrader.core.internal.XMLRepository.java

void saveDocument(Document document, String path, String name) {
    try {//from   ww  w. j a  va 2s  .c o m
        TransformerFactory factory = TransformerFactory.newInstance();
        try {
            factory.setAttribute("indent-number", new Integer(4)); //$NON-NLS-1$
        } catch (Exception e) {
        }
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
        transformer.setOutputProperty("{http\u003a//xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$
        DOMSource source = new DOMSource(document);

        File file = new File(Platform.getLocation().toFile(), path);
        file.mkdirs();
        file = new File(file, name);

        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
        out.flush();
        out.close();
    } catch (Exception e) {
        log.error(e.toString(), e);
    }
}

From source file:net.sourceforge.pmd.RuleSetWriter.java

public void write(RuleSet ruleSet) {
    try {/*w w  w .  j  a v a2 s.  c  om*/
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        document = documentBuilder.newDocument();
        ruleSetFileNames = new HashSet<>();

        Element ruleSetElement = createRuleSetElement(ruleSet);
        document.appendChild(ruleSetElement);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        try {
            transformerFactory.setAttribute("indent-number", 3);
        } catch (IllegalArgumentException iae) {
            // ignore it, specific to one parser
            LOG.log(Level.FINE, "Couldn't set indentation", iae);
        }
        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(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(new DOMSource(document), new StreamResult(outputStream));
    } catch (DOMException e) {
        throw new RuntimeException(e);
    } catch (FactoryConfigurationError e) {
        throw new RuntimeException(e);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:net.sourceforge.pmd.util.designer.Designer.java

private void saveSettings() {
    try {/*from   ww  w.j  a va  2  s.c o m*/
        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);
        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(
                Files.newBufferedWriter(new File(SETTINGS_FILE_NAME).toPath(), StandardCharsets.UTF_8));
        transformer.transform(source, result);
    } catch (ParserConfigurationException | IOException | TransformerException e) {
        e.printStackTrace();
    }
}

From source file:net.straylightlabs.tivolibre.DecoderApp.java

private static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

From source file:net.unicon.warlock.fac.xml.XmlWarlockFactory.java

private void initializeSerializer() {
    if (serializerProperties == null) {
        synchronized (systemLock) {
            if (serializerProperties == null) {
                serializerProperties = new Properties();
                serializerProperties.put(OutputKeys.METHOD, Method.HTML);
            }//ww w.  java  2 s.c  o  m
            if (xmlSerializerProperties == null) {
                xmlSerializerProperties = new Properties();
                xmlSerializerProperties.put(OutputKeys.METHOD, Method.XML);
            }
        }
    }
}

From source file:nl.armatiek.xslweb.pipeline.PipelineHandler.java

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    if (serializingHandler != null) {
        serializingHandler.startElement(uri, localName, qName, atts);
    }/*  w  w  w. ja  v  a2s. c o m*/
    try {
        if (StringUtils.equals(uri, Definitions.NAMESPACEURI_XSLWEB_PIPELINE)) {
            if (localName.equals("transformer")) {
                String xslPath = getAttribute(atts, "xsl-path", null);
                if (StringUtils.isBlank(xslPath)) {
                    throw new SAXException("Transformer step must have an attribute \"xsl-path\"");
                }
                String name = getAttribute(atts, "name",
                        "transformer-" + Integer.toString(pipelineSteps.size() + 1));
                boolean log = getAttribute(atts, "log", "false").equals("true");
                pipelineSteps.add(new TransformerStep(xslPath, name, log));
            } else if (localName.equals("parameter")) {
                TransformerStep step = (TransformerStep) pipelineSteps.peek();
                String name = getAttribute(atts, "name", null);
                if (StringUtils.isBlank(name)) {
                    throw new SAXException("Element \"parameter\" must have an attribute \"name\"");
                }
                Parameter parameter = new Parameter(processor, getAttribute(atts, "uri", null), name,
                        getAttribute(atts, "type", "xs:string"));
                step.addParameter(parameter);
            } else if (localName.equals("schema-validator")) {
                String name = getAttribute(atts, "name",
                        "validator-" + Integer.toString(pipelineSteps.size() + 1));
                boolean log = getAttribute(atts, "log", "false").equals("true");
                String xslParamName = getAttribute(atts, "xsl-param-name", null);
                String xslParamNamespace = getAttribute(atts, "xsl-param-namespace", null);
                pipelineSteps.add(new SchemaValidatorStep(name, log, xslParamNamespace, xslParamName));
            } else if (localName.equals("properties")) {
            } else if (localName.equals("property")) {
                ((SchemaValidatorStep) pipelineSteps.peek()).addProperty(getAttribute(atts, "name", null),
                        getAttribute(atts, "value", null));
            } else if (localName.equals("features")) {
            } else if (localName.equals("feature")) {
                ((SchemaValidatorStep) pipelineSteps.peek()).addFeature(getAttribute(atts, "name", null),
                        getAttribute(atts, "value", null));
            } else if (localName.equals("schematron-validator")) {
                String name = getAttribute(atts, "name",
                        "validator-" + Integer.toString(pipelineSteps.size() + 1));
                boolean log = getAttribute(atts, "log", "false").equals("true");
                String schematronPath = getAttribute(atts, "schematron-path", null);
                String phase = getAttribute(atts, "phase", null);
                String xslParamName = getAttribute(atts, "xsl-param-name", null);
                String xslParamNamespace = getAttribute(atts, "xsl-param-namespace", null);
                pipelineSteps.add(new SchematronValidatorStep(name, schematronPath, log, xslParamNamespace,
                        xslParamName, phase));
            } else if (localName.equals("schema-path")) {
            } else if (localName.equals("schema-paths")) {
            } else if (localName.equals("pipeline")) {
                cache = getAttribute(atts, "cache", "false").equals("true");
                if (cache) {
                    cacheKey = getAttribute(atts, "cache-key", null);
                    cacheTimeToLive = Integer.parseInt(getAttribute(atts, "cache-time-to-live", "60"));
                    cacheTimeToIdle = Integer.parseInt(getAttribute(atts, "cache-time-to-idle", "60"));
                    cacheScope = getAttribute(atts, "cache-scope", "webapp");
                    cacheHeaders = getAttribute(atts, "cache-headers", "false").equals("true");
                }
            } else if (localName.equals("json-serializer")) {
                JSONSerializerStep step = new JSONSerializerStep(atts);
                pipelineSteps.add(step);
            } else if (localName.equals("namespace-declarations")) {
            } else if (localName.equals("namespace-declaration")) {
                ((JSONSerializerStep) pipelineSteps.peek()).addNamespaceDeclaration(
                        getAttribute(atts, "namespace-uri", null), getAttribute(atts, "name", null));
            } else if (localName.equals("zip-serializer")) {
                ZipSerializerStep step = new ZipSerializerStep(atts);
                pipelineSteps.add(step);
            } else if (localName.equals("resource-serializer")) {
                ResourceSerializerStep step = new ResourceSerializerStep(atts);
                pipelineSteps.add(step);
            } else if (localName.equals("fop-serializer")) {
                FopSerializerStep step = new FopSerializerStep(atts);
                pipelineSteps.add(step);
            } else if (localName.equals("value")) {
            } else {
                throw new SAXException(String.format("Pipeline element \"%s\" not supported", localName));
            }
        } else if (StringUtils.equals(uri, Definitions.NAMESPACEURI_XSLWEB_RESPONSE)) {
            if (localName.equals("response")) {
                Properties props = new Properties();
                props.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                props.setProperty(OutputKeys.METHOD, "xml");
                props.setProperty(OutputKeys.INDENT, "no");
                os = new ByteArrayOutputStream();
                serializingHandler = new SerializingContentHandler(os, conf, props);
                serializingHandler.startElement(uri, localName, qName, atts);
            }
        }
    } catch (Exception e) {
        throw new SAXException(e);
    }
}

From source file:nl.armatiek.xslweb.saxon.functions.ExtensionFunctionCall.java

protected String serialize(NodeInfo nodeInfo) throws XPathException {
    Properties props = new Properties();
    props.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    props.setProperty(OutputKeys.METHOD, "xml");
    props.setProperty(OutputKeys.INDENT, "no");
    return serialize(nodeInfo, props);
}

From source file:nl.b3p.ogc.utils.OGCResponse.java

protected String serializeNode(Node doc) throws TransformerConfigurationException, TransformerException {
    StringWriter outText = new StringWriter();
    StreamResult sr = new StreamResult(outText);
    Properties oprops = new Properties();
    oprops.put(OutputKeys.METHOD, "xml");
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.setOutputProperties(oprops);// w  w  w . j a  va  2s  .  c  o m
    t.transform(new DOMSource(doc), sr);
    return outText.toString();
}