Example usage for javax.xml.transform OutputKeys OMIT_XML_DECLARATION

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

Introduction

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

Prototype

String OMIT_XML_DECLARATION

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

Click Source Link

Document

omit-xml-declaration = "yes" | "no".

Usage

From source file:com.portfolio.data.provider.MysqlAdminProvider.java

@Override
public String getRolePortfolio(MimeType mimeType, String portfolioId, int userId) {
    PreparedStatement st;/*from w  w w  . j  a v  a2  s  . c om*/
    String sql;
    ResultSet res;

    String retVal = "";

    try {
        sql = "SELECT gri.grid, gri.owner, gri.change_rights, bin2uuid(gr.id) AS uuid, gr.RD, gr.WR, gr.DL, gr.SB, gr.AD, gr.types_id, gr.rules_id, gr.notify_roles "
                + "FROM group_right_info gri " + "LEFT JOIN group_rights gr ON gri.grid=gr.grid "
                + "WHERE gri.portfolio_id=uuid2bin(?) " + "ORDER BY gri.grid";
        st = connection.prepareStatement(sql);
        st.setString(1, portfolioId);

        res = st.executeQuery();

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();

        Element root = document.createElement("portfolio");
        root.setAttribute("id", portfolioId);
        document.appendChild(root);

        int prevgrid = 0;

        while (res.next()) {
            int grid = res.getInt("grid");
            int owner = res.getInt("owner");
            int change = res.getInt("change_rights");
            Element rights = null;
            if (prevgrid != grid) {
                rights = document.createElement("rights");
                rights.setAttribute("id", Integer.toString(grid));
                rights.setAttribute("owner", Integer.toString(owner));
                rights.setAttribute("change_rights", Integer.toString(change));
                root.appendChild(rights);
                prevgrid = grid;
            }

            String uuid = res.getString("uuid");
            int rd = res.getInt("RD");
            int wr = res.getInt("WR");
            int dl = res.getInt("DL");
            int sb = res.getInt("SB");
            int ad = res.getInt("AD");
            String types_id = res.getString("types_id");
            if (types_id == null)
                types_id = "";
            String rules_id = res.getString("rules_id");
            if (rules_id == null)
                rules_id = "";
            String notify = res.getString("notify_roles");
            if (notify == null)
                notify = "";

            Element right = document.createElement("right");
            right.setAttribute("id", uuid);
            right.setAttribute("RD", Integer.toString(rd));
            right.setAttribute("WR", Integer.toString(wr));
            right.setAttribute("DL", Integer.toString(dl));
            right.setAttribute("SB", Integer.toString(sb));
            right.setAttribute("AD", Integer.toString(ad));
            right.setAttribute("types_id", types_id);
            right.setAttribute("rules_id", rules_id);
            right.setAttribute("notify", notify);

            rights.appendChild(right);

        }

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));

        retVal = writer.getBuffer().toString().replaceAll("\n|\r", "");

    } catch (Exception e) {

    }

    return retVal;
}

From source file:com.portfolio.data.provider.MysqlAdminProvider.java

@Override
public String getGroupRightList() {
    PreparedStatement st;/*  w  ww  .  j av a  2 s  . c om*/
    String sql;
    ResultSet res;
    String retVal = "";

    try {
        sql = "SELECT grid, owner, label, change_rights, bin2uuid(portfolio_id) "
                + "FROM group_right_info ORDER BY portfolio_id";
        st = connection.prepareStatement(sql);
        res = st.executeQuery();

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();

        Element groups = document.createElement("group_rights");
        document.appendChild(groups);

        String prevPort = "DUMMY";
        Element portNode = null;
        while (res.next()) {
            Integer grid = res.getInt(1);
            Integer owner = res.getInt(2);
            String label = res.getString(3);
            Integer change = res.getInt(4);
            String portfolio = res.getString(5);
            if (portfolio == null)
                portfolio = "";

            if (!prevPort.equals(portfolio)) {
                portNode = document.createElement("portfolio");
                portNode.setAttribute("id", portfolio);
                groups.appendChild(portNode);

                prevPort = portfolio;
            }

            Element group = document.createElement("group");
            group.setAttribute("grid", grid.toString());
            group.setAttribute("owner", owner.toString());
            group.setAttribute("change_rights", change.toString());
            //          group.setAttribute("portfolio", portfolio);
            group.setNodeValue(label);

            portNode.appendChild(group);
        }

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));

        retVal = writer.getBuffer().toString().replaceAll("\n|\r", "");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return retVal;
}

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:netinf.common.communication.MessageEncoderXML.java

private byte[] buildString(Node xml) {
    try {//  w ww.  j a va2  s.com
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(outputStream);
        DOMSource source = new DOMSource(xml);
        trans.transform(source, result);
        return outputStream.toByteArray();
    } catch (TransformerException e) {
        throw new NetInfUncheckedException(e);
    }
}

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);
    }/*  ww w. ja  v  a 2s .c om*/
    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.OgcWfsClient.java

public static String elementToString(Element e) throws Exception {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    DOMSource ds = new DOMSource(e);
    trans.transform(ds, sr);/*from w  w  w . j a v a  2  s  .  c o  m*/
    return sw.toString();
}

From source file:nl.ellipsis.webdav.server.util.XMLHelper.java

public static String format(Document document) {
    String retval = null;//from  ww w.  ja  v  a2  s.  c om
    if (document != null) {
        TransformerFactory transfac = TransformerFactory.newInstance();
        StringWriter sw = null;
        try {
            Transformer transformer = transfac.newTransformer();

            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            //create string from xml tree
            sw = new StringWriter();
            StreamResult result = new StreamResult(sw);

            DOMSource source = new DOMSource(document);

            transformer.transform(source, result);

            retval = sw.toString();
        } catch (TransformerException e) {
            e.printStackTrace();
        } finally {
            try {
                sw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return retval;
}

From source file:nl.nn.adapterframework.util.XmlUtils.java

public static String nodeToString(Node node) throws TransformerException {
    Transformer t = getTransformerFactory().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter sw = new StringWriter();
    t.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:objective.taskboard.utils.XmlUtilsTest.java

private static void format(File inputXmlFile, File outputXmlFile) {
    try {/* w  w  w  .j  ava2  s. c o m*/
        Document document = XmlUtils.asDocument(inputXmlFile);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer idTransform = transFactory.newTransformer();
        idTransform.setOutputProperty(OutputKeys.METHOD, "xml");
        idTransform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        idTransform.setOutputProperty(OutputKeys.INDENT, "yes");
        idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        Source input = new DOMSource(document);
        Result output = new StreamResult(outputXmlFile);
        idTransform.transform(input, output);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}