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:delfos.group.grs.consensus.ConsensusOfIndividualRecommendationsToXML.java

License:Open Source License

public static void writeConsensusInputXML(DatasetLoader datasetLoader,
        GroupRecommendations groupRecommendations, Collection<RecommendationsToUser> membersRecommendations,
        File outputFile) {//from w  w  w  .j av a  2s.c om

    Element root = new Element(CONSENSUS_ROOT_ELEMENT_NAME);

    for (RecommendationsToUser recommendationsToMember : membersRecommendations) {
        User member = recommendationsToMember.getUser();
        Element thisMemberElement = new Element(MEMBER_ELEMENT_NAME);
        thisMemberElement.setAttribute(MEMBER_ELEMENT_NAME_ID_ATTRIBUTE_NAME, Integer.toString(member.getId()));

        List<Recommendation> sortedRecommendations = recommendationsToMember.getRecommendations().stream()
                .sorted(Recommendation.BY_PREFERENCE_DESC).collect(Collectors.toList());

        int rank = 1;
        for (Recommendation r : sortedRecommendations) {
            Element recommendation = new Element(RECOMMENDATION_ELEMENT_NAME);
            recommendation.setAttribute(RECOMMENDATION_ELEMENT_ID_ITEM_ATTRIBUTE_NAME,
                    Integer.toString(r.getItem().getId()));

            double preferenceInDomain = datasetLoader.getRatingsDataset().getRatingsDomain()
                    .trimValueToDomain(r.getPreference()).doubleValue();
            recommendation.setAttribute(RECOMMENDATION_ELEMENT_PREFERENCE_ATTRIBUTE_NAME,
                    Double.toString(preferenceInDomain));
            recommendation.setAttribute(RECOMMENDATION_ELEMENT_RANK_ATTRIBUTE_NAME, Integer.toString(rank));
            thisMemberElement.addContent(recommendation);
            rank++;

        }
        root.addContent(thisMemberElement);
    }

    Element groupElement = new Element(GROUP_ELEMENT_NAME);
    String members = groupRecommendations.getGroupOfUsers().toString();
    groupElement.setAttribute(GROUP_ELEMENT_MEMBERS_ATTRIBUTE_NAME, members);
    int rank = 1;

    List<Recommendation> sortedGroupRecommendations = groupRecommendations.getRecommendations().stream()
            .sorted(Recommendation.BY_PREFERENCE_DESC).collect(Collectors.toList());
    for (Recommendation r : sortedGroupRecommendations) {

        Element recommendation = new Element(RECOMMENDATION_ELEMENT_NAME);
        recommendation.setAttribute(RECOMMENDATION_ELEMENT_ID_ITEM_ATTRIBUTE_NAME,
                Integer.toString(r.getIdItem()));
        recommendation.setAttribute(RECOMMENDATION_ELEMENT_PREFERENCE_ATTRIBUTE_NAME,
                Double.toString(r.getPreference().doubleValue()));
        recommendation.setAttribute(RECOMMENDATION_ELEMENT_RANK_ATTRIBUTE_NAME, Integer.toString(rank));
        groupElement.addContent(recommendation);

        rank++;
    }
    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);
    }
}

From source file:delfos.group.grs.consensus.ConsensusOfIndividualRecommendationsToXML.java

License:Open Source License

