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:org.exist.xmlrpc.XmlRpcTest.java

@Test
public void testQueryWithStylesheet() {
    System.out.println("---testQueryWithStylesheet");
    storeData();//from w  ww .  ja v  a  2 s.  c om
    try {
        HashMap<String, String> options = new HashMap<String, String>();
        options.put(EXistOutputKeys.STYLESHEET, "test.xsl");
        options.put(EXistOutputKeys.STYLESHEET_PARAM + ".testparam", "Test");
        options.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
        //TODO : check the number of resources before !
        Vector<Object> params = new Vector<Object>();
        String query = "//para[1]";
        params.addElement(query.getBytes(UTF_8));
        params.addElement(options);
        XmlRpcClient xmlrpc = getClient();
        Integer handle = (Integer) xmlrpc.execute("executeQuery", params);
        Assert.assertNotNull(handle);

        params.clear();
        params.addElement(handle);
        params.addElement(Integer.valueOf(0));
        params.addElement(options);
        byte[] item = (byte[]) xmlrpc.execute("retrieve", params);
        Assert.assertNotNull(item);
        Assert.assertTrue(item.length > 0);
        String out = new String(item, UTF_8);
        System.out.println("Received: " + out);
        XMLAssert.assertXMLEqual("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + "<p>Test: \u00E4\u00E4\u00F6\u00F6\u00FC\u00FC\u00C4\u00C4\u00D6\u00D6\u00DC\u00DC\u00DF\u00DF</p>",
                out);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}

From source file:org.exist.xquery.functions.util.Serialize.java

private Properties parseSerializationOptions(Sequence sSerializeParams) throws XPathException {
    //parse serialization options
    final Properties outputProperties = new Properties();
    if (sSerializeParams.hasOne() && Type.subTypeOf(sSerializeParams.getItemType(), Type.NODE)) {
        SerializerUtils.getSerializationOptions(this, (NodeValue) sSerializeParams.itemAt(0), outputProperties);
    } else {/*w ww. j a v a2  s.c o m*/
        SequenceIterator siSerializeParams = sSerializeParams.iterate();
        outputProperties.setProperty(OutputKeys.INDENT, "yes");
        outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        while (siSerializeParams.hasNext()) {
            final String serializeParams = siSerializeParams.nextItem().getStringValue();
            final String params[] = serializeParams.split(" ");
            for (final String param : params) {
                final String opt[] = Option.parseKeyValuePair(param);
                outputProperties.setProperty(opt[0], opt[1]);
            }
        }
    }
    return outputProperties;
}

From source file:org.expath.tools.model.exist.EXistSequence.java

/**
 * Borrowed from {@link org.expath.tools.saxon.model.SaxonSequence}
 *//*from   www .  ja va  2s .c  o  m*/
private Properties makeOutputProperties(final SerialParameters params) throws ToolsException {
    final Properties props = new Properties();

    setOutputKey(props, OutputKeys.METHOD, params.getMethod());
    setOutputKey(props, OutputKeys.MEDIA_TYPE, params.getMediaType());
    setOutputKey(props, OutputKeys.ENCODING, params.getEncoding());
    setOutputKey(props, OutputKeys.CDATA_SECTION_ELEMENTS, params.getCdataSectionElements());
    setOutputKey(props, OutputKeys.DOCTYPE_PUBLIC, params.getDoctypePublic());
    setOutputKey(props, OutputKeys.DOCTYPE_SYSTEM, params.getDoctypeSystem());
    setOutputKey(props, OutputKeys.INDENT, params.getIndent());
    setOutputKey(props, OutputKeys.OMIT_XML_DECLARATION, params.getOmitXmlDeclaration());
    setOutputKey(props, OutputKeys.STANDALONE, params.getStandalone());
    setOutputKey(props, OutputKeys.VERSION, params.getVersion());

    return props;
}

From source file:org.fireflow.clientwidget.FpdlExporter.java

public static String getXml(Node node, String encoding) {
    try {/*from   w  ww.j  a  va2  s.co m*/
        Transformer tf = TransformerFactory.newInstance().newTransformer();

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

        StreamResult dest = new StreamResult(new StringWriter());
        tf.transform(new DOMSource(node), dest);

        return dest.getWriter().toString();
    } catch (Exception e) {
        // ignore
    }

    return "";
}

From source file:org.fireflow.pdl.fpdl.io.FPDLSerializer.java

/**
 * xmloutString//w w w  .  j ava 2 s. c o m
 * @param workflowProcess
 * @param out
 * @return
 * @throws IOException
 * @throws SerializerException
 * @throws InvalidModelException
 */
public String serialize(WorkflowProcess workflowProcess, OutputStream out)
        throws IOException, SerializerException, InvalidModelException {
    try {
        Document document = serializeToDOM(workflowProcess);

        //??
        Element root = document.getDocumentElement();
        String displayname = root.getAttribute(DISPLAY_NAME);
        //============

        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        if (JDK_TRANSFORMER_CLASS.equals(transformerFactory.getClass().getName())) {
            useJDKTransformerFactory = true;
        }

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, charset);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");

        transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, CDATA_SECTION_ELEMENT_LIST);

        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        transformer.transform(new DOMSource(document), new StreamResult(out));

        out.flush();

        return charset;
    } catch (TransformerConfigurationException e) {
        throw new SerializerException(e);
    } catch (TransformerException e) {
        throw new SerializerException(e);
    } finally {

    }

}

