Example usage for org.jdom2 Document addContent

List of usage examples for org.jdom2 Document addContent

Introduction

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

Prototype

@Override
public Document addContent(Collection<? extends Content> c) 

Source Link

Document

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

Usage

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);
    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);/*from w ww.j a  v  a  2 s .  com*/

        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);
    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);// w ww .jav  a  2 s  . co m

        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);
    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);//w w w.  j a va2s. c om

        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.rometools.opml.io.impl.OPML10Generator.java

License:Apache License

/**
 * Creates an XML document (JDOM) for the given feed bean.
 *
 * @param feed the feed bean to generate the XML document from.
 * @return the generated XML document (JDOM).
 * @throws IllegalArgumentException thrown if the type of the given feed bean does not match with the type of the
 *             WireFeedGenerator./*from w w w .j  a v a2s .  c  o m*/
 * @throws FeedException thrown if the XML Document could not be created.
 */
@Override
public Document generate(final WireFeed feed) throws IllegalArgumentException, FeedException {

    if (!(feed instanceof Opml)) {
        throw new IllegalArgumentException("Not an OPML file");
    }

    final Opml opml = (Opml) feed;
    final Document doc = new Document();
    final Element root = new Element("opml");
    root.setAttribute("version", "1.0");
    doc.addContent(root);

    final Element head = generateHead(opml);

    if (head != null) {
        root.addContent(head);
    }

    final Element body = new Element("body");
    root.addContent(body);
    super.generateFeedModules(opml.getModules(), root);
    body.addContent(generateOutlines(opml.getOutlines()));

    return doc;
}

From source file:com.rometools.rome.io.impl.OPML10Generator.java

License:Apache License

/**
 * Creates an XML document (JDOM) for the given feed bean.
 * <p>/* w w w  .ja v  a  2  s  .  c o  m*/
 *
 * @param feed the feed bean to generate the XML document from.
 * @return the generated XML document (JDOM).
 * @throws IllegalArgumentException thrown if the type of the given feed bean does not match
 *             with the type of the WireFeedGenerator.
 * @throws FeedException thrown if the XML Document could not be created.
 */
@Override
public Document generate(final WireFeed feed) throws IllegalArgumentException, FeedException {
    if (!(feed instanceof Opml)) {
        throw new IllegalArgumentException("Not an OPML file");
    }

    final Opml opml = (Opml) feed;
    final Document doc = new Document();
    final Element root = new Element("opml");
    doc.addContent(root);

    final Element head = generateHead(opml);

    if (head != null) {
        root.addContent(head);
    }

    final Element body = new Element("body");
    root.addContent(body);
    super.generateFeedModules(opml.getModules(), root);
    body.addContent(generateOutlines(opml.getOutlines()));

    return doc;
}

From source file:de.knewcleus.openradar.gui.flightplan.FlightPlanExchangeManager.java

License:Open Source License

private String buildXml(FlightPlanData fpd) {
    Document doc = new Document();
    Element root = new Element("flightplanList");
    doc.addContent(root);

    try {/*from w  w  w .  ja v a 2  s  . co m*/
        root.setAttribute("version", "1.0");
        Element elementFp = FpXml_1_0.createXml(fpd);
        if (elementFp != null) {
            root.addContent(elementFp);
        }
    } catch (Exception ex) {
        log.error("Problem to create flightplan...", ex);
    }
    StringWriter sw = new StringWriter();
    try {
        // XMLOutputter outputter = new XMLOutputter(Format.getCompactFormat());
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        sw = new StringWriter();
        outputter.output(doc, sw);
    } catch (Exception e) {
        log.error("Problem to create XML output...", e);
    }

    return sw.toString();
}

From source file:delfos.configfile.rs.single.DatasetConfigurationFileParser.java

License:Open Source License

/**
 * Almacena la configuracin completa del dataset en el fichero indicado.
 *
 * @param configFile Nombre del fichero en que se almacena la configuracin.
 * @param datasetLoader Objeto para recuperar los datos de entrada.
 * @throws java.io.IOException//from www  . ja  va 2 s . c o  m
 */
