Example usage for org.jdom2 Document getRootElement

List of usage examples for org.jdom2 Document getRootElement

Introduction

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

Prototype

public Element getRootElement() 

Source Link

Document

This will return the root Element for this Document

Usage

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

License:Open Source License

/**
 * Carga un caso de estudio desde un archivo XML
 *
 * @param file Archivo XML del que se carga la configuracin del caso de estudio.
 * @return/* w w w.j  a v  a2 s  . c o  m*/
 * @throws org.jdom2.JDOMException
 * @throws java.io.IOException
 */
public static CaseStudyConfiguration loadCase(File file) throws JDOMException, IOException {

    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(file);

    Element caseStudy = doc.getRootElement();
    if (!caseStudy.getName().equals(CASE_ROOT_ELEMENT_NAME)) {
        throw new IllegalArgumentException(
                "The XML does not contains a Case Study (" + file.getAbsolutePath() + ")");
    }
    GenericRecommenderSystem<Object> recommenderSystem = RecommenderSystemXML
            .getRecommenderSystem(caseStudy.getChild(RecommenderSystemXML.ELEMENT_NAME));
    DatasetLoader<? extends Rating> datasetLoader = DatasetLoaderXML
            .getDatasetLoader(caseStudy.getChild(DatasetLoaderXML.ELEMENT_NAME));
    ValidationTechnique validationTechnique = ValidationTechniqueXML
            .getValidationTechnique(caseStudy.getChild(ValidationTechniqueXML.ELEMENT_NAME));
    PredictionProtocol predictionProtocol = PredictionProtocolXML
            .getPredictionProtocol(caseStudy.getChild(PredictionProtocolXML.ELEMENT_NAME));
    RelevanceCriteria relevanceCriteria = RelevanceCriteriaXML
            .getRelevanceCriteria(caseStudy.getChild(RelevanceCriteriaXML.ELEMENT_NAME));

    return new CaseStudyConfiguration(recommenderSystem, datasetLoader, validationTechnique, predictionProtocol,
            relevanceCriteria);
}

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

License:Open Source License

/**
 * Carga un caso de estudio desde un archivo XML
 *
 * @param file Archivo XML del que se carga la configuracin del caso de estudio.
 * @return/*from ww  w.  java 2 s  .c o  m*/
 * @throws org.jdom2.JDOMException
 * @throws java.io.IOException
 */
public static CaseStudyResults loadCaseResults(File file) throws JDOMException, IOException {

    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(file);

    Element caseStudy = doc.getRootElement();
    if (!caseStudy.getName().equals(CASE_ROOT_ELEMENT_NAME)) {
        throw new IllegalArgumentException("The XML does not contains a Case Study.");
    }
    GenericRecommenderSystem<Object> recommenderSystem = RecommenderSystemXML
            .getRecommenderSystem(caseStudy.getChild(RecommenderSystemXML.ELEMENT_NAME));
    ValidationTechnique validationTechnique = ValidationTechniqueXML
            .getValidationTechnique(caseStudy.getChild(ValidationTechniqueXML.ELEMENT_NAME));
    PredictionProtocol predictionProtocol = PredictionProtocolXML
            .getPredictionProtocol(caseStudy.getChild(PredictionProtocolXML.ELEMENT_NAME));
    DatasetLoader<? extends Rating> datasetLoader = DatasetLoaderXML
            .getDatasetLoader(caseStudy.getChild(DatasetLoaderXML.ELEMENT_NAME));

    EvaluationMeasuresResults aggregatedElement = getAggregateEvaluationMeasures(
            caseStudy.getChild("Aggregate_values"));

    return new CaseStudyResults(recommenderSystem, datasetLoader, validationTechnique, predictionProtocol,
            aggregatedElement.evaluationMeasuresResults, aggregatedElement.buildTime,
            aggregatedElement.recommendationTime);

}

From source file:delfos.main.managers.experiment.join.xml.AggregateResultsXML.java

License:Open Source License

public Map<String, Object> extractMapFromFile(File inputFile) throws JDOMException, IOException {

    Element caseStudy;/*ww  w.  ja  v  a2 s. co  m*/

    long fileSize = FileUtils.sizeOf(inputFile);
    if (fileSize > Math.pow(2, 20)) {
        File smallerFile = new File(inputFile.getPath() + "_smaller");

        BufferedWriter writer;
        try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
            writer = new BufferedWriter(new FileWriter(smallerFile));
            String line = reader.readLine();
            while (line != null) {
                if (!line.contains("ndcg_value=")) {
                    writer.write(line);
                }
                line = reader.readLine();
            }
        }
        writer.close();

        SAXBuilder builder = new SAXBuilder();

        Document doc = builder.build(smallerFile);
        caseStudy = doc.getRootElement();
        smallerFile.delete();
    } else {
        SAXBuilder builder = new SAXBuilder();

        Document doc = builder.build(inputFile);
        caseStudy = doc.getRootElement();
    }

    Map<String, Object> valuesByColumnName = new TreeMap<>();

    if (!caseStudy.getName().equals(CASE_ROOT_ELEMENT_NAME)) {
        throw new IllegalArgumentException(
                "The XML does not contains a Case Study (" + inputFile.getAbsolutePath() + ")");
    }

    valuesByColumnName.putAll(extractCaseParametersMapFromElement(caseStudy));
    valuesByColumnName.putAll(extractCaseResultsMapFromElement(caseStudy));

    return valuesByColumnName;

}

From source file:delfos.rs.output.RecommendationsOutputFileXML.java

License:Open Source License

