List of usage examples for org.w3c.dom DocumentType getSystemId
public String getSystemId();
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.ja v a 2 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: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 ww w .ja v a2 s.c o m System.out.println(" standalone=\"no\"?>"); } } else { System.out.println("?>"); } }
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\"?>"); }/* w w w . j a v a2 s . com*/ } else { System.out.println("?>"); } }
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 w ww. j av a2s. c om
From source file:Main.java
/** * @param node//from w w w. j a v a 2s . c o m * @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/* w ww .j av a 2 s. c o m*/ data += " SYSTEM \"" + doctype.getSystemId() + "\""; return data; }
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 a 2s.c om*/ // 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()); }// www. ja va 2s . com 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
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintStream</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * /*from w w w .ja va 2 s.co m*/ * @param out The <CODE>PrintStream</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(Node) * @see #dump(PrintStream, Node) * @since TFP 1.0 */ private static void doDump(PrintStream out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }
From source file:Main.java
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintWriter</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * /*from ww w .ja va 2 s .co m*/ * @param out The <CODE>PrintWriter</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(PrintWriter, Node) * @since TFP 1.0 */ private static void doDump(PrintWriter out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }