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:Main.java
private static void makeNamelist(Document doc) { String names = null;/*w w w . ja va 2 s .co m*/ Element root = doc.getDocumentElement(); NodeList nameElements = root.getElementsByTagName("name"); for (int i = 0; i < nameElements.getLength(); i++) { Element name = (Element) nameElements.item(i); Text nametext = (Text) name.getFirstChild(); if (names == null) { names = nametext.getData(); } else { names += ", " + nametext.getData(); } } Element namelist = doc.createElement("names"); Text namelisttext = doc.createTextNode(names); namelist.appendChild(namelisttext); root.insertBefore(namelist, root.getFirstChild()); }
From source file:Main.java
public static Node createNodeFromPath(Element modelElement, String path) { Document document = modelElement.getOwnerDocument(); StringTokenizer st = new StringTokenizer(path, "/"); while (st.hasMoreTokens()) { String t = st.nextToken(); if (st.hasMoreTokens()) { if (t.equals("..")) { modelElement = (Element) modelElement.getParentNode(); } else { Element elm = getFirstChildElement(modelElement, t); if (elm == null) modelElement = (Element) modelElement.insertBefore(document.createElement(t), getFirstChildElement(modelElement, t)); else modelElement = elm;// w ww .j av a 2 s .c o m } } else { modelElement = (Element) modelElement.insertBefore(document.createElement(t), getFirstChildElement(modelElement, t)); } } return modelElement; }
From source file:Main.java
/** * Set the text content of an element. All exisitng Text Node are * removed before adding a new Text Node containing the given text. * * @param element target element to set text content, cannot be null. * @param text content of the element, cannot be null. *///from ww w . j a va 2s.c o m public static void setElementText(Element element, String text) { // Remove all text element NodeList list = element.getChildNodes(); int len = list.getLength(); for (int i = 0; i < len; i++) { Node n = list.item(i); if (n.getNodeType() == Node.TEXT_NODE) { element.removeChild(n); } } Node child = element.getFirstChild(); Node textnode = element.getOwnerDocument().createTextNode(text); // insert text node as first child if (child == null) { element.appendChild(textnode); } else { element.insertBefore(textnode, child); } }
From source file:DOMEdit.java
private static void makeNamelist(Document doc) { String names = null;/* ww w. j a va 2 s .co m*/ Element root = doc.getDocumentElement(); NodeList nameElements = root.getElementsByTagName("name"); for (int i = 0; i < nameElements.getLength(); i++) { Element name = (Element) nameElements.item(i); Text nametext = (Text) name.getFirstChild(); if (names == null) { names = nametext.getData(); } else { names += ", " + nametext.getData(); } } Element namelist = doc.createElement("names"); Text namelisttext = doc.createTextNode(names); namelist.appendChild(namelisttext); root.insertBefore(namelist, root.getFirstChild()); }
From source file:Main.java
public static void addHook(String hookName, String interfaceName, String identity, boolean superOverride, String superOverrideClass) { try {/* w ww .ja v a2 s . c o m*/ DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBldr = dFactory.newDocumentBuilder(); Document doc = dBldr.parse(new File("hooks.xml")); Element hooks = doc.getDocumentElement(); Element hook = doc.createElement("hook"); hook.setAttribute("name", hookName); Element eIdentity = doc.createElement("identity"); eIdentity.appendChild(doc.createTextNode(identity)); hook.appendChild(eIdentity); Element eInterface = doc.createElement("interface"); eInterface.appendChild(doc.createTextNode(interfaceName)); hook.appendChild(eInterface); Element eSuper = doc.createElement("super"); if (superOverride) { eSuper.appendChild(doc.createTextNode(superOverrideClass)); } else { eSuper.appendChild(doc.createTextNode("null")); } hook.appendChild(eSuper); hooks.insertBefore(hook, hooks.getLastChild()); write(doc); } catch (SAXException | IOException | ParserConfigurationException e) { e.printStackTrace(); } }
From source file:com.evolveum.midpoint.prism.util.PrismUtil.java
private static void fortifyNamespaceDeclarations(Element definitionElement, Element childElement) { Document doc = definitionElement.getOwnerDocument(); NamedNodeMap attributes = childElement.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (DOMUtil.isNamespaceDefinition(attr)) { String prefix = DOMUtil.getNamespaceDeclarationPrefix(attr); String namespace = DOMUtil.getNamespaceDeclarationNamespace(attr); Element namespaceElement = doc.createElementNS(PrismConstants.A_NAMESPACE.getNamespaceURI(), PrismConstants.A_NAMESPACE.getLocalPart()); namespaceElement.setAttribute(PrismConstants.A_NAMESPACE_PREFIX, prefix); namespaceElement.setAttribute(PrismConstants.A_NAMESPACE_URL, namespace); definitionElement.insertBefore(namespaceElement, childElement); }/* w ww. j a v a2s . co m*/ } }
From source file:Main.java
/** * Append a child element to the parent at the specified location. * * Starting with a valid document, append an element according to the schema * sequence represented by the <code>order</code>. All existing child * elements must be include as well as the new element. The existing child * element following the new child is important, as the element will be * 'inserted before', not 'inserted after'. * * @param parent parent to which the child will be appended * @param el element to be added//from www. ja va 2 s .com * @param order order of the elements which must be followed * @throws IllegalArgumentException if the order cannot be followed, either * a missing existing or new child element * is not specified in order * * @since 8.4 */ public static void appendChildElement(Element parent, Element el, String[] order) throws IllegalArgumentException { List<String> l = Arrays.asList(order); int index = l.indexOf(el.getLocalName()); // ensure the new new element is contained in the 'order' if (index == -1) { throw new IllegalArgumentException( "new child element '" + el.getLocalName() + "' not specified in order " + l); // NOI18N } List<Element> elements = findSubElements(parent); Element insertBefore = null; for (Element e : elements) { int index2 = l.indexOf(e.getLocalName()); // ensure that all existing elements are in 'order' if (index2 == -1) { throw new IllegalArgumentException( "Existing child element '" + e.getLocalName() + "' not specified in order " + l); // NOI18N } if (index2 > index) { insertBefore = e; break; } } parent.insertBefore(el, insertBefore); }
From source file:com.sitewhere.configuration.ConfigurationMigrationSupport.java
/** * Perform the migration work.// w w w. j a v a2 s . c o m * * @param document * @throws SiteWhereException */ protected static void migrateTenantConfiguration(Document document) throws SiteWhereException { Element beans = document.getDocumentElement(); Element config = DomUtils.getChildElementByTagName(beans, "tenant-configuration"); if (config == null) { throw new SiteWhereException("Tenant configuration element not found."); } Element dcomm = DomUtils.getChildElementByTagName(config, "device-communication"); if (dcomm == null) { throw new SiteWhereException("Device communication element missing."); } Element asset = DomUtils.getChildElementByTagName(config, "asset-management"); if (asset == null) { throw new SiteWhereException("Asset management element missing."); } Element eproc = DomUtils.getChildElementByTagName(config, "event-processing"); if (eproc == null) { LOGGER.info("Event processing element missing. Migrating to new configuration format."); eproc = document.createElementNS(config.getNamespaceURI(), "event-processing"); eproc.setPrefix(config.getPrefix()); config.insertBefore(eproc, asset); moveEventProcessingElements(config, dcomm, eproc, document); } document.normalizeDocument(); }
From source file:com.amalto.core.history.accessor.ManyFieldAccessor.java
public void insert() { Document domDocument = document.asDOM(); Element parentNode = (Element) parent.getNode(); NodeList children = parentNode.getElementsByTagName(fieldName); Node refNode = children.item(index); Node node = domDocument.createElementNS(domDocument.getNamespaceURI(), fieldName); parentNode.insertBefore(node, refNode); cachedCollectionItemNode = (Element) node; }
From source file:com.aipo.container.gadgets.render.AipoRenderingGadgetRewriter.java
@Override public void rewrite(Gadget gadget, MutableContent mutableContent) throws RewritingException { // Don't touch sanitized gadgets. if (gadget.sanitizeOutput()) { return;//from w w w . j a v a 2 s. co m } try { Document document = mutableContent.getDocument(); Element head = (Element) DomUtil.getFirstNamedChildNode(document.getDocumentElement(), "head"); // Insert new content before any of the existing children of the head // element Node firstHeadChild = head.getFirstChild(); Element contentLanguage = document.createElement("meta"); contentLanguage.setAttribute("http-equiv", "Content-Language"); contentLanguage.setAttribute("content", "ja"); head.insertBefore(contentLanguage, firstHeadChild); Element contentType = document.createElement("meta"); contentType.setAttribute("http-equiv", "Content-Type"); contentType.setAttribute("content", "text/html; charset=UTF-8"); head.insertBefore(contentType, firstHeadChild); Element contentStyleType = document.createElement("meta"); contentStyleType.setAttribute("http-equiv", "Content-Style-Type"); contentStyleType.setAttribute("content", "text/css"); head.insertBefore(contentStyleType, firstHeadChild); Element contentScriptType = document.createElement("meta"); contentScriptType.setAttribute("http-equiv", "Content-Script-Type"); contentScriptType.setAttribute("content", "text/javascript"); head.insertBefore(contentScriptType, firstHeadChild); // Only inject default styles if no doctype was specified. // if (document.getDoctype() == null) { boolean isAipoStyle = gadget.getAllFeatures().contains("aipostyle") && gadget.getSpec().getModulePrefs().getFeatures().keySet().contains("aipostyle"); Element defaultStyle = document.createElement("style"); defaultStyle.setAttribute("type", "text/css"); head.insertBefore(defaultStyle, firstHeadChild); defaultStyle.appendChild( defaultStyle.getOwnerDocument().createTextNode(isAipoStyle ? aipoStyleCss : DEFAULT_CSS)); // } injectBaseTag(gadget, head); injectGadgetBeacon(gadget, head, firstHeadChild); injectFeatureLibraries(gadget, head, firstHeadChild); // This can be one script block. Element mainScriptTag = document.createElement("script"); GadgetContext context = gadget.getContext(); MessageBundle bundle = messageBundleFactory.getBundle(gadget.getSpec(), context.getLocale(), context.getIgnoreCache(), context.getContainer()); injectMessageBundles(bundle, mainScriptTag); injectDefaultPrefs(gadget, mainScriptTag); injectPreloads(gadget, mainScriptTag); // We need to inject our script before any developer scripts. head.insertBefore(mainScriptTag, firstHeadChild); Element body = (Element) DomUtil.getFirstNamedChildNode(document.getDocumentElement(), "body"); body.setAttribute("dir", bundle.getLanguageDirection()); injectOnLoadHandlers(body); mutableContent.documentChanged(); } catch (GadgetException e) { throw new RewritingException(e.getLocalizedMessage(), e, e.getHttpStatusCode()); } }