From source file:org.fireflow.pdl.fpdl20.io.FPDLSerializer.java

public void serialize(WorkflowProcess workflowProcess, OutputStream out)
        throws IOException, SerializerException, InvalidModelException {
    try {/*from   ww w .ja v a  2 s.c o m*/
        Document document = serializeToDOM(workflowProcess);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        if (JDK_TRANSFORMER_CLASS.equals(transformerFactory.getClass().getName())) {
            useJDKTransformerFactory = true;
        }

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, charset);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");

        transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, CDATA_SECTION_ELEMENT_LIST);

        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        transformer.transform(new DOMSource(document), new StreamResult(out));
        out.flush();
    } catch (TransformerConfigurationException e) {
        throw new SerializerException(e);
    } catch (TransformerException e) {
        throw new SerializerException(e);
    } finally {

    }

}

From source file:org.fireflow.webdesigner.transformer.AbstractFpdlDiagramSerializer.java

public String serializeDiagramToStr(WorkflowProcess workflowProcess, String subProcessName, String encoding,
        boolean omitXmlDeclaration) {
    try {/*from  www .j a v a  2  s  .  c  o m*/
        Document doc = this.serializeDiagramToDoc(workflowProcess, subProcessName);
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        if (omitXmlDeclaration) {
            tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }

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

        StreamResult dest = new StreamResult(new StringWriter());
        tf.transform(new DOMSource(doc), dest);

        return dest.getWriter().toString();
    } catch (Exception e) {
        //TODO 
        e.printStackTrace();
    }

    return "";
}

From source file:org.fosstrak.epcis.repository.capture.CaptureOperationsModule.java

/**
 * Parses the attribute <b>value</b> of a VocabularyElement. The value can
 * be null in which case an empty String is returned. Otherwise, the value
 * is either given as XML attribute named 'value' or as inline text, see the
 * sample below. <br>/*from   w  ww . j  a va  2s  . c o  m*/
 * 
 * <pre>
 * {@code
 * <VocabularyElement id="urn:epc:id:sgln:0037000.00729.0">
 *   <attribute id="urn:epcglobal:fmcg:mda:slt:retail"/>
 *   <attribute id="urn:epcglobal:fmcg:mda:location">Warehouse 1</attribute>
 *   <attribute id="urn:epcglobal:fmcg:mda:room" value="22b"/>
 *   <attribute id="urn:epcglobal:fmcg:mda:address">
 *     <sample:Address xmlns:sample="http://sample.com/ComplexTypeExample">
 *       <Street>100 Nowhere Street</Street>
 *       <City>Fancy</City>
 *     </sample:Address>
 *   </attribute>
 * </VocabularyElement>
 * }
 * </pre>
 * 
 * @return the attribute value as String.
 */
