Example usage for org.w3c.dom DocumentType getInternalSubset

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

Introduction

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

Prototype

public String getInternalSubset();

Source Link

Document

The internal subset as a string, or null if there is none.

Usage

From source file:Main.java

public static void main(String args[]) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true); // Set namespace aware
    builderFactory.setValidating(true); // and validating parser feaures
    builderFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = null;
    try {/*w w w  . j a  v a  2 s  . c  om*/
        builder = builderFactory.newDocumentBuilder(); // Create the parser
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Document xmlDoc = null;

    try {
        xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    } catch (SAXException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }
    DocumentType doctype = xmlDoc.getDoctype();
    if (doctype == null) {
        System.out.println("DOCTYPE is null");
    } else {
        System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
    }

    System.out.println("\nDocument body contents are:");
    listNodes(xmlDoc.getDocumentElement(), ""); // Root element & children
}

From source file:MainClass.java

public static void main(String args[]) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setValidating(true); // and validating parser feaures
    builderFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = null;
    try {//from   w w  w  .j ava  2s  .  c o  m
        builder = builderFactory.newDocumentBuilder(); // Create the parser
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Document xmlDoc = null;

    try {
        xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    } catch (SAXException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }
    DocumentType doctype = xmlDoc.getDoctype();
    if (doctype == null) {
        System.out.println("DOCTYPE is null");
    } else {
        System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
    }

    System.out.println("\nDocument body contents are:");
    listNodes(xmlDoc.getDocumentElement(), ""); // Root element & children
}

From source file:Main.java

/**
 * @param node/* ww  w  . j  ava2s  . 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:ca.uviccscu.lp.utils.Utils.java

public static String readOutDomTree(Document d) {
    StringBuilder sb = new StringBuilder();
    Document xmlDoc = d;//w  ww. j  ava  2 s .c  o m
    DocumentType doctype = xmlDoc.getDoctype();
    if (doctype == null) {
        sb.append("\n");
        sb.append("DOCTYPE is null");
        sb.append("\n");
    } else {
        sb.append("DOCTYPE node:\n" + doctype.getInternalSubset());
        sb.append("\n");
    }

    sb.append("\nDocument body contents are:");
    sb.append("\n");
    listNodes(xmlDoc.getDocumentElement(), "", sb); // Root element & children
    return sb.toString();
}

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('\"');
    }//from w w  w. j a v  a2 s  . co m
    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();
}

From source file:TryDOM.java

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

        final char QUOTE = '\"';
        if ((str = doctype.getSystemId()) != null)
            doctypeStr.append(" SYSTEM ").append(QUOTE).append(str).append(QUOTE);
        else if ((str = doctype.getPublicId()) != null) // Check for a public ID
            doctypeStr.append(" PUBLIC ").append(QUOTE).append(str).append(QUOTE);

        if ((str = doctype.getInternalSubset()) != null)
            doctypeStr.append('[').append(str).append(']');

        return doctypeStr.append('>').toString(); // Append '>', return the string
    }//from   ww w  .ja v a  2s .c om

From source file:bridge.toolkit.commands.S1000DConverter.java

/**
 * Iterate through the DOM tree/* w  w w  . j a v a 2 s .c  om*/
 * 
 * @param node
 * @param output
 * @param isDocument
 * @param encoding
 * @param internalSubset
 * @throws IOException
 */
static void writeNode(Node node, Writer output, boolean isDocument, String encoding, String internalSubset)
        throws IOException {

    if (isDocument) {
        if (encoding == null)
            output.write("<?xml version=\"1.0\"?>\n\n");
        else
            output.write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n\n");

        DocumentType doctype = node.getOwnerDocument().getDoctype();

        if (doctype != null) {
            String pubid = doctype.getPublicId();
            String sysid = doctype.getSystemId();
            output.write("<!DOCTYPE ");
            output.write(node.getNodeName());
            if (pubid != null) {
                output.write(" PUBLIC \"");
                output.write(pubid);
                if (sysid != null) {
                    output.write("\" \"");
                    output.write(sysid);
                }
                output.write('"');
            } else if (sysid != null) {
                output.write(" SYSTEM \"");
                output.write(sysid);
                output.write('"');
            }
            String subset = internalSubset;
            if (subset == null)
                subset = doctype.getInternalSubset();
            if (subset != null) {
                output.write(" [");
                output.write(subset);
                output.write(']');
            }
            output.write(">\n\n");
        }
    }
    writeNode(node, output);

    if (isDocument)
        output.write("\n");
}

From source file:fr.gouv.finances.dgfip.xemelios.utils.TextWriter.java

private void documentType(Writer writer, DocumentType docType) throws IOException {
    writer.write("<!DOCTYPE ");
    writer.write(docType.getName());/*from  w  w w.  j  a v a2 s.c  om*/
    String pubID = docType.getPublicId();
    String sysID = docType.getSystemId();
    if (pubID != null) {
        writer.write(" PUBLIC ");
        writer.write(pubID);
        if (sysID != null) {
            writer.write(' ');
            writer.write(sysID);
        }
    } else if (sysID != null) {
        writer.write(" SYSTEM ");
        writer.write(sysID);
    }

    String is = docType.getInternalSubset();
    if (is != null) {
        writer.write(" [");
        writer.write(is);
        writer.write("]");
    }
    writer.write(">\n");
}

