List of usage examples for org.jdom2 Element addContent
@Override public Element addContent(final int index, final Collection<? extends Content> newContent)
From source file:AIR.Common.xml.XmlElement.java
License:Open Source License
public XmlElement insertBefore(Content node, Content refNode) { if (_element instanceof Element) { Element castedElement = (Element) _element; int existingPosition = castedElement.indexOf(refNode); if (existingPosition < 0) existingPosition = 0;/*ww w . jav a2 s. c o m*/ return new XmlElement(castedElement.addContent(existingPosition, node)); } return null; }
From source file:com.bc.ceres.nbmgen.NbmGenTool.java
License:Open Source License
private static Element getOrAddElement(Element parent, String name, int index, Namespace ns) { Element child = parent.getChild(name, ns); if (child == null) { child = new Element(name, ns); if (index >= 0) { parent.addContent(index, child); } else {/*from w ww . j a v a2 s .co m*/ parent.addContent(child); } } return child; }
From source file:com.c4om.autoconf.ulysses.extra.svinchangesetgenerator.SVINChangesetGenerator.java
License:Apache License
/** * Recursive method that mixes two XML trees in the following way: * <ul>// w ww . java 2s. c o m * <li>If a child exists at the source leaf but not at destination, the * element is copied as a child to the destination leaf</li> * <li>If a child exists at both the source and the destination leafs and * the source child has children, this method is recursively called to mix * both children</li> * </ul> * Some important remarks: * <ul> * <li>Equality comparison is not made via common methods but via * {@link JDOMUtils#elementsEqualAtCNameAndAttributes(Element, Element)} * .</li> * <li>Results of this method are returned as changes to the * destinationLeaf.</li> * <li>An attribute may be appended to all the elements added to the * destination leaf by this method.</li> * <li>Elements of a concrete namespace can be ignored by this method, if desired.</li> * </ul> * * @param sourceLeaf * The source leaf to mix into the destination leaf. It remains * unchanged. * @param destinationLeaf * The destination leaf, where the source leaf will be mixed * into. Results will be returned as changes at this element, so * IT WILL NOT REMAIN UNCHANGED AFTER THIS METHOD (normally). You * should consider using {@link Element#clone()} if necessary. * @param metadataAttributeToAppend * an attribute to be appended to each element added by this * method to the destinationLeaf. If a node with descendants is * added, the attribute will be added only to the top-level * element (not to all the descendants). If null, no attribute * will be added. * @param ignoreNamespaceURIIfTextPresent any element whose namespace URI matches this one * will be ignored if the source element has text content. * If null, no element is ignored. */ private void mixTreesRecursive(Element sourceLeaf, Element destinationLeaf, Attribute metadataAttributeToAppend, String ignoreNamespaceURIIfTextPresent) { List<Content> sourceLeafContent = sourceLeaf.getContent(); //j is the index for "only-element" content for (int i = 0, j = 0; i < sourceLeafContent.size(); i++) { Content currentContent = sourceLeafContent.get(i); if (!(currentContent instanceof Element)) { continue; } Element currentSourceChild = (Element) currentContent; Element currentDestinationChild = searchElementEqualAtCNameAndAttributes(destinationLeaf.getChildren(), currentSourceChild); if (currentDestinationChild == null) { if (ignoreNamespaceURIIfTextPresent != null && !destinationLeaf.getTextNormalize().equals("") && ignoreNamespaceURIIfTextPresent.equals(currentSourceChild.getNamespaceURI())) { continue; } // There is not equivalent node at destination, so we copy the // whole currentSourceChild. Element elementToAdd = currentSourceChild.clone(); if (metadataAttributeToAppend != null) { elementToAdd.setAttribute(metadataAttributeToAppend.clone()); } destinationLeaf.addContent(j, elementToAdd); } else { // Element exists at destination. If it has children, we recurse // to fill them (they might be not completely filled). if (currentSourceChild.getChildren().size() > 0) { mixTreesRecursive(currentSourceChild, currentDestinationChild, metadataAttributeToAppend, ignoreNamespaceURIIfTextPresent); } } j++; } }
From source file:com.tactfactory.harmony.generator.androidxml.ManifestUpdater.java
License:Open Source License
/** * Add a permission to manifest.//from w w w. j a v a2 s . c o m * @param permission The permission name */ public void addPermission(final String permission) { // Load Root element final Element rootNode = this.getDocument().getRootElement(); // Load Name space (required for manipulate attributes) final Namespace ns = rootNode.getNamespace(ManifestConstants.NAMESPACE_ANDROID); boolean setPerm = true; for (Element elem : rootNode.getChildren(ManifestConstants.ELEMENT_PERMISSION)) { if (elem.getAttributeValue(ManifestConstants.ATTRIBUTE_NAME, ns).equals(permission)) { setPerm = false; break; } } if (setPerm) { final Element permissionElem = new Element(ManifestConstants.ELEMENT_PERMISSION); permissionElem.setAttribute(ManifestConstants.ATTRIBUTE_NAME, permission, ns); rootNode.addContent(2, permissionElem); } }
From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.FunctionNormalizer.java
License:Apache License
private void normalizeFunctionApplication(final Element element, final Collection<String> functionOperators) { assert element != null && functionOperators != null; final List<Element> children = element.getChildren(); for (int i = 0; i < children.size(); i++) { if (isFunction(i, children, functionOperators)) { final int parameterPosition = i + 2; Element parameter = children.get(parameterPosition); // mrow in which the parameter will be stored final Element newParameter = new Element(ROW); if (parameter.getName().equals(ROW)) { if (hasInsideBrackets(parameter)) { children.get(i + 1).detach(); // just detach operator } else { // add parentheses parameter.addContent(1, new Element(OPERATOR).setText("(")); parameter.addContent(new Element(OPERATOR).setText(")")); LOGGER.fine("Parentheses around function argument added"); children.get(i + 1).detach(); // detach funct app operator }/* w ww .j av a 2 s. c o m*/ LOGGER.fine("Function application operator removed"); continue; // no need to set newParameter } else if (isOperator(parameter, "(")) { int bracketsDepth = 1; newParameter.addContent(parameter.detach()); while ((parameterPosition < children.size()) && (bracketsDepth > 0)) { parameter = children.get(parameterPosition); if (isOperator(parameter, "(")) { bracketsDepth++; } else if (isOperator(parameter, ")")) { bracketsDepth--; } newParameter.addContent(parameter.detach()); } for (; bracketsDepth > 0; bracketsDepth--) { // add missing right brackets newParameter.addContent(new Element(OPERATOR).setText(")")); LOGGER.fine("Added missing )"); } } else { // if the paramether is neither mrow or ( newParameter.addContent(new Element(OPERATOR).setText("(")); // add left bracket newParameter.addContent(children.get(parameterPosition).detach()); newParameter.addContent(new Element(OPERATOR).setText(")")); // add right bracket LOGGER.fine("Function argument wrapped with parentheses and mrow"); } children.set(i + 1, newParameter); // replace function app operator with newParameter LOGGER.fine("Function application operator removed"); } else { // if there isnt start of function application apply normalization on children normalizeFunctionApplication(children.get(i), functionOperators); } } }
From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.MrowNormalizer.java
License:Apache License
private static void removeElement(final Element element, final Element parent) { assert element != null && parent != null; parent.addContent(parent.indexOf(element), element.cloneContent()); element.detach();//from www. j ava 2 s . c o m }
From source file:de.danielluedecke.zettelkasten.database.DesktopData.java
License:Open Source License
/** * This method moves an element (entry-node or bullet-point) one position up- or downwards, depending * on the parameter {@code movement}./*from w ww. j ava 2 s. c o m*/ * @param movement indicates whether the element should be moved up or down. use following constants:<br> * - {@code CConstants.MOVE_UP}<br> * - {@code CConstants.MOVE_DOWN}<br> * @param timestamp */ public void moveElement(int movement, String timestamp) { // get the selected element, independent from whether it's a node or a bullet Element e = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp); if (e != null) { // get the element's parent Element p = e.getParentElement(); // get the index of the element that should be moved int index = p.indexOf(e); // remove the element that should be moved Element dummy = (Element) p.removeContent(index); try { // and insert element one index-position below the previous index-position p.addContent((Constants.MOVE_UP == movement) ? index - 1 : index + 1, dummy); // change modifed state setModified(true); } catch (IllegalAddException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } } }
From source file:de.danielluedecke.zettelkasten.database.DesktopData.java
License:Open Source License
/** * This method adds a new entry (child-node) to the xml-document. * /*from ww w . jav a2 s .c o m*/ * @param timestamp the timestamp of the element, where the entry "nr" should be inserted as new child * @param nr the entry-number of the entry that should be added * @param insertpos the position where the new entry should be inserted. necessary when we have already * more children and the entry should be inserted in between, at the beginning or end of the children-list. * @return the timestamp of the added entry-element as {@code String} or {@code null} if an error * occured. */ public String addEntry(String timestamp, String nr, int insertpos) { // find the bullet that is described in the treepath Element b = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp); // if we have a valid bullet, add the new enry to the xml-file if (b != null) { // check whether element is a bullet, if not, retrieve its parent element if (!b.getName().equals(ELEMENT_BULLET)) b = b.getParentElement(); // create a new element Element e = new Element(ELEMENT_ENTRY); try { e.setAttribute("id", nr); // create timestamp String ts = Tools.getTimeStampWithMilliseconds(); // check whether timestamp already exists. this is particulary the // case when a user adds several entries at once. while (timeStampExists(ts)) ts = Tools.getTimeStampWithMilliseconds(); // add timestamp to entry element e.setAttribute(ATTR_TIMESTAMP, ts); // add new enry to the bullet at insert-position+1 (because at first // position in the bullet is always the comment) b.addContent(insertpos, e); // change modified state setModified(true); // return timestamp return ts; } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } catch (IllegalAddException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } } return null; }