/** * Get n'th attribute (DOM NamedNodeMap method). In this implementation we number the attributes * as follows: 0 - the xmlns:xml namespace declaration 1-n further namespace declarations n+1... * "real" attribute declarations */ public Node item(int index) { if (index < 0) { return null; } if (index == 0) { NamespaceIterator.NamespaceNodeImpl nn = new NamespaceIterator.NamespaceNodeImpl(parent, NamespaceConstant.XML_NAMESPACE_CODE, 0); return NodeOverNodeInfo.wrap(nn); } int nscount = getNumberOfNamespaces(); if (index < nscount) { int[] buffer = new int[8]; int[] nsList = parent.getDeclaredNamespaces(buffer); int nscode = nsList[index - 1]; NamespaceIterator.NamespaceNodeImpl nn = new NamespaceIterator.NamespaceNodeImpl(parent, nscode, index); return NodeOverNodeInfo.wrap(nn); } int pos = 0; int attNr = (index - nscount); AxisIterator atts = parent.iterateAxis(Axis.ATTRIBUTE); while (true) { NodeInfo att = (NodeInfo) atts.next(); if (att == null) { return null; } if (pos == attNr) { return NodeOverNodeInfo.wrap(att); } pos++; } }
/** Get named attribute (DOM NamedNodeMap method) */ public Node getNamedItem(String name) { if (name.equals("xmlns")) { int[] nsarray = parent.getDeclaredNamespaces(null); for (int i = 0; i < nsarray.length; i++) { if (nsarray[i] == -1) { return null; } else if (((nsarray[i] >> 16) & 0xffff) == 0) { NamespaceIterator.NamespaceNodeImpl nn = new NamespaceIterator.NamespaceNodeImpl(parent, nsarray[i], i + 1); return NodeOverNodeInfo.wrap(nn); } } return null; } else if (name.startsWith("xmlns:")) { String prefix = name.substring(6); if (prefix.equals("xml")) { NamespaceIterator.NamespaceNodeImpl nn = new NamespaceIterator.NamespaceNodeImpl(parent, NamespaceConstant.XML_CODE, 0); return NodeOverNodeInfo.wrap(nn); } int[] buffer = new int[8]; int[] nsarray = parent.getDeclaredNamespaces(buffer); for (int i = 0; i < nsarray.length; i++) { if (nsarray[i] == -1) { return null; } else if (prefix.equals(parent.getNamePool().getPrefixFromNamespaceCode(nsarray[i]))) { NamespaceIterator.NamespaceNodeImpl nn = new NamespaceIterator.NamespaceNodeImpl(parent, nsarray[i], i + 1); return NodeOverNodeInfo.wrap(nn); } } return null; } else { AxisIterator atts = parent.iterateAxis(Axis.ATTRIBUTE); while (true) { NodeInfo att = (NodeInfo) atts.next(); if (att == null) { return null; } if (name.equals(att.getDisplayName())) { return NodeOverNodeInfo.wrap(att); } } } }
/** Get the number of declared namespaces */ private int getNumberOfNamespaces() { if (numberOfNamespaces == -1) { int[] buffer = new int[8]; int[] nsList = parent.getDeclaredNamespaces(buffer); int count = nsList.length; for (int i = 0; i < count; i++) { if (nsList[i] == -1) { count = i; break; } } numberOfNamespaces = count + 1; // +1 for the XML namespace } return numberOfNamespaces; }