From source file:DOMWriter.java

/** Writes the specified node, recursively. */
public void write(Node node) {

    // is there anything to do?
    if (node == null) {
        return;//w ww. j  a v a  2s .  c om
    }

    short type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        Document document = (Document) node;
        fXML11 = "1.1".equals(getVersion(document));
        if (!fCanonical) {
            if (fXML11) {
                fOut.println("<?xml version=\"1.1\" encoding=\"UTF-8\"?>");
            } else {
                fOut.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            }
            fOut.flush();
            write(document.getDoctype());
        }
        write(document.getDocumentElement());
        break;
    }

    case Node.DOCUMENT_TYPE_NODE: {
        DocumentType doctype = (DocumentType) node;
        fOut.print("<!DOCTYPE ");
        fOut.print(doctype.getName());
        String publicId = doctype.getPublicId();
        String systemId = doctype.getSystemId();
        if (publicId != null) {
            fOut.print(" PUBLIC '");
            fOut.print(publicId);
            fOut.print("' '");
            fOut.print(systemId);
            fOut.print('\'');
        } else if (systemId != null) {
            fOut.print(" SYSTEM '");
            fOut.print(systemId);
            fOut.print('\'');
        }
        String internalSubset = doctype.getInternalSubset();
        if (internalSubset != null) {
            fOut.println(" [");
            fOut.print(internalSubset);
            fOut.print(']');
        }
        fOut.println('>');
        break;
    }

    case Node.ELEMENT_NODE: {
        fOut.print('<');
        fOut.print(node.getNodeName());
        Attr attrs[] = sortAttributes(node.getAttributes());
        for (int i = 0; i < attrs.length; i++) {
            Attr attr = attrs[i];
            fOut.print(' ');
            fOut.print(attr.getNodeName());
            fOut.print("=\"");
            normalizeAndPrint(attr.getNodeValue(), true);
            fOut.print('"');
        }
        fOut.print('>');
        fOut.flush();

        Node child = node.getFirstChild();
        while (child != null) {
            write(child);
            child = child.getNextSibling();
        }
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: {
        if (fCanonical) {
            Node child = node.getFirstChild();
            while (child != null) {
                write(child);
                child = child.getNextSibling();
            }
        } else {
            fOut.print('&');
            fOut.print(node.getNodeName());
            fOut.print(';');
            fOut.flush();
        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        if (fCanonical) {
            normalizeAndPrint(node.getNodeValue(), false);
        } else {
            fOut.print("<![CDATA[");
            fOut.print(node.getNodeValue());
            fOut.print("]]&gt;");
        }
        fOut.flush();
        break;
    }

    case Node.TEXT_NODE: {
        normalizeAndPrint(node.getNodeValue(), false);
        fOut.flush();
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: {
        fOut.print("<?");
        fOut.print(node.getNodeName());
        String data = node.getNodeValue();
        if (data != null && data.length() > 0) {
            fOut.print(' ');
            fOut.print(data);
        }
        fOut.print("?>");
        fOut.flush();
        break;
    }

    case Node.COMMENT_NODE: {
        if (!fCanonical) {
            fOut.print("<!--");
            String comment = node.getNodeValue();
            if (comment != null && comment.length() > 0) {
                fOut.print(comment);
            }
            fOut.print("-->");
            fOut.flush();
        }
    }
    }

    if (type == Node.ELEMENT_NODE) {
        fOut.print("</");
        fOut.print(node.getNodeName());
        fOut.print('>');
        fOut.flush();
    }

}

From source file:net.sourceforge.pmd.lang.xml.ast.DOMLineNumbers.java

private String unexpandEntities(Node n, String te) {
    String result = te;// ww w .  j  ava2s  .c  om
    DocumentType doctype = n.getOwnerDocument().getDoctype();
    // implicit entities
    result = result.replaceAll(Matcher.quoteReplacement("&"), "&amp;");
    result = result.replaceAll(Matcher.quoteReplacement("<"), "&lt;");
    result = result.replaceAll(Matcher.quoteReplacement(">"), "&gt;");
    result = result.replaceAll(Matcher.quoteReplacement("\""), "&quot;");
    result = result.replaceAll(Matcher.quoteReplacement("'"), "&apos;");

    if (doctype != null) {
        NamedNodeMap entities = doctype.getEntities();
        String internalSubset = doctype.getInternalSubset();
        if (internalSubset == null) {
            internalSubset = "";
        }
        for (int i = 0; i < entities.getLength(); i++) {
            Node item = entities.item(i);
            String entityName = item.getNodeName();
            Node firstChild = item.getFirstChild();
            if (firstChild != null) {
                result = result.replaceAll(Matcher.quoteReplacement(firstChild.getNodeValue()),
                        "&" + entityName + ";");
            } else {
                Matcher m = Pattern
                        .compile(Matcher.quoteReplacement("<!ENTITY " + entityName + " ") + "[']([^']*)[']>")
                        .matcher(internalSubset);
                if (m.find()) {
                    result = result.replaceAll(Matcher.quoteReplacement(m.group(1)), "&" + entityName + ";");
                }
            }
        }
    }
    return result;
}