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:com.rometools.opml.io.impl.OPML20Parser.java

License:Apache License

/**
 * Inspects an XML Document (JDOM) to check if it can parse it.
 * <p>// w  w w .ja v  a2  s. c  om
 * It checks if the given document if the type of feeds the parser understands.
 * <p>
 *
 * @param document XML Document (JDOM) to check if it can be parsed by this parser.
 * @return <b>true</b> if the parser know how to parser this feed, <b>false</b> otherwise.
 */
@Override
public boolean isMyType(final Document document) {
    final Element e = document.getRootElement();

    if (e.getName().equals("opml") && (e.getChild("head") != null && e.getChild("head").getChild("docs") != null
            || e.getAttributeValue("version") != null && e.getAttributeValue("version").equals("2.0")
            || e.getChild("head") != null && e.getChild("head").getChild("ownerId") != null)) {
        return true;
    }

    return false;
}

From source file:com.rometools.opml.io.impl.OPML20Parser.java

License:Apache License

/**
 * Parses an XML document (JDOM Document) into a feed bean.
 * <p>// w w w  .  jav  a 2 s  .co  m
 *
 * @param document XML document (JDOM) to parse.
 * @param validate indicates if the feed should be strictly validated (NOT YET IMPLEMENTED).
 * @return the resulting feed bean.
 * @throws IllegalArgumentException thrown if the parser cannot handle the given feed type.
 * @throws FeedException thrown if a feed bean cannot be created out of the XML document (JDOM).
 */
@Override
public WireFeed parse(final Document document, final boolean validate, final Locale locale)
        throws IllegalArgumentException, FeedException {
    Opml opml;
    opml = (Opml) super.parse(document, validate, locale);

    final Element head = document.getRootElement().getChild("head");

    if (head != null) {
        opml.setOwnerId(head.getChildTextTrim("ownerId"));
        opml.setDocs(head.getChildTextTrim("docs"));

        if (opml.getDocs() == null) {
            opml.setDocs("http://www.opml.org/spec2");
        }
    }

    opml.setFeedType("opml_2.0");

    return opml;
}

From source file:com.rometools.propono.atom.client.ClientAtomService.java

License:Open Source License

/** Deserialize an Atom service XML document into an object */
private void parseAtomServiceDocument(final Document document) throws ProponoException {
    final Element root = document.getRootElement();
    final List<Element> spaces = root.getChildren("workspace", AtomService.ATOM_PROTOCOL);
    for (final Element e : spaces) {
        addWorkspace(new ClientWorkspace(e, this, uri));
    }/*w  w w . ja va  2s. c  o  m*/
}

From source file:com.rometools.propono.atom.client.ClientCategories.java

License:Apache License