public static <RatingType extends Rating> void writeRecommendationMembersRatingsXML(
        Map<Integer, Map<Integer, RatingType>> membersRatings, Collection<Integer> candidateItems,
        File groupPredictionRequestsFile) {

    Element root = new Element(RECOMMENDATION_INPUT_ROOT_ELEMENT_NAME);

    Element membersRatingsElement = new Element(RECOMMENDATION_INPUT_MEMBERS_RATINGS_ELEMENT_NAME);
    for (int idMember : membersRatings.keySet()) {
        Element thisMemberRatingsElement = new Element(RECOMMENDATION_INPUT_MEMBER_RATINGS_ELEMENT_NAME);
        thisMemberRatingsElement.setAttribute(RECOMMENDATION_INPUT_MEMBER_RATINGS_ID_USER_ATTRIBUTE_NAME,
                Integer.toString(idMember));

        Map<Integer, RatingType> memberRatings = membersRatings.get(idMember);

        for (RatingType memberRating : memberRatings.values()) {
            Element ratingElement = new Element(RECOMMENDATION_INPUT_MEMBER_RATINGS_RATING_ELEMENT_NAME);
            ratingElement.setAttribute(RECOMMENDATION_INPUT_MEMBER_RATINGS_ID_USER_ATTRIBUTE_NAME,
                    Integer.toString(memberRating.getIdUser()));
            ratingElement.setAttribute(RECOMMENDATION_INPUT_MEMBER_RATINGS_ID_ITEM_ATTRIBUTE_NAME,
                    Integer.toString(memberRating.getIdItem()));
            ratingElement.setAttribute(RECOMMENDATION_INPUT_MEMBER_RATINGS_RATING_VALUE_ATTRIBUTE_NAME,
                    Double.toString(memberRating.getRatingValue().doubleValue()));
            thisMemberRatingsElement.addContent(ratingElement);
        }/*from  ww  w. j a  v  a  2 s.c  o  m*/
        membersRatingsElement.addContent(thisMemberRatingsElement);
    }
    root.addContent(membersRatingsElement);

    Element candidateItemsElement = new Element(RECOMMENDATION_INPUT_ID_ITEM_LIST_ELEMENT_NAME);
    for (int idItemRequested : candidateItems) {
        Element itemRequestedElement = new Element(RECOMMENDATION_INPUT_ITEM_REQUEST_ELEMENT_NAME);
        itemRequestedElement.setAttribute(RECOMMENDATION_INPUT_ITEM_REQUEST_ID_ITEM_ATTRIBUTE_NAME,
                Integer.toString(idItemRequested));
        candidateItemsElement.addContent(itemRequestedElement);
    }
    root.addContent(candidateItemsElement);

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

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

From source file:delfos.group.io.xml.casestudy.GroupCaseStudyXML.java

License:Open Source License

public synchronized static void caseStudyToXMLFile_fullResults(GroupCaseStudy caseStudyGroup, File file) {
    if (!caseStudyGroup.isFinished()) {
        throw new UnsupportedOperationException("No se ha ejecutado el caso de uso todava");
    }//from w  ww. ja va  2 s . c o  m

    Document doc = new Document();
    Element casoDeUso = new Element("Case");

    casoDeUso.setAttribute(SeedHolder.SEED.getName(), Long.toString(caseStudyGroup.getSeedValue()));
    casoDeUso.setAttribute(NUM_EXEC_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.getNumExecutions()));
    casoDeUso.setAttribute(ParameterOwner.ALIAS.getName(), caseStudyGroup.getAlias());

    casoDeUso.addContent(GroupRecommenderSystemXML.getElement(caseStudyGroup.getGroupRecommenderSystem()));
    casoDeUso.addContent(DatasetLoaderXML.getElement(caseStudyGroup.getDatasetLoader()));

    casoDeUso.addContent(GroupFormationTechniqueXML.getElement(caseStudyGroup.getGroupFormationTechnique()));
    casoDeUso.addContent(ValidationTechniqueXML.getElement(caseStudyGroup.getValidationTechnique()));
    casoDeUso.addContent(GroupPredictionProtocolXML.getElement(caseStudyGroup.getGroupPredictionProtocol()));

    casoDeUso.addContent(RelevanceCriteriaXML.getElement(caseStudyGroup.getRelevanceCriteria()));

    casoDeUso.addContent(getResultsElement(caseStudyGroup));
    casoDeUso.addContent(getAggregatedResultsElement(caseStudyGroup));
    doc.addContent(casoDeUso);

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

    FileUtilities.createDirectoriesForFile(file);
    try (FileWriter fileWriter = new FileWriter(file)) {
        outputter.output(doc, fileWriter);
    } catch (IOException ex) {
        ERROR_CODES.CANNOT_WRITE_RESULTS_FILE.exit(ex);
    }
}

