All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rometools.rome.io.impl.BaseWireFeedParser Maven / Gradle / Ivy

Go to download

All Roads Lead to ROME. ROME is a set of Atom/RSS Java utilities that make it easy to work in Java with most syndication formats. Today it accepts all flavors of RSS (0.90, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0), Atom 0.3 and Atom 1.0 feeds. Rome includes a set of parsers and generators for the various flavors of feeds, as well as converters to convert from one format to another. The parsers can give you back Java objects that are either specific for the format you want to work with, or a generic normalized SyndFeed object that lets you work on with the data without bothering about the underlying format.

There is a newer version: 2.1.0
Show newest version
package com.rometools.rome.io.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.jdom2.Attribute;
import org.jdom2.Content;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.ProcessingInstruction;
import org.jdom2.filter.ContentFilter;

import com.rometools.rome.feed.WireFeed;
import com.rometools.rome.feed.module.Extendable;
import com.rometools.rome.feed.module.Module;
import com.rometools.rome.io.WireFeedParser;

/**
 * @author Alejandro Abdelnur
 */
public abstract class BaseWireFeedParser implements WireFeedParser {
    /**
     * [TYPE].feed.ModuleParser.classes= [className] ...
     *
     */
    private static final String FEED_MODULE_PARSERS_POSFIX_KEY = ".feed.ModuleParser.classes";

    /**
     * [TYPE].item.ModuleParser.classes= [className] ...
     *
     */
    private static final String ITEM_MODULE_PARSERS_POSFIX_KEY = ".item.ModuleParser.classes";

    /**
     * [TYPE].person.ModuleParser.classes= [className] ...
     *
     */
    private static final String PERSON_MODULE_PARSERS_POSFIX_KEY = ".person.ModuleParser.classes";

    private final String type;
    private final ModuleParsers feedModuleParsers;
    private final ModuleParsers itemModuleParsers;
    private final ModuleParsers personModuleParsers;
    private final Namespace namespace;

    protected BaseWireFeedParser(final String type, final Namespace namespace) {
        this.type = type;
        this.namespace = namespace;
        feedModuleParsers = new ModuleParsers(type + FEED_MODULE_PARSERS_POSFIX_KEY, this);
        itemModuleParsers = new ModuleParsers(type + ITEM_MODULE_PARSERS_POSFIX_KEY, this);
        personModuleParsers = new ModuleParsers(type + PERSON_MODULE_PARSERS_POSFIX_KEY, this);
    }

    /**
     * Returns the type of feed the parser handles.
     * 

* * @see WireFeed for details on the format of this string. *

* @return the type of feed the parser handles. * */ @Override public String getType() { return type; } protected List parseFeedModules(final Element feedElement, final Locale locale) { return feedModuleParsers.parseModules(feedElement, locale); } protected List parseItemModules(final Element itemElement, final Locale locale) { return itemModuleParsers.parseModules(itemElement, locale); } protected List parsePersonModules(final Element itemElement, final Locale locale) { return personModuleParsers.parseModules(itemElement, locale); } protected List extractForeignMarkup(final Element e, final Extendable ext, final Namespace namespace) { final ArrayList foreignElements = new ArrayList(); for (final Element element : e.getChildren()) { if (!namespace.equals(element.getNamespace()) && ext.getModule(element.getNamespaceURI()) == null) { // if element not in the RSS namespace and elem was not handled by a module save it // as foreign markup but we can't detach it while we're iterating foreignElements.add(element.clone()); } } // now we can detach the foreign markup elements for (final Element foreignElement : foreignElements) { foreignElement.detach(); } return foreignElements; } protected Attribute getAttribute(final Element e, final String attributeName) { Attribute attribute = e.getAttribute(attributeName); if (attribute == null) { attribute = e.getAttribute(attributeName, namespace); } return attribute; } protected String getAttributeValue(final Element e, final String attributeName) { final Attribute attr = getAttribute(e, attributeName); if (attr != null) { return attr.getValue(); } else { return null; } } protected String getStyleSheet(final Document doc) { String styleSheet = null; for (final Content c : doc.getContent(new ContentFilter(ContentFilter.PI))) { final ProcessingInstruction pi = (ProcessingInstruction) c; if ("text/xsl".equals(pi.getPseudoAttributeValue("type"))) { styleSheet = pi.getPseudoAttributeValue("href"); break; } } return styleSheet; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy