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:org.apache.ode.bpel.rtrep.v2.ASSIGN.java
private Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) { Document doc = ptr.getOwnerDocument(); Node parent = ptr.getParentNode(); if (keepSrcElement) { Element replacement = (Element) doc.importNode(src, true); parent.replaceChild(replacement, ptr); return (lval == ptr) ? replacement : lval; }//from w w w. j a v a 2s . c om Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getLocalName()); if (ptr.getPrefix() != null) { replacement.setPrefix(ptr.getPrefix()); } NodeList nl = src.getChildNodes(); for (int i = 0; i < nl.getLength(); ++i) replacement.appendChild(doc.importNode(nl.item(i), true)); NamedNodeMap attrs = src.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { Attr attr = (Attr) attrs.item(i); if (!attr.getName().startsWith("xmlns")) { replacement.setAttributeNodeNS((Attr) doc.importNode(attrs.item(i), true)); // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually int colonIdx = attr.getValue().indexOf(":"); if (colonIdx > 0) { String prefix = attr.getValue().substring(0, colonIdx); String attrValNs = src.lookupPrefix(prefix); if (attrValNs != null) replacement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs); } } } parent.replaceChild(replacement, ptr); DOMUtils.copyNSContext(ptr, replacement); return (lval == ptr) ? replacement : lval; }
From source file:org.apache.ode.bpel.rtrep.v2.AssignHelper.java
/** * madars.vitolins _at gmail.com - 2009.04.17 - moved from ASSIGN here *///from w w w.j a v a2 s .c o m public Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) { Document doc = ptr.getOwnerDocument(); Node parent = ptr.getParentNode(); if (keepSrcElement) { Element replacement = (Element) doc.importNode(src, true); parent.replaceChild(replacement, ptr); return (lval == ptr) ? replacement : lval; } Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getLocalName()); NodeList nl = src.getChildNodes(); for (int i = 0; i < nl.getLength(); ++i) replacement.appendChild(doc.importNode(nl.item(i), true)); NamedNodeMap attrs = src.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { Attr attr = (Attr) attrs.item(i); if (!attr.getName().startsWith("xmlns")) { replacement.setAttributeNodeNS((Attr) doc.importNode(attrs.item(i), true)); // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually int colonIdx = attr.getValue().indexOf(":"); if (colonIdx > 0) { String prefix = attr.getValue().substring(0, colonIdx); String attrValNs = src.lookupPrefix(prefix); if (attrValNs != null) replacement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs); } } } parent.replaceChild(replacement, ptr); DOMUtils.copyNSContext(ptr, replacement); return (lval == ptr) ? replacement : lval; }
From source file:org.apache.ode.bpel.runtime.ASSIGN.java
private void copyAttributes(Document doc, Element original, Element replacement) { NamedNodeMap attrs = original.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { Attr attr = (Attr) attrs.item(i); replacement.setAttributeNodeNS((Attr) doc.importNode(attr, true)); }//from ww w.j av a2s . co m }
From source file:org.apache.ode.bpel.runtime.AssignHelper.java
private Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) { Document doc = ptr.getOwnerDocument(); Node parent = ptr.getParentNode(); if (keepSrcElement) { Element replacement = (Element) doc.importNode(src, true); parent.replaceChild(replacement, ptr); return (lval == ptr) ? replacement : lval; }/*from w w w . j a v a2 s . co m*/ Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getTagName()); NodeList nl = src.getChildNodes(); for (int i = 0; i < nl.getLength(); ++i) replacement.appendChild(doc.importNode(nl.item(i), true)); NamedNodeMap attrs = src.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { Attr attr = (Attr) attrs.item(i); if (!attr.getName().startsWith("xmlns")) { replacement.setAttributeNodeNS((Attr) doc.importNode(attrs.item(i), true)); // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually int colonIdx = attr.getValue().indexOf(":"); if (colonIdx > 0) { String prefix = attr.getValue().substring(0, colonIdx); String attrValNs = src.lookupPrefix(prefix); if (attrValNs != null) replacement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs); } } } parent.replaceChild(replacement, ptr); DOMUtils.copyNSContext(ptr, replacement); return (lval == ptr) ? replacement : lval; }
From source file:org.apache.ode.il.OMUtils.java
public static OMElement toOM(Element src, OMFactory omf, OMContainer parent) { OMElement omElement = parent == null ? omf.createOMElement(src.getLocalName(), null) : omf.createOMElement(src.getLocalName(), null, parent); if (src.getNamespaceURI() != null) { if (src.getPrefix() != null) omElement.setNamespace(omf.createOMNamespace(src.getNamespaceURI(), src.getPrefix())); else/*from w ww . ja v a 2 s .co m*/ omElement.declareDefaultNamespace(src.getNamespaceURI()); } if (parent == null) { NSContext nscontext = DOMUtils.getMyNSContext(src); injectNamespaces(omElement, nscontext.toMap()); } else { Map<String, String> nss = DOMUtils.getMyNamespaces(src); injectNamespaces(omElement, nss); } NamedNodeMap attrs = src.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { Attr attr = (Attr) attrs.item(i); if (attr.getLocalName().equals("xmlns") || (attr.getNamespaceURI() != null && attr.getNamespaceURI().equals(DOMUtils.NS_URI_XMLNS))) continue; OMNamespace attrOmNs = null; String attrNs = attr.getNamespaceURI(); String attrPrefix = attr.getPrefix(); if (attrNs != null) attrOmNs = omElement.findNamespace(attrNs, null); if (attrOmNs == null && attrPrefix != null) attrOmNs = omElement.findNamespace(null, attrPrefix); omElement.addAttribute(attr.getLocalName(), attr.getValue(), attrOmNs); } NodeList children = src.getChildNodes(); for (int i = 0; i < children.getLength(); ++i) { Node n = children.item(i); switch (n.getNodeType()) { case Node.CDATA_SECTION_NODE: omElement.addChild(omf.createOMText(((CDATASection) n).getTextContent(), XMLStreamConstants.CDATA)); break; case Node.TEXT_NODE: omElement.addChild(omf.createOMText(((Text) n).getTextContent(), XMLStreamConstants.CHARACTERS)); break; case Node.ELEMENT_NODE: toOM((Element) n, omf, omElement); break; } } return omElement; }
From source file:org.apache.ode.utils.DOMUtils.java
/** * This method traverses the DOM and grabs namespace declarations * on parent elements with the intent of preserving them for children. <em>Note * that the DOM level 3 document method {@link Element#getAttribute(java.lang.String)} * is not desirable in this case, as it does not respect namespace prefix * bindings that may affect attribute values. (Namespaces in DOM are * uncategorically a mess, especially in the context of XML Schema.)</em> * @param el the starting element//from w ww .j av a2 s . c o m * @return a {@link Map} containing prefix bindings. */ public static Map<String, String> getParentNamespaces(Element el) { HashMap<String, String> pref = new HashMap<String, String>(); Map<String, String> mine = getMyNamespaces(el); Node n = el.getParentNode(); while (n != null && n.getNodeType() != Node.DOCUMENT_NODE) { if (n instanceof Element) { Element l = (Element) n; NamedNodeMap nnm = l.getAttributes(); int len = nnm.getLength(); for (int i = 0; i < len; ++i) { Attr a = (Attr) nnm.item(i); if (isNSAttribute(a)) { String key = getNSPrefixFromNSAttr(a); String uri = a.getValue(); // prefer prefix bindings that are lower down in the tree. if (pref.containsKey(key) || mine.containsKey(key)) continue; pref.put(key, uri); } } } n = n.getParentNode(); } return pref; }
From source file:org.apache.ode.utils.DOMUtils.java
public static Map<String, String> getMyNamespaces(Element el) { HashMap<String, String> mine = new HashMap<String, String>(); NamedNodeMap nnm = el.getAttributes(); int len = nnm.getLength(); for (int i = 0; i < len; ++i) { Attr a = (Attr) nnm.item(i); if (isNSAttribute(a)) { mine.put(getNSPrefixFromNSAttr(a), a.getValue()); }//w w w. j a va2 s . co m } return mine; }
From source file:org.apache.ode.utils.DOMUtils.java
/** * Drop the attributes from an element, except possibly an <code>xmlns</code> * attribute that declares its namespace. * @param target the element whose attributes will be removed. * @param flag preserve namespace declaration *//*from ww w.j a v a 2s.c om*/ public static void removeAttributes(Element target, boolean flag) { if (!target.hasAttributes()) { return; } String prefix = target.getPrefix(); NamedNodeMap nnm = target.getAttributes(); Attr toPutBack = null; if (flag) { if (prefix == null) { toPutBack = target.getAttributeNodeNS(NS_URI_XMLNS, "xmlns"); } else { toPutBack = target.getAttributeNodeNS(NS_URI_XMLNS, "xmlns:" + prefix); } } while (nnm.getLength() != 0) { target.removeAttributeNode((Attr) nnm.item(0)); } if (toPutBack != null) { target.setAttributeNodeNS(toPutBack); } }
From source file:org.apache.olingo.client.core.serialization.ClientODataDeserializerImpl.java
private List<List<String>> getAllSchemaNameSpace(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException { List<List<String>> schemaNameSpaces = new ArrayList<List<String>>(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setFeature("http://xml.org/sax/features/namespaces", true); dbFactory.setFeature("http://apache.org/xml/features/validation/schema", false); dbFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); dbFactory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputStream); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName(SCHEMA); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); List<String> nameSpaces = new ArrayList<String>(); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; NamedNodeMap attributes = eElement.getAttributes(); int len = attributes.getLength(); for (int i = 0; i < len; i++) { // check for all atributes begining with name xmlns or xmlns: String attrName = attributes.item(i).getNodeName(); if (XMLNS.equals(attrName) || attrName.startsWith(XMLNS + ":")) { nameSpaces.add(attributes.item(i).getNodeValue()); }/*from www .j a v a2 s .c o m*/ } } schemaNameSpaces.add(nameSpaces); } return schemaNameSpaces; }
From source file:org.apache.openejb.server.axis.assembler.CommonsSchemaInfoBuilder.java
private static String getNamespaceForPrefix(String prefix, Element element) { NamedNodeMap attributes = element.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node node = attributes.item(i); if (node instanceof Attr) { Attr attr = (Attr) node; if (XML_NS_NS.equals(attr.getNamespaceURI())) { // this is a namespace declaration, is it the one we are looking for? if (attr.getLocalName().equals(prefix)) { return attr.getValue(); }//w w w. ja v a2 s . c om } } } // try parent if (element.getParentNode() instanceof Element) { return getNamespaceForPrefix(prefix, (Element) element.getParentNode()); } // didn't find it - just use prefix as the namespace return prefix; }