@Override public org.w3c.dom.Node cloneNode(boolean deep) { return node.cloneNode(deep); }
/** * Adds a new line to the DOM. * * @throws DOMException If thrown by method invoked on the underlying DOM document */ private void newLine() { this.currentElement.appendChild(this.newline.cloneNode(false)); }
protected boolean replaceNode(Node[] primaryNodes, Node testNode, List<Node> usedNodes) { boolean foundItem = false; for (int j=0;j<primaryNodes.length;j++){ if (primaryNodes[j].getNodeName().equals(testNode.getNodeName())) { Node newNode = primaryNodes[j].getOwnerDocument().importNode(testNode.cloneNode(true), true); primaryNodes[j].getParentNode().replaceChild(newNode, primaryNodes[j]); usedNodes.add(testNode); foundItem = true; } } return foundItem; } }
public Node[] merge(List<Node> nodeList1, List<Node> nodeList2, List<Node> exhaustedNodes) { if (CollectionUtils.isEmpty(nodeList1) || CollectionUtils.isEmpty(nodeList2)) { return null; } Node node1 = nodeList1.get(0); Node node2 = nodeList2.get(0); NodeList list2 = node2.getChildNodes(); for (int j = 0; j < list2.getLength(); j++) { node1.appendChild(node1.getOwnerDocument().importNode(list2.item(j).cloneNode(true), true)); } Node[] response = new Node[nodeList2.size()]; for (int j = 0; j < response.length; j++) { response[j] = nodeList2.get(j); } return response; }
public static void applyPatterns(Document document) { NodeList nl = document.getElementsByTagName("apply"); for (int i = 0; i < nl.getLength();) { Element app = (Element) nl.item(i); String pt = app.getAttribute("pattern"); if (!pt.equals("")) { if (!patterns.containsKey(pt)) { throw new IllegalStateException("Pattern '" + pt + "' is not defined"); } Element patt = patterns.get(pt); final NodeList pattChildren = patt.getChildNodes(); for(int ki = 0; ki < pattChildren.getLength(); ki++) { Node ni = patt.getChildNodes().item(ki); app.getParentNode().insertBefore(ni.cloneNode(true), app); } app.getParentNode().removeChild(app); } else { i++; } } }
@Override public Node[] merge(List<Node> nodeList1, List<Node> nodeList2, List<Node> exhaustedNodes) { if (CollectionUtils.isEmpty(nodeList1) || CollectionUtils.isEmpty(nodeList2)) { return null; } Node node1 = nodeList1.get(0); Node node2 = nodeList2.get(0); NamedNodeMap attributes2 = node2.getAttributes(); Comparator<Object> nameCompare = new Comparator<Object>() { @Override public int compare(Object arg0, Object arg1) { return ((Node) arg0).getNodeName().compareTo(((Node) arg1).getNodeName()); } }; Node[] tempNodes = {}; tempNodes = exhaustedNodes.toArray(tempNodes); Arrays.sort(tempNodes, nameCompare); int length = attributes2.getLength(); for (int j = 0; j < length; j++) { Node temp = attributes2.item(j); int pos = Arrays.binarySearch(tempNodes, temp, nameCompare); if (pos < 0) { Attr clone = (Attr) temp.cloneNode(true); ((Element) node1).setAttributeNode((Attr) node1.getOwnerDocument().importNode(clone, true)); } } return null; }
protected boolean replaceNode(Node[] primaryNodes, Node testNode, final String attribute, List<Node> usedNodes) { if (testNode.getAttributes().getNamedItem(attribute) == null) { return false; } Node[] filtered = NodeUtil.filterByAttribute(primaryNodes, attribute); int pos = NodeUtil.findNode(filtered, testNode, attribute, true); if (pos >= 0) { Node foundNode = filtered[pos]; Node newNode = foundNode.getOwnerDocument().importNode(testNode.cloneNode(true), true); foundNode.getParentNode().replaceChild(newNode, foundNode); usedNodes.add(testNode); return true; } return false; }
/** * Convert an XML fragment from one namespace to another. * * @param from element to translate * @param namespace namespace to be translated to * @return * * @since 8.4 */ public static Element translateXML(Element from, String namespace) { Element to = from.getOwnerDocument().createElementNS(namespace, from.getLocalName()); NodeList nl = from.getChildNodes(); int length = nl.getLength(); for (int i = 0; i < length; i++) { Node node = nl.item(i); Node newNode; if (node.getNodeType() == Node.ELEMENT_NODE) { newNode = translateXML((Element) node, namespace); } else { newNode = node.cloneNode(true); } to.appendChild(newNode); } NamedNodeMap m = from.getAttributes(); for (int i = 0; i < m.getLength(); i++) { Node attr = m.item(i); to.setAttribute(attr.getNodeName(), attr.getNodeValue()); } return to; }
Node newNode = ownerDocument.importNode(node.cloneNode(true), true); parentNode.appendChild(newNode); usedNodes.add(node);
newNode = node.cloneNode(true); newNode = to.getOwnerDocument().importNode(newNode, true);
Node node1Parent = nodeList1.get(0).getParentNode(); for (Node aNodeList2 : nodeList2) { Node tempNode = node1Parent.getOwnerDocument().importNode(aNodeList2.cloneNode(true), true); if (LOG.isDebugEnabled()) { StringBuffer sb = new StringBuffer();
Node foundNode = filtered[pos]; Node targetNode = foundNode.getOwnerDocument().importNode(foundNode.cloneNode(false), false); Node newTestNode = foundNode.getOwnerDocument().importNode(testNode.cloneNode(true), true); NodeUtil.mergeNodeLists(targetNode, newTestNode.getChildNodes(), foundNode.getChildNodes(), "name"); foundNode.getParentNode().replaceChild(targetNode, foundNode);
break evaluate; Node newNode = filtered[pos].getOwnerDocument().importNode(testNode.cloneNode(true), true); filtered[pos].getParentNode().replaceChild(newNode, filtered[pos]);
appendSelf(contextNodes.item(i).cloneNode(true));
Node singleNode = result.item(i).cloneNode(true);
public static void mergeElement(Element from, Element to) { // attrs for (int idx = 0; idx < from.getAttributes().getLength(); idx++) { Node node = from.getAttributes().item(idx); to.getAttributes().setNamedItem(node.cloneNode(false)); } // children for (int idx = 0; idx < from.getChildNodes().getLength(); idx++) { Node node = from.getChildNodes().item(idx); if (!Element.class.isInstance(node)) { continue; } to.appendChild(node.cloneNode(true)); } } }
/** * Compares two DOM {@link Node nodes} by comparing the representations of * the nodes as XML strings * * @param node1 the first node * @param node2 the second node * @return true if the XML representation node1 is the same as the XML * representation of node2, otherwise false */ public static boolean compareNodes(Node node1, Node node2) { Validate.notNull(node1, "First node required"); Validate.notNull(node2, "Second node required"); // The documents need to be cloned as normalization has side-effects node1 = node1.cloneNode(true); node2 = node2.cloneNode(true); // The documents need to be normalized before comparison takes place to // remove any formatting that interfere with comparison if (node1 instanceof Document && node2 instanceof Document) { ((Document) node1).normalizeDocument(); ((Document) node2).normalizeDocument(); } else { node1.normalize(); node2.normalize(); } return nodeToString(node1).equals(nodeToString(node2)); }
private Node findSqlFragment(String refid, Properties variables) { refid = PropertyParser.parse(refid, variables); refid = builderAssistant.applyCurrentNamespace(refid, true); try { XNode nodeToInclude = configuration.getSqlFragments().get(refid); return nodeToInclude.getNode().cloneNode(true); } catch (IllegalArgumentException e) { throw new IncompleteElementException("Could not find SQL statement to include with refid '" + refid + "'", e); } }
private Node findSqlFragment(String refid, Properties variables) { refid = PropertyParser.parse(refid, variables); refid = builderAssistant.applyCurrentNamespace(refid, true); try { XNode nodeToInclude = configuration.getSqlFragments().get(refid); return nodeToInclude.getNode().cloneNode(true); } catch (IllegalArgumentException e) { throw new IncompleteElementException("Could not find SQL statement to include with refid '" + refid + "'", e); } }
elem.setAttributeNodeNS( (Attr) attrs.item( i ).cloneNode( true ) );