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:org.sakaiproject.kernel.component.core.PersistenceUnitClassLoader.java
/** * Insert nodes before a given reference node in a * {@link org.w3c.dom.NodeList}.// www.j av a 2 s .c o m * * @param mappings * @param parentNode * @param refChild */ private void insertBefore(final Set<String> texts, final Document doc, final Node parentNode, final Node refChild, final EntityType type) { for (String text : texts) { Node n = doc.createElement(type.getType()); n.setTextContent(text); parentNode.insertBefore(n, refChild); } }
From source file:org.wso2.carbon.bpmn.core.types.datatypes.xml.api.XMLDocument.java
/** * Function to set/replace/update an object (String / Element) to matching the xPath provided. (In case new element * is added, this api will clone it and merge the new node to the target location pointed by xPath and return the new cloned node) * * @param xPathStr xPath to the location object need to set * @param obj String or Node// w ww.j a v a 2 s . c om * @return returns the node get updated when the set object is String, or returns newly added Node in case object is Element * @throws XPathExpressionException If expression cannot be evaluated * @throws BPMNXmlException is thrown due to : Provided XPath and object does not match, provided object is not a Node or String * result is NodeList, not a Text node or Element */ public Node set(String xPathStr, Object obj) throws XPathExpressionException, BPMNXmlException { NodeList evalResult = (NodeList) Utils.evaluateXPath(this.doc, xPathStr, XPathConstants.NODESET); if (evalResult.getLength() == 1) { Node targetNode = evalResult.item(0); if (obj instanceof String && targetNode instanceof Text) { //if string is provided, assume that user //need to replace the node value targetNode.setNodeValue((String) obj); //return updated Text Node return targetNode; } else if ((obj instanceof Integer || obj instanceof Byte || obj instanceof Character || obj instanceof Short || obj instanceof Long || obj instanceof Float || obj instanceof Double || obj instanceof Boolean) && targetNode instanceof Text) { //need to replace the node value targetNode.setNodeValue(obj.toString()); //return updated Text Node return targetNode; } else if (obj instanceof Element && targetNode instanceof Element && targetNode.getParentNode() != null) { //if the user provides Node object, // assume that need to replace the target node Node targetParent = targetNode.getParentNode(); Node nextSibling = targetNode.getNextSibling(); //remove the target node targetParent.removeChild(targetNode); //add new node Node newNode = doc.importNode((Node) obj, true); if (nextSibling != null) { //If next sibling exists we have to put the new node before it targetParent.insertBefore(newNode, nextSibling); } else { targetParent.appendChild(newNode); } //return new node return newNode; } else { //provided XPath and object to set does not match throw new BPMNXmlException("Provided XPath and provided object does not match"); } } else if (evalResult.getLength() > 0) { throw new BPMNXmlException( "Error in provided xPath. Evaluation result is NodeList, not a Text node or Element"); } else { throw new BPMNXmlException("Error in provided xPath. Evaluation result is not a Text node or Element"); } }
From source file:org.wso2.carbon.bpmn.core.types.datatypes.xml.api.XMLDocument.java
/** * Inserts the node newChild node before the existing node * @param xPathToTargetNode xPath to target node * @param element element to insert// ww w. j a v a 2 s .co m * @return returns the node get inserted on success * @throws XPathExpressionException * @throws BPMNXmlException */ public Node insertBefore(String xPathToTargetNode, Element element) throws XPathExpressionException, BPMNXmlException { Object evalResult = Utils.evaluateXPath(doc, xPathToTargetNode); if (evalResult instanceof Node && evalResult instanceof Element) { Node parentNode = ((Node) evalResult).getParentNode(); if (parentNode != null) { Node newNode = doc.importNode((Node) element, true); return parentNode.insertBefore(newNode, (Node) evalResult); } throw new BPMNXmlException("Target node is the root node (no parent node found)."); } else if (evalResult instanceof NodeList) { throw new BPMNXmlException( (((NodeList) evalResult).getLength() > 0 ? "xpath does not evaluated to a unique parent node" : "xPath evaluation failed. Node does not exists for xPath: " + xPathToTargetNode)); } else { throw new BPMNXmlException("Error in provided xPath. Evaluation result is not a Node." + "The evaluation result is in type:" + evalResult.getClass().getName()); } }
From source file:org.xwiki.xml.html.HTMLUtils.java
/** * Remove the first element inside a parent element and copy the element's children in the parent. * * @param document the w3c document from which to remove the top level paragraph * @param parentTagName the name of the parent tag to look under * @param elementTagName the name of the first element to remove *///from w w w . j av a2 s . c om public static void stripFirstElementInside(Document document, String parentTagName, String elementTagName) { NodeList parentNodes = document.getElementsByTagName(parentTagName); if (parentNodes.getLength() > 0) { Node parentNode = parentNodes.item(0); // Look for a p element below the first parent element Node pNode = parentNode.getFirstChild(); if (elementTagName.equalsIgnoreCase(pNode.getNodeName())) { // Move all children of p node under the root element NodeList pChildrenNodes = pNode.getChildNodes(); while (pChildrenNodes.getLength() > 0) { parentNode.insertBefore(pChildrenNodes.item(0), null); } parentNode.removeChild(pNode); } } }
From source file:ru.runa.wf.web.FormPresentationUtils.java
private static void handleErrors(Map<String, String> errors, String inputName, PageContext pageContext, Document document, Element node) { if (errors.containsKey(inputName)) { String errorText = getErrorText(pageContext, errors, inputName); if ("file".equalsIgnoreCase(node.getAttribute(TYPE_ATTR)) && WebResources.isAjaxFileInputEnabled()) { try { node = (Element) ((Element) ((Element) node.getParentNode()).getParentNode()).getParentNode(); } catch (Exception e) { log.error("Unexpected file input format", e); }/* w ww . java 2s . c om*/ } if (WebResources.useImagesForValidationErrors()) { Element errorImg = document.createElement("img"); errorImg.setAttribute("title", errorText); errorImg.setAttribute("src", Commons.getUrl("/images/error.gif", pageContext, PortletUrlType.Resource)); Node parent = node.getParentNode(); parent.insertBefore(errorImg, node.getNextSibling()); } else { node.setAttribute("title", errorText); if ("select".equalsIgnoreCase(node.getTagName())) { node = wrapSelectToErrorContainer(document, node, inputName, false); } addClassAttribute(node, Resources.CLASS_INVALID); } // avoiding multiple error labels errors.remove(inputName); } }