public String getNodeNamespace() { int nodeType = node.getNodeType(); if (nodeType != Node.ATTRIBUTE_NODE && nodeType != Node.ELEMENT_NODE) { return null; } String result = node.getNamespaceURI(); if (result == null && nodeType == Node.ELEMENT_NODE) { result = ""; } else if ("".equals(result) && nodeType == Node.ATTRIBUTE_NODE) { result = null; } return result; }
/** * @param sibling * @param uri * @param nodeName * @param number * @return nodes with the constrain */ public static Element selectNode(Node sibling, String uri, String nodeName, int number) { while (sibling != null) { if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri) && sibling.getLocalName().equals(nodeName)) { if (number == 0){ return (Element)sibling; } number--; } sibling = sibling.getNextSibling(); } return null; }
/** * Constructor. * * @param parent the parent DOM element for the attributes. */ AttributeIterator (Node parent) { this.map = parent.getAttributes(); this.pos = 0; for (int i = this.map.getLength()-1; i >= 0; i--) { Node node = map.item(i); if (! "http://www.w3.org/2000/xmlns/".equals(node.getNamespaceURI())) { this.lastAttribute = i; break; } } }
/** * Look up the index of an attribute by Namespace name. * * @param uri The Namespace URI, or the empty string if * the name has no Namespace URI. * @param localPart The attribute's local name. * @return The index of the attribute, or -1 if it does not * appear in the list. */ public int getIndex(String uri, String localPart) { for(int i=m_attrs.getLength()-1;i>=0;--i) { Node a=m_attrs.item(i); String u=a.getNamespaceURI(); if( (u==null ? uri==null : u.equals(uri)) && a.getLocalName().equals(localPart) ) return i; } return -1; }
int nodeCount = l.getLength(); for (int i = 0; i < nodeCount; i++) { if (l.item(i).getNodeType() == Node.ELEMENT_NODE) { Node node = l.item(i); String localName = node.getLocalName(); localName = localName == null ? node.getNodeName() : localName; && (namespace == null || namespace.equals(node.getNamespaceURI()))) { if (result == null) { result = (Element)node;
Node p = (root.getNodeType() == Node.ATTRIBUTE_NODE) ? ((org.w3c.dom.Attr)root).getOwnerElement() : root.getParentNode(); for (; p != null; p = p.getParentNode()) handle=dtm.getAttributeNode(handle,node.getNamespaceURI(),node.getLocalName());
/** * Extracts a Node's name, namespace URI (if any) and prefix as a * QName. */ public static QName getQName(Node n) { String s = n.getLocalName(); String p = n.getPrefix(); return s != null ? new QName(n.getNamespaceURI(), s, p != null ? p: XMLConstants.DEFAULT_NS_PREFIX) : new QName(n.getNodeName()); }
@Override void getChildren(Object node, String localName, String namespaceUri, List result) { if ("".equals(namespaceUri)) { namespaceUri = null; } NodeList children = ((Node) node).getChildNodes(); for (int i = 0; i < children.getLength(); ++i) { Node subnode = children.item(i); // IMO, we should get the text nodes as well -- will discuss. if (subnode.getNodeType() == Node.ELEMENT_NODE || subnode.getNodeType() == Node.TEXT_NODE) { if (localName == null || (equal(subnode.getNodeName(), localName) && equal(subnode.getNamespaceURI(), namespaceUri))) { result.add(subnode); } } } }
protected void write(OutputStream out) throws IOException { XmlObject rootObject = XmlObject.Factory.newInstance(); XmlCursor rootCursor = rootObject.newCursor(); rootCursor.toNextToken(); rootCursor.beginElement("xml"); for(int i=0; i < _items.size(); i++){ XmlCursor xc = _items.get(i).newCursor(); rootCursor.beginElement(_qnames.get(i)); while(xc.toNextToken() == XmlCursor.TokenType.ATTR) { Node anode = xc.getDomNode(); rootCursor.insertAttributeWithValue(anode.getLocalName(), anode.getNamespaceURI(), anode.getNodeValue()); } xc.toStartDoc(); xc.copyXmlContents(rootCursor); rootCursor.toNextToken(); xc.dispose(); } rootCursor.dispose(); rootObject.save(out, DEFAULT_XML_OPTIONS); }
private void outputQualifiedName(Node n, StringBuilder buf) { String nsURI = n.getNamespaceURI(); if (nsURI == null || nsURI.length() == 0) { buf.append(n.getNodeName()); } else { String prefix = namespacesToPrefixLookup.get(nsURI); if (prefix == null) { //REVISIT! buf.append(n.getNodeName()); } else { if (prefix.length() > 0) { buf.append(prefix); buf.append(':'); } buf.append(n.getLocalName()); } } }
private static void collectCDATASections(Node node, Set<String> cdataQNames) { if (node instanceof CDATASection) { Node parent = node.getParentNode(); if (parent != null) { String uri = parent.getNamespaceURI(); if (uri != null) { cdataQNames.add("{" + uri + "}" + parent.getNodeName()); //NOI18N } else { cdataQNames.add(parent.getNodeName()); } } } NodeList children = node.getChildNodes(); for(int i = 0; i < children.getLength(); i++) { collectCDATASections(children.item(i), cdataQNames); } }
/** * Look up the index of an attribute by Namespace name. * * @param uri The Namespace URI, or the empty string if * the name has no Namespace URI. * @param localPart The attribute's local name. * @return The index of the attribute, or -1 if it does not * appear in the list. */ public int getIndex(String uri, String localPart) { for(int i=m_attrs.getLength()-1;i>=0;--i) { Node a=m_attrs.item(i); String u=a.getNamespaceURI(); if( (u==null ? uri==null : u.equals(uri)) && a.getLocalName().equals(localPart) ) return i; } return -1; }
values.add(node.getNodeType()); values.add(node.getNodeName()); values.add(node.getLocalName()); values.add(node.getNamespaceURI()); values.add(node.getPrefix()); values.add(node.getNodeValue()); for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { values.add(child); switch (node.getNodeType()) { case DOCUMENT_TYPE_NODE: DocumentTypeImpl doctype = (DocumentTypeImpl) node;
Node p = (root.getNodeType() == Node.ATTRIBUTE_NODE) ? ((org.w3c.dom.Attr)root).getOwnerElement() : root.getParentNode(); for (; p != null; p = p.getParentNode()) handle=dtm.getAttributeNode(handle,node.getNamespaceURI(),node.getLocalName());
protected final void updateQName (Node node, QName qname){ String prefix = node.getPrefix(); String namespace = node.getNamespaceURI(); String localName = node.getLocalName(); // REVISIT: the symbols are added too often: start/endElement // and in the namespaceFixup. Should reduce number of calls to symbol table. qname.prefix = (prefix!=null && prefix.length()!=0)?fSymbolTable.addSymbol(prefix):null; qname.localpart = (localName != null)?fSymbolTable.addSymbol(localName):null; qname.rawname = fSymbolTable.addSymbol(node.getNodeName()); qname.uri = (namespace != null)?fSymbolTable.addSymbol(namespace):null; }
private void parseAttributesForKnownElements(Map<String, List<Entry>> subdirs, Node desc) { // NOTE: NamedNodeMap does not have any particular order... NamedNodeMap attributes = desc.getAttributes(); for (Node attr : asIterable(attributes)) { if (!XMP.ELEMENTS.contains(attr.getNamespaceURI())) { continue; } List<Entry> dir = subdirs.get(attr.getNamespaceURI()); if (dir == null) { dir = new ArrayList<Entry>(); subdirs.put(attr.getNamespaceURI(), dir); } dir.add(new XMPEntry(attr.getNamespaceURI() + attr.getLocalName(), attr.getLocalName(), attr.getNodeValue())); } }
/** * Test if a node is an attribute. <code>xmlns</code> and * <code>xmlns:pre</code> attributes do not count as attributes * for the purposes of XPath. * * @param object the target node * @return true if the node is an attribute, false otherwise */ public boolean isAttribute (Object object) { return (object instanceof Node) && (((Node)object).getNodeType() == Node.ATTRIBUTE_NODE) && ! "http://www.w3.org/2000/xmlns/".equals(((Node) object).getNamespaceURI()); }
@Override String getQualifiedName() { String nsURI = node.getNamespaceURI(); if (nsURI == null || nsURI.equals("")) return node.getNodeName(); Environment env = Environment.getCurrentEnvironment(); String defaultNS = env.getDefaultNS(); String prefix = null; if (nsURI.equals(defaultNS)) { prefix = "D"; } else { prefix = env.getPrefixForNamespace(nsURI); } if (prefix == null) { return null; } return prefix + ":" + node.getLocalName(); } }
/** * @param sibling * @param uri * @param nodeName * @return nodes with the constraint */ public static Element[] selectNodes(Node sibling, String uri, String nodeName) { List<Element> list = new ArrayList<Element>(); while (sibling != null) { if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri) && sibling.getLocalName().equals(nodeName)) { list.add((Element)sibling); } sibling = sibling.getNextSibling(); } return list.toArray(new Element[list.size()]); }
/** * Change the namespace URI of a <code>node</code> and its children to * <code>newNamespaceUri</code> if that node is in the given <code>oldNamespaceUri</code> * namespace URI. * * * @param node {@link Node} to change namespace URI on. * @param oldNamespaceUri old namespace URI to change from. * @param newNamespaceUri new Namespace URI to change to. * @return the modified version of the passed in node. * @since GemFire 8.1 */ static Node changeNamespace(final Node node, final String oldNamespaceUri, final String newNamespaceUri) throws XPathExpressionException { Node result = null; final NodeList nodes = query(node, "//*"); for (int i = 0; i < nodes.getLength(); i++) { final Node element = nodes.item(i); if (element.getNamespaceURI() == null || element.getNamespaceURI().equals(oldNamespaceUri)) { Node renamed = node.getOwnerDocument().renameNode(element, newNamespaceUri, element.getNodeName()); if (element == node) { result = renamed; } } } return result; }