From source file:delfos.group.io.xml.casestudy.GroupCaseStudyXML.java

License:Open Source License

/**
 * Saves the xml with the description of the case study in de file
 * specified./*from w  w  w .  j a va 2 s. com*/
 *
 * @param caseStudyGroup Group case study whose description is saved.
 * @param file File in which the description is saved.
 */
public static void caseStudyToXMLFile_onlyDescription(GroupCaseStudy caseStudyGroup, File file) {

    if (caseStudyGroup.isFinished()) {
        throw new IllegalArgumentException("Ya se ha ejecutado el caso de estudio!");
    }

    Document doc = new Document();
    Element casoDeUso = new Element(CASE_ROOT_ELEMENT_NAME);

    casoDeUso.setAttribute(SeedHolder.SEED.getName(), Long.toString(caseStudyGroup.getSeedValue()));
    casoDeUso.setAttribute(NUM_EXEC_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.getNumExecutions()));
    casoDeUso.setAttribute(ParameterOwner.ALIAS.getName(), caseStudyGroup.getAlias());

    casoDeUso.setAttribute(HASH_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.hashCode()));
    casoDeUso.setAttribute(HASH_DATA_VALIDATION_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.hashCode()));

    casoDeUso.addContent(GroupRecommenderSystemXML.getElement(caseStudyGroup.getGroupRecommenderSystem()));
    casoDeUso.addContent(DatasetLoaderXML.getElement(caseStudyGroup.getDatasetLoader()));

    casoDeUso.addContent(GroupFormationTechniqueXML.getElement(caseStudyGroup.getGroupFormationTechnique()));
    casoDeUso.addContent(ValidationTechniqueXML.getElement(caseStudyGroup.getValidationTechnique()));
    casoDeUso.addContent(GroupPredictionProtocolXML.getElement(caseStudyGroup.getGroupPredictionProtocol()));
    casoDeUso.addContent(RelevanceCriteriaXML.getElement(caseStudyGroup.getRelevanceCriteria()));
    doc.addContent(casoDeUso);

    FileUtilities.createDirectoriesForFile(file);

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

From source file:delfos.group.io.xml.casestudy.GroupCaseStudyXML.java

License:Open Source License

private static void caseStudyToXMLFile_aggregateResults(GroupCaseStudy caseStudyGroup, File file) {
    if (!caseStudyGroup.isFinished()) {
        throw new UnsupportedOperationException("No se ha ejecutado el caso de uso todava");
    }/*from  ww  w.j  a  va  2  s.  co  m*/

    Document doc = new Document();
    Element casoDeUso = new Element(CASE_ROOT_ELEMENT_NAME);

    casoDeUso.setAttribute(SeedHolder.SEED.getName(), Long.toString(caseStudyGroup.getSeedValue()));
    casoDeUso.setAttribute(NUM_EXEC_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.getNumExecutions()));
    casoDeUso.setAttribute(ParameterOwner.ALIAS.getName(), caseStudyGroup.getAlias());

    casoDeUso.setAttribute(HASH_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.hashCode()));
    casoDeUso.setAttribute(HASH_DATA_VALIDATION_ATTRIBUTE_NAME,
            Integer.toString(caseStudyGroup.hashDataValidation()));
    casoDeUso.setAttribute(HASH_TECHNIQUE_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.hashTechnique()));

    casoDeUso.addContent(GroupRecommenderSystemXML.getElement(caseStudyGroup.getGroupRecommenderSystem()));
    casoDeUso.addContent(DatasetLoaderXML.getElement(caseStudyGroup.getDatasetLoader()));

    casoDeUso.addContent(GroupFormationTechniqueXML.getElement(caseStudyGroup.getGroupFormationTechnique()));
    casoDeUso.addContent(ValidationTechniqueXML.getElement(caseStudyGroup.getValidationTechnique()));
    casoDeUso.addContent(GroupPredictionProtocolXML.getElement(caseStudyGroup.getGroupPredictionProtocol()));

    casoDeUso.addContent(RelevanceCriteriaXML.getElement(caseStudyGroup.getRelevanceCriteria()));

    casoDeUso.addContent(getAggregatedResultsElement(caseStudyGroup));
    doc.addContent(casoDeUso);

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

    FileUtilities.createDirectoriesForFile(file);

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

