List of usage examples for org.w3c.dom Attr getNamespaceURI
public String getNamespaceURI();
null
if it is unspecified (see ). From source file:Utils.java
public static void copyAttributes(Element from, Element to) { NamedNodeMap attributes = from.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr node = (Attr) attributes.item(i); to.setAttributeNS(node.getNamespaceURI(), node.getName(), node.getValue()); }/* w w w .j a va 2 s .co m*/ }
From source file:Main.java
private static void convertAttribute(Attr toCopy, Element saveTo, Document doc) { if (toCopy.getNamespaceURI() == null || !toCopy.getNamespaceURI().equals(xmlnsURI)) { saveTo.setAttributeNS(toCopy.getNamespaceURI(), toCopy.getNodeName(), toCopy.getValue()); }/* w w w.j a v a2 s . c om*/ }
From source file:Main.java
private static void fixupAttrsSingle(Element e) throws DOMException { removeXmlBase(e);/*from www.j a va 2s . 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:Main.java
public static String getAttrvalue(Node item, String name, boolean ignoreNs) { NamedNodeMap attributes = item.getAttributes(); int numAttrs = attributes.getLength(); for (int i = 0; i < numAttrs; i++) { Attr attr = (Attr) attributes.item(i); String attrName = attr.getNodeName(); String NSName = attr.getNamespaceURI(); if (ignoreNs) { if ((attrName.indexOf(":" + name) != -1) || (name.equals(attrName))) return attr.getNodeValue(); } else {/*from w w w . ja va 2s.c o m*/ if (name.equals(attrName)) { return attr.getNodeValue(); } } } return null; }
From source file:Main.java
public static String getNamespaceUriDeclaration(Element ele) { NamedNodeMap attribs = ele.getAttributes(); for (int i = 0; i < attribs.getLength(); i++) { Attr attr = (Attr) attribs.item(i); if ("xmlns".equals(attr.getLocalName()) || XMLConstants.XML_NS_URI.equals(attr.getNamespaceURI())) { return attr.getTextContent(); }/*from w ww . ja v a 2s .c om*/ } return ""; }
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/* w w w. j av a 2 s . com*/ attr2 = (Attr) attributes2.getNamedItem(attr1.getName()); if (attr2 == null || !equals(attr1.getTextContent(), attr2.getTextContent())) return false; } return true; }
From source file:Main.java
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintStream</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * /*from w w w . j a v a2 s . com*/ * @param out The <CODE>PrintStream</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(Node) * @see #dump(PrintStream, Node) * @since TFP 1.0 */ private static void doDump(PrintStream out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }
From source file:Main.java
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintWriter</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * //from w w w . j a v a 2 s .c om * @param out The <CODE>PrintWriter</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(PrintWriter, Node) * @since TFP 1.0 */ private static void doDump(PrintWriter out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }
From source file:DOMUtils.java
public static void compareNodes(Node expected, Node actual) throws Exception { if (expected.getNodeType() != actual.getNodeType()) { throw new Exception("Different types of nodes: " + expected + " " + actual); }//from ww w . j a v a2 s .co m if (expected instanceof Document) { Document expectedDoc = (Document) expected; Document actualDoc = (Document) actual; compareNodes(expectedDoc.getDocumentElement(), actualDoc.getDocumentElement()); } else if (expected instanceof Element) { Element expectedElement = (Element) expected; Element actualElement = (Element) actual; // compare element names if (!expectedElement.getLocalName().equals(actualElement.getLocalName())) { throw new Exception("Element names do not match: " + expectedElement.getLocalName() + " " + actualElement.getLocalName()); } // compare element ns String expectedNS = expectedElement.getNamespaceURI(); String actualNS = actualElement.getNamespaceURI(); if ((expectedNS == null && actualNS != null) || (expectedNS != null && !expectedNS.equals(actualNS))) { throw new Exception("Element namespaces names do not match: " + expectedNS + " " + actualNS); } String elementName = "{" + expectedElement.getNamespaceURI() + "}" + actualElement.getLocalName(); // compare attributes NamedNodeMap expectedAttrs = expectedElement.getAttributes(); NamedNodeMap actualAttrs = actualElement.getAttributes(); if (countNonNamespaceAttribures(expectedAttrs) != countNonNamespaceAttribures(actualAttrs)) { throw new Exception(elementName + ": Number of attributes do not match up: " + countNonNamespaceAttribures(expectedAttrs) + " " + countNonNamespaceAttribures(actualAttrs)); } for (int i = 0; i < expectedAttrs.getLength(); i++) { Attr expectedAttr = (Attr) expectedAttrs.item(i); if (expectedAttr.getName().startsWith("xmlns")) { continue; } Attr actualAttr = null; if (expectedAttr.getNamespaceURI() == null) { actualAttr = (Attr) actualAttrs.getNamedItem(expectedAttr.getName()); } else { actualAttr = (Attr) actualAttrs.getNamedItemNS(expectedAttr.getNamespaceURI(), expectedAttr.getLocalName()); } if (actualAttr == null) { throw new Exception(elementName + ": No attribute found:" + expectedAttr); } if (!expectedAttr.getValue().equals(actualAttr.getValue())) { throw new Exception(elementName + ": Attribute values do not match: " + expectedAttr.getValue() + " " + actualAttr.getValue()); } } // compare children NodeList expectedChildren = expectedElement.getChildNodes(); NodeList actualChildren = actualElement.getChildNodes(); if (expectedChildren.getLength() != actualChildren.getLength()) { throw new Exception(elementName + ": Number of children do not match up: " + expectedChildren.getLength() + " " + actualChildren.getLength()); } for (int i = 0; i < expectedChildren.getLength(); i++) { Node expectedChild = expectedChildren.item(i); Node actualChild = actualChildren.item(i); compareNodes(expectedChild, actualChild); } } else if (expected instanceof Text) { String expectedData = ((Text) expected).getData().trim(); String actualData = ((Text) actual).getData().trim(); if (!expectedData.equals(actualData)) { throw new Exception("Text does not match: " + expectedData + " " + actualData); } } }
From source file:com.gargoylesoftware.htmlunit.xml.XmlUtil.java
private static DomNode createFrom(final SgmlPage page, final Node source, final boolean handleXHTMLAsHTML) { if (source.getNodeType() == Node.TEXT_NODE) { return new DomText(page, source.getNodeValue()); }// w w w. ja v a 2 s. c om if (source.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { return new DomProcessingInstruction(page, source.getNodeName(), source.getNodeValue()); } if (source.getNodeType() == Node.COMMENT_NODE) { return new DomComment(page, source.getNodeValue()); } if (source.getNodeType() == Node.DOCUMENT_TYPE_NODE) { final DocumentType documentType = (DocumentType) source; return new DomDocumentType(page, documentType.getName(), documentType.getPublicId(), documentType.getSystemId()); } final String ns = source.getNamespaceURI(); String localName = source.getLocalName(); if (handleXHTMLAsHTML && HTMLParser.XHTML_NAMESPACE.equals(ns)) { final ElementFactory factory = HTMLParser.getFactory(localName); return factory.createElementNS(page, ns, localName, namedNodeMapToSaxAttributes(source.getAttributes())); } final NamedNodeMap nodeAttributes = source.getAttributes(); if (page != null && page.isHtmlPage()) { localName = localName.toUpperCase(Locale.ROOT); } final String qualifiedName; if (source.getPrefix() == null) { qualifiedName = localName; } else { qualifiedName = source.getPrefix() + ':' + localName; } final String namespaceURI = source.getNamespaceURI(); if (HTMLParser.SVG_NAMESPACE.equals(namespaceURI)) { return HTMLParser.SVG_FACTORY.createElementNS(page, namespaceURI, qualifiedName, namedNodeMapToSaxAttributes(nodeAttributes)); } final Map<String, DomAttr> attributes = new LinkedHashMap<>(); for (int i = 0; i < nodeAttributes.getLength(); i++) { final Attr attribute = (Attr) nodeAttributes.item(i); final String attributeNamespaceURI = attribute.getNamespaceURI(); final String attributeQualifiedName; if (attribute.getPrefix() != null) { attributeQualifiedName = attribute.getPrefix() + ':' + attribute.getLocalName(); } else { attributeQualifiedName = attribute.getLocalName(); } final String value = attribute.getNodeValue(); final boolean specified = attribute.getSpecified(); final DomAttr xmlAttribute = new DomAttr(page, attributeNamespaceURI, attributeQualifiedName, value, specified); attributes.put(attribute.getNodeName(), xmlAttribute); } return new DomElement(namespaceURI, qualifiedName, page, attributes); }