List of usage examples for org.w3c.dom Node hasChildNodes
public boolean hasChildNodes();
From source file:net.sourceforge.pmd.lang.xml.ast.DOMLineNumbers.java
private int determineLocation(Node n, int index) { int nextIndex = index; if (n.getNodeType() == Node.DOCUMENT_TYPE_NODE) { nextIndex = xmlString.indexOf("<!DOCTYPE", nextIndex); } else if (n.getNodeType() == Node.COMMENT_NODE) { nextIndex = xmlString.indexOf("<!--", nextIndex); } else if (n.getNodeType() == Node.ELEMENT_NODE) { nextIndex = xmlString.indexOf("<" + n.getNodeName(), nextIndex); } else if (n.getNodeType() == Node.CDATA_SECTION_NODE) { nextIndex = xmlString.indexOf("<![CDATA[", nextIndex); } else if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { ProcessingInstruction pi = (ProcessingInstruction) n; nextIndex = xmlString.indexOf("<?" + pi.getTarget(), nextIndex); } else if (n.getNodeType() == Node.TEXT_NODE) { String te = unexpandEntities(n, n.getNodeValue()); int newIndex = xmlString.indexOf(te, nextIndex); if (newIndex > 0) { nextIndex = newIndex;//from w w w . jav a2s.c o m } } else if (n.getNodeType() == Node.ENTITY_REFERENCE_NODE) { nextIndex = xmlString.indexOf("&" + n.getNodeName() + ";", nextIndex); } setBeginLocation(n, nextIndex); if (n.hasChildNodes()) { NodeList childs = n.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) { nextIndex = determineLocation(childs.item(i), nextIndex); } } if (n.getNodeType() == Node.ELEMENT_NODE) { nextIndex += 2 + n.getNodeName().length() + 1; // </nodename> } else if (n.getNodeType() == Node.DOCUMENT_TYPE_NODE) { Node nextSibling = n.getNextSibling(); if (nextSibling.getNodeType() == Node.ELEMENT_NODE) { nextIndex = xmlString.indexOf("<" + nextSibling.getNodeName(), nextIndex) - 1; } else if (nextSibling.getNodeType() == Node.COMMENT_NODE) { nextIndex = xmlString.indexOf("<!--", nextIndex); } else { nextIndex = xmlString.indexOf(">", nextIndex); } } else if (n.getNodeType() == Node.COMMENT_NODE) { nextIndex += 4 + 3; // <!-- and --> nextIndex += n.getNodeValue().length(); } else if (n.getNodeType() == Node.TEXT_NODE) { String te = unexpandEntities(n, n.getNodeValue()); nextIndex += te.length(); } else if (n.getNodeType() == Node.CDATA_SECTION_NODE) { nextIndex += "<![CDATA[".length() + n.getNodeValue().length() + "]]>".length(); } else if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { ProcessingInstruction pi = (ProcessingInstruction) n; nextIndex += "<?".length() + pi.getTarget().length() + "?>".length() + pi.getData().length(); } setEndLocation(n, nextIndex - 1); return nextIndex; }
From source file:net.ymate.framework.commons.XPathHelper.java
public Map<String, Object> toMap(Node parent) { Map<String, Object> _returnMap = new HashMap<String, Object>(); NodeList _nodes = parent.getChildNodes(); for (int _idx = 0; _idx < _nodes.getLength(); _idx++) { Node _node = _nodes.item(_idx); if (_node.getNodeType() == Node.ELEMENT_NODE) { // ?? if (_node.hasChildNodes() && _node.getChildNodes().getLength() == 1) { _returnMap.put(_node.getNodeName(), _node.getTextContent()); }/* www.ja va 2 s. co m*/ } } return _returnMap; }
From source file:org.apache.axis2.jaxws.wsdl.impl.SchemaReaderImpl.java
private String readSchemaBindingPackageName(Schema schema) { /* JAXB Specification section 7.6 have following important points * 1) <schemaBindings> binding declaration have schema scope * 2) For inline annotation a <schemaBindings> is valid only in the annotation element of the <schema> element. * 3) There must only be a single instance of <schemaBindings> declaration in the annotation element of the <schema> element. *///from ww w .ja va 2 s . c o m //Get root node for schema. Node root = schema.getElement(); if (root.hasChildNodes()) { //get all child nodes for schema NodeList list = root.getChildNodes(); //search for JAXB schemaBinding customization in schema element definitions. for (int i = 0; i < list.getLength(); i++) { Node childNode = list.item(i); if (isElementName(JAXB_SCHEMA_BINDING, childNode)) { //SchemaBinding has been defined, so lets look for package element. NodeList schemaBindingNodeList = childNode.getChildNodes(); for (int j = 0; j < schemaBindingNodeList.getLength(); j++) { Node schemaBindingNode = schemaBindingNodeList.item(j); if (isElementName(JAXB_SCHEMA_BINDING_PACKAGE, schemaBindingNode)) { //Package Element found, so lets read the package name attribute and return that. NamedNodeMap nodeMap = schemaBindingNode.getAttributes(); Node attributeNode = nodeMap.getNamedItem(JAXB_SCHEMA_Binding_PACKAGENAME); return attributeNode.getNodeValue(); } } } } } return null; }
From source file:org.apache.cocoon.xml.dom.DOMUtil.java
/** * Set the value of the DOM node./*w ww . ja v a 2s . c o m*/ * All current children of the node are removed and a new text node * with the value is appended. */ public static void setValueOfNode(Node node, String value) { if (node.getNodeType() == Node.ATTRIBUTE_NODE) { node.setNodeValue(value); } else { while (node.hasChildNodes() == true) { node.removeChild(node.getFirstChild()); } node.appendChild(node.getOwnerDocument().createTextNode(value)); } }
From source file:org.apache.cocoon.xml.dom.DOMUtil.java
/** * Get a document fragment from a <code>Reader</code>. * The reader must provide valid XML, but it is allowed that the XML * has more than one root node. This xml is parsed by the * specified parser instance and a DOM DocumentFragment is created. *//*from w w w . j av a2 s . co m*/ public static DocumentFragment getDocumentFragment(SAXParser parser, Reader stream) throws ProcessingException { DocumentFragment frag = null; Writer writer; Reader reader; boolean removeRoot = true; try { // create a writer, // write the root element, then the input from the // reader writer = new StringWriter(); writer.write(XML_ROOT_DEFINITION); char[] cbuf = new char[16384]; int len; do { len = stream.read(cbuf, 0, 16384); if (len != -1) { writer.write(cbuf, 0, len); } } while (len != -1); writer.write("</root>"); // now test if xml input start with <?xml String xml = writer.toString(); String searchString = XML_ROOT_DEFINITION + "<?xml "; if (xml.startsWith(searchString) == true) { // now remove the surrounding root element xml = xml.substring(XML_ROOT_DEFINITION.length(), xml.length() - 7); removeRoot = false; } reader = new StringReader(xml); InputSource input = new InputSource(reader); DOMBuilder builder = new DOMBuilder(); builder.startDocument(); builder.startElement("", "root", "root", XMLUtils.EMPTY_ATTRIBUTES); IncludeXMLConsumer filter = new IncludeXMLConsumer(builder, builder); parser.parse(input, filter); builder.endElement("", "root", "root"); builder.endDocument(); // Create Document Fragment, remove <root> final Document doc = builder.getDocument(); frag = doc.createDocumentFragment(); final Node root = doc.getDocumentElement().getFirstChild(); root.normalize(); if (removeRoot == false) { root.getParentNode().removeChild(root); frag.appendChild(root); } else { Node child; while (root.hasChildNodes() == true) { child = root.getFirstChild(); root.removeChild(child); frag.appendChild(child); } } } catch (SAXException sax) { throw new ProcessingException("SAXException: " + sax, sax); } catch (IOException ioe) { throw new ProcessingException("IOException: " + ioe, ioe); } return frag; }
From source file:org.apache.ode.bpel.rtrep.v1.ASSIGN.java
/** * isInsert flag desginates this as an 'element' type insertion, which * requires insert the actual element value, rather than it's children * * @return//from w w w . jav a2 s. co m * @throws FaultException */ private Node replaceContent(Node lvalue, Node lvaluePtr, String rvalue) throws FaultException { Document d = lvaluePtr.getOwnerDocument(); if (__log.isDebugEnabled()) { __log.debug("lvaluePtr type " + lvaluePtr.getNodeType()); __log.debug("lvaluePtr " + DOMUtils.domToString(lvaluePtr)); __log.debug("lvalue " + lvalue); __log.debug("rvalue " + rvalue); } switch (lvaluePtr.getNodeType()) { case Node.ELEMENT_NODE: // Remove all the children. while (lvaluePtr.hasChildNodes()) lvaluePtr.removeChild(lvaluePtr.getFirstChild()); // Append a new text node. lvaluePtr.appendChild(d.createTextNode(rvalue)); // If lvalue is a text, removing all lvaluePtr children had just removed it // so we need to rebuild it as a child of lvaluePtr if (lvalue instanceof Text) lvalue = lvaluePtr.getFirstChild(); break; case Node.TEXT_NODE: Node newval = d.createTextNode(rvalue); // Replace ourselves . lvaluePtr.getParentNode().replaceChild(newval, lvaluePtr); // A little kludge, let our caller know that the root element has changed. // (used for assignment to a simple typed variable) if (lvalue.getNodeType() == Node.ELEMENT_NODE) { // No children, adding an empty text children to point to if (lvalue.getFirstChild() == null) { Text txt = lvalue.getOwnerDocument().createTextNode(""); lvalue.appendChild(txt); } if (lvalue.getFirstChild().getNodeType() == Node.TEXT_NODE) lvalue = lvalue.getFirstChild(); } if (lvalue.getNodeType() == Node.TEXT_NODE && ((Text) lvalue).getWholeText().equals(((Text) lvaluePtr).getWholeText())) lvalue = lvaluePtr = newval; break; case Node.ATTRIBUTE_NODE: ((Attr) lvaluePtr).setValue(rvalue); break; default: // This could occur if the expression language selects something // like // a PI or a CDATA. String msg = __msgs.msgInvalidLValue(); if (__log.isDebugEnabled()) __log.debug(lvaluePtr + ": " + msg); throw new FaultException(getOAsssign().getOwner().constants.qnSelectionFailure, msg); } return lvalue; }
From source file:org.apache.ode.bpel.rtrep.v2.AssignHelper.java
/** * isInsert flag desginates this as an 'element' type insertion, which * requires insert the actual element value, rather than it's children * (madars.vitolins _at gmail.com - 2009.04.17 - moved from ASSIGN here) * @return//w w w.j a v a2s . c o m * @throws FaultException */ public Node replaceContent(Node lvalue, Node lvaluePtr, String rvalue) throws FaultException { Document d = lvaluePtr.getOwnerDocument(); if (__log.isDebugEnabled()) { __log.debug("lvaluePtr type " + lvaluePtr.getNodeType()); __log.debug("lvaluePtr " + DOMUtils.domToString(lvaluePtr)); __log.debug("lvalue " + lvalue); __log.debug("rvalue " + rvalue); } switch (lvaluePtr.getNodeType()) { case Node.ELEMENT_NODE: // Remove all the children. while (lvaluePtr.hasChildNodes()) lvaluePtr.removeChild(lvaluePtr.getFirstChild()); // Append a new text node. lvaluePtr.appendChild(d.createTextNode(rvalue)); // If lvalue is a text, removing all lvaluePtr children had just removed it // so we need to rebuild it as a child of lvaluePtr if (lvalue instanceof Text) lvalue = lvaluePtr.getFirstChild(); break; case Node.TEXT_NODE: Node newval = d.createTextNode(rvalue); // Replace ourselves . lvaluePtr.getParentNode().replaceChild(newval, lvaluePtr); // A little kludge, let our caller know that the root element has changed. // (used for assignment to a simple typed variable) if (lvalue.getNodeType() == Node.ELEMENT_NODE) { // No children, adding an empty text children to point to if (lvalue.getFirstChild() == null) { Text txt = lvalue.getOwnerDocument().createTextNode(""); lvalue.appendChild(txt); } if (lvalue.getFirstChild().getNodeType() == Node.TEXT_NODE) lvalue = lvalue.getFirstChild(); } if (lvalue.getNodeType() == Node.TEXT_NODE && ((Text) lvalue).getWholeText().equals(((Text) lvaluePtr).getWholeText())) lvalue = lvaluePtr = newval; break; case Node.ATTRIBUTE_NODE: ((Attr) lvaluePtr).setValue(rvalue); break; default: // This could occur if the expression language selects something // like // a PI or a CDATA. String msg = __msgs.msgInvalidLValue(); if (__log.isDebugEnabled()) __log.debug(lvaluePtr + ": " + msg); throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg); } return lvalue; }
From source file:org.apache.ode.bpel.runtime.AssignHelper.java
/** * isInsert flag desginates this as an 'element' type insertion, which * requires insert the actual element value, rather than it's children * * @return// w w w. j av a 2 s . c o m * @throws FaultException */ private Node replaceContent(Node lvalue, Node lvaluePtr, String rvalue) throws FaultException { Document d = lvaluePtr.getOwnerDocument(); if (__log.isDebugEnabled()) { __log.debug("lvaluePtr type " + lvaluePtr.getNodeType()); __log.debug("lvaluePtr " + DOMUtils.domToString(lvaluePtr)); __log.debug("lvalue " + lvalue); __log.debug("rvalue " + rvalue); } switch (lvaluePtr.getNodeType()) { case Node.ELEMENT_NODE: // Remove all the children. while (lvaluePtr.hasChildNodes()) lvaluePtr.removeChild(lvaluePtr.getFirstChild()); // Append a new text node. lvaluePtr.appendChild(d.createTextNode(rvalue)); // If lvalue is a text, removing all lvaluePtr children had just removed it // so we need to rebuild it as a child of lvaluePtr if (lvalue instanceof Text) lvalue = lvaluePtr.getFirstChild(); break; case Node.TEXT_NODE: Node newval = d.createTextNode(rvalue); // Replace ourselves . lvaluePtr.getParentNode().replaceChild(newval, lvaluePtr); // A little kludge, let our caller know that the root element has changed. // (used for assignment to a simple typed variable) if (lvalue.getNodeType() == Node.ELEMENT_NODE) { // No children, adding an empty text children to point to if (lvalue.getFirstChild() == null) { Text txt = lvalue.getOwnerDocument().createTextNode(""); lvalue.appendChild(txt); } if (lvalue.getFirstChild().getNodeType() == Node.TEXT_NODE) lvalue = lvalue.getFirstChild(); } if (lvalue.getNodeType() == Node.TEXT_NODE && ((Text) lvalue).getWholeText().equals(((Text) lvaluePtr).getWholeText())) lvalue = lvaluePtr = newval; break; case Node.ATTRIBUTE_NODE: ((Attr) lvaluePtr).setValue(rvalue); break; default: // This could occur if the expression language selects something // like // a PI or a CDATA. String msg = __msgs.msgInvalidLValue(); if (__log.isDebugEnabled()) __log.debug(lvaluePtr + ": " + msg); throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg); } return lvalue; }
From source file:org.apache.ode.utils.DOMUtils.java
/** * Remove the child nodes under another node. * @param target the <code>Node</code> to remove the children from. *//*w w w . j av a2 s .c om*/ public static void removeChildren(Node target) { while (target.hasChildNodes()) { target.removeChild(target.getFirstChild()); } }