From source file:delfos.io.xml.casestudy.CaseStudyXML.java

License:Open Source License

public synchronized static void caseStudyToXMLFile(CaseStudy caseStudy, File file) {
    if (!caseStudy.isFinished()) {
        throw new UnsupportedOperationException("No se ha ejecutado el caso de uso todava");
    }//from   w  ww  . ja v  a  2s. c o m

    Document doc = new Document();
    Element casoDeUso = new Element(CASE_ROOT_ELEMENT_NAME);

    casoDeUso.setAttribute(SEED_ATTRIBUTE_NAME, Long.toString(caseStudy.getSeedValue()));
    casoDeUso.setAttribute(NUM_EXEC_ATTRIBUTE_NAME, Integer.toString(caseStudy.getNumExecutions()));

    casoDeUso.addContent(RecommenderSystemXML.getElement(caseStudy.getRecommenderSystem()));
    casoDeUso.addContent(ValidationTechniqueXML.getElement(caseStudy.getValidationTechnique()));
    casoDeUso.addContent(PredictionProtocolXML.getElement(caseStudy.getPredictionProtocol()));

    casoDeUso.addContent(RelevanceCriteriaXML.getElement(caseStudy.getRelevanceCriteria()));
    casoDeUso.addContent(DatasetLoaderXML.getElement(caseStudy.getDatasetLoader()));

    casoDeUso.addContent(getResultsElement(caseStudy));
    casoDeUso.addContent(getAggregatedResultsElement(caseStudy));
    doc.addContent(casoDeUso);

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

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

    } catch (IOException ex) {
        ERROR_CODES.CANNOT_WRITE_RESULTS_FILE.exit(ex);
    }
}

From source file:delfos.io.xml.casestudy.CaseStudyXML.java

License:Open Source License

