Example usage for org.jdom2 Element getChild

List of usage examples for org.jdom2 Element getChild

Introduction

In this page you can find the example usage for org.jdom2 Element getChild.

Prototype

public Element getChild(final String cname, final Namespace ns) 

Source Link

Document

This returns the first child element within this element with the given local name and belonging to the given namespace.

Usage

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

License:Open Source License

/**
 * Utility method to parse a taxonomy from an element.
 * <p>//from  w w  w  .j a v  a2s  .  c o m
 *
 * @param desc the taxonomy description element.
 * @return the string contained in the resource of the element.
 */
protected final String getTaxonomy(final Element desc) {
    String taxonomy = null;
    final Element topic = desc.getChild("topic", getTaxonomyNamespace());
    if (topic != null) {
        final Attribute resource = topic.getAttribute("resource", getRDFNamespace());
        if (resource != null) {
            taxonomy = resource.getValue();
        }
    }
    return taxonomy;
}

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

License:Open Source License

/**
 * Utility method to parse a list of subjects out of a list of elements.
 * <p>//from  ww  w . j  a  v  a 2  s.com
 *
 * @param eList the element list to parse.
 * @return a list of subjects parsed from the elements.
 */
protected final List<DCSubject> parseSubjects(final List<Element> eList) {

    final List<DCSubject> subjects = new ArrayList<DCSubject>();

    for (final Element eSubject : eList) {

        final Element description = eSubject.getChild("Description", getRDFNamespace());

        if (description != null) {

            final String taxonomy = getTaxonomy(description);

            final List<Element> values = description.getChildren("value", getRDFNamespace());
            for (final Element value : values) {

                final DCSubject subject = new DCSubjectImpl();
                subject.setTaxonomyUri(taxonomy);
                subject.setValue(value.getText());
                subjects.add(subject);

            }

        } else {
            final DCSubject subject = new DCSubjectImpl();
            subject.setValue(eSubject.getText());
            subjects.add(subject);
        }
    }

    return subjects;
}

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

License:Open Source License

protected void checkNotNullAndLength(final Element parent, final String childName, final int minLen,
        final int maxLen) throws FeedException {
    final Element child = parent.getChild(childName, getFeedNamespace());
    if (child == null) {
        throw new FeedException(
                "Invalid " + getType() + " feed, missing " + parent.getName() + " " + childName);
    }// w ww  . j  a  v  a 2  s .  c o m
    checkLength(parent, childName, minLen, maxLen);
}

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

License:Open Source License

protected void checkLength(final Element parent, final String childName, final int minLen, final int maxLen)
        throws FeedException {
    final Element child = parent.getChild(childName, getFeedNamespace());
    if (child != null) {
        if (minLen > 0 && child.getText().length() < minLen) {
            throw new FeedException("Invalid " + getType() + " feed, " + parent.getName() + " " + childName
                    + "short of " + minLen + " length");
        }// w  w  w . j a  va 2  s .c  om
        if (maxLen > -1 && child.getText().length() > maxLen) {
            throw new FeedException("Invalid " + getType() + " feed, " + parent.getName() + " " + childName
                    + "exceeds " + maxLen + " length");
        }
    }
}

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

License:Open Source License

/**
 * Parses the root element of an RSS document into a Channel bean.
 * <p/>// ww w .j  a va 2 s  .c  om
 * It reads title, link and description and delegates to parseImage, parseItems and
 * parseTextInput. This delegation always passes the root element of the RSS document as
 * different RSS version may have this information in different parts of the XML tree (no
 * assumptions made thanks to the specs variaty)
 * <p/>
 *
 * @param rssRoot the root element of the RSS document to parse.
 * @return the parsed Channel bean.
 */
protected WireFeed parseChannel(final Element rssRoot, final Locale locale) {

    final Channel channel = new Channel(getType());
    channel.setStyleSheet(getStyleSheet(rssRoot.getDocument()));

    final Element eChannel = rssRoot.getChild("channel", getRSSNamespace());

    final Element title = eChannel.getChild("title", getRSSNamespace());
    if (title != null) {
        channel.setTitle(title.getText());
    }

    final Element link = eChannel.getChild("link", getRSSNamespace());
    if (link != null) {
        channel.setLink(link.getText());
    }

    final Element description = eChannel.getChild("description", getRSSNamespace());
    if (description != null) {
        channel.setDescription(description.getText());
    }

    channel.setImage(parseImage(rssRoot));

    channel.setTextInput(parseTextInput(rssRoot));

    // Unfortunately Microsoft's SSE extension has a special case of effectively putting the
    // sharing channel module inside the RSS tag and not inside the channel itself. So we also
    // need to look for channel modules from the root RSS element.
    final List<Module> allFeedModules = new ArrayList<Module>();
    final List<Module> rootModules = parseFeedModules(rssRoot, locale);
    final List<Module> channelModules = parseFeedModules(eChannel, locale);

    if (rootModules != null) {
        allFeedModules.addAll(rootModules);
    }

    if (channelModules != null) {
        allFeedModules.addAll(channelModules);
    }

    channel.setModules(allFeedModules);
    channel.setItems(parseItems(rssRoot, locale));

    final List<Element> foreignMarkup = extractForeignMarkup(eChannel, channel, getRSSNamespace());
    if (!foreignMarkup.isEmpty()) {
        channel.setForeignMarkup(foreignMarkup);
    }

    return channel;

}

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

