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.slc.sli.api.security.saml.SamlHelper.java

@PostConstruct
public void init() throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from   w  ww.  j  a  va 2 s.  c o m
    factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    domBuilder = factory.newDocumentBuilder();

    transform = TransformerFactory.newInstance().newTransformer();
    transform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
}

From source file:org.sonar.server.debt.DebtModelXMLExporter.java

private static String prettyFormatXml(String xml) {
    try {// w  w  w .  j av  a  2  s  .  c o m
        Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", DEFAULT_INDENT);
        Source xmlSource = new SAXSource(
                new InputSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
        StreamResult res = new StreamResult(new ByteArrayOutputStream());
        serializer.transform(xmlSource, res);
        return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray(),
                StandardCharsets.UTF_8);
    } catch (TransformerException ignored) {
        // Ignore, raw XML will be returned
    }
    return xml;
}

From source file:org.springsource.ide.eclipse.commons.browser.javafx.JavaFxBrowserManager.java

/**
 * For debugging../* www  .  j  a v  a 2s .com*/
 */
private void printPageHtml() {
    StringWriter sw = new StringWriter();
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "html");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        transformer.transform(new DOMSource(getView().getEngine().getDocument()), new StreamResult(sw));
        System.out.println(sw.toString());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.talend.dataquality.libraries.devops.ReleaseVersionBumper.java

private ReleaseVersionBumper() throws TransformerConfigurationException, TransformerFactoryConfigurationError {
    xTransformer = TransformerFactory.newInstance().newTransformer();
    xTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    microVersion = targetVersion.substring(4);
}

From source file:org.talend.designer.maven.utils.PomUtil.java

/**
 * //from w w  w . j  a va2 s . co m
 * Create pom without refresh eclipse resources
 * 
 * @param artifact
 * @return
 */
public static String generatePom2(MavenArtifact artifact) {
    try {
        Project project = ProjectManager.getInstance().getCurrentProject();
        IProject fsProject = ResourceUtils.getProject(project);
        SecureRandom random = new SecureRandom();
        IPath tempPath = fsProject.getLocation().append("temp").append("pom" + Math.abs(random.nextLong()));
        File tmpFolder = new File(tempPath.toPortableString());
        tmpFolder.mkdirs();
        String pomFile = tempPath.append(TalendMavenConstants.POM_FILE_NAME).toPortableString();
        Model pomModel = new Model();
        pomModel.setModelVersion(TalendMavenConstants.POM_VERSION);
        pomModel.setModelEncoding(TalendMavenConstants.DEFAULT_ENCODING);
        pomModel.setGroupId(artifact.getGroupId());
        pomModel.setArtifactId(artifact.getArtifactId());
        pomModel.setVersion(artifact.getVersion());
        String artifactType = artifact.getType();
        if (artifactType == null || "".equals(artifactType)) {
            artifactType = TalendMavenConstants.PACKAGING_JAR;
        }
        pomModel.setPackaging(artifactType);

        ByteArrayOutputStream buf = new ByteArrayOutputStream();

        MavenPlugin.getMaven().writeModel(pomModel, buf);

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(false);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        TransformerFactory tfactory = TransformerFactory.newInstance();

        Document document = documentBuilder.parse(new ByteArrayInputStream(buf.toByteArray()));
        Element documentElement = document.getDocumentElement();

        NamedNodeMap attributes = documentElement.getAttributes();

        if (attributes == null || attributes.getNamedItem("xmlns") == null) { //$NON-NLS-1$
            Attr attr = document.createAttribute("xmlns"); //$NON-NLS-1$
            attr.setTextContent("http://maven.apache.org/POM/4.0.0"); //$NON-NLS-1$
            documentElement.setAttributeNode(attr);
        }

        if (attributes == null || attributes.getNamedItem("xmlns:xsi") == null) { //$NON-NLS-1$
            Attr attr = document.createAttribute("xmlns:xsi"); //$NON-NLS-1$
            attr.setTextContent("http://www.w3.org/2001/XMLSchema-instance"); //$NON-NLS-1$
            documentElement.setAttributeNode(attr);
        }

        if (attributes == null || attributes.getNamedItem("xsi:schemaLocation") == null) { //$NON-NLS-1$
            Attr attr = document.createAttributeNS("http://www.w3.org/2001/XMLSchema-instance", //$NON-NLS-1$
                    "xsi:schemaLocation"); //$NON-NLS-1$
            attr.setTextContent(
                    "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"); //$NON-NLS-1$
            documentElement.setAttributeNode(attr);
        }
        Transformer transformer = tfactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new File(pomFile));
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(source, result);

        return pomFile;
    } catch (PersistenceException e) {
        ExceptionHandler.process(e);
    } catch (CoreException e) {
        ExceptionHandler.process(e);
    } catch (ParserConfigurationException e) {
        ExceptionHandler.process(e);
    } catch (SAXException e) {
        ExceptionHandler.process(e);
    } catch (IOException e) {
        ExceptionHandler.process(e);
    } catch (TransformerConfigurationException e) {
        ExceptionHandler.process(e);
    } catch (TransformerException e) {
        ExceptionHandler.process(e);
    }
    return null;
}

