Example usage for org.jdom2 Element addContent

List of usage examples for org.jdom2 Element addContent

Introduction

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

Prototype

@Override
public Element addContent(final Collection<? extends Content> newContent) 

Source Link

Document

Appends all children in the given collection to the end of the content list.

Usage

From source file:com.cybernostics.jsp2thymeleaf.JSP2Thymeleaf.java

private List<Content> rootContentFor(JspTree jspTree) {
    List<Content> contents = new ArrayList<>();
    if (showBanner) {
        contents.add(new Comment("Created with JSP2Thymeleaf"));
        contents.add(new Text(NEWLINE));
    }// ww w .j av a 2s . com
    contents.addAll(contentFor(jspTree, this));

    final Optional<Content> foundHtmlElement = contents.stream().filter(JSP2Thymeleaf::isHtmlElement)
            .findFirst();

    if (foundHtmlElement.isPresent()) {
        final Element htmlElement = (Element) foundHtmlElement.get();
        contents.remove(htmlElement);
        trimTrailingWhitespace(contents);
        htmlElement.addContent(contents);
        htmlElement.setNamespace(xmlns);
        ActiveNamespaces.get().forEach(ns -> htmlElement.addNamespaceDeclaration(ns));
        return Arrays.asList(new DocType("html", THYMELEAF_DTD), htmlElement);
    } else {
        Element thFragment = createFragmentDef(contents);
        return Arrays.asList(new DocType("html", THYMELEAF_DTD), thFragment);
    }

}

From source file:com.cybernostics.jsp2thymeleaf.JSP2Thymeleaf.java

private Element createFragmentDef(List<Content> contents) {
    Element html = new Element("html", xmlns);

    ActiveNamespaces.get().forEach(ns -> html.addNamespaceDeclaration(ns));
    html.addContent(NEWLINE);
    Element head = new Element("head", xmlns);
    html.addContent(head);// w  w  w  .  ja  va  2  s .c  om
    html.addContent(NEWLINE);
    Element title = new Element("title", xmlns);
    title.setText("Thymeleaf Fragment Definition");
    head.addContent(NEWLINE);
    head.addContent(title);
    head.addContent(NEWLINE);
    Element body = new Element("body", xmlns);
    html.addContent(body);
    html.addContent(NEWLINE);
    body.addContent(contents);
    return html;
}

From source file:com.dexterapps.android.translator.TranslateAndSaveAndroid.java

License:Apache License

