Example usage for org.jdom2 Element setText

List of usage examples for org.jdom2 Element setText

Introduction

In this page you can find the example usage for org.jdom2 Element setText.

Prototype

public Element setText(final String text) 

Source Link

Document

Sets the content of the element to be the text given.

Usage

From source file:no.imr.stox.functions.utils.JDOMUtils.java

public static void insertElement(Element elm, int idx, String name, String text) {
    if (name == null || text == null || name.isEmpty() || text.isEmpty()) {
        return;// w  w  w  .  j  a  v a2s .c  o  m
    }
    if (elm == null) {
        return;
    }
    Element c = new Element(name, elm.getNamespace());
    c.setText(text);
    if (idx == -1) {
        elm.addContent(c);
    } else {
        elm.addContent(idx, c);
    }
}

From source file:odml.core.Writer.java

License:Open Source License

/**
 * Creates the Dom document from the section.
 *
 * @param rootSection {@link Section}: the section to start the dom creation.
 * @param asTerminology {@link boolean}: flag to indicate whether Template is used or not
 *
 *//* w  w w  .  j av  a  2 s .  c  om*/
private void createDom(Section rootSection, boolean asTerminology) {
    doc = new Document();
    ProcessingInstruction instruction;
    ProcessingInstruction alternativeInstruction;
    if (asTerminology) {
        alternativeInstruction = new ProcessingInstruction("xml-stylesheet",
                "type=\"text/xsl\" href=\"odml.xsl\"");
        instruction = new ProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"odmlTerms.xsl\"");
    } else {
        alternativeInstruction = new ProcessingInstruction("xml-stylesheet",
                "type=\"text/xsl\" href=\"odmlTerms.xsl\"");
        instruction = new ProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"odml.xsl\"");
    }
    doc.addContent(instruction);
    doc.addContent(alternativeInstruction);
    Element rootElement = new Element("odML");
    rootElement.setAttribute("version", "1");
    doc.setRootElement(rootElement);

    Section dummyRoot;
    if (rootSection.propertyCount() != 0) {
        dummyRoot = new Section();
        dummyRoot.add(rootSection);
        dummyRoot.setDocumentAuthor(rootSection.getDocumentAuthor());
        dummyRoot.setDocumentDate(rootSection.getDocumentDate());
        dummyRoot.setDocumentVersion(rootSection.getDocumentVersion());
        dummyRoot.setRepository(rootSection.getRepository());
    } else {
        dummyRoot = rootSection;
    }
    String author = dummyRoot.getDocumentAuthor();
    if (author != null) {
        Element authorElement = new Element("author");
        authorElement.setText(author);
        rootElement.addContent(authorElement);
    }
    String version = dummyRoot.getDocumentVersion();
    if (version != null) {
        Element versionElement = new Element("version");
        versionElement.setText(version);
        rootElement.addContent(versionElement);
    }
    String dateString;
    Date date = dummyRoot.getDocumentDate();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    if (date != null) {
        dateString = sdf.format(date);
    } else {
        date = new Date(Calendar.getInstance().getTimeInMillis());
        dateString = sdf.format(date);
    }
    Element dateElement = new Element("date");
    dateElement.setText(dateString);
    rootElement.addContent(dateElement);
    URL repository = dummyRoot.getRepository();
    if (repository != null) {
        Element repElement = new Element("repository");
        repElement.setText(repository.toString());
        rootElement.addContent(repElement);
    }
    for (int i = 0; i < dummyRoot.sectionCount(); i++) {
        appendSection(rootElement, dummyRoot.getSection(i), asTerminology);
    }
}

From source file:odml.core.Writer.java

License:Open Source License

/**
 * Add a new {@link org.jdom2.Element} to a parent Element. If content is not null or empty the toString method is
 * invoked to store the content.//from w  ww  . java 2s .  c  o m
 *
 * @param parent - {@link org.jdom2.Element} the parent Element.
 * @param name - {@link java.lang.String} the new elements name.
 * @param content - {@link java.util.Objects} the content.
 */