public static void saveConfigFile(File configFile, DatasetLoader datasetLoader) throws IOException {

    Document doc = new Document();
    Element root = new Element("config");

    //Creo el objeto Jdom del datasetLoader
    root.addContent(DatasetLoaderXML.getElement(datasetLoader));

    doc.addContent(root);
    XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat());

    if (!configFile.getAbsolutePath().endsWith("." + CONFIGURATION_EXTENSION)) {
        configFile = new File(configFile.getAbsolutePath() + "." + CONFIGURATION_EXTENSION);
    }

    try (FileWriter fileWriter = new FileWriter(configFile)) {
        outputter.output(doc, fileWriter);
    }
}

From source file:delfos.configfile.rs.single.RecommenderSystemConfigurationFileParser.java

License:Open Source License

/**
 * Almacena la configuracin completa del sistema en el fichero indicado.
 *
 * @param fileName Fichero en el que se almacena la configuracin.
 * @param recommenderSystem Sistema de recomendacin que utiliza.
 * @param datasetLoader Objeto para recuperar los datos de entrada.
 * @param relevanceCriteria Criterio de relevancia utilizado.
 * @param persistenceMethod/*ww  w.j  a v a2 s .  co m*/
 * @param recommendationCandidatesSelector
 * @param recommdendationsOutputMethod
 */
public static void saveConfigFile(String fileName, GenericRecommenderSystem recommenderSystem,
        DatasetLoader<? extends Rating> datasetLoader, RelevanceCriteria relevanceCriteria,
        PersistenceMethod persistenceMethod, RecommendationCandidatesSelector recommendationCandidatesSelector,
        RecommendationsOutputMethod recommdendationsOutputMethod) {

    Document doc = new Document();
    Element root = new Element("config");

    //Creo el objeto Jdom del sistema de recomendacin
    root.addContent(RecommenderSystemXML.getElement(recommenderSystem));

    //Creo el objeto Jdom del datasetLoader
    root.addContent(DatasetLoaderXML.getElement(datasetLoader));

    //Umbral de relevancia
    root.addContent(RelevanceCriteriaXML.getElement(relevanceCriteria));

    //Persistence Method
    root.addContent(PersistenceMethodXML.getElement(persistenceMethod));

    root.addContent(RecommendationCandidatesSelectorXML.getElement(recommendationCandidatesSelector));

    root.addContent(RecommdendationsOutputMethodXML.getElement(recommdendationsOutputMethod));

    doc.addContent(root);

    XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat());

    try {
        if (!fileName.endsWith("." + CONFIGURATION_EXTENSION)) {
            fileName += "." + CONFIGURATION_EXTENSION;
        }
        try (FileWriter fileWriter = new FileWriter(fileName)) {
            outputter.output(doc, fileWriter);
        }
    } catch (IOException ex) {
        ERROR_CODES.CANNOT_WRITE_FILE.exit(ex);
    }
}

From source file:delfos.configuration.scopes.ConfiguredDatasetsScope.java

License:Open Source License

public synchronized void saveConfiguredDatasets() {

    Document doc = new Document();
    Element root = new Element(CONFIGURED_DATASETS_ROOT_ELEMENT_NAME);

    for (ConfiguredDataset configuredDataset : ConfiguredDatasetsFactory.getInstance()
            .getAllConfiguredDatasets()) {
        Element thisDatasetLoader = new Element(CONFIGURED_DATASET_ELEMENT_NAME);

        thisDatasetLoader.setAttribute(CONFIGURED_DATASET_ELEMENT_NAME_ATTRIBUTE, configuredDataset.getName());

        thisDatasetLoader.setAttribute(CONFIGURED_DATASET_ELEMENT_DESCRIPTION_ATTRIBUTE,
                configuredDataset.getDescription());

        Element datasetLoaderElement = DatasetLoaderXML.getElement(configuredDataset.getDatasetLoader());
        thisDatasetLoader.addContent(datasetLoaderElement);
        root.addContent(thisDatasetLoader);
    }/*from w  ww . j  a v a  2  s . c  o  m*/

    doc.addContent(root);

    XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat());

    File fileOfConfiguredDatasets = ConfigurationManager.getConfigurationFile(this);

    try (FileWriter fileWriter = new FileWriter(fileOfConfiguredDatasets)) {
        outputter.output(doc, fileWriter);
    } catch (IOException ex) {
        ERROR_CODES.CANNOT_WRITE_CONFIGURED_DATASETS_FILE.exit(ex);
    }
}