public void fetchContents() throws ProponoException {
    final GetMethod method = new GetMethod(getHrefResolved());
    clientCollection.addAuthentication(method);
    try {/*  w  w  w. j a  va  2  s . c o  m*/
        clientCollection.getHttpClient().executeMethod(method);
        if (method.getStatusCode() != 200) {
            throw new ProponoException("ERROR HTTP status code=" + method.getStatusCode());
        }
        final SAXBuilder builder = new SAXBuilder();
        final Document catsDoc = builder.build(new InputStreamReader(method.getResponseBodyAsStream()));
        parseCategoriesElement(catsDoc.getRootElement());

    } catch (final IOException ioe) {
        throw new ProponoException("ERROR: reading out-of-line categories", ioe);
    } catch (final JDOMException jde) {
        throw new ProponoException("ERROR: parsing out-of-line categories", jde);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.rometools.propono.atom.common.AtomService.java

License:Apache License

/**
 * Deserialize an Atom service XML document into an object
 *///from  w  ww  .ja va 2  s  .c o m
public static AtomService documentToService(final Document document) throws ProponoException {
    final AtomService service = new AtomService();
    final Element root = document.getRootElement();
    final List<Element> spaces = root.getChildren("workspace", ATOM_PROTOCOL);
    for (final Element e : spaces) {
        service.addWorkspace(Workspace.elementToWorkspace(e));
    }
    return service;
}

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

License:Open Source License

protected void fillContentElement(final Element contentElement, final Content content) throws FeedException {

    final String type = content.getType();
    if (type != null) {
        final Attribute typeAttribute = new Attribute("type", type);
        contentElement.setAttribute(typeAttribute);
    }//from  w  ww. j  a va 2s  .c  o  m

    final String mode = content.getMode();
    if (mode != null) {
        final Attribute modeAttribute = new Attribute("mode", mode.toString());
        contentElement.setAttribute(modeAttribute);
    }

    final String value = content.getValue();
    if (value != null) {

        if (mode == null || mode.equals(Content.ESCAPED)) {

            contentElement.addContent(value);

        } else if (mode.equals(Content.BASE64)) {

            contentElement.addContent(Base64.encode(value));

        } else if (mode.equals(Content.XML)) {

            final StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
            tmpDocString.append(value);
            tmpDocString.append("</tmpdoc>");
            final StringReader tmpDocReader = new StringReader(tmpDocString.toString());
            Document tmpDoc;

            try {
                final SAXBuilder saxBuilder = new SAXBuilder();
                tmpDoc = saxBuilder.build(tmpDocReader);
            } catch (final Exception ex) {
                throw new FeedException("Invalid XML", ex);
            }

            final List<org.jdom2.Content> children = tmpDoc.getRootElement().removeContent();
            contentElement.addContent(children);
        }

    }
}

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

License:Open Source License

@Override
public boolean isMyType(final Document document) {
    final Element rssRoot = document.getRootElement();
    final Namespace defaultNS = rssRoot.getNamespace();
    return defaultNS != null && defaultNS.equals(getAtomNamespace());
}

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

License:Open Source License

@Override
public WireFeed parse(final Document document, final boolean validate, final Locale locale)
        throws IllegalArgumentException, FeedException {

    if (validate) {
        validateFeed(document);/*from w  w  w . j  a  v a  2  s  .c  o  m*/
    }

    final Element rssRoot = document.getRootElement();

    return parseFeed(rssRoot, locale);

}

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

License:Open Source License

protected void fillContentElement(final Element contentElement, final Content content) throws FeedException {

    final String type = content.getType();

    String atomType = type;/*  ww  w . j  av a  2 s  . co  m*/

    if (type != null) {
        // Fix for issue #39 "Atom 1.0 Text Types Not Set Correctly"
        // we're not sure who set this value, so ensure Atom types are used
        if ("text/plain".equals(type)) {
            atomType = Content.TEXT;
        } else if ("text/html".equals(type)) {
            atomType = Content.HTML;
        } else if ("application/xhtml+xml".equals(type)) {
            atomType = Content.XHTML;
        }

        final Attribute typeAttribute = new Attribute("type", atomType);
        contentElement.setAttribute(typeAttribute);
    }

    final String href = content.getSrc();
    if (href != null) {
        final Attribute srcAttribute = new Attribute("src", href);
        contentElement.setAttribute(srcAttribute);
    }

    final String value = content.getValue();
    if (value != null) {

        if (atomType != null && (atomType.equals(Content.XHTML) || atomType.indexOf("/xml") != -1
                || atomType.indexOf("+xml") != -1)) {

            final StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
            tmpDocString.append(value);
            tmpDocString.append("</tmpdoc>");
            final StringReader tmpDocReader = new StringReader(tmpDocString.toString());
            Document tmpDoc;
            try {
                final SAXBuilder saxBuilder = new SAXBuilder();
                tmpDoc = saxBuilder.build(tmpDocReader);
            } catch (final Exception ex) {
                throw new FeedException("Invalid XML", ex);
            }
            final List<org.jdom2.Content> children = tmpDoc.getRootElement().removeContent();
            contentElement.addContent(children);

        } else {

            // must be type html, text or some other non-XML format
            // JDOM will escape property for XML
            contentElement.addContent(value);

        }

    }
}

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

License:Open Source License

/**
 * Utility method to serialize an entry to writer.
 *//*  ww  w.j  a va2 s.c om*/
public static void serializeEntry(final Entry entry, final Writer writer)
        throws IllegalArgumentException, FeedException, IOException {

    // Build a feed containing only the entry
    final List<Entry> entries = new ArrayList<Entry>();
    entries.add(entry);
    final Feed feed1 = new Feed();
    feed1.setFeedType("atom_1.0");
    feed1.setEntries(entries);

    // Get Rome to output feed as a JDOM document
    final WireFeedOutput wireFeedOutput = new WireFeedOutput();
    final Document feedDoc = wireFeedOutput.outputJDom(feed1);

    // Grab entry element from feed and get JDOM to serialize it
    final Element entryElement = feedDoc.getRootElement().getChildren().get(0);

    final XMLOutputter outputter = new XMLOutputter();
    outputter.output(entryElement, writer);
}