use of org.w3c.dom.NamedNodeMap in project robovm by robovm.
the class DOMHelper method isNodeAfterSibling.
/**
* Figure out if child2 is after child1 in document order.
* <p>
* Warning: Some aspects of "document order" are not well defined.
* For example, the order of attributes is considered
* meaningless in XML, and the order reported by our model will
* be consistant for a given invocation but may not
* match that of either the source file or the serialized output.
*
* @param parent Must be the parent of both child1 and child2.
* @param child1 Must be the child of parent and not equal to child2.
* @param child2 Must be the child of parent and not equal to child1.
* @return true if child 2 is after child1 in document order.
*/
private static boolean isNodeAfterSibling(Node parent, Node child1, Node child2) {
boolean isNodeAfterSibling = false;
short child1type = child1.getNodeType();
short child2type = child2.getNodeType();
if ((Node.ATTRIBUTE_NODE != child1type) && (Node.ATTRIBUTE_NODE == child2type)) {
// always sort attributes before non-attributes.
isNodeAfterSibling = false;
} else if ((Node.ATTRIBUTE_NODE == child1type) && (Node.ATTRIBUTE_NODE != child2type)) {
// always sort attributes before non-attributes.
isNodeAfterSibling = true;
} else if (Node.ATTRIBUTE_NODE == child1type) {
NamedNodeMap children = parent.getAttributes();
int nNodes = children.getLength();
boolean found1 = false, found2 = false;
// Count from the start until we find one or the other.
for (int i = 0; i < nNodes; i++) {
Node child = children.item(i);
if (child1 == child || isNodeTheSame(child1, child)) {
if (found2) {
isNodeAfterSibling = false;
break;
}
found1 = true;
} else if (child2 == child || isNodeTheSame(child2, child)) {
if (found1) {
isNodeAfterSibling = true;
break;
}
found2 = true;
}
}
} else {
// TODO: Check performance of alternate solution:
// There are two choices here: Count from the start of
// the document until we find one or the other, or count
// from one until we find or fail to find the other.
// Either can wind up scanning all the siblings in the worst
// case, which on a wide document can be a lot of work but
// is more typically is a short list.
// Scanning from the start involves two tests per iteration,
// but it isn't clear that scanning from the middle doesn't
// yield more iterations on average.
// We should run some testcases.
Node child = parent.getFirstChild();
boolean found1 = false, found2 = false;
while (null != child) {
// Node child = children.item(i);
if (child1 == child || isNodeTheSame(child1, child)) {
if (found2) {
isNodeAfterSibling = false;
break;
}
found1 = true;
} else if (child2 == child || isNodeTheSame(child2, child)) {
if (found1) {
isNodeAfterSibling = true;
break;
}
found2 = true;
}
child = child.getNextSibling();
}
}
return isNodeAfterSibling;
}
use of org.w3c.dom.NamedNodeMap in project robovm by robovm.
the class XsltXPathConformanceTestSuite method emitAttributes.
private void emitAttributes(XmlSerializer serializer, Node node) throws IOException {
NamedNodeMap map = node.getAttributes();
if (map == null) {
return;
}
List<Attr> attributes = new ArrayList<Attr>();
for (int i = 0; i < map.getLength(); i++) {
attributes.add((Attr) map.item(i));
}
Collections.sort(attributes, orderByName);
for (Attr attr : attributes) {
if ("xmlns".equals(attr.getPrefix()) || "xmlns".equals(attr.getLocalName())) {
/*
* Omit namespace declarations because they aren't considered
* data. Ie. <foo:a xmlns:bar="http://google.com"> is semantically
* equal to <bar:a xmlns:bar="http://google.com"> since the
* prefix doesn't matter, only the URI it points to.
*
* When we omit the prefix, our XML serializer will still
* generate one for us, using a predictable pattern.
*/
} else {
serializer.attribute(attr.getNamespaceURI(), attr.getLocalName(), attr.getValue());
}
}
}
use of org.w3c.dom.NamedNodeMap in project robovm by robovm.
the class AttrGetOwnerElement method testGetOwnerElement5.
public void testGetOwnerElement5() throws Throwable {
Document doc;
Node element;
Element ownerElement;
Element parentElement;
NodeList elementList;
String ownerElementName;
Attr attr;
NamedNodeMap nodeMap;
String nullNS = null;
doc = (Document) load("staffNS", builder);
elementList = doc.getElementsByTagNameNS("*", "address");
element = elementList.item(1);
parentElement = (Element) element.getParentNode();
nodeMap = element.getAttributes();
parentElement.removeChild(element);
attr = (Attr) nodeMap.getNamedItemNS(nullNS, "street");
ownerElement = attr.getOwnerElement();
ownerElementName = ownerElement.getNodeName();
assertEquals("attrgetownerelement05", "address", ownerElementName);
}
use of org.w3c.dom.NamedNodeMap in project robovm by robovm.
the class ElementSetAttributeNodeNS method testSetAttributeNodeNS1.
/**
* Runs the test case.
*
* @throws Throwable
* Any uncaught exception causes test to fail
*/
public void testSetAttributeNodeNS1() throws Throwable {
Document doc;
Element element;
Attr attribute1;
Attr attribute2;
Attr attrNode;
String attrName;
String attrNS;
NamedNodeMap attributes;
int length;
doc = (Document) load("staff", builder);
element = doc.createElementNS("http://www.w3.org/DOM/Test/Level2", "new:element");
attribute1 = doc.createAttributeNS("http://www.w3.org/DOM/Test/att1", "p1:att");
attribute2 = doc.createAttributeNS("http://www.w3.org/DOM/Test/att1", "p2:att");
attribute2.setValue("value2");
element.setAttributeNodeNS(attribute1);
element.setAttributeNodeNS(attribute2);
attrNode = element.getAttributeNodeNS("http://www.w3.org/DOM/Test/att1", "att");
attrName = attrNode.getNodeName();
attrNS = attrNode.getNamespaceURI();
assertEquals("elementsetattributenodens01_attrName", "p2:att", attrName);
assertEquals("elementsetattributenodens01_attrNS", "http://www.w3.org/DOM/Test/att1", attrNS);
attributes = element.getAttributes();
length = (int) attributes.getLength();
assertEquals("length", 1, length);
}
use of org.w3c.dom.NamedNodeMap in project robovm by robovm.
the class GetNamedItemNS method testGetNamedItemNS1.
/**
* Runs the test case.
*
* @throws Throwable
* Any uncaught exception causes test to fail
*/
public void testGetNamedItemNS1() throws Throwable {
Document doc;
NodeList elementList;
Node testEmployee;
NamedNodeMap attributes;
Attr domesticAttr;
String attrName;
doc = (Document) load("staffNS", builder);
elementList = doc.getElementsByTagName("address");
testEmployee = elementList.item(1);
attributes = testEmployee.getAttributes();
domesticAttr = (Attr) attributes.getNamedItemNS("http://www.usa.com", "domestic");
attrName = domesticAttr.getNodeName();
assertEquals("attrName", "dmstc:domestic", attrName);
}
Aggregations