📄 node.java
字号:
/**
* Node.java
*
* This file was generated by XMLSpy 2007sp2 Enterprise Edition.
*
* YOU SHOULD NOT MODIFY THIS FILE, BECAUSE IT WILL BE
* OVERWRITTEN WHEN YOU RE-RUN CODE GENERATION.
*
* Refer to the XMLSpy Documentation for further details.
* http://www.altova.com/xmlspy
*/
package com.jmex.xml.xml;
public abstract class Node implements java.io.Serializable {
final protected static short Attribute = 0;
final protected static short Element = 1;
final protected static short Text = 2;
final protected static short CData = 3;
final protected static short Comment = 4;
final protected static short ProcessingInstruction = 5;
protected org.w3c.dom.Node domNode = null;
public Node(Node node) {
domNode = node.domNode;
}
public Node(org.w3c.dom.Node domNode) {
this.domNode = domNode;
}
public Node(org.w3c.dom.Document domDocument) {
this.domNode = domDocument.getDocumentElement();
}
public Node(Document doc, String namespaceURI, String prefix, String name) {
this.domNode = doc.createRootElement(namespaceURI, name);
mapPrefix(prefix, namespaceURI);
doc.declareNamespaces(this);
}
public org.w3c.dom.Node getDomNode() {
return domNode;
}
public void copyOf(Node other) {
org.w3c.dom.Node node = domNode.getOwnerDocument().importNode(other.domNode, true);
if (domNode.getParentNode() != null)
domNode.getParentNode().replaceChild(node, domNode);
domNode = node;
}
public static String getDomNodeValue(org.w3c.dom.Node node) {
if (node == null)
return null;
String value = node.getNodeValue();
if (value != null)
return value;
return getInnerText(node);
}
protected static String getInnerText(org.w3c.dom.Node node) {
org.w3c.dom.NodeList elements = node.getChildNodes();
int length = elements.getLength();
StringBuffer text = new StringBuffer();
for (int i = 0; i < length; i++) {
org.w3c.dom.Node child = elements.item(i);
if (child.getNodeType() == org.w3c.dom.Node.TEXT_NODE)
text.append(child.getNodeValue());
else if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE)
text.append(getInnerText(child));
else if (child.getNodeType() == org.w3c.dom.Node.CDATA_SECTION_NODE)
text.append(child.getNodeValue());
}
return text.toString();
}
public static String getNodeTextValue(org.w3c.dom.Node node) {
return getDomNodeValue( node );
}
protected static void setDomNodeValue(org.w3c.dom.Node node, String value) {
if (node == null)
return;
if (node.getNodeValue() != null) {
node.setNodeValue(value);
return;
}
org.w3c.dom.Node child = node.getFirstChild();
if (child == null || child.getNodeType() != org.w3c.dom.Node.TEXT_NODE) {
node.appendChild(node.getOwnerDocument().createTextNode(value));
return;
}
}
public void mapPrefix(String prefix, String URI) {
if (URI == null || URI.equals(""))
return;
org.w3c.dom.Element element = (org.w3c.dom.Element)domNode;
if (prefix == null || prefix.equals(""))
element.setAttribute("xmlns", URI);
else
element.setAttribute("xmlns:" + prefix, URI);
}
public void assign(com.jmex.xml.xml.Node other)
{
setDomNodeValue(domNode, getDomNodeValue(other.domNode).toString());
}
protected void declareNamespace(String prefix, String URI) {
if (URI == null || URI.equals(""))
return;
org.w3c.dom.Element root = domNode.getOwnerDocument().getDocumentElement();
org.w3c.dom.NamedNodeMap attrs = root.getAttributes();
if (attrs != null) {
for (int i = 0; i < attrs.getLength(); i++) {
org.w3c.dom.Attr attr = (org.w3c.dom.Attr)attrs.item(i);
if (attr.getValue().equals(URI) && attr.getName().startsWith("xmlns")) // namespace URI already mapped?
return; // do not overwrite
}
}
if (prefix.equals("xml") && URI.equals("http://www.w3.org/XML/1998/namespace"))
return;
if (prefix == null || prefix.equals(""))
root.setAttribute("xmlns", URI);
else
root.setAttribute("xmlns:" + prefix, URI);
}
protected org.w3c.dom.Node appendDomChild(int type, String namespaceURI, String name, String value) {
switch (type) {
case Attribute:
org.w3c.dom.Attr attribute = domNode.getOwnerDocument().createAttributeNS(namespaceURI, name);
attribute.setNodeValue(value);
domNode.getAttributes().setNamedItemNS(attribute);
return attribute;
case Element:
org.w3c.dom.Element element = domNode.getOwnerDocument().createElementNS(namespaceURI, name);
if (value != null && value.length() != 0)
element.appendChild(domNode.getOwnerDocument().createTextNode(value));
domNode.appendChild(element);
return element;
case Text:
org.w3c.dom.Text text = domNode.getOwnerDocument().createTextNode(value);
domNode.appendChild(text);
return text;
case CData:
org.w3c.dom.CDATASection cdata = domNode.getOwnerDocument().createCDATASection(value);
domNode.appendChild(cdata);
return cdata;
case Comment:
org.w3c.dom.Comment comment = domNode.getOwnerDocument().createComment(value);
domNode.appendChild(comment);
return comment;
case ProcessingInstruction:
org.w3c.dom.ProcessingInstruction piNode = domNode.getOwnerDocument().createProcessingInstruction(name, value);
domNode.appendChild(piNode);
return piNode;
default:
throw new XmlException("Unknown type");
}
}
public static boolean domNodeNameEquals(org.w3c.dom.Node node, String namespaceURI, String localName) {
if (node == null)
return false;
if (localName == null)
return true;
String nodeURI = node.getNamespaceURI() == null ? "" : node.getNamespaceURI();
String nodeLocalName = node.getLocalName() == null ? "" : node.getLocalName();
if (namespaceURI == null)
namespaceURI = "";
return nodeURI.equals(namespaceURI) && nodeLocalName.equals(localName);
}
protected int getDomChildCount(int type, String namespaceURI, String name) {
switch (type) {
case Attribute:
try {
return ((org.w3c.dom.Element)domNode).hasAttributeNS(namespaceURI, name) ? 1 : 0;
} catch (Exception e) {
return 0;
}
case Element:
org.w3c.dom.NodeList elements = domNode.getChildNodes();
int length = elements.getLength();
int count = 0;
for (int i = 0; i < length; i++) {
org.w3c.dom.Node child = elements.item(i);
if (domNodeNameEquals(child, namespaceURI, name))
count++;
}
return count;
default:
throw new XmlException("Unknown type");
}
}
protected boolean hasDomChild(int type, String namespaceURI, String name) {
switch (type) {
case Attribute:
return ((org.w3c.dom.Element)domNode).hasAttributeNS(namespaceURI, name) ? true : false;
case Element:
org.w3c.dom.NodeList elements = domNode.getChildNodes();
int length = elements.getLength();
for (int i = 0; i < length; i++)
if (domNodeNameEquals(elements.item(i), namespaceURI, name))
return true;
return false;
default:
throw new XmlException("Unknown type");
}
}
protected org.w3c.dom.Node getDomChildAt(int type, String namespaceURI, String name, int index) {
int length, count = 0;
switch (type) {
case Attribute:
return domNode.getAttributes().getNamedItemNS(namespaceURI, name);
case Element:
org.w3c.dom.NodeList elements = domNode.getChildNodes();
length = elements.getLength();
for (int i = 0; i < length; i++) {
org.w3c.dom.Node child = elements.item(i);
if (domNodeNameEquals(child, namespaceURI, name) && count++ == index)
return child;
}
throw new XmlException("Index out of range");
default:
throw new XmlException("Unknown type");
}
}
protected org.w3c.dom.Node getDomFirstChild(int type, String namespaceURI, String name ) {
int length, count = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -