1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
package com.jclark.xsl.dom;
import com.jclark.xsl.om.*;
class ElementNode extends ContainerNode {
private Name name;
private AttributeNode[] attributeNodes = null;
ElementNode(org.w3c.dom.Node domNode, ContainerNode parent, int childIndex) {
super(domNode, parent, childIndex);
org.w3c.dom.NamedNodeMap domAttributes = domNode.getAttributes();
int len = domAttributes.getLength();
if (len == 0)
return;
attributeNodes = new AttributeNode[len];
int firstPrefixIndex = -1;
for (int i = 0; i < len; i++) {
org.w3c.dom.Node domAttribute = domAttributes.item(i);
String qName = domAttribute.getNodeName();
switch (qName.indexOf(':')) {
case -1:
if (qName.equals("xmlns")) {
String uri = domAttribute.getNodeValue();
if (uri.length() == 0)
prefixMap = prefixMap.unbindDefault();
else
prefixMap = prefixMap.bindDefault(uri);
}
else
attributeNodes[i] = new AttributeNode(root.nameTable.createName(domAttribute.getNodeName()),
domAttribute,
this,
// attributes occur before children
i - len - 1);
break;
case 3:
if (qName.equals("xml:space")) {
String value = domAttribute.getNodeValue();
if ("preserve".equals(value))
preserveSpace = true;
else if ("default".equals(value))
preserveSpace = false;
}
if (firstPrefixIndex < 0)
firstPrefixIndex = i;
break;
case 5:
if (qName.startsWith("xmlns")) {
prefixMap = prefixMap.bind(qName.substring(6),
domAttribute.getNodeValue());
break;
}
// fall through
default:
if (firstPrefixIndex < 0)
firstPrefixIndex = i;
break;
}
}
if (firstPrefixIndex >= 0) {
// global attributes are hopefully relatively rare
for (int i = firstPrefixIndex; i < len; i++) {
if (attributeNodes[i] == null) {
org.w3c.dom.Node domAttribute = domAttributes.item(i);
String qName = domAttribute.getNodeName();
if (qName.startsWith("xmlns")
&& (qName.length() == 5 || qName.charAt(5) == ':'))
;
else {
try {
attributeNodes[i]
= new AttributeNode(prefixMap.expandAttributeName(domAttribute.getNodeName(),
null),
domAttribute,
this,
// attributes occur before children
i - len - 1);
}
catch (XSLException e) { }
}
}
}
}
}
public byte getType() {
return ELEMENT;
}
public Name getName() {
if (name == null) {
String qName = domNode.getNodeName();
try {
name = prefixMap.expandElementTypeName(qName, null);
}
catch (XSLException e) {
name = root.nameTable.createName(qName);
}
}
return name;
}
public String getAttributeValue(Name name) {
if (attributeNodes == null)
return null;
for (int i = 0; i < attributeNodes.length; i++)
if (attributeNodes[i] != null && name.equals(attributeNodes[i].name))
return attributeNodes[i].getData();
return null;
}
public Node getAttribute(Name name) {
if (attributeNodes == null)
return null;
for (int i = 0; i < attributeNodes.length; i++)
if (attributeNodes[i] != null && name.equals(attributeNodes[i].name))
return attributeNodes[i];
return null;
}
class AttributesIterator implements SafeNodeIterator {
int i = 0;
public Node next() {
while (i < attributeNodes.length) {
Node attributeNode = attributeNodes[i++];
if (attributeNode != null)
return attributeNode;
}
return null;
}
}
public SafeNodeIterator getAttributes() {
if (attributeNodes == null)
return NullNodeIterator.getInstance();
return new AttributesIterator();
}
public boolean getPreserveSpace() {
return preserveSpace || !root.loadContext.getStripSource(getName());
}
}
|