Example usage for javax.xml.transform OutputKeys DOCTYPE_SYSTEM

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

Introduction

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

Prototype

String DOCTYPE_SYSTEM

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

Click Source Link

Document

doctype-system = string.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from  ww w .ja v  a2s  .  c  om

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Transformer xformer = TransformerFactory.newInstance().newTransformer();

    xformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "publicId");
    xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "systemId");

    Source source = new DOMSource(doc);
    Result result = new StreamResult(new File("outfilename.xml"));
    xformer.transform(source, result);
}

From source file:Main.java

public static void saveDocument(Document dom, String file) throws TransformerException, IOException {

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();

    transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, dom.getDoctype().getPublicId());
    transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dom.getDoctype().getSystemId());

    DOMSource source = new DOMSource(dom);
    StreamResult result = new StreamResult();

    FileOutputStream outputStream = null;

    try {/*from  w w w.j av  a 2  s.c o  m*/
        outputStream = new FileOutputStream(file);
        result.setOutputStream(outputStream);
        transformer.transform(source, result);
        outputStream.flush();
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

From source file:Main.java

public static boolean write(String filename, Document document, boolean addDocType) {
    try {//w w w.  j a v  a 2 s  .  c om
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", new Integer(4));
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        if (addDocType) {
            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
                    "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN");
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                    "http://java.sun.com/dtd/facelet-taglib_1_0.dtd");
        }
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source,
                new StreamResult(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)))));
        return true;
    } catch (Exception e) {
        return false;
    }

}

From source file:Main.java

/**
 * Get DOM as a string/*w  ww  . j  a va 2 s. c o m*/
 * @param doc
 * @return
 */
public static String getStringFromDoc(org.w3c.dom.Document doc) {
    if (doc == null) {
        System.out.println("XML document is null");
    }
    try {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                "http://commons.omniupdate.com/dtd/standard.dtd");
        transformer.transform(domSource, result);
        writer.flush();
        return writer.toString();
    } catch (TransformerException ex) {
        System.out.println("Transformer Exception");
        // ex.printStackTrace();
        return "Error in transformation";
    }
}

From source file:Main.java

/**
 * Serialize XML from a {@link Document} to an OutputStream.
 * @param doc/* ww  w. j  a va  2 s  . co m*/
 * @param out
 */
public static void serialiseXml(Document doc, Writer out) {
    DOMSource domSource = new DOMSource(doc.getDocumentElement());
    StreamResult streamResult = new StreamResult(out);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = null;
    try {
        serializer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
    }
    serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "users.dtd");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    try {
        serializer.transform(domSource, streamResult);
    } catch (TransformerException e) {
    }
}

From source file:Main.java

static void emitDocument(Document doc, OutputStream os, String encoding) throws IOException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = null;/*from w  w  w . ja v  a2  s.co m*/
    try {
        t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, PROPS_DTD_URI);
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.ENCODING, encoding);
    } catch (TransformerConfigurationException tce) {
        assert (false);
    }
    DOMSource doms = new DOMSource(doc);
    StreamResult sr = new StreamResult(os);
    try {
        t.transform(doms, sr);
    } catch (TransformerException te) {
        IOException ioe = new IOException();
        ioe.initCause(te);
        throw ioe;
    }
}

From source file:Main.java

public static void getStringFromXML(Node node, String dtdFilename, Writer outputWriter)
        throws TransformerException {
    File dtdFile = null;/*ww w  .j  a v  a 2  s. com*/
    String workingPath = null;
    if (dtdFilename != null) {
        dtdFile = new File(dtdFilename);
        workingPath = System.setProperty("user.dir", dtdFile.getAbsoluteFile().getParent());
    }
    try {
        if (node instanceof Document)
            node = ((Document) node).getDocumentElement();

        TransformerFactory tranFact = TransformerFactory.newInstance();
        Transformer tf = tranFact.newTransformer();
        if (dtdFile != null) {
            tf.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dtdFile.getName());
            tf.setOutputProperty(OutputKeys.INDENT, "yes");
        }

        Source src = new DOMSource(node);
        Result dest = new StreamResult(outputWriter);

        tf.transform(src, dest);
    } finally {
        if (workingPath != null)
            System.setProperty("user.dir", workingPath);
    }
}

From source file:XMLUtil.java

public static void write(Document doc, OutputStream out) throws IOException {
    // XXX note that this may fail to write out namespaces correctly if the
    // document//  ww w .j  av a2s. c o m
    // is created with namespaces and no explicit prefixes; however no code in
    // this package is likely to be doing so
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        DocumentType dt = doc.getDoctype();
        if (dt != null) {
            String pub = dt.getPublicId();
            if (pub != null) {
                t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub);
            }
            t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId());
        }
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
        t.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N
        Source source = new DOMSource(doc);
        Result result = new StreamResult(out);
        t.transform(source, result);
    } catch (Exception e) {
        throw (IOException) new IOException(e.toString()).initCause(e);
    } catch (TransformerFactoryConfigurationError e) {
        throw (IOException) new IOException(e.toString()).initCause(e);
    }
}

From source file:Main.java

private static boolean updateXML(Document document, String path) throws TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();

    // out put encoding.
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DocumentType type = document.getDoctype();
    if (type != null) {
        System.out.println("doctype -->" + type.getPublicId());
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, type.getPublicId());
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, type.getSystemId());
    }/*from  ww w . j a  v a 2 s.  c o  m*/

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(new File(path));
    transformer.transform(source, result);

    transformer.reset();
    return true;
}

From source file:com.casewaresa.framework.util.LegacyJasperInputStream.java

public static String addDocTypeAndConvertDOMToString(final Document document) {

    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = null;//  w  w w .  j  ava 2  s .c  o m
    try {
        trans = transfac.newTransformer();
    } catch (TransformerConfigurationException ex) {
        log.error(ex.getMessage(), ex);
    }

    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    trans.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "//JasperReports//DTD Report Design//EN");
    trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
            "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd");

    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(document);
    try {
        trans.transform(source, result);
    } catch (TransformerException ex) {
        log.error(ex.getMessage(), ex);
    }

    return sw.toString();
}