private void addElement(Element parent, String name, Object content) {
    if (content == null || content.toString().isEmpty()) {
        return;
    }
    Element element = new Element(name);
    element.setText(content.toString());
    parent.addContent(element);
}

From source file:org.apache.marmotta.maven.plugins.refpack.RefPackMojo.java

License:Apache License

private void writeModuleXML(Artifact module, OutputStream out) throws IOException {
    Element installation = new Element("installation");
    installation.setAttribute("version", "1.0");

    Element packs = new Element("packs");
    installation.addContent(packs);/*w  ww.  j a  va2  s  .  co  m*/

    Element pack = new Element("pack");
    packs.addContent(pack);

    // get the model for the artifact, we read name and description from it

    Model pom = getArtifactModel(module);

    // set name of pack from artifact
    if (pom != null && pom.getName() != null) {
        pack.setAttribute("name", pom.getName());
    } else {
        pack.setAttribute("name", module.getArtifactId());
    }

    if (pom != null && pom.getDescription() != null) {
        Element description = new Element("description");
        description.setText(Text.normalizeString(pom.getDescription()));
        pack.addContent(description);
    }

    // add a file entry for the module itself
    if (!module.getExtension().equals("war")) {
        Element mainFile = new Element("file");
        pack.addContent(mainFile);
        mainFile.setAttribute("src", module.getFile().getAbsolutePath());
        mainFile.setAttribute("targetdir",
                "$INSTALL_PATH/apache-tomcat-$TOMCAT_VERSION/webapps/marmotta/WEB-INF/lib");
    }

    // add a file entry for each library of the artifact
    for (Artifact library : moduleLibraries.get(module)) {
        Element file = new Element("file");
        pack.addContent(file);
        file.setAttribute("src", library.getFile().getAbsolutePath());
        file.setAttribute("targetdir",
                "$INSTALL_PATH/apache-tomcat-$TOMCAT_VERSION/webapps/marmotta/WEB-INF/lib");
    }

    // add a depends name for each module the current one depends on  (in case the project is not the webapp)
    if (!module.getExtension().equals("war")) {
        if (requiredModules.contains(module.getArtifactId())) {
            pack.setAttribute("required", "yes");
        } else {
            pack.setAttribute("required", "no");
        }

        for (Artifact dependency : moduleDependencies.get(module)) {
            Element depends = new Element("depends");
            pack.addContent(depends);

            // get the model for the artifact, we read name and description from it
            Model pom2 = getArtifactModel(dependency);

            // set name of pack from artifact
            if (pom2 != null && pom2.getName() != null) {
                depends.setAttribute("packname", pom2.getName());
            } else {
                depends.setAttribute("packname", module.getArtifactId());
            }
        }
    } else {
        pack.setAttribute("required", "yes");

        // add webapp directory from installer configuration
        Element appDir = new Element("fileset");
        appDir.setAttribute("dir", outputDirectory + "/../webapp/");
        appDir.setAttribute("targetdir", "$INSTALL_PATH/apache-tomcat-$TOMCAT_VERSION/webapps/marmotta/");
        appDir.setAttribute("includes", "**");

        pack.addContent(appDir);

        Element logDir = new Element("fileset");
        logDir.setAttribute("dir", outputDirectory + "/../log/");

        logDir.setAttribute("targetdir", "$INSTALL_PATH/apache-tomcat-$TOMCAT_VERSION/logs/");
        logDir.setAttribute("includes", "**");

        pack.addContent(logDir);
    }

    XMLOutputter writer = new XMLOutputter(Format.getPrettyFormat());
    writer.output(installation, out);

}

From source file:org.apache.maven.io.util.WriterUtils.java

License:Apache License

/**
 * Method replaceXpp3DOM./*from  w w  w . j a  v  a 2s  .c  o  m*/
 * 
 * @param parent
 * @param counter
 * @param parentDom
 */