private String parseVocAttributeValue(Node vocAttrNode) {
    String vocAttrValue;
    if (vocAttrNode.getAttributes().getNamedItem("value") != null) {
        // the value is given as attribute 'value'
        vocAttrValue = vocAttrNode.getAttributes().getNamedItem("value").getNodeValue();
    } else if (vocAttrNode.getChildNodes().getLength() > 1) {
        // the value is given as DOM-tree
        TransformerFactory transFactory = TransformerFactory.newInstance();
        StringWriter buffer = new StringWriter();
        try {
            Transformer transformer = transFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "no");
            transformer.transform(new DOMSource(vocAttrNode.getChildNodes().item(1)), new StreamResult(buffer));
            vocAttrValue = buffer.toString();
        } catch (Throwable t) {
            LOG.warn("unable to transform vocabulary attribute value (XML) into a string", t);
            vocAttrValue = vocAttrNode.getTextContent();
        }
    } else if (vocAttrNode.getFirstChild() != null) {
        // the value is given as text
        vocAttrValue = vocAttrNode.getFirstChild().getNodeValue();
    } else {
        vocAttrValue = "";
    }
    return vocAttrValue;
}

From source file:org.geoserver.test.onlineTest.Resources.java

public static String xmlDocToString(Document document) {
    String output = "";
    try {/*w  ww  . j  a va  2s  . c  o  m*/
        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));
        output = writer.getBuffer().toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return output;
}

From source file:org.gluu.saml.AuthRequest.java

public String getRequest(boolean useBase64, String assertionConsumerServiceUrl)
        throws ParserConfigurationException, XMLStreamException, IOException, TransformerException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = docBuilder.newDocument();

    // Add AuthnRequest
    Element authnRequestElement = doc.createElementNS("urn:oasis:names:tc:SAML:2.0:protocol",
            "samlp:AuthnRequest");
    authnRequestElement.setAttribute("ID", this.id);
    authnRequestElement.setAttribute("Version", "2.0");
    authnRequestElement.setAttribute("IssueInstant", this.issueInstant);
    authnRequestElement.setAttribute("ProtocolBinding", "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST");
    authnRequestElement.setAttribute("AssertionConsumerServiceURL", assertionConsumerServiceUrl);

    doc.appendChild(authnRequestElement);

    // Add AuthnRequest
    Element issuerElement = doc.createElementNS("urn:oasis:names:tc:SAML:2.0:assertion", "saml:Issuer");
    issuerElement.appendChild(doc.createTextNode(this.samlSettings.getIssuer()));

    authnRequestElement.appendChild(issuerElement);

    // Add NameIDPolicy
    Element nameIDPolicyElement = doc.createElementNS("urn:oasis:names:tc:SAML:2.0:protocol",
            "samlp:NameIDPolicy");
    nameIDPolicyElement.setAttribute("Format", this.samlSettings.getNameIdentifierFormat());
    nameIDPolicyElement.setAttribute("AllowCreate", "true");

    authnRequestElement.appendChild(nameIDPolicyElement);

    if (this.samlSettings.isUseRequestedAuthnContext()) {
        // Add RequestedAuthnContext
        Element requestedAuthnContextElement = doc.createElementNS("urn:oasis:names:tc:SAML:2.0:protocol",
                "samlp:RequestedAuthnContext");
        requestedAuthnContextElement.setAttribute("Comparison", "exact");

        authnRequestElement.appendChild(requestedAuthnContextElement);

        // Add AuthnContextClassRef
        Element authnContextClassRefElement = doc.createElementNS("urn:oasis:names:tc:SAML:2.0:assertion",
                "saml:AuthnContextClassRef");
        authnContextClassRefElement.appendChild(
                doc.createTextNode("urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"));

        requestedAuthnContextElement.appendChild(authnContextClassRefElement);
    }//from w  w w  . j av  a2s. c  o m

    // Convert the content into xml
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    DOMSource source = new DOMSource(doc);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(baos);

    transformer.transform(source, result);

    if (log.isDebugEnabled()) {
        log.debug("Genereated Saml Request " + new String(baos.toByteArray(), "UTF-8"));
    }

    if (useBase64) {
        byte[] deflated = CompressionHelper.deflate(baos.toByteArray(), true);
        String base64 = Base64.encodeBase64String(deflated);
        String encoded = URLEncoder.encode(base64, "UTF-8");

        return encoded;
    }

    return new String(baos.toByteArray(), "UTF-8");
}