domnodepointer.java
来自「JXPath」· Java 代码 · 共 786 行 · 第 1/2 页
JAVA
786 行
nodeTest = new NodeNameTest(name, namespaceURI);
}
else {
nodeTest = new NodeNameTest(name);
}
NodeIterator it = childIterator(nodeTest, false, null);
if (it != null && it.setPosition(index + 1)) {
return it.getNodePointer();
}
}
throw new JXPathException(
"Factory could not create a child node for path: "
+ asPath()
+ "/"
+ name
+ "["
+ (index + 1)
+ "]");
}
public NodePointer createChild(JXPathContext context,
QName name, int index, Object value)
{
NodePointer ptr = createChild(context, name, index);
ptr.setValue(value);
return ptr;
}
public NodePointer createAttribute(JXPathContext context, QName name) {
if (!(node instanceof Element)) {
return super.createAttribute(context, name);
}
Element element = (Element) node;
String prefix = name.getPrefix();
if (prefix != null) {
String ns = getNamespaceURI(prefix);
if (ns == null) {
throw new JXPathException(
"Unknown namespace prefix: " + prefix);
}
element.setAttributeNS(ns, name.toString(), "");
}
else {
if (!element.hasAttribute(name.getName())) {
element.setAttribute(name.getName(), "");
}
}
NodeIterator it = attributeIterator(name);
it.setPosition(1);
return it.getNodePointer();
}
public void remove() {
Node parent = node.getParentNode();
if (parent == null) {
throw new JXPathException("Cannot remove root DOM node");
}
parent.removeChild(node);
}
public String asPath() {
if (id != null) {
return "id('" + escape(id) + "')";
}
StringBuffer buffer = new StringBuffer();
if (parent != null) {
buffer.append(parent.asPath());
}
switch (node.getNodeType()) {
case Node.ELEMENT_NODE :
// If the parent pointer is not a DOMNodePointer, it is
// the parent's responsibility to produce the node test part
// of the path
if (parent instanceof DOMNodePointer) {
if (buffer.length() == 0
|| buffer.charAt(buffer.length() - 1) != '/') {
buffer.append('/');
}
String nsURI = getNamespaceURI();
String ln = DOMNodePointer.getLocalName(node);
if (nsURI == null) {
buffer.append(ln);
buffer.append('[');
buffer.append(getRelativePositionByName()).append(']');
}
else {
String prefix = getNamespaceResolver().getPrefix(nsURI);
if (prefix != null) {
buffer.append(prefix);
buffer.append(':');
buffer.append(ln);
buffer.append('[');
buffer.append(getRelativePositionByName());
buffer.append(']');
}
else {
buffer.append("node()");
buffer.append('[');
buffer.append(getRelativePositionOfElement());
buffer.append(']');
}
}
}
break;
case Node.TEXT_NODE :
case Node.CDATA_SECTION_NODE :
buffer.append("/text()");
buffer.append('[');
buffer.append(getRelativePositionOfTextNode()).append(']');
break;
case Node.PROCESSING_INSTRUCTION_NODE :
String target = ((ProcessingInstruction) node).getTarget();
buffer.append("/processing-instruction(\'");
buffer.append(target).append("')");
buffer.append('[');
buffer.append(getRelativePositionOfPI(target)).append(']');
break;
case Node.DOCUMENT_NODE :
// That'll be empty
}
return buffer.toString();
}
private String escape(String string) {
int index = string.indexOf('\'');
while (index != -1) {
string =
string.substring(0, index)
+ "'"
+ string.substring(index + 1);
index = string.indexOf('\'');
}
index = string.indexOf('\"');
while (index != -1) {
string =
string.substring(0, index)
+ """
+ string.substring(index + 1);
index = string.indexOf('\"');
}
return string;
}
private int getRelativePositionByName() {
int count = 1;
Node n = node.getPreviousSibling();
while (n != null) {
if (n.getNodeType() == Node.ELEMENT_NODE) {
String nm = n.getNodeName();
if (nm.equals(node.getNodeName())) {
count++;
}
}
n = n.getPreviousSibling();
}
return count;
}
private int getRelativePositionOfElement() {
int count = 1;
Node n = node.getPreviousSibling();
while (n != null) {
if (n.getNodeType() == Node.ELEMENT_NODE) {
count++;
}
n = n.getPreviousSibling();
}
return count;
}
private int getRelativePositionOfTextNode() {
int count = 1;
Node n = node.getPreviousSibling();
while (n != null) {
if (n.getNodeType() == Node.TEXT_NODE
|| n.getNodeType() == Node.CDATA_SECTION_NODE) {
count++;
}
n = n.getPreviousSibling();
}
return count;
}
private int getRelativePositionOfPI(String target) {
int count = 1;
Node n = node.getPreviousSibling();
while (n != null) {
if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE
&& ((ProcessingInstruction) n).getTarget().equals(target)) {
count++;
}
n = n.getPreviousSibling();
}
return count;
}
public int hashCode() {
return System.identityHashCode(node);
}
public boolean equals(Object object) {
if (object == this) {
return true;
}
if (!(object instanceof DOMNodePointer)) {
return false;
}
DOMNodePointer other = (DOMNodePointer) object;
return node == other.node;
}
public static String getPrefix(Node node) {
String prefix = node.getPrefix();
if (prefix != null) {
return prefix;
}
String name = node.getNodeName();
int index = name.lastIndexOf(':');
if (index == -1) {
return null;
}
return name.substring(0, index);
}
public static String getLocalName(Node node) {
String localName = node.getLocalName();
if (localName != null) {
return localName;
}
String name = node.getNodeName();
int index = name.lastIndexOf(':');
if (index == -1) {
return name;
}
return name.substring(index + 1);
}
public static String getNamespaceURI(Node node) {
if (node instanceof Document) {
node = ((Document) node).getDocumentElement();
}
Element element = (Element) node;
String uri = element.getNamespaceURI();
if (uri != null) {
return uri;
}
String qname;
String prefix = getPrefix(node);
if (prefix == null) {
qname = "xmlns";
}
else {
qname = "xmlns:" + prefix;
}
Node aNode = node;
while (aNode != null) {
if (aNode.getNodeType() == Node.ELEMENT_NODE) {
Attr attr = ((Element) aNode).getAttributeNode(qname);
if (attr != null) {
return attr.getValue();
}
}
aNode = aNode.getParentNode();
}
return null;
}
public Object getValue() {
return stringValue(node);
}
private String stringValue(Node node) {
int nodeType = node.getNodeType();
if (nodeType == Node.COMMENT_NODE) {
String text = ((Comment) node).getData();
return text == null ? "" : text.trim();
}
else if (
nodeType == Node.TEXT_NODE
|| nodeType == Node.CDATA_SECTION_NODE) {
String text = node.getNodeValue();
return text == null ? "" : text.trim();
}
else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) {
String text = ((ProcessingInstruction) node).getData();
return text == null ? "" : text.trim();
}
else {
NodeList list = node.getChildNodes();
StringBuffer buf = new StringBuffer(16);
for (int i = 0; i < list.getLength(); i++) {
Node child = list.item(i);
if (child.getNodeType() == Node.TEXT_NODE) {
buf.append(child.getNodeValue());
}
else {
buf.append(stringValue(child));
}
}
return buf.toString().trim();
}
}
/**
* Locates a node by ID.
*/
public Pointer getPointerByID(JXPathContext context, String id) {
Document document;
if (node.getNodeType() == Node.DOCUMENT_NODE) {
document = (Document) node;
}
else {
document = node.getOwnerDocument();
}
Element element = document.getElementById(id);
if (element != null) {
return new DOMNodePointer(element, getLocale(), id);
}
else {
return new NullPointer(getLocale(), id);
}
}
private AbstractFactory getAbstractFactory(JXPathContext context) {
AbstractFactory factory = context.getFactory();
if (factory == null) {
throw new JXPathException(
"Factory is not set on the JXPathContext - "
+ "cannot create path: "
+ asPath());
}
return factory;
}
public int compareChildNodePointers(
NodePointer pointer1, NodePointer pointer2)
{
Node node1 = (Node) pointer1.getBaseValue();
Node node2 = (Node) pointer2.getBaseValue();
if (node1 == node2) {
return 0;
}
int t1 = node1.getNodeType();
int t2 = node2.getNodeType();
if (t1 == Node.ATTRIBUTE_NODE && t2 != Node.ATTRIBUTE_NODE) {
return -1;
}
else if (t1 != Node.ATTRIBUTE_NODE && t2 == Node.ATTRIBUTE_NODE) {
return 1;
}
else if (t1 == Node.ATTRIBUTE_NODE && t2 == Node.ATTRIBUTE_NODE) {
NamedNodeMap map = ((Node) getNode()).getAttributes();
int length = map.getLength();
for (int i = 0; i < length; i++) {
Node n = map.item(i);
if (n == node1) {
return -1;
}
else if (n == node2) {
return 1;
}
}
return 0; // Should not happen
}
Node current = node.getFirstChild();
while (current != null) {
if (current == node1) {
return -1;
}
else if (current == node2) {
return 1;
}
current = current.getNextSibling();
}
return 0;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?