public static void replaceXpp3DOM(final Element parent, final Xpp3Dom parentDom,
        final IndentationCounter counter) {
    if (parentDom.getChildCount() > 0) {
        final Xpp3Dom[] childs = parentDom.getChildren();
        final Collection domChilds = new ArrayList();
        for (final Xpp3Dom child : childs) {
            domChilds.add(child);
        }
        final ListIterator it = parent.getChildren().listIterator();
        while (it.hasNext()) {
            final Element elem = (Element) it.next();
            final Iterator it2 = domChilds.iterator();
            Xpp3Dom corrDom = null;
            while (it2.hasNext()) {
                final Xpp3Dom dm = (Xpp3Dom) it2.next();
                if (dm.getName().equals(elem.getName())) {
                    corrDom = dm;
                    break;
                }
            }
            if (corrDom != null) {
                domChilds.remove(corrDom);
                replaceXpp3DOM(elem, corrDom, new IndentationCounter(counter.getDepth() + 1));
                counter.increaseCount();
            } else {
                it.remove();
            }
        }
        final Iterator it2 = domChilds.iterator();
        while (it2.hasNext()) {
            final Xpp3Dom dm = (Xpp3Dom) it2.next();
            final String rawName = dm.getName();
            final String[] parts = rawName.split(":");

            Element elem;
            if (parts.length > 1) {
                final String nsId = parts[0];
                String nsUrl = dm.getAttribute("xmlns:" + nsId);
                final String name = parts[1];
                if (nsUrl == null) {
                    nsUrl = parentDom.getAttribute("xmlns:" + nsId);
                }
                elem = factory.element(name, Namespace.getNamespace(nsId, nsUrl));
            } else {
                Namespace root = parent.getNamespace();
                for (Namespace n : parent.getNamespacesInherited()) {
                    if (n.getPrefix() == null || n.getPrefix().length() == 0) {
                        root = n;
                        break;
                    }
                }
                elem = factory.element(dm.getName(), root);
            }

            final String[] attributeNames = dm.getAttributeNames();
            for (final String attrName : attributeNames) {
                if (attrName.startsWith("xmlns:")) {
                    continue;
                }
                elem.setAttribute(attrName, dm.getAttribute(attrName));
            }

            insertAtPreferredLocation(parent, elem, counter);
            counter.increaseCount();
            replaceXpp3DOM(elem, dm, new IndentationCounter(counter.getDepth() + 1));
        }
    } else if (parentDom.getValue() != null) {
        List<Content> cl = parent.getContent();
        boolean foundCdata = false;
        for (Content c : cl) {
            if (c instanceof CDATA) {
                foundCdata = true;
            }
        }

        if (!foundCdata) {
            parent.setText(parentDom.getValue());
        }
    }
}

From source file:org.apache.maven.io.util.WriterUtils.java

License:Apache License

/**
 * Method findAndReplaceSimpleElement./*from w  w w . java2 s  .c  o  m*/
 * 
 * @param counter
 * @param defaultValue
 * @param text
 * @param name
 * @param parent
 * @return Element
 */
public static Element findAndReplaceSimpleElement(final IndentationCounter counter, final Element parent,
        final String name, final String text, final String defaultValue) {
    if ((defaultValue != null) && (text != null) && defaultValue.equals(text)) {
        final Element element = parent.getChild(name, parent.getNamespace());
        // if exist and is default value or if doesn't exist.. just keep the way it is..
        if ((element != null && defaultValue.equals(element.getText())) || element == null) {
            return element;
        }
    }
    final boolean shouldExist = (text != null) && (text.trim().length() >= 0);
    final Element element = updateElement(counter, parent, name, shouldExist);
    if (shouldExist) {
        element.setText(text);
    }
    return element;
}

From source file:org.apache.maven.io.util.WriterUtils.java

License:Apache License

