@Override public org.w3c.dom.Node insertBefore(org.w3c.dom.Node newChild, org.w3c.dom.Node refChild) throws DOMException { return node.insertBefore(newChild, refChild); }
/** 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); }
/** 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); }
/** 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); } }
/** 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); } }
/** * adds processing instruction node to DOM. */ public void processingInstruction(String target, String data) { final Node last = (Node)_nodeStk.peek(); ProcessingInstruction pi = _document.createProcessingInstruction( target, data); if (pi != null){ if (last == _root && _nextSibling != null) last.insertBefore(pi, _nextSibling); else last.appendChild(pi); _lastSibling = pi; } }
/** * Lexical Handler method to create comment node in DOM tree. */ public void comment(char[] ch, int start, int length) { final Node last = (Node)_nodeStk.peek(); Comment comment = _document.createComment(new String(ch,start,length)); if (comment != null){ if (last == _root && _nextSibling != null) last.insertBefore(comment, _nextSibling); else last.appendChild(comment); _lastSibling = comment; } }
/** * Analogous to the Node.insertBefore() method, insert a newNode after a refNode. * * @param newNode * new node * @param refNode * ref node * @throws DOMException * DOMException */ public static void insertAfter(Node newNode, Node refNode) throws DOMException { Node parent = refNode.getParentNode(); Node next = refNode.getNextSibling(); if (next == null) { parent.appendChild(newNode); } else { parent.insertBefore(newNode, next); } }
/** * Analogous to the Node.insertBefore() method, insert a newNode after a refNode. * * @param newNode * new node * @param refNode * ref node * @throws DOMException * DOMException */ public static void insertAfter(Node newNode, Node refNode) throws DOMException { Node parent = refNode.getParentNode(); Node next = refNode.getNextSibling(); if (next == null) { parent.appendChild(newNode); } else { parent.insertBefore(newNode, next); } }
public static void replaceElement(Element oldElement, NodeList newNodes) { Document doc = oldElement.getOwnerDocument(); Node parent = oldElement.getParentNode(); int len = newNodes.getLength(); for (int i = 0; i < len; i++) { Node n = newNodes.item(i); if (!doc.equals(n.getOwnerDocument())) { // first we need to import the node into the document n = doc.importNode(n, true); } parent.insertBefore(n, oldElement); } parent.removeChild(oldElement); }
public static void replaceElement(Element oldElement, NodeList newNodes) { Document doc = oldElement.getOwnerDocument(); Node parent = oldElement.getParentNode(); int len = newNodes.getLength(); for (int i = 0; i < len; i++) { Node n = newNodes.item(i); if (!doc.equals(n.getOwnerDocument())) { // first we need to import the node into the document n = doc.importNode(n, true); } parent.insertBefore(n, oldElement); } parent.removeChild(oldElement); }
private static Source mergeIntoSource(InputStream baseXmlStream, InputStream xmlStream) throws Exception { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setIgnoringComments(true); domFactory.setValidating(false); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document baseDoc = builder.parse(baseXmlStream); Document ruleDoc = builder.parse(xmlStream); // Shall this be more generic, i.e. reuse not just unification ??? NodeList unificationNodes = baseDoc.getElementsByTagName("unification"); Node ruleNode = ruleDoc.getElementsByTagName("rules").item(0); Node firstChildRuleNode = ruleNode.getChildNodes().item(1); for (int i = 0; i < unificationNodes.getLength(); i++) { Node unificationNode = ruleDoc.importNode(unificationNodes.item(i), true); ruleNode.insertBefore(unificationNode, firstChildRuleNode); } return new DOMSource(ruleDoc); }
public static void combineAllApplyTags(Document document) { NodeList nl = document.getElementsByTagName("apply"); while(nl.getLength() > 0) { Element app = (Element) nl.item(0); Element parent = (Element) app.getParentNode(); NamedNodeMap attrs = app.getAttributes(); for(int i = 0; i < attrs.getLength(); i++) { Node ns = attrs.item(i); parent.setAttribute(ns.getNodeName(), ns.getNodeValue()); } while(app.getChildNodes().getLength() > 0) { Node ni = app.getChildNodes().item(0); app.getParentNode().insertBefore(ni, app); } app.getParentNode().removeChild(app); } }
public void characters(char[] ch, int start, int length) { final Node last = (Node)_nodeStk.peek(); // No text nodes can be children of root (DOM006 exception) if (last != _document) { final String text = new String(ch, start, length); if( _lastSibling != null && _lastSibling.getNodeType() == Node.TEXT_NODE ){ ((Text)_lastSibling).appendData(text); } else if (last == _root && _nextSibling != null) { _lastSibling = last.insertBefore(_document.createTextNode(text), _nextSibling); } else { _lastSibling = last.appendChild(_document.createTextNode(text)); } } }
private static String styleXML(Node dxfsNode, Node tableStyleNode) throws IOException, TransformerException { // built-ins doc uses 1-based dxf indexing, Excel uses 0 based. // add a dummy node to adjust properly. dxfsNode.insertBefore(dxfsNode.getOwnerDocument().createElement("dxf"), dxfsNode.getFirstChild()); StringBuilder sb = new StringBuilder(); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n") .append("<styleSheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" ") .append("xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" ") .append("xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\" ") .append("xmlns:x16r2=\"http://schemas.microsoft.com/office/spreadsheetml/2015/02/main\" ") .append("mc:Ignorable=\"x14ac x16r2\">\n"); sb.append(writeToString(dxfsNode)); sb.append(writeToString(tableStyleNode)); sb.append("</styleSheet>"); return sb.toString(); }
/** * Enclose the elements' closest common ancestor. * * @param first * first * @param last * last */ protected void slowDown(Element first, Element last) { Element phonol = MaryDomUtils.encloseNodesWithNewElement(first, last, MaryXML.PHONOLOGY); phonol.setAttribute("precision", "precise"); Document doc = phonol.getOwnerDocument(); Element prosody = MaryXML.createElement(doc, MaryXML.PROSODY); prosody.setAttribute("rate", "-20%"); phonol.getParentNode().insertBefore(prosody, phonol); prosody.appendChild(phonol); } }
/** * Enclose the elements' closest common ancestor. * * @param first * first * @param last * last */ protected void slowDown(Element first, Element last) { Element phonol = MaryDomUtils.encloseNodesWithNewElement(first, last, MaryXML.PHONOLOGY); phonol.setAttribute("precision", "precise"); Document doc = phonol.getOwnerDocument(); Element prosody = MaryXML.createElement(doc, MaryXML.PROSODY); prosody.setAttribute("rate", "-20%"); phonol.getParentNode().insertBefore(prosody, phonol); prosody.appendChild(phonol); } }
private static void appendNodes(Node self, Closure c) { Node parent = self.getParentNode(); Node beforeNode = self.getNextSibling(); DOMBuilder b = new DOMBuilder(self.getOwnerDocument()); Element newNodes = (Element) b.invokeMethod("rootNode", c); Iterator<Node> iter = XmlGroovyMethods.iterator(children(newNodes)); while (iter.hasNext()) { parent.insertBefore(iter.next(), beforeNode); } }
/** * Splits this CDATA node into parts that do not contain a "]]>" sequence. * Any newly created nodes will be inserted before this node. */ public void split() { if (!needsSplitting()) { return; } Node parent = getParentNode(); String[] parts = getData().split("\\]\\]>"); parent.insertBefore(new CDATASectionImpl(document, parts[0] + "]]"), this); for (int p = 1; p < parts.length - 1; p++) { parent.insertBefore(new CDATASectionImpl(document, ">" + parts[p] + "]]"), this); } setData(">" + parts[parts.length - 1]); }
public final Text splitText(int offset) throws DOMException { Text newText = document.createTextNode( substringData(offset, getLength() - offset)); deleteData(0, offset); Node refNode = getNextSibling(); if (refNode == null) { getParentNode().appendChild(newText); } else { getParentNode().insertBefore(newText, refNode); } return this; }