private void generateXMLForCountry(String countryCode, List<String> translatedText,
        ArrayList<AndroidStringMapping> stringXmlDOM, HashMap<String, Integer> baseStringResourcesMapping,
        String outPutFolder) {/*from ww w . j  av a2 s . c om*/
    Element resources = new Element("resources");
    Document doc = new Document(resources);

    //System.out.println("Generating XML");

    //int totalNumberOfStrings = 0;
    for (int i = 0; i < stringXmlDOM.size(); i++) {
        AndroidStringMapping stringMapping = stringXmlDOM.get(i);

        if (stringMapping.getType().equalsIgnoreCase("string")) {
            Element string = new Element("string");
            string.setAttribute(new Attribute("name", stringMapping.getAttributeName()));

            //To get the attribute value, use the hasmap and then string array
            int translatedTextIndex = baseStringResourcesMapping.get(stringMapping.getAttributeValue());
            string.setText(translatedText.get(translatedTextIndex));

            //Add element to root
            doc.getRootElement().addContent(string);
        } else if (stringMapping.getType().equalsIgnoreCase("string-array")) {
            Element stringArray = new Element("string-array");
            stringArray.setAttribute(new Attribute("name", stringMapping.getAttributeName()));

            //Since this is String array it will have a list of string items, get the list of string items

            ArrayList<String> stringArrayItems = (ArrayList<String>) stringMapping.getAttributeValue();

            for (int j = 0; j < stringArrayItems.size(); j++) {
                int translatedTextIndex = baseStringResourcesMapping.get(stringArrayItems.get(j));
                stringArray.addContent(new Element("item").setText(translatedText.get(translatedTextIndex)));
            }

            //Add element to root
            doc.getRootElement().addContent(stringArray);
        } else {
            Element stringArray = new Element("plurals");
            stringArray.setAttribute(new Attribute("name", stringMapping.getAttributeName()));

            //Since this is plurals it will have a list of string items with values, get the list of string items

            ArrayList<AndroidStringPlurals> stringPluralItems = (ArrayList<AndroidStringPlurals>) stringMapping
                    .getAttributeValue();

            for (int j = 0; j < stringPluralItems.size(); j++) {

                int translatedTextIndex = baseStringResourcesMapping
                        .get(stringPluralItems.get(j).getAttributeValue());
                Element pluralItem = new Element("item");
                pluralItem.setAttribute("quantity", stringPluralItems.get(j).getAttributeName());
                pluralItem.setText(translatedText.get(translatedTextIndex));

                stringArray.addContent(pluralItem);
            }

            //Add element to root
            doc.getRootElement().addContent(stringArray);
        }
    }

    // new XMLOutputter().output(doc, System.out);
    XMLOutputter xmlOutput = new XMLOutputter();

    try {
        // System.out.println("Saving File");
        Format format = Format.getPrettyFormat();
        format.setEncoding("UTF-8");
        xmlOutput.setFormat(format);

        File file = new File(outPutFolder + "/values-" + countryCode);
        if (!file.exists()) {
            file.mkdir();
        }

        file = new File(outPutFolder + "/values-" + countryCode + "/strings.xml");
        FileOutputStream fop = new FileOutputStream(file);
        xmlOutput.output(doc, fop);
        System.out.println("Translation Successful !!");

        // System.out.println("File Saved!");
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:com.facebook.buck.ide.intellij.projectview.ProjectView.java

License:Apache License

private static Element addElement(Element parent, String name, Attribute... attributes) {
    Element child = newElement(name, attributes);
    parent.addContent(child);
    return child;
}

From source file:com.github.cat.yum.store.util.YumUtil.java

private static void createRepoMd(String rootPath, RepoModule... repos) throws IOException {
    Document doc = new Document();
    Element root = new Element("repomd", REPONAMESPACE);
    doc.addContent(root);//from  w w  w  . j a  v  a 2  s  .  c om
    root.addNamespaceDeclaration(RPMNAMESPACE);
    long now = System.currentTimeMillis();

    for (RepoModule repo : repos) {
        Element data = new Element("data", REPONAMESPACE);
        data.setAttribute("type", repo.getModule());
        Element location = new Element("location", REPONAMESPACE);
        File xmlGzFie = getXmlGzFile(repo, repo.getXmlGzCode());
        location.setAttribute("href", replacePath(FileUtils.getFileRelativePath(repo.getRootPath(), xmlGzFie)));
        data.addContent(location);
        Element checksum = new Element("checksum", REPONAMESPACE);
        checksum.setAttribute("type", ALGORITHM);
        checksum.setAttribute("pkgid", "YES");
        checksum.setText(repo.getXmlGzCode());
        data.addContent(checksum);
        Element size = new Element("size", REPONAMESPACE);
        size.setText(repo.getXmlGzSize() + "");
        data.addContent(size);
        Element timestamp = new Element("timestamp", REPONAMESPACE);
        timestamp.setText(now + "");
        data.addContent(timestamp);
        Element openCheckSum = new Element("open-checksum", REPONAMESPACE);
        openCheckSum.setAttribute("type", ALGORITHM);
        openCheckSum.setAttribute("pkgid", "YES");
        openCheckSum.setText(repo.getXmlCode());
        data.addContent(openCheckSum);
        Element openSize = new Element("open-size", REPONAMESPACE);
        openSize.setText(repo.getXmlSize() + "");
        data.addContent(openSize);
        Element revision = new Element("revision", REPONAMESPACE);
        data.addContent(revision);
        root.addContent(data);
    }
    File repoMd = new File(rootPath + File.separator + REPOPATH + File.separator + "repomd" + ".xml");
    xmlToFile(doc, repoMd);
}

From source file:com.github.cat.yum.store.util.YumUtil.java

private static RepoModule createOther(RpmData[] rpmdatas, String rootPath)
        throws IOException, NoSuchAlgorithmException {
    RepoModule repo = new RepoModule(rootPath, "other");
    Document doc = new Document();
    Element root = new Element("otherdata", OTHERNAMESPACE);
    doc.addContent(root);//w  ww  .  j a  va  2 s  .  c  o m
    root.setAttribute("packages", rpmdatas.length + "");

    for (RpmData rpmdata : rpmdatas) {
        RpmMetadata rpmMetadata = rpmdata.rpmMetadata;

        Element packAge = new Element("package", OTHERNAMESPACE);
        packAge.setAttribute("pkgid", HashFile.getsum(rpmdata.rpm, ALGORITHM));
        packAge.setAttribute("name", rpmMetadata.name);
        packAge.setAttribute("arch", rpmMetadata.architecture);

        root.addContent(packAge);

        Element version = new Element("version", OTHERNAMESPACE);
        version.setAttribute("epoch", rpmMetadata.epoch + "");
        version.setAttribute("ver", rpmMetadata.version);
        version.setAttribute("rel", rpmMetadata.release);
        packAge.setContent(version);

        for (ChangeLog log : rpmMetadata.changeLogs) {
            Element fileElement = new Element("changelog", OTHERNAMESPACE);
            fileElement.setAttribute("author", log.author);
            fileElement.setAttribute("date", log.date + "");
            fileElement.setText(log.text);
            packAge.addContent(fileElement);
        }
    }
    yumXmlSave(doc, repo);
    return repo;
}

From source file:com.github.cat.yum.store.util.YumUtil.java

private static RepoModule createPrimary(RpmData[] rpmdatas, String rootPath)
        throws IOException, NoSuchAlgorithmException {
    RepoModule repo = new RepoModule(rootPath, "primary");
    Document doc = new Document();
    Element root = new Element("metadata", COMMONNAMESPACE);
    doc.addContent(root);/*  ww  w . jav  a2  s  .c o  m*/
    root.addNamespaceDeclaration(RPMNAMESPACE);
    root.setAttribute("packages", rpmdatas.length + "");

    for (RpmData rpmdata : rpmdatas) {
        RpmMetadata rpmMetadata = rpmdata.rpmMetadata;

        Element packAge = new Element("package", COMMONNAMESPACE);
        packAge.setAttribute("type", "rpm");
        root.addContent(packAge);

        Element name = new Element("name", COMMONNAMESPACE);
        name.setText(rpmMetadata.name);
        packAge.addContent(name);

        Element arch = new Element("arch", COMMONNAMESPACE);
        arch.setText(rpmMetadata.architecture);
        packAge.addContent(arch);

        Element version = new Element("version", COMMONNAMESPACE);
        version.setAttribute("epoch", rpmMetadata.epoch + "");
        version.setAttribute("ver", rpmMetadata.version);
        version.setAttribute("rel", rpmMetadata.release);
        packAge.addContent(version);

        Element checksum = new Element("checksum", COMMONNAMESPACE);
        checksum.setAttribute("type", ALGORITHM);
        checksum.setAttribute("pkgid", "YES");
        checksum.setText(HashFile.getsum(rpmdata.rpm, ALGORITHM));
        packAge.addContent(checksum);

        Element summary = new Element("summary", COMMONNAMESPACE);
        summary.setText(rpmMetadata.summary);
        packAge.addContent(summary);

        Element description = new Element("description", COMMONNAMESPACE);
        description.setText(rpmMetadata.description);
        packAge.addContent(description);

        Element packager = new Element("packager", COMMONNAMESPACE);
        packager.setText(rpmMetadata.packager);
        packAge.addContent(packager);

        Element url = new Element("url", COMMONNAMESPACE);
        url.setText(rpmMetadata.url);
        packAge.addContent(url);

        Element time = new Element("time", COMMONNAMESPACE);
        time.setAttribute("file", rpmdata.rpm.lastModified() / 1000 + "");
        time.setAttribute("build", rpmMetadata.buildTime + "");
        packAge.addContent(time);

        Element size = new Element("size", COMMONNAMESPACE);
        size.setAttribute("package", rpmdata.rpm.length() + "");
        size.setAttribute("installed", rpmMetadata.installedSize + "");
        size.setAttribute("archive", rpmMetadata.archiveSize + "");
        packAge.addContent(size);

        Element location = new Element("location", COMMONNAMESPACE);
        location.setAttribute("href", replacePath(FileUtils.getFileRelativePath(rootPath, rpmdata.rpm)));
        packAge.addContent(location);

        Element format = new Element("format", COMMONNAMESPACE);
        packAge.addContent(format);

        Element license = new Element("license", RPMNAMESPACE);
        license.setText(rpmMetadata.license);
        format.addContent(license);

        Element vendor = new Element("vendor", RPMNAMESPACE);
        vendor.setText(rpmMetadata.vendor);
        format.addContent(vendor);

        Element group = new Element("group", RPMNAMESPACE);
        group.setText(rpmMetadata.group);
        format.addContent(group);

        Element buildhost = new Element("buildhost", RPMNAMESPACE);
        buildhost.setText(rpmMetadata.buildHost);
        format.addContent(buildhost);

        Element sourcerpm = new Element("sourcerpm", RPMNAMESPACE);
        sourcerpm.setText(rpmMetadata.sourceRpm);
        format.addContent(sourcerpm);

        Element headerRange = new Element("header-range", RPMNAMESPACE);
        headerRange.setAttribute("start", rpmMetadata.headerStart + "");
        headerRange.setAttribute("end", rpmMetadata.headerEnd + "");
        format.addContent(headerRange);

        Element provides = new Element("provides", RPMNAMESPACE);
        format.addContent(provides);
        addEntry(provides, rpmMetadata.provide, null);

        Element requires = new Element("requires", RPMNAMESPACE);
        format.addContent(requires);
        addEntry(requires, rpmMetadata.require, new PrivateRequireFilter());

        Element conflicts = new Element("conflicts", RPMNAMESPACE);
        format.addContent(conflicts);
        addEntry(conflicts, rpmMetadata.conflict, null);

        Element obsoletes = new Element("obsoletes", RPMNAMESPACE);
        format.addContent(obsoletes);
        addEntry(obsoletes, rpmMetadata.obsolete, null);

        YumFileter fileflter = new PrivateFileFilter();
        YumFileter fileDirflter = new PrivateFileDirFilter();
        for (com.github.cat.yum.store.model.File file : rpmMetadata.files) {
            if (StringUtils.isBlank(file.type)) {
                if (fileflter.filter(file.path)) {
                    continue;
                }
            } else if ("dir".equals(file.type)) {
                if (fileDirflter.filter(file.path)) {
                    continue;
                }
            }

            Element fileElemenrt = new Element("file", COMMONNAMESPACE);
            fileElemenrt.setText(file.path);
            if (!StringUtils.isBlank(file.type)) {
                fileElemenrt.setAttribute("type", file.type);
            }
            format.addContent(fileElemenrt);
        }
    }
    yumXmlSave(doc, repo);
    return repo;
}

From source file:com.github.cat.yum.store.util.YumUtil.java

private static RepoModule createFilelitsts(RpmData[] rpmdatas, String rootPath)
        throws IOException, NoSuchAlgorithmException {
    RepoModule repo = new RepoModule(rootPath, "filelists");
    Document doc = new Document();
    Element root = new Element("filelists", FILELISTSNAMESPACE);
    doc.addContent(root);/*  w  w w  .ja  v  a  2s  .  c  o  m*/
    root.setAttribute("packages", rpmdatas.length + "");

    for (RpmData rpmdata : rpmdatas) {
        RpmMetadata rpmMetadata = rpmdata.rpmMetadata;

        Element packAge = new Element("package", FILELISTSNAMESPACE);
        packAge.setAttribute("pkgid", HashFile.getsum(rpmdata.rpm, ALGORITHM));
        packAge.setAttribute("name", rpmMetadata.name);
        packAge.setAttribute("arch", rpmMetadata.architecture);
        root.addContent(packAge);

        Element version = new Element("version", FILELISTSNAMESPACE);
        version.setAttribute("epoch", rpmMetadata.epoch + "");
        version.setAttribute("ver", rpmMetadata.version);
        version.setAttribute("rel", rpmMetadata.release);
        packAge.setContent(version);

        for (com.github.cat.yum.store.model.File file : rpmMetadata.files) {
            Element fileElement = new Element("file", FILELISTSNAMESPACE);
            fileElement.setText(file.path);
            if (file.type != null) {
                fileElement.setAttribute("type", file.type);
            }
            packAge.addContent(fileElement);
        }
    }
    yumXmlSave(doc, repo);
    return repo;
}

From source file:com.github.cat.yum.store.util.YumUtil.java

private static void addEntry(Element parent, List<Entry> entrys, YumFileter filter) {
    for (Entry entry : entrys) {
        Element entryElement = new Element("entry", RPMNAMESPACE);
        String name = entry.name;
        if (null != filter && filter.filter(name)) {
            continue;
        }/*w  ww. ja va  2  s  . com*/
        entryElement.setAttribute("name", name);
        if (null != entry.flags) {
            entryElement.setAttribute("flags", entry.flags);
        }
        if (null != entry.epoch) {
            entryElement.setAttribute("epoch", entry.epoch);
        }
        if (null != entry.version) {
            entryElement.setAttribute("ver", entry.version);
        }
        if (null != entry.release) {
            entryElement.setAttribute("rel", entry.release);
        }
        if (null != entry.pre) {
            entryElement.setAttribute("pre", entry.pre);
        }
        parent.addContent(entryElement);
    }
}

From source file:com.globalsight.dispatcher.bo.JobTask.java

License:Apache License

private void createTargetFile(JobBO p_job, String[] p_targetSegments) throws IOException {
    OutputStream writer = null;/*from  w w w .  j a v a 2 s .c  o  m*/
    File fileStorage = CommonDAO.getFileStorage();
    File srcFile = p_job.getSrcFile();
    Account account = DispatcherDAOFactory.getAccountDAO().getAccount(p_job.getAccountId());
    File trgDir = CommonDAO.getFolder(fileStorage, account.getAccountName() + File.separator + p_job.getJobID()
            + File.separator + AppConstants.XLF_TARGET_FOLDER);
    File trgFile = new File(trgDir, srcFile.getName());
    FileUtils.copyFile(srcFile, trgFile);
    String encoding = FileUtil.getEncodingOfXml(trgFile);

    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(p_job.getSrcFile());
        Element root = doc.getRootElement(); // Get root element
        Namespace namespace = root.getNamespace();
        Element fileElem = root.getChild("file", namespace);
        XPathFactory xFactory = XPathFactory.instance();
        XPathExpression<Element> expr = xFactory.compile("//trans-unit", Filters.element(), null, namespace);
        List<Element> tuList = expr.evaluate(fileElem.getChild("body", namespace));
        for (int tuIndex = 0, trgIndex = 0; tuIndex < tuList.size()
                && trgIndex < p_targetSegments.length; tuIndex++, trgIndex++) {
            if (p_targetSegments[trgIndex] == null) {
                continue;
            }

            Element elem = (Element) tuList.get(tuIndex);
            Element srcElem = elem.getChild("source", namespace);
            Element trgElem = elem.getChild("target", namespace);
            if (srcElem == null || srcElem.getContentSize() == 0) {
                trgIndex--;
                continue;
            }

            if (trgElem != null) {
                setTargetSegment(trgElem, p_targetSegments[trgIndex], encoding);
            } else {
                trgElem = new Element("target", namespace);
                setTargetSegment(trgElem, p_targetSegments[trgIndex], encoding);
                elem.addContent(trgElem);
            }

        }

        XMLOutputter xmlOutput = new XMLOutputter();
        Format format = Format.getRawFormat();
        format.setEncoding(encoding);
        writer = new FileOutputStream(trgFile);
        xmlOutput.setFormat(format);
        writeBOM(writer, format.getEncoding());
        xmlOutput.output(doc, writer);
        p_job.setTrgFile(trgFile);
        logger.info("Create Target File: " + trgFile);
    } catch (JDOMException e1) {
        logger.error("CreateTargetFile Error: ", e1);
    } catch (IOException e1) {
        logger.error("CreateTargetFile Error: ", e1);
    } finally {
        if (writer != null)
            writer.close();
    }
}