/**
 * Method findAndReplaceSimpleLists./*from   w  w  w.j  av a 2  s . com*/
 * 
 * @param counter
 * @param childName
 * @param parentName
 * @param list
 * @param parent
 * @return Element
 */
public static Element findAndReplaceSimpleLists(final IndentationCounter counter, final Element parent,
        final java.util.Collection list, final String parentName, final String childName) {
    final boolean shouldExist = (list != null) && (list.size() > 0);
    final Element element = updateElement(counter, parent, parentName, shouldExist);
    if (shouldExist) {
        final Iterator it = list.iterator();
        Iterator elIt = element.getChildren(childName, element.getNamespace()).iterator();
        if (!elIt.hasNext()) {
            elIt = null;
        }
        final IndentationCounter innerCount = new IndentationCounter(counter.getDepth() + 1);
        while (it.hasNext()) {
            final String value = (String) it.next();
            Element el;
            if (elIt != null && elIt.hasNext()) {
                el = (Element) elIt.next();
                if (!elIt.hasNext()) {
                    elIt = null;
                }
            } else {
                el = factory.element(childName, element.getNamespace());
                insertAtPreferredLocation(element, el, innerCount);
            }
            el.setText(value);
            innerCount.increaseCount();
        }
        if (elIt != null) {
            while (elIt.hasNext()) {
                elIt.next();
                elIt.remove();
            }
        }
    }
    return element;
}

From source file:org.artifactory.update.md.v125rc0.MdStatsConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    Element root = doc.getRootElement();
    Element downloadCount = root.getChild("downloadCount");
    if (downloadCount == null) {
        downloadCount = new Element("downloadCount");
        downloadCount.setText("0");
    }/*from  w w  w .  jav a  2  s .  c  o m*/
    // rename the root to the stats name
    root.setName(STATS_NAME);
    // remove all childer
    root.removeContent();
    // add the download count
    root.addContent(downloadCount);
}

From source file:org.artifactory.update.md.v130beta6.ChecksumsConverter.java

License:Open Source License

private Element createTextElement(String elementName, String value) {
    Element element = new Element(elementName);
    element.setText(value);
    return element;
}

From source file:org.artifactory.update.security.v2.RepoPathAclConverter.java

License:Open Source License

@Override
@SuppressWarnings({ "unchecked" })
public void convert(Document doc) {
    Element aclsTag = doc.getRootElement().getChild("acls");
    List<Element> acls = aclsTag.getChildren();
    for (Element acl : acls) {
        if (acl.getName().contains("RepoPathAcl")) {
            acl.setName("acl");
            convertIdentifierToPermissionTarget(acl);
            Element acesTag = acl.getChild(ACES);
            Element aceListTag = acesTag.getChild("list");
            List<Element> aces = aceListTag.getChildren("org.artifactory.security.RepoPathAce");
            Element newAces = new Element(ACES);
            Element aceTemplate = new Element("ace");
            Element groupEl = new Element("group");
            groupEl.setText("false");
            aceTemplate.addContent(new Element(PRINCIPAL)).addContent(groupEl).addContent(new Element(MASK));
            for (Element ace : aces) {
                Element newAce = (Element) aceTemplate.clone();
                newAce.getChild(PRINCIPAL).setText(ace.getChildText(PRINCIPAL));
                Element maskEl = ace.getChild(MASK);
                int mask = Integer.parseInt(maskEl.getText());
                if ((mask & (ArtifactoryPermission.MANAGE.getMask()
                        | ArtifactoryPermission.DEPLOY.getMask())) > 0) {
                    mask |= ArtifactoryPermission.DELETE.getMask();
                }/*  ww w .j a v  a 2s .  c  om*/
                newAce.getChild(MASK).setText("" + mask);
                newAces.addContent(newAce);
            }
            acl.removeChild(ACES);
            acl.addContent(newAces);
        } else {
            log.warn("Acl tag " + acl + " under acls is not a RepoPAthAcl!");
        }
    }
}