From source file:org.tizzit.util.XercesHelper.java

public static String doc2String(Document doc) {
    StringWriter stringOut = new StringWriter();
    try {/*  w  w w  .  ja v  a 2s .  co m*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.transform(new DOMSource(doc), new StreamResult(stringOut));

        /*OutputFormat format = new OutputFormat(doc, "ISO-8859-1", true);
        format.setOmitXMLDeclaration(true);
        format.setOmitDocumentType(true);
        stringOut = new StringWriter();
        XMLSerializer serial = new XMLSerializer(stringOut, format);
        serial.asDOMSerializer();
        serial.serialize(doc);*/
    } catch (Exception exe) {
        log.error("unknown error occured", exe);
    }
    return stringOut.toString();
}

From source file:org.tizzit.util.XercesHelper.java

public static String node2Html(Node node) {
    Document doc = getNewDocument();
    Node newnde = doc.importNode(node, true);
    doc.appendChild(newnde);//ww w . j a v a2s.  c o m

    StringWriter stringOut = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        // for "XHTML" serialization, use the output method "xml" and set publicId as shown

        /*t.setOutputProperty(OutputKeys.METHOD, "xml");
         t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//W3C//DTD XHTML 1.0 Transitional//EN");
         t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");*/

        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");

        t.transform(new DOMSource(doc), new StreamResult(stringOut));
    } catch (Exception exe) {
        log.error("unknown error occured", exe);
    }
    return stringOut.toString();
}

From source file:org.tizzit.util.xml.SAXHelper.java

/**
 * @param node the string to stream.//  w  w  w. j a va 2  s . c o m
 * @param handler the content handler.
 * @since tizzit-common 15.10.2009
 */
public static void string2sax(String node, ContentHandler handler) {
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.STANDALONE, "no");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        StreamSource source = new StreamSource(new StringReader(node));
        SAXResult result = new SAXResult(new OmitXmlDeclarationContentHandler(handler));
        t.transform(source, result);
    } catch (Exception exe) {
        log.error("unknown error occured", exe);
    }
}

From source file:org.tizzit.util.xml.SAXHelper.java

/**
 * @param node the string to stream./*from  w ww . j  av a2s.c om*/
 * @param handler the content handler.
 * @since tizzit-common 15.10.2009
 */
public static void string2sax(String node, ContentHandler handler, String encoding) {
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.STANDALONE, "no");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        if (!"".equals(encoding) && encoding != null) {
            t.setOutputProperty(OutputKeys.ENCODING, encoding);
        }
        StreamSource source = new StreamSource(new StringReader(node));
        SAXResult result = new SAXResult(new OmitXmlDeclarationContentHandler(handler));
        t.transform(source, result);
    } catch (Exception exe) {
        log.error("unknown error occured", exe);
    }
}