List of usage examples for org.w3c.dom NamedNodeMap getLength
public int getLength();
From source file:de.betterform.xml.xforms.model.constraints.RelevanceSelector.java
private static void addAttributes(Element relevantElement, Node instanceNode) { NamedNodeMap instanceAttributes = instanceNode.getAttributes(); for (int index = 0; index < instanceAttributes.getLength(); index++) { Node instanceAttr = (Node) instanceAttributes.item(index); if (isEnabled(instanceAttr)) { if (instanceAttr.getNamespaceURI() == null) { relevantElement.setAttribute(instanceAttr.getNodeName(), instanceAttr.getNodeValue()); } else { relevantElement.setAttributeNS(instanceAttr.getNamespaceURI(), instanceAttr.getNodeName(), instanceAttr.getNodeValue()); }//from w w w .j a va 2 s . c om } } }
From source file:Main.java
public static void assertEquivalent(Node node, Node node2) { if (node == null) { throw new IllegalArgumentException("the first node to be compared is null"); }/*from ww w . j a v a2 s .c o m*/ if (node2 == null) { throw new IllegalArgumentException("the second node to be compared is null"); } if (!node.getNodeName().equals(node2.getNodeName())) { throw new IllegalArgumentException("nodes have different node names"); } int attrCount = 0; NamedNodeMap attrs = node.getAttributes(); if (attrs != null) { attrCount = attrs.getLength(); } int attrCount2 = 0; NamedNodeMap attrs2 = node2.getAttributes(); if (attrs2 != null) { attrCount2 = attrs2.getLength(); } if (attrCount != attrCount2) { throw new IllegalArgumentException("nodes hava a different number of attributes"); } outer: for (int i = 0; i < attrCount; i++) { Node n = attrs.item(i); String name = n.getNodeName(); String value = n.getNodeValue(); for (int j = 0; j < attrCount; j++) { Node n2 = attrs2.item(j); String name2 = n2.getNodeName(); String value2 = n2.getNodeValue(); if (name.equals(name2) && value.equals(value2)) { continue outer; } } throw new IllegalArgumentException("attribute " + name + "=" + value + " doesn't match"); } boolean hasChildren = node.hasChildNodes(); if (hasChildren != node2.hasChildNodes()) { throw new IllegalArgumentException("one node has children and the other doesn't"); } if (hasChildren) { NodeList nl = node.getChildNodes(); NodeList nl2 = node2.getChildNodes(); short[] toFilter = new short[] { Node.TEXT_NODE, Node.ATTRIBUTE_NODE, Node.COMMENT_NODE }; List nodes = filter(nl, toFilter); List nodes2 = filter(nl2, toFilter); int length = nodes.size(); if (length != nodes2.size()) { throw new IllegalArgumentException("nodes hava a different number of children"); } for (int i = 0; i < length; i++) { Node n = (Node) nodes.get(i); Node n2 = (Node) nodes2.get(i); assertEquivalent(n, n2); } } }
From source file:Main.java
public static Map<String, String> attrbiuteToMap(NamedNodeMap attributes) { if (attributes == null) return new LinkedHashMap<String, String>(); Map<String, String> result = new LinkedHashMap<String, String>(); for (int i = 0; i < attributes.getLength(); i++) { result.put(attributes.item(i).getNodeName(), attributes.item(i).getNodeValue()); }/*from w ww. j a v a 2 s. co m*/ return result; }
From source file:Main.java
/** * @param node//from w ww .j a va 2 s .com * @throws IOException */ public static void serializeNode(Node node) throws IOException { if (writer == null) writer = new BufferedWriter(new OutputStreamWriter(System.out)); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: Document doc = (Document) node; writer.write("<?xml version=\""); writer.write(doc.getXmlVersion()); writer.write("\" encoding=\"UTF-8\" standalone=\""); if (doc.getXmlStandalone()) writer.write("yes"); else writer.write("no"); writer.write("\"?>\n"); NodeList nodes = node.getChildNodes(); if (nodes != null) for (int i = 0; i < nodes.getLength(); i++) serializeNode(nodes.item(i)); break; case Node.ELEMENT_NODE: String name = node.getNodeName(); writer.write("<" + name); NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node current = attributes.item(i); writer.write(" " + current.getNodeName() + "=\""); print(current.getNodeValue()); writer.write("\""); } writer.write(">"); NodeList children = node.getChildNodes(); if (children != null) { //if ((children.item(0) != null) && (children.item(0).getNodeType() == Node.ELEMENT_NODE)) // writer.write("\n"); for (int i = 0; i < children.getLength(); i++) serializeNode(children.item(i)); if ((children.item(0) != null) && (children.item(children.getLength() - 1).getNodeType() == Node.ELEMENT_NODE)) writer.write(""); } writer.write("</" + name + ">"); break; case Node.TEXT_NODE: print(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: writer.write("CDATA"); print(node.getNodeValue()); writer.write(""); break; case Node.COMMENT_NODE: writer.write("<!-- " + node.getNodeValue() + " -->\n"); break; case Node.PROCESSING_INSTRUCTION_NODE: writer.write("<?" + node.getNodeName() + " " + node.getNodeValue() + "?>\n"); break; case Node.ENTITY_REFERENCE_NODE: writer.write("&" + node.getNodeName() + ";"); break; case Node.DOCUMENT_TYPE_NODE: DocumentType docType = (DocumentType) node; String publicId = docType.getPublicId(); String systemId = docType.getSystemId(); String internalSubset = docType.getInternalSubset(); writer.write("<!DOCTYPE " + docType.getName()); if (publicId != null) writer.write(" PUBLIC \"" + publicId + "\" "); else writer.write(" SYSTEM "); writer.write("\"" + systemId + "\""); if (internalSubset != null) writer.write(" [" + internalSubset + "]"); writer.write(">\n"); break; } writer.flush(); }
From source file:com.enioka.jqm.tools.ResourceParser.java
private static void importXml() throws NamingException { InputStream is = ResourceParser.class.getClassLoader().getResourceAsStream(Helpers.resourceFile); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); try {//from w w w. jav a2 s . co m DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(is); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("resource"); String jndiAlias = null, resourceClass = null, description = "no description", scope = null, auth = "Container", factory = null; boolean singleton = false; for (int i = 0; i < nList.getLength(); i++) { Node n = nList.item(i); Map<String, String> otherParams = new HashMap<String, String>(); NamedNodeMap attrs = n.getAttributes(); for (int j = 0; j < attrs.getLength(); j++) { Node attr = attrs.item(j); String key = attr.getNodeName(); String value = attr.getNodeValue(); if ("name".equals(key)) { jndiAlias = value; } else if ("type".equals(key)) { resourceClass = value; } else if ("description".equals(key)) { description = value; } else if ("factory".equals(key)) { factory = value; } else if ("auth".equals(key)) { auth = value; } else if ("singleton".equals(key)) { singleton = Boolean.parseBoolean(value); } else { otherParams.put(key, value); } } if (resourceClass == null || jndiAlias == null || factory == null) { throw new NamingException("could not load the resource.xml file"); } JndiResourceDescriptor jrd = new JndiResourceDescriptor(resourceClass, description, scope, auth, factory, singleton); for (Map.Entry<String, String> prm : otherParams.entrySet()) { jrd.add(new StringRefAddr(prm.getKey(), prm.getValue())); } xml.put(jndiAlias, jrd); } } catch (Exception e) { NamingException pp = new NamingException("could not initialize the JNDI local resources"); pp.setRootCause(e); throw pp; } finally { IOUtils.closeQuietly(is); } }
From source file:Main.java
public static LinkedHashMap<String, String> attrbiuteToMap(NamedNodeMap attributes) { if (attributes == null) return new LinkedHashMap<String, String>(); LinkedHashMap<String, String> result = new LinkedHashMap<String, String>(); for (int i = 0; i < attributes.getLength(); i++) { result.put(attributes.item(i).getNodeName(), attributes.item(i).getNodeValue()); }// w ww . ja v a2 s . com return result; }
From source file:Main.java
/** * Clone given Node into target Document. If targe is null, same Document will be used. * If deep is specified, all children below will also be cloned. *///from www .j ava 2 s .c o m public final static Node cloneNode(Node node, Document target, boolean deep) throws DOMException { if ((target == null) || (node.getOwnerDocument() == target)) { // same Document return node.cloneNode(deep); } else { //DOM level 2 provides this in Document, so once xalan switches to that, //we can take out all the below and just call target.importNode(node, deep); //For now, we implement based on the javadocs for importNode Node newNode; int nodeType = node.getNodeType(); switch (nodeType) { case Node.ATTRIBUTE_NODE: newNode = target.createAttribute(node.getNodeName()); break; case Node.DOCUMENT_FRAGMENT_NODE: newNode = target.createDocumentFragment(); break; case Node.ELEMENT_NODE: Element newElement = target.createElement(node.getNodeName()); NamedNodeMap nodeAttr = node.getAttributes(); if (nodeAttr != null) { for (int i = 0; i < nodeAttr.getLength(); i++) { Attr attr = (Attr) nodeAttr.item(i); if (attr.getSpecified()) { Attr newAttr = (Attr) cloneNode(attr, target, true); newElement.setAttributeNode(newAttr); } } } newNode = newElement; break; case Node.ENTITY_REFERENCE_NODE: newNode = target.createEntityReference(node.getNodeName()); break; case Node.PROCESSING_INSTRUCTION_NODE: newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue()); break; case Node.TEXT_NODE: newNode = target.createTextNode(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: newNode = target.createCDATASection(node.getNodeValue()); break; case Node.COMMENT_NODE: newNode = target.createComment(node.getNodeValue()); break; case Node.NOTATION_NODE: case Node.ENTITY_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.DOCUMENT_NODE: default: throw new IllegalArgumentException("Importing of " + node + " not supported yet"); } if (deep) { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { newNode.appendChild(cloneNode(child, target, true)); } } return newNode; } }
From source file:Main.java
/** * Clone given Node into target Document. If targe is null, same Document will be used. * If deep is specified, all children below will also be cloned. *///from w w w. j a v a 2s.com public static Node cloneNode(Node node, Document target, boolean deep) throws DOMException { if (target == null || node.getOwnerDocument() == target) // same Document return node.cloneNode(deep); else { //DOM level 2 provides this in Document, so once xalan switches to that, //we can take out all the below and just call target.importNode(node, deep); //For now, we implement based on the javadocs for importNode Node newNode; int nodeType = node.getNodeType(); switch (nodeType) { case Node.ATTRIBUTE_NODE: newNode = target.createAttribute(node.getNodeName()); break; case Node.DOCUMENT_FRAGMENT_NODE: newNode = target.createDocumentFragment(); break; case Node.ELEMENT_NODE: Element newElement = target.createElement(node.getNodeName()); NamedNodeMap nodeAttr = node.getAttributes(); if (nodeAttr != null) for (int i = 0; i < nodeAttr.getLength(); i++) { Attr attr = (Attr) nodeAttr.item(i); if (attr.getSpecified()) { Attr newAttr = (Attr) cloneNode(attr, target, true); newElement.setAttributeNode(newAttr); } } newNode = newElement; break; case Node.ENTITY_REFERENCE_NODE: newNode = target.createEntityReference(node.getNodeName()); break; case Node.PROCESSING_INSTRUCTION_NODE: newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue()); break; case Node.TEXT_NODE: newNode = target.createTextNode(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: newNode = target.createCDATASection(node.getNodeValue()); break; case Node.COMMENT_NODE: newNode = target.createComment(node.getNodeValue()); break; case Node.NOTATION_NODE: case Node.ENTITY_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.DOCUMENT_NODE: default: throw new IllegalArgumentException("Importing of " + node + " not supported yet"); } if (deep) for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) newNode.appendChild(cloneNode(child, target, true)); return newNode; } }
From source file:Main.java
/** * _more_/*from w ww. j a v a 2 s .co m*/ * * @param html _more_ * @param node _more_ */ public static void toHtml(StringBuffer html, Node node) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: { NodeList children = node.getChildNodes(); int numChildren = children.getLength(); html.append("<b>" + node.getNodeName().replace("_", " ") + "</b>"); html.append(": "); for (int i = 0; i < numChildren; i++) { Node child = children.item(i); if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) { String v = child.getNodeValue(); if (v == null) { continue; } if (v.trim().length() == 0) { continue; } html.append(v); html.append(" "); } } boolean didone = false; NamedNodeMap nnm = node.getAttributes(); if (nnm != null) { for (int i = 0; i < nnm.getLength(); i++) { Attr attr = (Attr) nnm.item(i); String attrName = attr.getNodeName(); if (attrName.startsWith("xmlns") || attrName.startsWith("xsi:")) { continue; } if (!didone) { html.append("<ul>"); didone = true; } html.append(attrName.replace("_", " ") + "=" + attr.getNodeValue()); html.append("<br>\n"); } } int cnt = 0; for (int i = 0; i < numChildren; i++) { Node child = children.item(i); if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) { continue; } if (!didone) { html.append("<ul>"); didone = true; } if (cnt > 0) { html.append("<br>"); } toHtml(html, child); cnt++; } if (didone) { html.append("</ul>"); } break; } } }
From source file:Main.java
/** * Convenience method to copy the contents of the old {@link Node} into the * new one./* www . j a va 2s . c om*/ * * @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); } }