public Recommendations readRecommendations(String idTarget) throws JDOMException {

    SAXBuilder builder = new SAXBuilder();
    Document doc = null;

    try {//from w w  w .  j av  a  2 s.c  o m
        doc = builder.build(getCompleteFile(idTarget));
    } catch (IOException ex) {
        Global.showError(ex);
        ERROR_CODES.CANNOT_LOAD_CONFIG_FILE.exit(ex);
        throw new IllegalStateException(ex);
    }

    Element recommendationsElement = doc.getRootElement();
    Recommendations recommendations = RecommendationsToXML.getRecommendations(recommendationsElement);
    return recommendations;
}

From source file:delfos.web.DelfosWebConfiguration.java

public static String printXML(String xmlPath) {
    DelfosWebConfiguration.setConfiguration();
    File xml = new File(xmlPath);
    if (!xml.exists()) {
        ERROR_CODES.CONFIG_FILE_NOT_EXISTS.exit(
                new FileNotFoundException("Configuration file '" + xml.getAbsolutePath() + "' not found"));
    }//  w ww.j a  v  a  2 s.co m
    Global.showMessageTimestamped("Loading config file " + xml.getAbsolutePath());
    SAXBuilder builder = new SAXBuilder();
    Document doc = null;
    try {
        doc = builder.build(xml);
    } catch (JDOMException | IOException ex) {
        Global.showError(ex);
        ERROR_CODES.CANNOT_LOAD_CONFIG_FILE.exit(ex);
        throw new IllegalStateException(ex);
    }
    Element config = doc.getRootElement();
    config.setAttribute("configurationFilePath", xml.getAbsolutePath());
    XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat());
    return outputter.outputString(doc);
}

From source file:devicemodel.conversions.XmlConversions.java

public static DeviceNode xmlToNode(File f) throws IOException, JDOMException {
    SAXBuilder docBuilder = new SAXBuilder();
    Document doc = docBuilder.build(f);

    return xmlToNode(doc.getRootElement());
}

From source file:devicemodel.conversions.XmlConversions.java

public static Element xmlString2Element(String string) throws JDOMException, IOException {
    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(new StringReader(string));
    return document.getRootElement();
}

From source file:devicerestservice.ExampleApp.java

public static void main(String[] args) throws IOException {
    if (args.length != 2) {
        System.out.println("Must specify configuration file and device description");
        System.exit(1);/*from   ww  w . j av  a  2s .  co  m*/
    }

    File xmlConfig = new File(args[0]);
    File xmlDeviceDesc = new File(args[1]);
    if (!xmlConfig.exists()) {
        System.out.println("Configuration file not found.");
        System.exit(2);
    }
    if (!xmlDeviceDesc.exists()) {
        System.out.println("Device description file not found.");
        System.exit(3);
    }

    SAXBuilder builder = new SAXBuilder();
    Element configurationElement = null;
    Element deviceDescriptionElement = null;

    try {
        Document document = (Document) builder.build(xmlConfig);
        configurationElement = document.getRootElement();
    } catch (JDOMException ex) {
        System.out.println("Can't parse configuration file.");
        System.exit(4);
    }

    try {
        Document document = (Document) builder.build(xmlDeviceDesc);
        deviceDescriptionElement = document.getRootElement();
    } catch (JDOMException ex) {
        System.out.println("Can't parse device description file.");
        System.exit(5);
    }

    if (configurationElement == null || !configurationElement.getName().equals("Device")) {
        System.out.println("Not a proper configuration XML file.");
        System.out.println(
                "Ensure XML has elements in Pascal Case (i.e. DeviceName not deviceName or devicename)");
        System.exit(6);
    }

    new ExampleApp(configurationElement, deviceDescriptionElement);
}

From source file:ditatools.translate.DitaTranslator.java

License:Apache License

protected void translateTopicXML(Document doc, File outputXmlFile) throws JDOMException, IOException {
    // caller should have verified that root element 
    // is a DITA topic: concept, task, or reference
    Element rootElement = doc.getRootElement();

    // XXX handle case where xml:lang attr is not present
    rootElement.getAttribute("lang", Namespace.XML_NAMESPACE).setValue(language);

    process(rootElement);/* w w w . j  a v  a 2 s . co  m*/

    XMLOutputter xmlOutput = new XMLOutputter();

    // force use of UTF-8 in output
    FileOutputStream fos = null;
    OutputStreamWriter osw = null;
    try {
        fos = new FileOutputStream(outputXmlFile);
        osw = new OutputStreamWriter(fos, "UTF-8");

        xmlOutput.output(doc, osw);
    } finally {
        if (osw != null) {
            try {
                osw.close();
            } catch (Exception x) {
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (Exception x) {
            }
        }
    }
}

From source file:ditatools.translate.DitaTranslator.java

License:Apache License

/**
 * //from ww w .ja v  a 2  s.  co  m
 * @param xmlFile
 *            the xml file to be translated
 * @param outputXmlFile
 *            the output file
 */
public void translateXML(File xmlFile, File outputXmlFile) {

    try {
        Document doc = builder.build(xmlFile);

        Element rootElement = doc.getRootElement();
        String rootElementName = rootElement.getName();

        if (rootElementName.equalsIgnoreCase("concept") || rootElementName.equalsIgnoreCase("task")
                || rootElementName.equalsIgnoreCase("reference")) {
            translateTopicXML(doc, outputXmlFile);
        } else if (rootElementName.equalsIgnoreCase("map")) {
            translateDitaMap(doc, outputXmlFile);
        } else {
            System.err.println("Skipping file; Unknown element: " + rootElementName);
        }

    } catch (JDOMException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}