List of usage examples for org.w3c.dom NamedNodeMap item
public Node item(int index);
index
th item in the map. From source file:Main.java
/** * Convenience method to copy the contents of the old {@link Node} into the * new one./* w ww .j av a 2 s. com*/ * * @param newDoc * @param newNode * @param oldParent */ public static void copyContents(Document newDoc, Node newNode, Node oldNode) { // FIXME we should be able to achieve this with much less code (e.g. // the code commented out) but for some reason there are // incompatibility issues being spat out by the tests. // final NodeList childNodes = oldNode.getChildNodes(); // for (int i = 0; i < childNodes.getLength(); i++) { // final Node child = newDoc.importNode(childNodes.item(i), true); // newNode.appendChild(child); // } final NodeList childs = oldNode.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) { final Node child = childs.item(i); Element newElem = null; switch (child.getNodeType()) { case Node.ELEMENT_NODE: //Get all the attributes of an element in a map final NamedNodeMap attrs = child.getAttributes(); // Process each attribute newElem = newDoc.createElement(child.getNodeName()); for (int j = 0; j < attrs.getLength(); j++) { final Attr attr = (Attr) attrs.item(j); // add attribute name and value to the new element newElem.setAttribute(attr.getNodeName(), attr.getNodeValue()); } newNode.appendChild(newElem); break; case Node.TEXT_NODE: newNode.appendChild(newDoc.createTextNode(getString(child))); } copyContents(newDoc, newElem, child); } }
From source file:Main.java
/** * Returns a map of the passed node's attributes * @param node The nopde to get an attribute map for * @return a [possibly empty] map of the node's attributes *//* w ww . j av a 2 s .co m*/ public static Map<String, String> getAttributeMap(final Node node) { if (node == null) throw new IllegalArgumentException("The passed node was null"); final NamedNodeMap nnm = node.getAttributes(); final int size = nnm.getLength(); if (size == 0) return Collections.emptyMap(); final Map<String, String> map = new LinkedHashMap<String, String>(size); for (int i = 0; i < size; i++) { final Attr attr = (Attr) nnm.item(i); map.put(attr.getName(), attr.getValue()); } return map; }
From source file:Main.java
public static List<Attr> attributes(final Element element) { final NamedNodeMap attributeMap = element.getAttributes(); if (attributeMap == null || attributeMap.getLength() == 0) { return null; }/*from w w w. j a v a 2 s . c om*/ final List<Attr> attributes = new ArrayList<Attr>(); for (int i = 0; i < attributeMap.getLength(); i++) { attributes.add((Attr) attributeMap.item(i)); } return attributes; }
From source file:com.evolveum.midpoint.prism.util.PrismUtil.java
private static void fortifyNamespaceDeclarations(Element definitionElement, Element childElement) { Document doc = definitionElement.getOwnerDocument(); NamedNodeMap attributes = childElement.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (DOMUtil.isNamespaceDefinition(attr)) { String prefix = DOMUtil.getNamespaceDeclarationPrefix(attr); String namespace = DOMUtil.getNamespaceDeclarationNamespace(attr); Element namespaceElement = doc.createElementNS(PrismConstants.A_NAMESPACE.getNamespaceURI(), PrismConstants.A_NAMESPACE.getLocalPart()); namespaceElement.setAttribute(PrismConstants.A_NAMESPACE_PREFIX, prefix); namespaceElement.setAttribute(PrismConstants.A_NAMESPACE_URL, namespace); definitionElement.insertBefore(namespaceElement, childElement); }/*from w w w . j a v a2 s.c om*/ } }
From source file:Main.java
/** * Copies the source tree into the specified place in a destination * tree. The source node and its children are appended as children * of the destination node.//www . j a v a2s . c om * <p> * <em>Note:</em> This is an iterative implementation. */ public static void copyInto(Node src, Node dest) throws DOMException { // get node factory Document factory = dest.getOwnerDocument(); boolean domimpl = factory instanceof DocumentImpl; // placement variables Node start = src; Node parent = src; Node place = src; // traverse source tree while (place != null) { // copy this node Node node = null; int type = place.getNodeType(); switch (type) { case Node.CDATA_SECTION_NODE: { node = factory.createCDATASection(place.getNodeValue()); break; } case Node.COMMENT_NODE: { node = factory.createComment(place.getNodeValue()); break; } case Node.ELEMENT_NODE: { Element element = factory.createElement(place.getNodeName()); node = element; NamedNodeMap attrs = place.getAttributes(); int attrCount = attrs.getLength(); for (int i = 0; i < attrCount; i++) { Attr attr = (Attr) attrs.item(i); String attrName = attr.getNodeName(); String attrValue = attr.getNodeValue(); element.setAttribute(attrName, attrValue); if (domimpl && !attr.getSpecified()) { ((AttrImpl) element.getAttributeNode(attrName)).setSpecified(false); } } break; } case Node.ENTITY_REFERENCE_NODE: { node = factory.createEntityReference(place.getNodeName()); break; } case Node.PROCESSING_INSTRUCTION_NODE: { node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue()); break; } case Node.TEXT_NODE: { node = factory.createTextNode(place.getNodeValue()); break; } default: { throw new IllegalArgumentException( "can't copy node type, " + type + " (" + node.getNodeName() + ')'); } } dest.appendChild(node); // iterate over children if (place.hasChildNodes()) { parent = place; place = place.getFirstChild(); dest = node; } // advance else { place = place.getNextSibling(); while (place == null && parent != start) { place = parent.getNextSibling(); parent = parent.getParentNode(); dest = dest.getParentNode(); } } } }
From source file:Main.java
/** * This method is a tree-search to help prevent against wrapping attacks. It checks that no other * Element than the given "knownElement" argument has an ID attribute that matches the "value" * argument, which is the ID value of "knownElement". If this is the case then "false" is returned. *//*w ww. j a v a2s .c o m*/ public static boolean protectAgainstWrappingAttack(Node startNode, Element knownElement, String value) { Node startParent = startNode.getParentNode(); Node processedNode = null; String id = value.trim(); if (id.charAt(0) == '#') { id = id.substring(1); } while (startNode != null) { if (startNode.getNodeType() == Node.ELEMENT_NODE) { Element se = (Element) startNode; NamedNodeMap attributes = se.getAttributes(); if (attributes != null) { for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (attr.isId() && id.equals(attr.getValue()) && se != knownElement) { //log.debug("Multiple elements with the same 'Id' attribute value!"); return false; } } } } processedNode = startNode; startNode = startNode.getFirstChild(); // no child, this node is done. if (startNode == null) { // close node processing, get sibling startNode = processedNode.getNextSibling(); } // no more siblings, get parent, all children // of parent are processed. while (startNode == null) { processedNode = processedNode.getParentNode(); if (processedNode == startParent) { return true; } // close parent node processing (processed node now) startNode = processedNode.getNextSibling(); } } return true; }
From source file:Main.java
public static List<Attr> attributes(Element element) { NamedNodeMap attributeMap = element.getAttributes(); if ((attributeMap == null) || (attributeMap.getLength() == 0)) { return Collections.emptyList(); }//from w w w.j a v a 2 s . c o m List<Attr> attributes = new ArrayList<Attr>(); for (int i = 0; i < attributeMap.getLength(); i++) { attributes.add((Attr) attributeMap.item(i)); } return attributes; }
From source file:Main.java
/** * This method is a tree-search to help prevent against wrapping attacks. It checks that no * two Elements have ID Attributes that match the "value" argument, if this is the case then * "false" is returned. Note that a return value of "true" does not necessarily mean that * a matching Element has been found, just that no wrapping attack has been detected. *///from ww w. ja v a 2 s. c o m public static boolean protectAgainstWrappingAttack(Node startNode, String value) { Node startParent = startNode.getParentNode(); Node processedNode = null; Element foundElement = null; String id = value.trim(); if (id.charAt(0) == '#') { id = id.substring(1); } while (startNode != null) { if (startNode.getNodeType() == Node.ELEMENT_NODE) { Element se = (Element) startNode; NamedNodeMap attributes = se.getAttributes(); if (attributes != null) { for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (attr.isId() && id.equals(attr.getValue())) { if (foundElement == null) { // Continue searching to find duplicates foundElement = attr.getOwnerElement(); } else { //log.debug("Multiple elements with the same 'Id' attribute value!"); return false; } } } } } processedNode = startNode; startNode = startNode.getFirstChild(); // no child, this node is done. if (startNode == null) { // close node processing, get sibling startNode = processedNode.getNextSibling(); } // no more siblings, get parent, all children // of parent are processed. while (startNode == null) { processedNode = processedNode.getParentNode(); if (processedNode == startParent) { return true; } // close parent node processing (processed node now) startNode = processedNode.getNextSibling(); } } return true; }
From source file:Main.java
public static HashMap<String, String> getAttributes(Node p_node) throws Exception { NamedNodeMap l_nnm = null; Node l_n = null;/*from www . j av a2 s.com*/ String l_an = null; String l_av = null; HashMap<String, String> l_a = new HashMap<String, String>(); l_nnm = p_node.getAttributes(); if (l_nnm != null) { for (int l_i = 0; l_i < l_nnm.getLength(); l_i++) { l_n = l_nnm.item(l_i); l_an = l_n.getNodeName(); l_av = l_n.getNodeValue(); l_a.put(l_an, l_av); } } return l_a; }
From source file:Main.java
static boolean canBeMerged(Node node1, Node node2, String requiredTagName) { if (node1.getNodeType() != Node.ELEMENT_NODE || node2.getNodeType() != Node.ELEMENT_NODE) return false; Element element1 = (Element) node1; Element element2 = (Element) node2; if (!equals(requiredTagName, element1.getTagName()) || !equals(requiredTagName, element2.getTagName())) return false; NamedNodeMap attributes1 = element1.getAttributes(); NamedNodeMap attributes2 = element2.getAttributes(); if (attributes1.getLength() != attributes2.getLength()) return false; for (int i = 0; i < attributes1.getLength(); i++) { final Attr attr1 = (Attr) attributes1.item(i); final Attr attr2; if (isNotEmpty(attr1.getNamespaceURI())) attr2 = (Attr) attributes2.getNamedItemNS(attr1.getNamespaceURI(), attr1.getLocalName()); else// ww w. j a va2s . co m attr2 = (Attr) attributes2.getNamedItem(attr1.getName()); if (attr2 == null || !equals(attr1.getTextContent(), attr2.getTextContent())) return false; } return true; }