List of usage examples for org.w3c.dom Node insertBefore
public Node insertBefore(Node newChild, Node refChild) throws DOMException;
newChild
before the existing child node refChild
. From source file:com.amalto.core.history.accessor.UnaryFieldAccessor.java
private Element internalCreate() { parent.create();// w w w. j a v a2 s . c om Document domDocument = document.asDOM(); Element element = getElement(); if (element == null) { Element newElement = domDocument.createElementNS(domDocument.getNamespaceURI(), fieldName); Node parentNode = parent.getNode(); Node lastAccessedNode = document.getLastAccessedNode(); if (parentNode == lastAccessedNode) { parentNode.insertBefore(newElement, parentNode.getFirstChild()); } else if (lastAccessedNode != null && lastAccessedNode.getParentNode() == parentNode) { parentNode.insertBefore(newElement, lastAccessedNode.getNextSibling()); } else { parentNode.appendChild(newElement); } element = newElement; document.setLastAccessedNode(element); } return element; }
From source file:org.alfresco.web.config.ConfigRuntime.java
protected void insertChildAfter(Node parent, Node child, Node sibling) { if (sibling == null) { if (parent.getFirstChild() != null) { parent.insertBefore(child, parent.getFirstChild()); } else {/*from ww w. j av a 2 s . c om*/ appendChild(parent, child); } } else { if (sibling.getNextSibling() == null) { appendChild(parent, child); } else { parent.insertBefore(child, sibling.getNextSibling()); } } }
From source file:edu.lternet.pasta.datapackagemanager.LevelOneEMLFactory.java
private void appendContact(Document doc) { Element lnoContact = doc.createElement("contact"); Element positionName = doc.createElement("positionName"); positionName.appendChild(doc.createTextNode("Information Manager")); Element organizationName = doc.createElement("organizationName"); organizationName.appendChild(doc.createTextNode("Environmental Data Initiative")); Element address = doc.createElement("address"); Element deliveryPoint1 = doc.createElement("deliveryPoint"); deliveryPoint1.appendChild(doc.createTextNode("Center for Limnology")); Element deliveryPoint2 = doc.createElement("deliveryPoint"); deliveryPoint2.appendChild(doc.createTextNode("University of Wisconsin")); Element city = doc.createElement("city"); city.appendChild(doc.createTextNode("Madison")); Element administrativeArea = doc.createElement("administrativeArea"); administrativeArea.appendChild(doc.createTextNode("WI")); Element postalCode = doc.createElement("postalCode"); postalCode.appendChild(doc.createTextNode("53706")); Element country = doc.createElement("country"); country.appendChild(doc.createTextNode("USA")); address.appendChild(deliveryPoint1); address.appendChild(deliveryPoint2); address.appendChild(city);/*from w w w . java 2 s. c o m*/ address.appendChild(administrativeArea); address.appendChild(postalCode); address.appendChild(country); //Element phone1 = doc.createElement("phone"); //phone1.setAttribute("phonetype", "voice"); //phone1.appendChild(doc.createTextNode("505 277-2535")); //Element phone2 = doc.createElement("phone"); //phone2.setAttribute("phonetype", "fax"); //phone2.appendChild(doc.createTextNode("505 277-2541")); Element electronicMailAddress = doc.createElement("electronicMailAddress"); electronicMailAddress.appendChild(doc.createTextNode("info@environmentaldatainitiative.org")); Element onlineUrl = doc.createElement("onlineUrl"); onlineUrl.appendChild(doc.createTextNode("http://environmentaldatainitiative.org")); lnoContact.appendChild(positionName); lnoContact.appendChild(organizationName); lnoContact.appendChild(address); //lnoContact.appendChild(phone1); //lnoContact.appendChild(phone2); lnoContact.appendChild(electronicMailAddress); lnoContact.appendChild(onlineUrl); NodeList contacts = getContacts(doc); Node datasetNode = getDatasetNode(doc); datasetNode.insertBefore(lnoContact, contacts.item(0)); }
From source file:org.alfresco.web.config.ConfigRuntime.java
/** * @param parent//from w w w . j a v a 2 s .co m * @param child * @param sibling */ protected void insertChildBefore(Node parent, Node child, Node sibling) { if (sibling == null) { if (parent.getFirstChild() == null) { appendChild(parent, child); } else { parent.getFirstChild().insertBefore(child, parent.getFirstChild()); } } else { sibling.insertBefore(child, sibling); } }
From source file:DomUtils.java
/** * Insert the supplied node before the supplied reference node (refNode). * @param newNode Node to be inserted.//ww w.j a v a 2 s . com * @param refNode Reference node before which the supplied nodes should * be inserted. */ public static void insertBefore(Node newNode, Node refNode) { Node parentNode = refNode.getParentNode(); if (parentNode == null) { System.out.println( "Cannot insert [" + newNode + "] before [" + refNode + "]. [" + refNode + "] has no parent."); return; } if (parentNode instanceof Document && newNode.getNodeType() == Node.ELEMENT_NODE) { System.out.println( "Request to insert an element before the Document root node. This is not allowed. Replacing the Document root with the new Node."); parentNode.removeChild(refNode); parentNode.appendChild(newNode); } else { parentNode.insertBefore(newNode, refNode); } }
From source file:Main.java
private static Node replace2TextExceptTags(Document document, Node node, String... tags) { NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node subNode = nodeList.item(i); if (subNode.getNodeType() == Node.TEXT_NODE) { String text = ((Text) subNode).getTextContent(); text = text.replaceAll("[\t\n]+", "").replaceAll(" +", " "); subNode.setTextContent(text); continue; }/* ww w . j a v a 2 s .c o m*/ if (subNode.getNodeType() != Node.ELEMENT_NODE) { continue; } String nodeName = subNode.getNodeName(); boolean excepted = false; for (String tagName : tags) { if (tagName.equals(nodeName)) { excepted = true; replace2TextExceptTags(document, subNode, tags); break; } } if (excepted) { continue; } subNode = replace2TextExceptTags(document, subNode, tags); NodeList childList = subNode.getChildNodes(); List<Node> tempList = new ArrayList<Node>(); for (int j = 0; j < childList.getLength(); j++) { tempList.add(childList.item(j)); } for (Node child : tempList) { node.insertBefore(child, subNode); } node.removeChild(subNode); } return node; }
From source file:de.betterform.xml.dom.DOMUtil.java
/** * Moves a child the given index in a nodelist for a given number of steps. * * @param nodelist the nodelist to work on. if the nodelist is empty, nothing is done * @param index index pointing to the child to move. if the index is not in the list range nothing is done. * @param step the amount of slots to move the child. if step is negative the child is moved up (towards the list * start), if it is positive it is moved down (towards the list end). if the step is zero nothing is done. *//*w w w . j a va 2 s . c o m*/ public static void moveChild(NodeList nodelist, int index, int step) { if ((nodelist == null) || (nodelist.getLength() == 0)) { return; } if ((index >= nodelist.getLength()) || (index < 0)) { return; } if (step == 0) { return; } Node parent = nodelist.item(0).getParentNode(); Node deletedElt = parent.removeChild(nodelist.item(index)); if ((index + step) == (nodelist.getLength() - 1)) { parent.appendChild(deletedElt); } else { // SURE? it seems that after a removeChild the indices of the nodes // in the nodelist seem not to change. Checking the DOM spec the // nodelist is live, but this seems not to be true for index changes // is this a bug, or correct? // Due to this behaviour the following seperation betweem step forward // and backward is necessary. if (step < 0) { parent.insertBefore(deletedElt, nodelist.item(index + step)); } else { parent.insertBefore(deletedElt, nodelist.item(index + step + 1)); } } }
From source file:com.adaptris.util.XmlUtils.java
/** * Convenience method which enables a new Node to be added to a parent at a specified position, by specifying the Node to insert * before. Here is a sample of how to use: * // w ww. j a v a 2 s .c o m * <pre> * {@code * * // Example of how to insert a Node as the 3rd child of a parent * Node p = xmlUtils.getSingleNode("/mydoc/parent"); * Node c = xmlUtils.getSingleNode("/mydoc/parent/child[4]"); * Node n = // Node creation code here; * xmlUtils.insertNodeBefore(newNode, child, parent); * * } * </pre> * * @param newNode the Node to be added * @param existingNode the Node to insert before * @param parent the parent Node * @throws Exception on error. */ public void insertNodeBefore(Node newNode, Node existingNode, Node parent) throws Exception { Document parentDoc = parent.getOwnerDocument(); Document newDoc = newNode.getOwnerDocument(); Node nodeToAdd = newNode; if (!parentDoc.equals(newDoc)) { nodeToAdd = parentDoc.importNode(newNode, true); } parent.insertBefore(nodeToAdd, existingNode); }
From source file:org.piraso.maven.WebXmlPirasoModifier.java
private void insertFilterAndServletElement(Node root, Node insertBefore) { Element filter = createElementNameValue("filter", "filter-name", "filter-class", "pirasoFilter", "org.springframework.web.filter.DelegatingFilterProxy"); Element filterMapping = createElementNameValue("filter-mapping", "filter-name", "url-pattern", "pirasoFilter", "/*"); Element servlet = createElementNameValue("servlet", "servlet-name", "servlet-class", "pirasoServlet", "org.springframework.web.context.support.HttpRequestHandlerServlet", 0); Element servletMapping = createElementNameValue("servlet-mapping", "servlet-name", "url-pattern", "pirasoServlet", pirasoLoggingPath); Node buf1 = document.createTextNode("\n "); Node buf2 = document.createTextNode("\n "); Node buf3 = document.createTextNode("\n "); Node buf4 = document.createTextNode("\n "); root.insertBefore(buf1, insertBefore); root.insertBefore(servletMapping, buf1); root.insertBefore(buf2, servletMapping); root.insertBefore(servlet, buf2);/*from w ww . j a v a2s . com*/ root.insertBefore(buf3, servlet); root.insertBefore(filterMapping, buf3); root.insertBefore(buf4, filterMapping); root.insertBefore(filter, buf4); }
From source file:com.aipo.container.gadgets.parse.AipoNekoSimplifiedHtmlParser.java
private void fixNekoWeirdness(Document document) { // Neko as of versions > 1.9.13 stuffs all leading <script> nodes into // <head>. // This breaks all sorts of assumptions in gadgets, notably the existence of // document.body. // We can't tell Neko to avoid putting <script> into <head> however, since // gadgets//from w w w . j ava 2 s. c o m // like <Content><script>...</script><style>...</style> will break due to // both // <script> and <style> ending up in <body> -- at which point Neko // unceremoniously // drops the <style> (and <link>) elements. // Therefore we just search for <script> elements in <head> and stuff them // all into // the top of <body>. // This method assumes a normalized document as input. Node html = DomUtil.getFirstNamedChildNode(document, "html"); if (html.getNextSibling() != null && html.getNextSibling().getNodeName().equalsIgnoreCase("html")) { // if a doctype is specified, then the desired root <html> node is wrapped // by an <HTML> node // Pull out the <html> root. html = html.getNextSibling(); } Node head = DomUtil.getFirstNamedChildNode(html, "head"); if (head == null) { head = document.createElement("head"); html.insertBefore(head, html.getFirstChild()); } NodeList headNodes = head.getChildNodes(); Stack<Node> headScripts = new Stack<Node>(); for (int i = 0; i < headNodes.getLength(); ++i) { Node headChild = headNodes.item(i); if (headChild.getNodeName().equalsIgnoreCase("script")) { headScripts.add(headChild); } } // Remove from head, add to top of <body> in <head> order. Node body = DomUtil.getFirstNamedChildNode(html, "body"); if (body == null) { body = document.createElement("body"); html.insertBefore(body, head.getNextSibling()); } Node bodyFirst = body.getFirstChild(); while (!headScripts.isEmpty()) { Node headScript = headScripts.pop(); head.removeChild(headScript); body.insertBefore(headScript, bodyFirst); bodyFirst = headScript; } }