License:Open Source License

/**
 * This method exists because RSS0.90 and RSS1.0 have the 'image' element under the root
 * elemment. And RSS0.91, RSS0.02, RSS0.93, RSS0.94 and RSS2.0 have it under the 'channel'
 * element.//ww w .  java  2  s. com
 * <p/>
 */
protected Element getImage(final Element rssRoot) {
    return rssRoot.getChild("image", getRSSNamespace());
}

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

License:Open Source License

/**
 * This method exists because RSS0.90 and RSS1.0 have the 'textinput' element under the root
 * elemment. And RSS0.91, RSS0.02, RSS0.93, RSS0.94 and RSS2.0 have it under the 'channel'
 * element.//  w  w w .  j a  v a2 s  .  c o  m
 * <p/>
 */
protected Element getTextInput(final Element rssRoot) {
    return rssRoot.getChild("textinput", getRSSNamespace());
}

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

License:Open Source License

/**
 * Parses the root element of an RSS document looking for image information.
 * <p/>// w ww  .  j a va  2s .com
 * It reads title and url out of the 'image' element.
 * <p/>
 *
 * @param rssRoot the root element of the RSS document to parse for image information.
 * @return the parsed image bean.
 */
protected Image parseImage(final Element rssRoot) {

    Image image = null;

    final Element eImage = getImage(rssRoot);
    if (eImage != null) {

        image = new Image();

        final Element title = eImage.getChild("title", getRSSNamespace());
        if (title != null) {
            image.setTitle(title.getText());
        }

        final Element url = eImage.getChild("url", getRSSNamespace());
        if (url != null) {
            image.setUrl(url.getText());
        }

        final Element link = eImage.getChild("link", getRSSNamespace());
        if (link != null) {
            image.setLink(link.getText());
        }

    }

    return image;

}

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

License:Open Source License

/**
 * Parses an item element of an RSS document looking for item information.
 * <p/>//from   ww w  . j  a v  a  2s  .c om
 * It reads title and link out of the 'item' element.
 * <p/>
 *
 * @param rssRoot the root element of the RSS document in case it's needed for context.
 * @param eItem the item element to parse.
 * @return the parsed RSSItem bean.
 */
protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) {

    final Item item = new Item();

    final Element title = eItem.getChild("title", getRSSNamespace());
    if (title != null) {
        item.setTitle(title.getText());
    }

    final Element link = eItem.getChild("link", getRSSNamespace());
    if (link != null) {
        item.setLink(link.getText());
        item.setUri(link.getText());
    }

    item.setModules(parseItemModules(eItem, locale));

    final List<Element> foreignMarkup = extractForeignMarkup(eItem, item, getRSSNamespace());
    // content:encoded elements are treated special, without a module, they have to be removed
    // from the foreign markup to avoid duplication in case of read/write. Note that this fix
    // will break if a content module is used
    final Iterator<Element> iterator = foreignMarkup.iterator();
    while (iterator.hasNext()) {
        final Element element = iterator.next();
        final Namespace eNamespace = element.getNamespace();
        final String eName = element.getName();
        if (getContentNamespace().equals(eNamespace) && eName.equals("encoded")) {
            iterator.remove();
        }
    }

    if (!foreignMarkup.isEmpty()) {
        item.setForeignMarkup(foreignMarkup);
    }

    return item;
}

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

License:Open Source License

/**
 * Parses the root element of an RSS document looking for text-input information.
 * <p/>/*from   w  w w.j  a v  a 2  s .  com*/
 * It reads title, description, name and link out of the 'textinput' or 'textInput' element.
 * <p/>
 *
 * @param rssRoot the root element of the RSS document to parse for text-input information.
 * @return the parsed RSSTextInput bean.
 */
protected TextInput parseTextInput(final Element rssRoot) {

    TextInput textInput = null;

    final Element eTextInput = getTextInput(rssRoot);
    if (eTextInput != null) {

        textInput = new TextInput();

        final Element title = eTextInput.getChild("title", getRSSNamespace());
        if (title != null) {
            textInput.setTitle(title.getText());
        }

        final Element description = eTextInput.getChild("description", getRSSNamespace());
        if (description != null) {
            textInput.setDescription(description.getText());
        }

        final Element name = eTextInput.getChild("name", getRSSNamespace());
        if (name != null) {
            textInput.setName(name.getText());
        }

        final Element link = eTextInput.getChild("link", getRSSNamespace());
        if (link != null) {
            textInput.setLink(link.getText());
        }

    }

    return textInput;

}