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:net.sf.joost.trax.TransformerImpl.java

/**
 * Constructor/*  w  ww.j  ava  2s  .c  o m*/
 *
 * @param processor A <code>Processor</code> object.
 */
protected TransformerImpl(Processor processor) {
    this.processor = processor;

    // set tracing manager on processor object
    if (processor instanceof DebugProcessor) {
        DebugProcessor dbp = (DebugProcessor) processor;
        dbp.setTraceManager(traceManager);
        dbp.setTransformer(this);

        Emitter emitter = processor.getEmitter();
        if (emitter instanceof DebugEmitter) {
            ((DebugEmitter) emitter).setTraceManager(traceManager);
        }
    }
    supportedProperties.add(OutputKeys.ENCODING);
    supportedProperties.add(OutputKeys.MEDIA_TYPE);
    supportedProperties.add(OutputKeys.METHOD);
    supportedProperties.add(OutputKeys.OMIT_XML_DECLARATION);
    supportedProperties.add(OutputKeys.STANDALONE);
    supportedProperties.add(OutputKeys.VERSION);
    supportedProperties.add(TrAXConstants.OUTPUT_KEY_SUPPORT_DISABLE_OUTPUT_ESCAPING);

    ignoredProperties.add(OutputKeys.CDATA_SECTION_ELEMENTS);
    ignoredProperties.add(OutputKeys.DOCTYPE_PUBLIC);
    ignoredProperties.add(OutputKeys.DOCTYPE_SYSTEM);
    ignoredProperties.add(OutputKeys.INDENT);
}

From source file:XMLUtils.java

public static void writeTo(Source src, OutputStream os, int indent, String charset, String omitXmlDecl) {
    Transformer it;//from ww w. jav  a2s .  com
    try {

        //charset = "utf-8"; 

        it = newTransformer();
        it.setOutputProperty(OutputKeys.METHOD, "xml");
        if (indent > -1) {
            it.setOutputProperty(OutputKeys.INDENT, "yes");
            it.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
        }
        it.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDecl);
        it.setOutputProperty(OutputKeys.ENCODING, charset);
        it.transform(src, new StreamResult(os));
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to configure TRaX", e);
    }

}

From source file:com.qpark.eip.core.spring.PayloadLogger.java

/**
 * @return the/*  w w  w. j a v a 2 s  . c o m*/
 *         {@link org.springframework.integration.transformer.Transformer}
 * @throws TransformerConfigurationException
 */
private Transformer createNonIndentingTransformer() throws TransformerConfigurationException {
    final Transformer transformer = this.transformerHelper.createTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    return transformer;
}

From source file:com.petercho.Encoder.java

public static String formatXml(String xml) throws TransformerException {
    Source xmlInput = new StreamSource(new StringReader(xml));
    StringWriter stringWriter = new StringWriter();
    StreamResult xmlOutput = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 4);
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(xmlInput, xmlOutput);
    return xmlOutput.getWriter().toString();
}

From source file:com.krawler.formbuilder.servlet.workflowHandler.java

public String saveWorkFLow(HttpServletRequest request, HttpServletResponse response)
        throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
    try {/*from  w w w .  ja  v  a 2  s.c o  m*/

        String workflow = request.getParameter("jsonnodeobj");
        String linejson = request.getParameter("linejson");
        String processId = request.getParameter("flowid");
        String containerId = request.getParameter("containerId");
        this.parentSplit = request.getParameter("parentSplit");

        String path = ConfigReader.getinstance().get("workflowpath") + processId;

        File fdir = new File(path);
        if (!fdir.exists()) {
            fdir.mkdirs();
        }
        File file = new File(fdir + System.getProperty("file.separator") + "bpmn.xml");
        if (file.exists()) {
            file.delete();
        }

        Writer output = new BufferedWriter(new FileWriter(file));

        JSONObject jsonobj = new JSONObject(workflow);
        JSONObject json_line = new JSONObject(linejson);
        createDocument();
        writeXml(jsonobj, containerId, processId, json_line);

        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans1 = transfac.newTransformer();
        trans1.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans1.setOutputProperty(OutputKeys.INDENT, "yes");

        StringWriter sw = new StringWriter();
        StreamResult kwcresult = new StreamResult(sw);
        DOMSource kwcsource = new DOMSource(dom);
        trans1.transform(kwcsource, kwcresult);

        output.write(sw.toString());

        output.close();
        return "{success:true}";
    } catch (JSONException ex) {
        logger.warn(ex.getMessage(), ex);
        return "{success:true}";
    } catch (IOException ex) {
        logger.warn(ex.getMessage(), ex);
        return "{success:false}";
    }
}

From source file:eu.scidipes.toolkits.palibrary.utils.XMLUtils.java

/**
 * Converts a {@Node} instance into a readable <code>String</code> with optional formatting and XML
 * declaration//from   w  w  w. j  a va 2 s .c o m
 * 
 * @param doc
 * @param indentAndFormat
 * @param omitXmlDeclaration
 * @return the node contents as a String
 */
