Example usage for org.w3c.dom DocumentType getPublicId

List of usage examples for org.w3c.dom DocumentType getPublicId

Introduction

In this page you can find the example usage for org.w3c.dom DocumentType getPublicId.

Prototype

public String getPublicId();

Source Link

Document

The public identifier of the external subset.

Usage

From source file:Main.java

public static boolean checkDocumentType(Document document, String dtdPublicId) {
    DocumentType documentType = document.getDoctype();

    if (documentType != null) {
        String publicId = documentType.getPublicId();

        return publicId != null && publicId.equals(dtdPublicId);
    }/*from  w w  w . j  a  v a  2s.  c  o m*/
    return true; // Workaround until DTDs are published
    // return false;
}

From source file:Main.java

/**
 * Get the public id of the xml doc.//  w  w  w. j  a va  2 s . co m
 * @return string of the public id.
 */
public static String getPublicId() {
    DocumentType doctype = doc.getDoctype();
    if (null != doctype) {
        return doctype.getPublicId();
    }
    return null;
}

From source file:Main.java

public static boolean checkDocumentType(Document document, String dtdPublicId) {
    DocumentType documentType = document.getDoctype();
    if (documentType != null) {
        String publicId = documentType.getPublicId();
        return publicId != null && publicId.equals(dtdPublicId);
    }// w  w w. jav  a  2 s.co  m
    // System.err.println("Can't check document type: " + dtdPublicId);
    return true; // false; Due to problem of IdentityTransformer not creating the DocType nodes
}

From source file:DOMEdit.java

private static void outputHeading(Document doc) {
    System.out.print("<?xml version=\"1.0\"");
    DocumentType doctype = doc.getDoctype();
    if (doctype != null) {
        if ((doctype.getPublicId() == null) && (doctype.getSystemId() == null)) {
            System.out.println(" standalone=\"yes\"?>");
        } else {//from  www  .  ja  v a  2 s  . co m
            System.out.println(" standalone=\"no\"?>");
        }
    } else {
        System.out.println("?>");
    }
}

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/*from w w w. j ava  2  s . 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:DOMCopy.java

private static void outputHeading(Document doc) {
        System.out.print("<?xml version=\"1.0\"");
        DocumentType doctype = doc.getDoctype();
        if (doctype != null) {
            if ((doctype.getPublicId() == null) && (doctype.getSystemId() == null)) {
                System.out.println(" standalone=\"yes\"?>");
            } else {
                System.out.println(" standalone=\"no\"?>");
            }//from w w w.j  a v  a2s .co  m
        } else {
            System.out.println("?>");
        }
    }

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 w  ww  .jav a2s .  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:Main.java

/**
 * @param node// www  .  j  av a  2  s. c om
 * @throws IOException
 */
public static void serializeNode(Node node) throws IOException {
    if (writer == null)
        writer = new BufferedWriter(new OutputStreamWriter(System.out));

    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        Document doc = (Document) node;
        writer.write("<?xml version=\"");
        writer.write(doc.getXmlVersion());
        writer.write("\" encoding=\"UTF-8\" standalone=\"");
        if (doc.getXmlStandalone())
            writer.write("yes");
        else
            writer.write("no");
        writer.write("\"?>\n");

        NodeList nodes = node.getChildNodes();
        if (nodes != null)
            for (int i = 0; i < nodes.getLength(); i++)
                serializeNode(nodes.item(i));
        break;
    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        writer.write("<" + name);
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node current = attributes.item(i);
            writer.write(" " + current.getNodeName() + "=\"");
            print(current.getNodeValue());
            writer.write("\"");
        }
        writer.write(">");

        NodeList children = node.getChildNodes();
        if (children != null) {
            //if ((children.item(0) != null) && (children.item(0).getNodeType() == Node.ELEMENT_NODE))
            //  writer.write("\n");

            for (int i = 0; i < children.getLength(); i++)
                serializeNode(children.item(i));
            if ((children.item(0) != null)
                    && (children.item(children.getLength() - 1).getNodeType() == Node.ELEMENT_NODE))
                writer.write("");
        }

        writer.write("</" + name + ">");
        break;
    case Node.TEXT_NODE:
        print(node.getNodeValue());
        break;
    case Node.CDATA_SECTION_NODE:
        writer.write("CDATA");
        print(node.getNodeValue());
        writer.write("");
        break;
    case Node.COMMENT_NODE:
        writer.write("<!-- " + node.getNodeValue() + " -->\n");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writer.write("<?" + node.getNodeName() + " " + node.getNodeValue() + "?>\n");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writer.write("&" + node.getNodeName() + ";");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        DocumentType docType = (DocumentType) node;
        String publicId = docType.getPublicId();
        String systemId = docType.getSystemId();
        String internalSubset = docType.getInternalSubset();
        writer.write("<!DOCTYPE " + docType.getName());
        if (publicId != null)
            writer.write(" PUBLIC \"" + publicId + "\" ");
        else
            writer.write(" SYSTEM ");
        writer.write("\"" + systemId + "\"");
        if (internalSubset != null)
            writer.write(" [" + internalSubset + "]");
        writer.write(">\n");
        break;
    }
    writer.flush();
}

From source file:Main.java

protected static String getDocumentTypeData(DocumentType doctype) {
    String data = doctype.getName();
    if (doctype.getPublicId() != null) {
        data += " PUBLIC \"" + doctype.getPublicId() + "\"";
        String systemId = doctype.getSystemId();
        if (systemId == null)
            systemId = "";
        data += " \"" + systemId + "\"";
    } else//from  w w w .  j a va2  s.co m
        data += " SYSTEM \"" + doctype.getSystemId() + "\"";

    return data;
}

From source file:MainClass.java

private static String getDoctypeString(DocumentType doctype) {
    String str = doctype.getName();
    StringBuffer doctypeStr = new StringBuffer("<!DOCTYPE ").append(str);

    if ((str = doctype.getSystemId()) != null) {
        doctypeStr.append(" SYSTEM ").append('\"').append(str).append('\"');
    }//  ww w. j  a v a2  s .c om
    if ((str = doctype.getPublicId()) != null) {
        doctypeStr.append(" PUBLIC ").append('\"').append(str).append('\"');
    }
    if ((str = doctype.getInternalSubset()) != null) {
        doctypeStr.append('[').append(str).append(']');
    }
    return doctypeStr.append('>').toString();
}