public synchronized static void caseStudyToXMLFile(CaseStudy caseStudy, String descriptiveName, File f) {
    if (!caseStudy.isFinished()) {
        throw new UnsupportedOperationException("No se ha ejecutado el caso de uso todava");
    }//from   ww  w.  j a v  a  2s  .  c  o m

    Document doc = new Document();
    Element casoDeUso = new Element("Case");

    casoDeUso.addContent(RecommenderSystemXML.getElement(caseStudy.getRecommenderSystem()));
    casoDeUso.addContent(ValidationTechniqueXML.getElement(caseStudy.getValidationTechnique()));

    casoDeUso.addContent(PredictionProtocolXML.getElement(caseStudy.getPredictionProtocol()));

    casoDeUso.addContent(RelevanceCriteriaXML.getElement(caseStudy.getRelevanceCriteria()));
    casoDeUso.addContent(DatasetLoaderXML.getElement(caseStudy.getDatasetLoader()));

    casoDeUso.addContent(getResultsElement(caseStudy));
    casoDeUso.addContent(getAggregatedResultsElement(caseStudy));
    doc.addContent(casoDeUso);

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

From source file:delfos.io.xml.casestudy.CaseStudyXML.java

License:Open Source License

private static void caseStudyToXMLFile_onlyDescription(CaseStudy caseStudy, File file) {

    if (caseStudy.isFinished()) {
        throw new IllegalArgumentException("Ya se ha ejecutado el caso de estudio!");
    }//from ww  w  .  jav  a  2s  . c  o m

    Document doc = new Document();
    Element casoDeUso = new Element(CASE_ROOT_ELEMENT_NAME);

    casoDeUso.addContent(RecommenderSystemXML.getElement(caseStudy.getRecommenderSystem()));
    casoDeUso.addContent(ValidationTechniqueXML.getElement(caseStudy.getValidationTechnique()));

    casoDeUso.addContent(PredictionProtocolXML.getElement(caseStudy.getPredictionProtocol()));

    casoDeUso.addContent(RelevanceCriteriaXML.getElement(caseStudy.getRelevanceCriteria()));
    casoDeUso.addContent(DatasetLoaderXML.getElement(caseStudy.getDatasetLoader()));

    //casoDeUso.addContent(getResultsElement(caseStudy));
    //casoDeUso.addContent(getAggregatedResultsElement(caseStudy));
    doc.addContent(casoDeUso);

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

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

From source file:delfos.io.xml.casestudy.CaseStudyXML.java

License:Open Source License

private static void caseStudyToXMLFile_onlyAggregate(CaseStudy caseStudy, String descriptivePrefix, File file) {
    if (!caseStudy.isFinished()) {
        throw new UnsupportedOperationException("No se ha ejecutado el caso de uso todava");
    }/*from  ww w  .j a  va  2  s  .  c o  m*/

    Document doc = new Document();
    Element casoDeUso = new Element("Case");

    casoDeUso.setAttribute("seed", Long.toString(caseStudy.getSeedValue()));
    casoDeUso.setAttribute("numExec", Integer.toString(caseStudy.getNumExecutions()));

    casoDeUso.addContent(RecommenderSystemXML.getElement(caseStudy.getRecommenderSystem()));
    casoDeUso.addContent(ValidationTechniqueXML.getElement(caseStudy.getValidationTechnique()));

    casoDeUso.addContent(PredictionProtocolXML.getElement(caseStudy.getPredictionProtocol()));

    casoDeUso.addContent(RelevanceCriteriaXML.getElement(caseStudy.getRelevanceCriteria()));
    casoDeUso.addContent(DatasetLoaderXML.getElement(caseStudy.getDatasetLoader()));

    //casoDeUso.addContent(getResultsElement(caseStudy));
    casoDeUso.addContent(getAggregatedResultsElement(caseStudy));

    doc.addContent(casoDeUso);

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

From source file:edu.ucla.loni.server.ServerUtils.java

License:Open Source License

/**
 * Writes the access file for the root directory
 * Side Effect: Updates accessFileModified in the database
 *///from  w  w  w  .  ja  v  a  2s  .co m
public static void writeAccessFile(Directory root) throws Exception {
    File accessFile = getAccessFile(root);
    if (!accessFile.exists()) {
        boolean success = accessFile.createNewFile();
        if (!success) {
            throw new Exception("Could not create access file");
        }
    }

    // <files>
    Element filesRoot = new Element("files");

    // For each pipe, add a <file> child to <files>
    Pipefile[] pipes = Database.selectPipefiles(root.dirId);

    if (pipes != null) {
        for (Pipefile p : pipes) {
            // Only add the pipefile if access is not empty
            if (!p.access.equals("")) {
                Element file = new Element("file");
                file.setAttribute("type", p.type);
                file.setAttribute("name", p.name);
                file.setAttribute("package", p.packageName);

                // Add agents
                String[] agents = p.access.split(",");
                for (String agent : agents) {
                    if (!agent.equals("")) {
                        file.addContent(agentElement(agent));
                    }
                }

                filesRoot.addContent(file);
            }
        }
    }

    // <groups>
    Element groupsRoot = new Element("groups");

    // For each group, add a <group> child to <groups>
    Group[] groups = Database.selectGroups(root.dirId);

    if (groups != null) {
        for (Group g : groups) {
            Element group = new Element("group");
            group.setAttribute("name", g.name);

            // Add agents
            String[] agents = g.users.split(",");
            for (String agent : agents) {
                if (!agent.equals("")) {
                    group.addContent(agentElement(agent));
                }
            }

            groupsRoot.addContent(group);
        }
    }

    // Root Element
    Element access = new Element("access");
    access.addContent(filesRoot);
    access.addContent(groupsRoot);

    // Document
    Document doc = new Document();
    doc.addContent(access);

    // Write document
    writeXML(accessFile, doc);

    // Update when the access file was written
    root.accessModified = new Timestamp(accessFile.lastModified());
    Database.updateDirectory(root);
}