List of usage examples for org.w3c.dom Element insertBefore
public Node insertBefore(Node newChild, Node refChild) throws DOMException;
newChild
before the existing child node refChild
. From source file:com.evolveum.midpoint.util.DOMUtil.java
public static Element getOrCreateAsFirstElement(Element parentElement, QName elementQName) { Element element = getChildElement(parentElement, elementQName); if (element != null) { return element; }/* ww w. jav a 2 s. co m*/ Document doc = parentElement.getOwnerDocument(); element = doc.createElementNS(elementQName.getNamespaceURI(), elementQName.getLocalPart()); parentElement.insertBefore(element, getFirstChildElement(parentElement)); return element; }
From source file:com.amalto.core.history.accessor.ManyFieldAccessor.java
public void create() { parent.create();//from ww w . j av a 2 s . c om // TODO Refactor this Document domDocument = document.asDOM(); Node node = getCollectionItemNode(); if (node == null) { Element parentNode = (Element) parent.getNode(); NodeList children = parentNode.getElementsByTagName(fieldName); int currentCollectionSize = children.getLength(); if (currentCollectionSize > 0) { Node refNode = children.item(currentCollectionSize - 1).getNextSibling(); while (currentCollectionSize <= index) { node = domDocument.createElementNS(domDocument.getNamespaceURI(), fieldName); parentNode.insertBefore(node, refNode); currentCollectionSize++; } } else { // Collection is not present at all, append at the end of parent element. Node lastAccessedNode = document.getLastAccessedNode(); if (lastAccessedNode != null) { Node refNode = lastAccessedNode.getNextSibling(); while (refNode != null && !(refNode instanceof Element)) { refNode = refNode.getNextSibling(); } while (currentCollectionSize <= index) { node = domDocument.createElementNS(domDocument.getNamespaceURI(), fieldName); if (lastAccessedNode == parentNode) { if (lastAccessedNode == document.asDOM().getDocumentElement() && lastAccessedNode.getChildNodes().getLength() > 0) parentNode.insertBefore(node, parentNode.getFirstChild()); else parentNode.appendChild(node); } else if (refNode != null && refNode.getParentNode() == parentNode) { parentNode.insertBefore(node, refNode); } else { parentNode.appendChild(node); } currentCollectionSize++; } } else { while (currentCollectionSize <= index) { node = domDocument.createElementNS(domDocument.getNamespaceURI(), fieldName); parentNode.appendChild(node); currentCollectionSize++; } } } document.setLastAccessedNode(node); } else if (node.getChildNodes().getLength() == 0) { // This accessor creates (n-1) empty elements when accessing first collection element at index n. // This setLastAccessedNode call allows all (n-1) elements to find their parent. if (!(node.getLocalName().equals(document.getLastAccessedNode().getLocalName()) && document.getLastAccessedNode().getParentNode() == node.getParentNode())) { // if last accessed node is parallel with this node, can't modify last accessed node. eg, last accessed // node=/feature/vendor[2], this node=/feature/vendor[1], the last accessed is still /feature/vendor[2] document.setLastAccessedNode(node); } } }
From source file:com.icesoft.faces.context.DOMResponseWriter.java
private Element fixHead() { Element html = document.getDocumentElement(); Element head = document.createElement("head"); html.insertBefore(head, html.getFirstChild()); return head;/*from w ww .j a v a 2s . c om*/ }
From source file:com.redhat.plugin.eap6.EAP6DeploymentStructureMojo.java
protected void buildDeploymentStructure(Document doc, Map<Artifact, String> moduleMap, List<SubDeployment> subdeployments) throws MojoFailureException, XPathExpressionException { Element root = doc.getDocumentElement(); if (!root.getTagName().equals("jboss-deployment-structure")) throw new MojoFailureException("Root element is not jboss-deployment-structure"); Element deployment = (Element) xp_deployment.evaluate(doc, XPathConstants.NODE); if (deployment == null) { deployment = doc.createElement("deployment"); root.insertBefore(deployment, root.getFirstChild()); }//from w w w . ja v a 2 s.c o m Element depDependencies = (Element) xp_dependencies.evaluate(deployment, XPathConstants.NODE); if (depDependencies == null) { depDependencies = doc.createElement("dependencies"); deployment.appendChild(depDependencies); } Collection<String> mods = moduleMap.values(); getLog().debug("From project-dependencies" + mods); fillModuleEntries(doc, depDependencies, mods); getLog().debug("Element <" + depDependencies.getTagName() + ">: " + depDependencies.getChildNodes().getLength() + " elements"); if (subdeployments != null && !subdeployments.isEmpty()) { for (SubDeployment sd : subdeployments) { XPathExpression xp = xpf.newXPath() .compile("/jboss-deployment-structure/sub-deployment [@name='" + sd.getName() + "']"); Element subEl = (Element) xp.evaluate(doc, XPathConstants.NODE); if (subEl == null) { getLog().debug("Creating sub-deployment-section for <" + sd.getName() + ">"); subEl = doc.createElement("sub-deployment"); root.appendChild(subEl); subEl.setAttribute("name", sd.getName()); } Element subDependencies = (Element) xp_dependencies.evaluate(subEl, XPathConstants.NODE); if (subDependencies == null) { subDependencies = doc.createElement("dependencies"); subEl.appendChild(subDependencies); } Set<String> modules = new HashSet<String>(); xp = xpf.newXPath().compile("/jboss-deployment-structure/deployment/dependencies/module/@name"); NodeList nl = (NodeList) xp.evaluate(sd.getDocument(), XPathConstants.NODESET); int n = nl.getLength(); for (int i = 0; i < n; i++) { if (moduleMap.values().contains(nl.item(i).getTextContent())) continue; modules.add(nl.item(i).getTextContent()); } getLog().debug("From sub-deployment <" + sd.getName() + ">:" + modules); fillModuleEntries(doc, subDependencies, modules); getLog().debug("Child-Elements for <" + subEl.getAttribute("name") + ">: " + subEl.getChildNodes().getLength()); getLog().debug("Element <" + subEl.getTagName() + "." + subDependencies.getTagName() + ">: " + subDependencies.getChildNodes().getLength() + " elements"); } } NodeList nlSub = (NodeList) xp_subdeployment.evaluate(doc, XPathConstants.NODESET); int nSub = nlSub.getLength(); getLog().debug("Retrieved subdeployment-sections (" + nSub + ")"); }
From source file:com.enonic.esl.xml.XMLTool.java
public static Element createElementBeforeChild(Element root, Element child, String name, String text) { if (name == null) { throw new XMLToolException("Element name cannot be null!"); } else if (name.trim().length() == 0) { throw new XMLToolException("Element name has to contain at least one character!"); }/*from w w w . ja va 2 s . c om*/ Document doc = root.getOwnerDocument(); Element elem = doc.createElement(name); if (text != null) { Text textNode = doc.createTextNode(text); elem.appendChild(textNode); } root.insertBefore(elem, child); return elem; }
From source file:com.haulmont.cuba.restapi.XMLConverter.java
Document decorate(Document doc, String uri) { Element root = doc.getDocumentElement(); Element instance = (Element) root.getElementsByTagName(ELEMENT_INSTANCE).item(0); Element uriElement = doc.createElement(ELEMENT_URI); uriElement.setTextContent(uri == null ? NULL_VALUE : uri); root.insertBefore(uriElement, instance); return doc;//from ww w.ja va 2s .com }
From source file:com.meltmedia.cadmium.core.util.WarUtils.java
/** * <p>Adds a child xml element to a parent element relative to other elements with a tagname.</p> * <p>If first is true then the child is added before any elements with a tagname, the opposite happens when first is false.</p> * @param parent The parent xml element. * @param child The child xml element to add. * @param tagname The tagname to be relative to. * @param first A flag that describes the relative relationship between the child element and its siblings named by tagname. *///from w w w.j av a 2 s . c o m public static void addRelativeTo(Element parent, Element child, String tagname, boolean first) { NodeList nodes = parent.getChildNodes(); if (nodes.getLength() > 0) { Node relativeEl = null; boolean found = false; for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element el = (Element) node; if (el.getTagName().equals(tagname)) { if (relativeEl == null || !first) { relativeEl = el; } else if (first && !found) { relativeEl = el; } found = true; } } } if (relativeEl != null && !first) { relativeEl = relativeEl.getNextSibling(); } if (relativeEl == null && first) { relativeEl = nodes.item(0); } else { parent.appendChild(child); } if (relativeEl != null) { parent.insertBefore(child, relativeEl); } } else { //There are no elements in the parent node so lets just append child. parent.appendChild(child); } }
From source file:com.bstek.dorado.view.config.XmlDocumentPreprocessor.java
private void mergeArguments(Document templateDocument, TemplateContext templateContext) throws Exception { Document document = templateContext.getSourceDocument(); Map<String, Element> argumentMap = getArgumentElements(document, templateContext.getSourceContext()); Map<String, Element> templateArgumentMap = getArgumentElements(templateDocument, templateContext); if (!argumentMap.isEmpty()) { Element templateDocumentElement = templateDocument.getDocumentElement(); Element argumentsElement = ViewConfigParserUtils.findArgumentsElement(templateDocumentElement, templateContext.getResource()); if (argumentsElement == null) { argumentsElement = templateDocument.createElement(ViewXmlConstants.ARGUMENTS); templateDocumentElement.insertBefore(argumentsElement, templateDocumentElement.getFirstChild()); }//from ww w. j a va 2 s. c o m for (Map.Entry<String, Element> entry : argumentMap.entrySet()) { String name = entry.getKey(); if (templateArgumentMap == null || !templateArgumentMap.containsKey(name)) { Element element = entry.getValue(); Element clonedElement = (Element) templateDocument.importNode(element, true); argumentsElement.appendChild(clonedElement); } } } }
From source file:com.evolveum.midpoint.prism.schema.SchemaToDomProcessor.java
private Element getOrCreateElement(QName qName, Element parentElement) { NodeList elements = parentElement.getElementsByTagNameNS(qName.getNamespaceURI(), qName.getLocalPart()); if (elements.getLength() == 0) { Element element = createElement(qName); Element refChild = DOMUtil.getFirstChildElement(parentElement); parentElement.insertBefore(element, refChild); return element; }//from w w w.j av a 2s. c om return (Element) elements.item(0); }
From source file:org.gvnix.web.exception.handler.roo.addon.addon.WebModalDialogOperationsImpl.java
/** * Adds the element util:message-box in the right place in default.jspx * layout//from w w w. j a va2 s.c om */ private void addMessageBoxInLayout() { PathResolver pathResolver = projectOperations.getPathResolver(); String defaultJspx = pathResolver.getIdentifier(LogicalPath.getInstance(Path.SRC_MAIN_WEBAPP, ""), "WEB-INF/layouts/default.jspx"); if (!fileManager.exists(defaultJspx)) { // layouts/default.jspx doesn't exist, so nothing to do return; } InputStream defulatJspxIs = fileManager.getInputStream(defaultJspx); Document defaultJspxXml; try { defaultJspxXml = XmlUtils.getDocumentBuilder().parse(defulatJspxIs); } catch (Exception ex) { throw new IllegalStateException("Could not open default.jspx file", ex); } Element lsHtml = defaultJspxXml.getDocumentElement(); // Set dialog tag lib as attribute in html element lsHtml.setAttribute("xmlns:dialog", "urn:jsptagdir:/WEB-INF/tags/dialog/modal"); Element messageBoxElement = DomUtils.findFirstElementByName("dialog:message-box", lsHtml); if (messageBoxElement == null) { Element divMain = XmlUtils.findFirstElement("/html/body/div/div[@id='main']", lsHtml); Element insertAttributeBodyElement = XmlUtils .findFirstElement("/html/body/div/div/insertAttribute[@name='body']", lsHtml); Element messageBox = new XmlElementBuilder("dialog:message-box", defaultJspxXml).build(); divMain.insertBefore(messageBox, insertAttributeBodyElement); } writeToDiskIfNecessary(defaultJspx, defaultJspxXml.getDocumentElement()); }