Refine search
/** If the child node doesn't exist, it is created. */ private static Node getFirstChildNodeByName (Node parent, String child) { NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(child)) { return childNodes.item(i); } } Node newNode = parent.getOwnerDocument().createElement(child); if (childNodes.item(0) != null) return parent.insertBefore(newNode, childNodes.item(0)); else return parent.appendChild(newNode); }
/** * Get the root node of the document tree, regardless of * whether or not the node passed in is a document node. * <p> * TODO: This doesn't handle DocumentFragments or "orphaned" subtrees * -- it's currently returning ownerDocument even when the tree is * not actually part of the main Document tree. We should either * rewrite the description to say that it finds the Document node, * or change the code to walk up the ancestor chain. * * @param n Node to be examined * * @return the Document node. Note that this is not the correct answer * if n was (or was a child of) a DocumentFragment or an orphaned node, * as can arise if the DOM has been edited rather than being generated * by a parser. */ public Node getRootNode(Node n) { int nt = n.getNodeType(); return ( (Node.DOCUMENT_NODE == nt) || (Node.DOCUMENT_FRAGMENT_NODE == nt) ) ? n : n.getOwnerDocument(); }
static class LoggingErrorListener implements ErrorListener { // See http://www.cafeconleche.org/slides/sd2003west/xmlandjava/346.html boolean strict; public LoggingErrorListener(boolean strict) { } public void warning(TransformerException exception) { log.warn(exception.getMessage(), exception); // Don't throw an exception and stop the processor // just for a warning; but do log the problem } public void error(TransformerException exception) throws TransformerException { log.error(exception.getMessage(), exception); // XSLT is not as draconian as XML. There are numerous errors // which the processor may but does not have to recover from; // e.g. multiple templates that match a node with the same // priority. If I do not want to allow that, I'd throw this // exception here. if (strict) { throw exception; }
/** * Get the root node of the document tree, regardless of * whether or not the node passed in is a document node. * <p> * TODO: This doesn't handle DocumentFragments or "orphaned" subtrees * -- it's currently returning ownerDocument even when the tree is * not actually part of the main Document tree. We should either * rewrite the description to say that it finds the Document node, * or change the code to walk up the ancestor chain. * * @param n Node to be examined * * @return the Document node. Note that this is not the correct answer * if n was (or was a child of) a DocumentFragment or an orphaned node, * as can arise if the DOM has been edited rather than being generated * by a parser. */ public Node getRootNode(Node n) { int nt = n.getNodeType(); return ( (Node.DOCUMENT_NODE == nt) || (Node.DOCUMENT_FRAGMENT_NODE == nt) ) ? n : n.getOwnerDocument(); }
public void setChild(Node wrapper, int i, ElementInfo childElement, Object value) { Node node = (Node)value; if (node.getNodeType() == Node.DOCUMENT_NODE) { node = ((Document)node).getDocumentElement(); } wrapper.appendChild(wrapper.getOwnerDocument().importNode(node, true)); }
/** If the child node doesn't exist, it is created. */ private static Node getFirstChildNodeByName (Node parent, String child) { NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(child)) { return childNodes.item(i); } } Node newNode = parent.getOwnerDocument().createElement(child); if (childNodes.item(0) != null) return parent.insertBefore(newNode, childNodes.item(0)); else return parent.appendChild(newNode); }
public static TreeWalker createTreeWalker(Node root, String... tagNames) { return createTreeWalker(root.getNodeType() == Node.DOCUMENT_NODE ? (Document) root : root.getOwnerDocument(), root, tagNames); }
public void setChild(Node wrapper, int i, ElementInfo childElement, Object value) { Node node = (Node)value; if (node.getNodeType() == Node.DOCUMENT_NODE) { node = ((Document)node).getDocumentElement(); } wrapper.appendChild(wrapper.getOwnerDocument().importNode(node, true)); }
/** * 在已有节点上创建子节点 * * @param node 节点 * @param tagName 标签名 * @return 子节点 * @since 4.0.9 */ public static Element appendChild(Node node, String tagName) { Document doc = (node instanceof Document) ? (Document) node : node.getOwnerDocument(); Element child = doc.createElement(tagName); node.appendChild(child); return child; }
public static NodeIterator createNodeIterator(Node root, String... tagNames) { return createNodeIterator(root.getNodeType() == Node.DOCUMENT_NODE ? (Document) root : root.getOwnerDocument(), root, tagNames); }
@Override public void characters(final char[] chars, final int offset, final int length) { String data = new String(chars, offset, length); Node parent = !_parents.isEmpty() ? _parents.peek() : _node; Node last = parent.getLastChild(); if ((last != null) && (last.getNodeType() == Node.TEXT_NODE)) { ((Text) last).appendData(data); } else { Text text = parent.getOwnerDocument().createTextNode(data); parent.appendChild(text); } }
/** * 在已有节点上创建子节点 * * @param node 节点 * @param tagName 标签名 * @return 子节点 * @since 4.0.9 */ public static Element appendChild(Node node, String tagName) { Document doc = (node instanceof Document) ? (Document) node : node.getOwnerDocument(); Element child = doc.createElement(tagName); node.appendChild(child); return child; }
public static NodeIterator createNodeIterator(Node root, String... tagNames) { return createNodeIterator(root.getNodeType() == Node.DOCUMENT_NODE ? (Document) root : root.getOwnerDocument(), root, tagNames); }
public void characters(final char[] chars, final int offset, final int length) { String data = new String(chars, offset, length); Node parent = (_parents.size() > 0) ? (Node) _parents.peek() : _document; Node last = parent.getLastChild(); if ((last != null) && (last.getNodeType() == Node.TEXT_NODE)) { ((Text)last).appendData(data); } else { Text text = parent.getOwnerDocument().createTextNode(data); parent.appendChild(text); } }
/** If the child node or attribute doesn't exist, it is created. Usage example: Node property = * getFirstChildByAttrValue(properties, "property", "name"); */ private static Node getFirstChildByNameAttrValue (Node node, String childName, String attr, String value) { NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(childName)) { NamedNodeMap attributes = childNodes.item(i).getAttributes(); Node attribute = attributes.getNamedItem(attr); if (attribute.getNodeValue().equals(value)) return childNodes.item(i); } } Node newNode = node.getOwnerDocument().createElement(childName); NamedNodeMap attributes = newNode.getAttributes(); Attr nodeAttr = node.getOwnerDocument().createAttribute(attr); nodeAttr.setNodeValue(value); attributes.setNamedItem(nodeAttr); if (childNodes.item(0) != null) { return node.insertBefore(newNode, childNodes.item(0)); } else { return node.appendChild(newNode); } }
public static TreeWalker createTreeWalker(Node root, String... tagNames) { return createTreeWalker(root.getNodeType() == Node.DOCUMENT_NODE ? (Document) root : root.getOwnerDocument(), root, tagNames); }
protected final void marshalDomNode(Object graph, Node node) throws XmlMappingException { Document document = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node : node.getOwnerDocument(); Node xmlBeansNode = ((XmlObject) graph).newDomNode(getXmlOptions()); NodeList xmlBeansChildNodes = xmlBeansNode.getChildNodes(); for (int i = 0; i < xmlBeansChildNodes.getLength(); i++) { Node xmlBeansChildNode = xmlBeansChildNodes.item(i); Node importedNode = document.importNode(xmlBeansChildNode, true); node.appendChild(importedNode); } }
/** If the child node or attribute doesn't exist, it is created. Usage example: Node property = * getFirstChildByAttrValue(properties, "property", "name"); */ private static Node getFirstChildByNameAttrValue (Node node, String childName, String attr, String value) { NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(childName)) { NamedNodeMap attributes = childNodes.item(i).getAttributes(); Node attribute = attributes.getNamedItem(attr); if (attribute.getNodeValue().equals(value)) return childNodes.item(i); } } Node newNode = node.getOwnerDocument().createElement(childName); NamedNodeMap attributes = newNode.getAttributes(); Attr nodeAttr = node.getOwnerDocument().createAttribute(attr); nodeAttr.setNodeValue(value); attributes.setNamedItem(nodeAttr); if (childNodes.item(0) != null) { return node.insertBefore(newNode, childNodes.item(0)); } else { return node.appendChild(newNode); } }
/** * This method returns the owner document of a particular node. * This method is necessary because it <I>always</I> returns a * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE> * if the {@link Node} is a {@link Document}. * * @param node * @return the owner document of the node */ public static Document getOwnerDocument(Node node) { if (node.getNodeType() == Node.DOCUMENT_NODE) { return (Document) node; } try { return node.getOwnerDocument(); } catch (NullPointerException npe) { throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0") + " Original message was \"" + npe.getMessage() + "\""); } }
public static Element appendChildElement(Node node, String childName) { if (node == null) throw new NullPointerException("Received null node"); return (Element) node.appendChild(createElement(node.getOwnerDocument(), childName)); }