public static String nodeToString(final Node doc, final boolean indentAndFormat,
        final boolean omitXmlDeclaration) {
    final Properties outputProperties = new Properties();
    outputProperties.put(OutputKeys.INDENT, indentAndFormat ? "yes" : "no");
    outputProperties.put(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
    outputProperties.put("{http://xml.apache.org/xslt}indent-amount", indentAndFormat ? "2" : "0");
    return nodeToString(doc, outputProperties);
}

From source file:de.betterform.connector.xslt.XSLTSubmissionHandler.java

/**
 * Serializes and submits the specified instance data over the
 * <code>xslt</code> protocol.
 *
 * @param submission the submission issuing the request.
 * @param instance the instance data to be serialized and submitted.
 * @return xslt transformation result.//from   w  w w.java  2s  . com
 * @throws XFormsException if any error occurred during submission.
 */
public Map submit(Submission submission, Node instance) throws XFormsException {
    try {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("uri: " + getURI());
        }

        URI uri = ConnectorHelper.resolve(submission.getContainerObject().getProcessor().getBaseURI(),
                getURI());
        Map parameters = ConnectorHelper.getURLParameters(uri);
        URI stylesheetURI = ConnectorHelper.removeURLParameters(uri);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("stylesheet uri: " + stylesheetURI);
        }

        // pass output properties from submission
        CachingTransformerService transformerService = (CachingTransformerService) getContext()
                .get(TransformerService.TRANSFORMER_SERVICE);

        if (transformerService == null) {
            if (uri.getScheme().equals("http")) {
                transformerService = new CachingTransformerService(new HttpResourceResolver());
            } else if (uri.getScheme().equals("file")) {
                transformerService = new CachingTransformerService(new FileResourceResolver());
            } else {
                throw new XFormsException(
                        "Protocol " + stylesheetURI.getScheme() + " not supported for XSLT Submission Handler");
            }
        }

        if (parameters != null && parameters.get("nocache") != null) {
            transformerService.setNoCache(true);
        }

        Transformer transformer = transformerService.getTransformer(stylesheetURI);

        if (submission.getVersion() != null) {
            transformer.setOutputProperty(OutputKeys.VERSION, submission.getVersion());
        }
        if (submission.getIndent() != null) {
            transformer.setOutputProperty(OutputKeys.INDENT,
                    Boolean.TRUE.equals(submission.getIndent()) ? "yes" : "no");
        }
        if (submission.getMediatype() != null) {
            transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, submission.getMediatype());
        }
        if (submission.getEncoding() != null) {
            transformer.setOutputProperty(OutputKeys.ENCODING, submission.getEncoding());
        } else {
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        }
        if (submission.getOmitXMLDeclaration() != null) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                    Boolean.TRUE.equals(submission.getOmitXMLDeclaration()) ? "yes" : "no");
        }
        if (submission.getStandalone() != null) {
            transformer.setOutputProperty(OutputKeys.STANDALONE,
                    Boolean.TRUE.equals(submission.getStandalone()) ? "yes" : "no");
        }
        if (submission.getCDATASectionElements() != null) {
            transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS,
                    submission.getCDATASectionElements());
        }

        // pass parameters form uri if any
        if (parameters != null) {
            Iterator iterator = parameters.keySet().iterator();
            String name;
            while (iterator.hasNext()) {
                name = (String) iterator.next();
                transformer.setParameter(name, parameters.get(name));
            }
        }

        // transform instance to byte array
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        long start = System.currentTimeMillis();

        DOMSource domSource;
        //check for 'parse' param
        String parseParam = null;
        if (parameters.containsKey("parseString")) {
            parseParam = (String) parameters.get("parseString");
        }
        if (parseParam != null && parseParam.equalsIgnoreCase("true")) {
            String xmlString = DOMUtil.getTextNodeAsString(instance);
            domSource = new DOMSource(DOMUtil.parseString(xmlString, true, false));
        } else {
            domSource = new DOMSource(instance);
        }

        transformer.transform(domSource, new StreamResult(outputStream));
        long end = System.currentTimeMillis();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("transformation time: " + (end - start) + " ms");
        }

        // create input stream from result
        InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        Map response = new HashMap();
        response.put(XFormsProcessor.SUBMISSION_RESPONSE_STREAM, inputStream);

        return response;
    } catch (Exception e) {
        throw new XFormsException(e);
    }
}

From source file:XMLUtils.java

public static String toString(Source source, Properties props) throws TransformerException, IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StreamResult sr = new StreamResult(bos);
    Transformer trans = newTransformer();
    if (props == null) {
        props = new Properties();
        props.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }// w w w  .j a  v a2s .co m
    trans.setOutputProperties(props);
    trans.transform(source, sr);
    bos.close();
    return bos.toString();
}

From source file:com.omertron.thetvdbapi.tools.DOMHelper.java

/**
 * Convert a DOM document to a string/*from  w  ww  .j a  va  2 s.  co m*/
 *
 * @param doc
 * @return
 * @throws TransformerException
 */
public static String convertDocToString(Document doc) throws TransformerException {
    //set up a transformer
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, YES);
    trans.setOutputProperty(OutputKeys.INDENT, YES);

    //create string from xml tree
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(doc);
    trans.transform(source, result);
    return sw.toString();
}

From source file:com.stratelia.webactiv.util.XMLConfigurationStore.java

@Override
public void serialize() throws FileNotFoundException, IOException {
    FileOutputStream out = new FileOutputStream(new File(configFileName));
    StreamResult streamResult = new StreamResult(out);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer;//from   www . j  a v a  2 s.c  o m
    try {
        serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, CharEncoding.UTF_8);
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        DOMSource domSource = new DOMSource(xmlConfigDOMDoc);
        serializer.transform(domSource, streamResult);
    } catch (TransformerException ex) {
        Logger.getLogger(XMLConfigurationStore.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        IOUtils.closeQuietly(out);
    }
}