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:io.sitespeed.jenkins.xml.impl.XMLToPageTimingsJDOM.java

License:Open Source License

private String getPageData(String name, Document doc) {

    for (Element el : doc.getRootElement().getChild("pageData").getChildren("entry")) {
        if (el.getChildText("key") != null && el.getChild("key").getValue().equals(name)) {
            return el.getChildText("value");
        }//  www  . jav  a2  s  .  co m
    }
    return "";
}

From source file:io.sitespeed.jenkins.xml.impl.XMLToPageTimingsJDOM.java

License:Open Source License

private Map<String, HashMap<String, String>> getMeasurements(Document doc) {

    List<Element> stats = doc.getRootElement().getChild("statistics").getChildren("statistic");
    Map<String, HashMap<String, String>> data = new HashMap<String, HashMap<String, String>>();

    for (Element statistic : stats) {
        String name = statistic.getChild("name").getValue();
        HashMap<String, String> values = new HashMap<String, String>();

        for (Element element : statistic.getChildren()) {
            if (!element.getName().equals("name")) {
                values.put(element.getName(), element.getValue());
            }/*from   w  w w  .  j av a  2  s.  c  om*/
        }

        data.put(name, values);

    }

    return data;
}

From source file:io.sitespeed.jenkins.xml.impl.XMLToSummaryJDOM.java

License:Open Source License

public SiteSummary get(File summaryXML) throws IOException {

    final SAXBuilder b = new SAXBuilder(new XMLReaderSAX2Factory(false));
    Document doc;
    try {// w ww.  j  a v a  2s  .  c o  m
        doc = b.build(summaryXML);
    } catch (JDOMException e) {
        throw new IOException(e);
    }

    Map<String, HashMap<String, String>> values = new HashMap<String, HashMap<String, String>>();
    // TODO today the cache time is in seconds, probably should be converted to minutes?
    for (Element metric : doc.getRootElement().getChild("metrics").getChildren()) {
        String name = metric.getName();
        name = fixBrowserKey(name);
        HashMap<String, String> the = new HashMap<String, String>();
        for (Element valueType : metric.getChildren()) {
            the.put(valueType.getName(), valueType.getValue());
        }
        values.put(name, the);
    }

    int pages = new Integer(doc.getRootElement().getChild("pages").getValue());

    return new SiteSummary(values, pages);
}

From source file:io.smartspaces.domain.support.JdomActivityDescriptionReader.java

License:Apache License

@Override
public ActivityDescription readDescription(InputStream activityDescriptionStream) {
    try {//from  w  ww .j av a 2  s. co m
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(activityDescriptionStream);

        Element rootElement = doc.getRootElement();

        ActivityDescription description = new ActivityDescription();

        List<String> errors = new ArrayList<>();

        getMainData(description, rootElement, errors);
        getMetadata(description, rootElement, errors);
        getDependencies(description, rootElement, errors);

        return description;
    } catch (Exception e) {
        throw new SmartSpacesException("Unable to read activity descriptiuon", e);
    }

}

From source file:io.smartspaces.master.server.services.internal.support.JdomMasterDomainModelImporter.java

License:Apache License

/**
 * Parse the description.//  ww w  . j a  v  a 2s .c o m
 *
 * @param description
 *          the model description
 *
 * @return the root element of the parse
 *
 * @throws SmartSpacesException
 *           if there was an error reading the description
 */
private Element readDescription(String description) throws SmartSpacesException {
    try {
        StringReader reader = new StringReader(description);

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

        return doc.getRootElement();
    } catch (Exception e) {
        throw new SmartSpacesException("Unable to read master domain model description", e);
    }
}

From source file:io.smartspaces.workbench.project.jdom.JdomReader.java

License:Apache License

/**
 * Get the root element for a given input file.
 *
 * @param inputFile/*from   w  ww  .  j  a  va 2 s .  c o m*/
 *          input project file
 *
 * @return top-level element
 */
Element getRootElement(File inputFile) {
    Document doc;
    try {
        SAXBuilder builder = new SAXBuilder();
        builder.setJDOMFactory(new LocatedJDOMFactory());
        builder.setFeature(XML_PARSER_FEATURE_XINCLUDE, true);
        builder.setEntityResolver(new MyEntityResolver());
        doc = builder.build(inputFile);
    } catch (Exception e) {
        throw new SmartSpacesException(
                String.format("Exception while processing %s", inputFile.getAbsolutePath()), e);
    }

    return doc.getRootElement();
}

From source file:io.wcm.handler.richtext.util.RichTextUtil.java

License:Apache License

/**
 * Parses XHTML text string. Adds a wrapping "root" element before parsing and returns this root element.
 * @param text XHTML text string (root element not needed)
 * @param xhtmlEntities If set to true, Resolving of XHtml entities in XHtml fragment is supported.
 * @return Root element with parsed xhtml content
 * @throws JDOMException Is thrown if the text could not be parsed as XHTML
 *//*from   w  w w . j a v  a 2s  .  c o m*/
public static Element parseText(String text, boolean xhtmlEntities) throws JDOMException {

    // add root element
    String xhtmlString = (xhtmlEntities ? "<!DOCTYPE root [" + XHTML_ENTITY_DEF + "]>" : "") + "<root>" + text
            + "</root>";

    try {
        SAXBuilder saxBuilder = new SAXBuilder();

        if (xhtmlEntities) {
            saxBuilder.setEntityResolver(XHtmlEntityResolver.getInstance());
        }

        Document doc = saxBuilder.build(new StringReader(xhtmlString));
        return doc.getRootElement();
    } catch (IOException ex) {
        throw new RuntimeException("Error parsing XHTML fragment.", ex);
    }

}

From source file:io.wcm.maven.plugins.contentpackage.unpacker.ContentUnpacker.java

License:Apache License

private void writeXmlWithExcludes(InputStream inputStream, OutputStream outputStream)
        throws IOException, JDOMException {
    SAXBuilder saxBuilder = new SAXBuilder();
    Document doc = saxBuilder.build(inputStream);
    applyXmlExcludes(doc.getRootElement(), "");

    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat().setLineSeparator(LineSeparator.UNIX));
    outputter.setXMLOutputProcessor(new OneAttributePerLineXmlProcessor());
    outputter.output(doc, outputStream);
    outputStream.flush();/*  ww w .  j a  va2 s.  com*/
}

From source file:io.wcm.maven.plugins.i18n.readers.XmlI18nReader.java

License:Apache License

@Override
public Map<String, String> read(File sourceFile) throws IOException {
    try {/*from w w  w  .  ja  v  a 2 s .  c o m*/
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(sourceFile);
        Map<String, String> map = new HashMap<String, String>();
        parseXml(doc.getRootElement(), map, "");
        return map;
    } catch (JDOMException ex) {
        throw new IOException("Unable to read XML from " + sourceFile.getAbsolutePath(), ex);
    }
}