List of usage examples for org.w3c.dom Element getAttributes
public NamedNodeMap getAttributes();
NamedNodeMap
containing the attributes of this node (if it is an Element
) or null
otherwise. From source file:com.evolveum.midpoint.util.UglyHacks.java
public static void fortifyNamespaceDeclarationsSingleElement(Element element) { List<String> xmlnss = new ArrayList<String>(); NamedNodeMap attributes = element.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); xmlnss.add(prefix + "=" + namespace); }/* w w w .j a v a 2 s . c om*/ } String fortifiedXmlnss = StringUtils.join(xmlnss, FORTIFIED_NAMESPACE_DECLARATIONS_SEPARATOR); element.setAttribute(FORTIFIED_NAMESPACE_DECLARATIONS_ELEMENT_NAME, fortifiedXmlnss); }
From source file:DOMCopy.java
private static void outputElement(Element node, String indent) { System.out.print(indent + "<" + node.getTagName()); NamedNodeMap nm = node.getAttributes(); for (int i = 0; i < nm.getLength(); i++) { Attr attr = (Attr) nm.item(i); System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\""); }/*from w ww.j a va2 s . c om*/ System.out.println(">"); NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) outputloop(list.item(i), indent + TAB); System.out.println(indent + "</" + node.getTagName() + ">"); }
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 w w. j a v a2 s . 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
private static void fixupAttrsSingle(Element e) throws DOMException { removeXmlBase(e);/* w w w . j av a2 s.co m*/ Map<String, String> replace = new HashMap<String, String>(); NamedNodeMap attrs = e.getAttributes(); for (int j = 0; j < attrs.getLength(); j++) { Attr attr = (Attr) attrs.item(j); if (attr.getNamespaceURI() == null && !attr.getName().equals("xmlns")) { // NOI18N replace.put(attr.getName(), attr.getValue()); } } for (Map.Entry<String, String> entry : replace.entrySet()) { e.removeAttribute(entry.getKey()); e.setAttributeNS(null, entry.getKey(), entry.getValue()); } }
From source file:Utils.java
/** * Starting from a node, find the namespace declaration for a prefix. for a matching namespace * declaration.//from ww w . j av a 2 s. c o m * * @param node search up from here to search for namespace definitions * @param searchPrefix the prefix we are searching for * @return the namespace if found. */ public static String getNamespace(Node node, String searchPrefix) { Element el; while (!(node instanceof Element)) { node = node.getParentNode(); } el = (Element) node; NamedNodeMap atts = el.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { Node currentAttribute = atts.item(i); String currentLocalName = currentAttribute.getLocalName(); String currentPrefix = currentAttribute.getPrefix(); if (searchPrefix.equals(currentLocalName) && XMLNAMESPACE.equals(currentPrefix)) { return currentAttribute.getNodeValue(); } else if (isEmpty(searchPrefix) && XMLNAMESPACE.equals(currentLocalName) && isEmpty(currentPrefix)) { return currentAttribute.getNodeValue(); } } Node parent = el.getParentNode(); if (parent instanceof Element) { return getNamespace((Element) parent, searchPrefix); } return null; }
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 w w w . j a v a2 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
/** * Copy elements from one document to another attaching at the specified * element and translating the namespace. * * @param from copy the children of this element (exclusive) * @param to where to attach the copied elements * @param newNamespace destination namespace * * @since 8.4/*ww w.jav a 2 s . c om*/ */ public static void copyDocument(Element from, Element to, String newNamespace) { Document doc = to.getOwnerDocument(); NodeList nl = from.getChildNodes(); int length = nl.getLength(); for (int i = 0; i < length; i++) { Node node = nl.item(i); Node newNode = null; if (Node.ELEMENT_NODE == node.getNodeType()) { Element oldElement = (Element) node; newNode = doc.createElementNS(newNamespace, oldElement.getTagName()); NamedNodeMap m = oldElement.getAttributes(); Element newElement = (Element) newNode; for (int index = 0; index < m.getLength(); index++) { Node attr = m.item(index); newElement.setAttribute(attr.getNodeName(), attr.getNodeValue()); } copyDocument(oldElement, newElement, newNamespace); } else { newNode = node.cloneNode(true); newNode = to.getOwnerDocument().importNode(newNode, true); } if (newNode != null) { to.appendChild(newNode); } } }
From source file:Main.java
/** * /* ww w. j a va 2 s .c om*/ * @param node * @param classname * @return */ public static Object decode(Element node, String classname) { try { Class classObject = Class.forName(classname); Object object = classObject.newInstance(); NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node child = attributes.item(i); String nodeName = child.getNodeName(); Field field = classObject.getField(nodeName); field.setAccessible(true); field.set(object, child.getNodeValue()); } return object; } catch (ClassNotFoundException e) { e.printStackTrace(); return null; } catch (InstantiationException e) { e.printStackTrace(); return null; } catch (IllegalAccessException e) { e.printStackTrace(); return null; } catch (SecurityException e) { e.printStackTrace(); return null; } catch (NoSuchFieldException e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** * Used for debuging/* www .ja va 2s . c om*/ * * @param parent * Element * @param out * PrintStream * @param deep * boolean * @param prefix * String */ public static void printChildElements(Element parent, PrintStream out, boolean deep, String prefix) { out.print(prefix + "<" + parent.getNodeName()); NamedNodeMap attrs = parent.getAttributes(); Node node; for (int i = 0; i < attrs.getLength(); i++) { node = attrs.item(i); out.print(" " + node.getNodeName() + "=\"" + node.getNodeValue() + "\""); } out.println(">"); // String data = getElementTextValueDeprecated(parent); String data = parent.getNodeValue(); if (data != null && data.trim().length() > 0) { out.println(prefix + "\t" + data); } data = getElementCDataValue(parent); if (data != null && data.trim().length() > 0) { out.println(prefix + "\t<![CDATA[" + data + "]]>"); } NodeList nodes = parent.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (deep) { printChildElements((Element) node, out, deep, prefix + "\t"); } else { out.println(prefix + node.getNodeName()); } } } out.println(prefix + "</" + parent.getNodeName() + ">"); }
From source file:Main.java
/** * Build {@link Map} of namespace URIs to prefixes. * //from w w w.ja v a 2 s .c om * @param root * {@link Element} to get namespaces and prefixes from. * @return {@link Map} of namespace URIs to prefixes. * @since 8.1 */ private static final Map<String, String> buildNamespacePrefixMap(final Element root) { final HashMap<String, String> namespacePrefixMap = new HashMap<>(); //Look for all of the attributes of cache that start with //xmlns NamedNodeMap attributes = root.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node item = attributes.item(i); if (item.getNodeName().startsWith("xmlns")) { //Anything after the colon is the prefix //eg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" //has a prefix of xsi String[] splitName = item.getNodeName().split(":"); String prefix; if (splitName.length > 1) { prefix = splitName[1]; } else { prefix = ""; } String uri = item.getTextContent(); namespacePrefixMap.put(uri, prefix); } } return namespacePrefixMap; }