From source file:delfos.group.GroupRecommendationManager.java

License:Open Source License

private static void writeXML(Collection<Recommendation> groupRecommendations,
        Map<Integer, Collection<Recommendation>> singleUserRecommendations, File outputFile) {

    Element root = new Element(CASE_ROOT_ELEMENT_NAME);

    Set<Integer> itemsIntersection = new TreeSet<>();

    //Miro los items recomendados para el grupo
    for (Recommendation r : groupRecommendations) {
        itemsIntersection.add(r.getItem().getId());
    }/*from  w  w w  .j  ava2 s . co m*/

    //Elimino los que no aparecen recomendades para los miembros
    for (int idMember : singleUserRecommendations.keySet()) {
        Set<Integer> thisMemberItems = new TreeSet<>();
        singleUserRecommendations.get(idMember).stream().forEach((r) -> {
            thisMemberItems.add(r.getItem().getId());
        });
        itemsIntersection.retainAll(thisMemberItems);
    }

    itemsIntersection = Collections.unmodifiableSet(itemsIntersection);

    for (int idMember : singleUserRecommendations.keySet()) {
        Element thisMemberElement = new Element(MEMBER_ELEMENT_NAME);
        thisMemberElement.setAttribute(MEMBER_ELEMENT_NAMEID_ATTRIBUTE_NAME, Integer.toString(idMember));
        for (Recommendation r : singleUserRecommendations.get(idMember)) {
            if (itemsIntersection.contains(r.getItem().getId())) {
                Element recommendation = new Element(RECOMMENDATION_ELEMENT_NAME);
                recommendation.setAttribute(RECOMMENDATION_ELEMENT_ID_ITEM_ATTRIBUTE_NAME,
                        Integer.toString(r.getItem().getId()));
                recommendation.setAttribute(RECOMMENDATION_ELEMENT_PREFERENCE_ATTRIBUTE_NAME,
                        Double.toString(r.getPreference().doubleValue()));
                thisMemberElement.addContent(recommendation);
            }
        }
        root.addContent(thisMemberElement);
    }

    Element groupElement = new Element(GROUP_ELEMENT_NAME);

    StringBuilder str = new StringBuilder();

    Integer[] idMembers = singleUserRecommendations.keySet().toArray(new Integer[0]);

    str.append(idMembers[0]);
    for (int i = 1; i < idMembers.length; i++) {
        str.append(",").append(idMembers[i]);

    }

    groupElement.setAttribute(GROUP_ELEMENT_MEMBERS_ATTRIBUTE_NAME, str.toString());
    for (Recommendation r : groupRecommendations) {
        if (itemsIntersection.contains(r.getItem().getId())) {
            Element recommendation = new Element(RECOMMENDATION_ELEMENT_NAME);
            recommendation.setAttribute(RECOMMENDATION_ELEMENT_ID_ITEM_ATTRIBUTE_NAME,
                    Integer.toString(r.getItem().getId()));
            recommendation.setAttribute(RECOMMENDATION_ELEMENT_PREFERENCE_ATTRIBUTE_NAME,
                    Double.toString(r.getPreference().doubleValue()));
            groupElement.addContent(recommendation);
        }
    }
    root.addContent(groupElement);

    Document doc = new Document();
    doc.addContent(root);
    XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat());
    try (FileWriter fileWriter = new FileWriter(outputFile)) {
        outputter.output(doc, fileWriter);
    } catch (IOException ex) {
        ERROR_CODES.CANNOT_WRITE_RESULTS_FILE.exit(ex);
    }
}