com.rometools.rome.io.FeedException Java Examples

The following examples show how to use com.rometools.rome.io.FeedException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OPML20Parser.java    From rome with Apache License 2.0 6 votes vote down vote up
@Override
protected Outline parseOutline(final Element e, final boolean validate, final Locale locale) throws FeedException {
    Outline retValue;

    retValue = super.parseOutline(e, validate, locale);

    if (e.getAttributeValue("created") != null) {
        retValue.setCreated(DateParser.parseRFC822(e.getAttributeValue("created"), locale));
    }

    final List<Attribute> atts = retValue.getAttributes();

    for (int i = 0; i < atts.size(); i++) {
        final Attribute a = atts.get(i);

        if (a.getName().equals("created")) {
            retValue.getAttributes().remove(a);

            break;
        }
    }

    return retValue;
}
 
Example #2
Source File: GeoRSSModuleTest.java    From rome with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether a feed with invalid point values can be parsed
 * (https://github.com/rometools/rome-modules/issues/02).
 *
 * @throws IOException if file not found or not accessible
 * @throws FeedException when the feed can't be parsed
 *
 */
public void testParseElevValue() throws IOException, FeedException {
    final SyndFeed feed = getSyndFeed("org/rometools/feed/module/georss/usgs-gov-earthquakes-all-hour-atom.xml");
    final List<SyndEntry> entries = feed.getEntries();
    for (int i = 0; i < entries.size(); i++) {
        final SyndEntry entry = entries.get(i);
        final GeoRSSModule geoRSSModule = GeoRSSUtils.getGeoRSS(entry);
        assert geoRSSModule != null;
        assert geoRSSModule.getFeatureTypeTag().equals("position");
        assert entry.getTitle().indexOf(geoRSSModule.getFeatureNameTag()) > 0;
        assert geoRSSModule.getRelationshipTag().equals("is-centered-at");
        assert geoRSSModule.getElev() != null;
        assert geoRSSModule.getFloor() == 0;
        assert geoRSSModule.getRadius() == 1.0;
    }
}
 
Example #3
Source File: AbstractWireFeedHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	WireFeedInput feedInput = new WireFeedInput();
	MediaType contentType = inputMessage.getHeaders().getContentType();
	Charset charset =
			(contentType != null && contentType.getCharSet() != null? contentType.getCharSet() : DEFAULT_CHARSET);
	try {
		Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
		return (T) feedInput.build(reader);
	}
	catch (FeedException ex) {
		throw new HttpMessageNotReadableException("Could not read WireFeed: " + ex.getMessage(), ex);
	}
}
 
Example #4
Source File: CsdbReleasesSD2IEC.java    From petscii-bbs with Mozilla Public License 2.0 6 votes vote down vote up
private void listPosts(String rssUrl) throws IOException, FeedException {
    cls();
    drawLogo();
    if (isEmpty(posts)) {
        waitOn();
        posts = getPosts(rssUrl, currentPage, pageSize);
        waitOff();
    }
    for (Map.Entry<Integer, ReleaseEntry> entry: posts.entrySet()) {
        int i = entry.getKey();
        ReleaseEntry post = entry.getValue();
        write(WHITE); print(i + "."); write(GREY3);
        final int iLen = 37-String.valueOf(i).length();
        String title = post.title + (isNotBlank(post.releasedBy) ? " (" + post.releasedBy+")" : EMPTY);
        String line = WordUtils.wrap(filterPrintable(HtmlUtils.htmlClean(title)), iLen, "\r", true);
        println(line.replaceAll("\r", "\r " + repeat(" ", 37-iLen)));
    }
    newline();
}
 
Example #5
Source File: AbstractWireFeedHttpMessageConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	WireFeedInput feedInput = new WireFeedInput();
	MediaType contentType = inputMessage.getHeaders().getContentType();
	Charset charset = (contentType != null && contentType.getCharset() != null ?
			contentType.getCharset() : DEFAULT_CHARSET);
	try {
		Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
		return (T) feedInput.build(reader);
	}
	catch (FeedException ex) {
		throw new HttpMessageNotReadableException("Could not read WireFeed: " + ex.getMessage(), ex);
	}
}
 
Example #6
Source File: Atom10Generator.java    From rome with Apache License 2.0 6 votes vote down vote up
protected void addEntry(final Entry entry, final Element parent) throws FeedException {

        final Element eEntry = new Element("entry", getFeedNamespace());

        final String xmlBase = entry.getXmlBase();
        if (xmlBase != null) {
            eEntry.setAttribute("base", xmlBase, Namespace.XML_NAMESPACE);
        }

        populateEntry(entry, eEntry);
        generateForeignMarkup(eEntry, entry.getForeignMarkup());
        checkEntryConstraints(eEntry);
        generateItemModules(entry.getModules(), eEntry);
        parent.addContent(eEntry);

    }
 
Example #7
Source File: ThreadingModuleTest.java    From rome with Apache License 2.0 6 votes vote down vote up
public void testReadMainSpec() throws IOException, FeedException {
    final SyndFeed feed = getSyndFeed("thr/threading-main.xml");
    List<SyndEntry> entries = feed.getEntries();
    SyndEntry parentEntry = entries.get(0);
    assertEquals("should be the parent entry", "My original entry", parentEntry.getTitle());
    assertNull(parentEntry.getModule(ThreadingModule.URI));

    SyndEntry replyEntry = entries.get(1);
    assertEquals("should be the reply entry", "A response to the original", replyEntry.getTitle());
    Module module = replyEntry.getModule(ThreadingModule.URI);
    assertNotNull(module);
    ThreadingModule threadingModule = (ThreadingModule) module;
    assertEquals("tag:example.org,2005:1", threadingModule.getRef());
    assertEquals("application/xhtml+xml", threadingModule.getType());
    assertEquals("http://www.example.org/entries/1", threadingModule.getHref());
    assertNull(threadingModule.getSource());
}
 
Example #8
Source File: Atom10Parser.java    From rome with Apache License 2.0 6 votes vote down vote up
/**
 * Parse entry from reader.
 */
public static Entry parseEntry(final Reader rd, final String baseURI, final Locale locale) throws JDOMException, IOException, IllegalArgumentException,
        FeedException {

    // Parse entry into JDOM tree
    final SAXBuilder builder = new SAXBuilder();
    final Document entryDoc = builder.build(rd);
    final Element fetchedEntryElement = entryDoc.getRootElement();
    fetchedEntryElement.detach();

    // Put entry into a JDOM document with 'feed' root so that Rome can
    // handle it
    final Feed feed = new Feed();
    feed.setFeedType("atom_1.0");
    final WireFeedOutput wireFeedOutput = new WireFeedOutput();
    final Document feedDoc = wireFeedOutput.outputJDom(feed);
    feedDoc.getRootElement().addContent(fetchedEntryElement);

    if (baseURI != null) {
        feedDoc.getRootElement().setAttribute("base", baseURI, Namespace.XML_NAMESPACE);
    }

    final WireFeedInput input = new WireFeedInput(false, locale);
    final Feed parsedFeed = (Feed) input.build(feedDoc);
    return parsedFeed.getEntries().get(0);
}
 
Example #9
Source File: CsdbReleases.java    From petscii-bbs with Mozilla Public License 2.0 6 votes vote down vote up
private void listPosts(String rssUrl) throws IOException, FeedException {
    cls();
    drawLogo();
    if (isEmpty(posts)) {
        waitOn();
        posts = getPosts(rssUrl, currentPage, pageSize);
        waitOff();
    }
    for (Map.Entry<Integer, ReleaseEntry> entry: posts.entrySet()) {
        int i = entry.getKey();
        ReleaseEntry post = entry.getValue();
        write(WHITE); print(i + "."); write(GREY3);
        final int iLen = 37-String.valueOf(i).length();
        String title = post.title + (isNotBlank(post.releasedBy) ? " (" + post.releasedBy+")" : EMPTY);
        String line = WordUtils.wrap(filterPrintable(HtmlUtils.htmlClean(title)), iLen, "\r", true);
        println(line.replaceAll("\r", "\r " + repeat(" ", 37-iLen)));
    }
    newline();
}
 
Example #10
Source File: RSS10Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
@Override
protected void checkTextInputConstraints(final Element eTextInput) throws FeedException {
    checkNotNullAndLength(eTextInput, "title", 0, -1);
    checkNotNullAndLength(eTextInput, "description", 0, -1);
    checkNotNullAndLength(eTextInput, "name", 0, -1);
    checkNotNullAndLength(eTextInput, "link", 0, -1);
}
 
Example #11
Source File: JsonChannelHttpMessageConverter.java    From tutorials with MIT License 5 votes vote down vote up
@Override
protected void writeInternal(Channel wireFeed, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    WireFeedOutput feedOutput = new WireFeedOutput();

    try {
        String xmlStr = feedOutput.outputString(wireFeed, true);
        JSONObject xmlJSONObj = XML.toJSONObject(xmlStr);
        String jsonPrettyPrintString = xmlJSONObj.toString(4);

        outputMessage.getBody().write(jsonPrettyPrintString.getBytes());
    } catch (JSONException | FeedException e) {
        e.printStackTrace();
    }
}
 
Example #12
Source File: RSS090Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
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);
    }
    checkLength(parent, childName, minLen, maxLen);
}
 
Example #13
Source File: RSS090Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
protected void populateFeed(final Channel channel, final Element parent) throws FeedException {
    addChannel(channel, parent);
    addImage(channel, parent);
    addTextInput(channel, parent);
    addItems(channel, parent);
    generateForeignMarkup(parent, channel.getForeignMarkup());
}
 
Example #14
Source File: RSS090Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
@Override
public Document generate(final WireFeed feed) throws FeedException {
    final Channel channel = (Channel) feed;
    final Element root = createRootElement(channel);
    populateFeed(channel, root);
    purgeUnusedNamespaceDeclarations(root);
    return createDocument(root);
}
 
Example #15
Source File: Atom03Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
protected void addEntry(final Entry entry, final Element parent) throws FeedException {
    final Element eEntry = new Element("entry", getFeedNamespace());
    populateEntry(entry, eEntry);
    checkEntryConstraints(eEntry);
    generateItemModules(entry.getModules(), eEntry);
    parent.addContent(eEntry);
}
 
Example #16
Source File: RSS090Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
protected void addItem(final Item item, final Element parent, final int index) throws FeedException {
    final Element eItem = new Element("item", getFeedNamespace());
    populateItem(item, eItem, index);
    checkItemConstraints(eItem);
    generateItemModules(item.getModules(), eItem);
    parent.addContent(eItem);
}
 
Example #17
Source File: RSS090Parser.java    From rome with Apache License 2.0 5 votes vote down vote up
@Override
public WireFeed parse(final Document document, final boolean validate, final Locale locale) throws IllegalArgumentException, FeedException {

    if (validate) {
        validateFeed(document);
    }

    final Element rssRoot = document.getRootElement();
    return parseChannel(rssRoot, locale);
}
 
Example #18
Source File: OPML10Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an XML document (JDOM) for the given feed bean.
 *
 * @param feed the feed bean to generate the XML document from.
 * @return the generated XML document (JDOM).
 * @throws IllegalArgumentException thrown if the type of the given feed bean does not match with the type of the
 *             WireFeedGenerator.
 * @throws FeedException thrown if the XML Document could not be created.
 */
@Override
public Document generate(final WireFeed feed) throws IllegalArgumentException, FeedException {

    if (!(feed instanceof Opml)) {
        throw new IllegalArgumentException("Not an OPML file");
    }

    final Opml opml = (Opml) feed;
    final Document doc = new Document();
    final Element root = new Element("opml");
    root.setAttribute("version", "1.0");
    doc.addContent(root);

    final Element head = generateHead(opml);

    if (head != null) {
        root.addContent(head);
    }

    final Element body = new Element("body");
    root.addContent(body);
    super.generateFeedModules(opml.getModules(), root);
    body.addContent(generateOutlines(opml.getOutlines()));

    return doc;
}
 
Example #19
Source File: Atom10Parser.java    From rome with Apache License 2.0 5 votes vote down vote up
protected WireFeed parseFeed(final Element eFeed, final Locale locale) throws FeedException {

        String baseURI = null;
        try {
            baseURI = findBaseURI(eFeed);
        } catch (final Exception e) {
            throw new FeedException("ERROR while finding base URI of feed", e);
        }

        final Feed feed = parseFeedMetadata(baseURI, eFeed, locale);
        feed.setStyleSheet(getStyleSheet(eFeed.getDocument()));

        final String xmlBase = eFeed.getAttributeValue("base", Namespace.XML_NAMESPACE);
        if (xmlBase != null) {
            feed.setXmlBase(xmlBase);
        }

        feed.setModules(parseFeedModules(eFeed, locale));

        final List<Element> eList = eFeed.getChildren("entry", getAtomNamespace());
        if (!eList.isEmpty()) {
            feed.setEntries(parseEntries(feed, baseURI, eList, locale));
        }

        final List<Element> foreignMarkup = extractForeignMarkup(eFeed, feed, getAtomNamespace());
        if (!foreignMarkup.isEmpty()) {
            feed.setForeignMarkup(foreignMarkup);
        }
        return feed;
    }
 
Example #20
Source File: RSS092Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
@Override
protected void checkTextInputConstraints(final Element eTextInput) throws FeedException {
    checkNotNullAndLength(eTextInput, "title", 0, -1);
    checkNotNullAndLength(eTextInput, "description", 0, -1);
    checkNotNullAndLength(eTextInput, "name", 0, -1);
    checkNotNullAndLength(eTextInput, "link", 0, -1);
}
 
Example #21
Source File: Atom03Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
protected void addEntries(final Feed feed, final Element parent) throws FeedException {
    final List<Entry> entries = feed.getEntries();
    for (final Entry entry : entries) {
        addEntry(entry, parent);
    }
    checkEntriesConstraints(parent);
}
 
Example #22
Source File: RSS091UserlandGenerator.java    From rome with Apache License 2.0 5 votes vote down vote up
@Override
protected void checkChannelConstraints(final Element eChannel) throws FeedException {

    checkNotNullAndLength(eChannel, "title", 1, 100);
    checkNotNullAndLength(eChannel, "description", 1, 500);
    checkNotNullAndLength(eChannel, "link", 1, 500);
    checkNotNullAndLength(eChannel, "language", 2, 5);
    checkLength(eChannel, "rating", 20, 500);
    checkLength(eChannel, "copyright", 1, 100);
    checkLength(eChannel, "pubDate", 1, 100);
    checkLength(eChannel, "lastBuildDate", 1, 100);
    checkLength(eChannel, "docs", 1, 500);
    checkLength(eChannel, "managingEditor", 1, 100);
    checkLength(eChannel, "webMaster", 1, 100);

    final Element skipHours = eChannel.getChild("skipHours");

    if (skipHours != null) {

        final List<Element> hours = skipHours.getChildren();
        for (final Element hour : hours) {

            final int value = Integer.parseInt(hour.getText().trim());

            if (isHourFormat24()) {
                if (value < 1 || value > 24) {
                    throw new FeedException("Invalid hour value " + value + ", it must be between 1 and 24");
                }
            } else {
                if (value < 0 || value > 23) {
                    throw new FeedException("Invalid hour value " + value + ", it must be between 0 and 23");
                }
            }

        }

    }

}
 
Example #23
Source File: CategoryFeedsFragment.java    From SimpleNews with Apache License 2.0 5 votes vote down vote up
private void shareOpml(Opml opml) {
    String finalMessage = null;
    try {
        finalMessage = new XMLOutputter().outputString(new OPML20Generator().generate(opml));
    } catch (FeedException e) {
        e.printStackTrace();
    }
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/xml");
    shareIntent.putExtra(Intent.EXTRA_TEXT,
            finalMessage);
    startActivity(shareIntent);
}
 
Example #24
Source File: ThreadingModuleTest.java    From rome with Apache License 2.0 5 votes vote down vote up
public void testEnd2End() throws IOException, FeedException {
    final SyndFeed feed = getSyndFeed("thr/threading-main.xml");
    final SyndFeedOutput output = new SyndFeedOutput();
    File outputFile = new File("target/threading-main-generated.xml");
    output.output(feed, outputFile);

    final SyndFeed feedOut = getSyndFeed(outputFile);

    ThreadingModule moduleSrc = (ThreadingModule) feed.getEntries().get(1).getModule(ThreadingModule.URI);
    ThreadingModule moduleOut = (ThreadingModule) feedOut.getEntries().get(1).getModule(ThreadingModule.URI);
    assertEquals(moduleSrc, moduleOut);
    assertEquals("tag:example.org,2005:1", moduleSrc.getRef());
    assertEquals("tag:example.org,2005:1", moduleOut.getRef());
}
 
Example #25
Source File: ThreadingModuleTest.java    From rome with Apache License 2.0 5 votes vote down vote up
public void testGenerate() throws IOException, FeedException {
    final SyndFeed feed = getSyndFeed("thr/threading-main.xml");
    List<SyndEntry> entries = feed.getEntries();

    // create a new "root" entry that the next entry will reference to
    SyndEntry newRootEntry = new SyndEntryImpl();
    newRootEntry.setTitle("New, 2nd root entry");
    newRootEntry.setUri("tag:example.org,2005:2");
    newRootEntry.setLink("http://www.example.org/entries/2");
    entries.add(newRootEntry);

    // create a new reply entry that will reference the new root entry
    SyndEntry newReplyEntry = new SyndEntryImpl();
    newReplyEntry.setTitle("New test reply entry");
    newReplyEntry.setUri("tag:example.org,2005:2,1");

    ThreadingModule threadingModule = new ThreadingModuleImpl();
    threadingModule.setRef("tag:example.org,2005:2");
    threadingModule.setType("application/xhtml+xml");
    threadingModule.setHref("http://www.example.org/entries/2");
    threadingModule.setSource("http://example.org/entries/2");

    newReplyEntry.getModules().add(threadingModule);
    entries.add(newReplyEntry);

    File outputFile = new File("target/threading-testGenerate.xml");
    final SyndFeedOutput output = new SyndFeedOutput();
    output.output(feed, outputFile);

    // read back in and validate
    final SyndFeed generatedFeed = getSyndFeed(outputFile);
    SyndEntry generatedReplyEntry = generatedFeed.getEntries().get(3);
    assertNotNull(generatedReplyEntry);
    ThreadingModule generatedReplyThreadingModule = (ThreadingModule) generatedReplyEntry.getModule(ThreadingModule.URI);
    assertEquals("tag:example.org,2005:2", generatedReplyThreadingModule.getRef());
    assertEquals("application/xhtml+xml", generatedReplyThreadingModule.getType());
    assertEquals("http://www.example.org/entries/2", generatedReplyThreadingModule.getHref());
    assertEquals("http://example.org/entries/2", generatedReplyThreadingModule.getSource());
}
 
Example #26
Source File: RSS090Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
protected void addTextInput(final Channel channel, final Element parent) throws FeedException {
    final TextInput textInput = channel.getTextInput();
    if (textInput != null) {
        final Element eTextInput = new Element(getTextInputLabel(), getFeedNamespace());
        populateTextInput(textInput, eTextInput);
        checkTextInputConstraints(eTextInput);
        parent.addContent(eTextInput);
    }
}
 
Example #27
Source File: MediaModuleTest.java    From rome with Apache License 2.0 5 votes vote down vote up
/**
 * test url with whitespace in media element (https://github.com/rometools/rome-modules/issues/20).
 * 
 * @throws IOException if file not found or not accessible
 * @throws FeedException when the feed can't be parsed
 */
public void testParseMediaContentContainingURLWithSpaces() throws FeedException, IOException {
    final MediaEntryModule module = getFirstModuleFromFile("org/rometools/feed/module/mediarss/issue-20.xml");
    assertNotNull("missing media entry module", module);
    final MediaContent[] mediaContents = module.getMediaContents();
    assertNotNull("missing media:content", mediaContents);
    assertEquals("wrong count of media:content", 1, mediaContents.length);
    final MediaContent mediaContent = mediaContents[0];
    assertEquals("http://www.foo.com/path/containing+spaces/trailer.mov", mediaContent.getReference().toString());
}
 
Example #28
Source File: MediaModuleTest.java    From rome with Apache License 2.0 5 votes vote down vote up
/**
 * tests parsing rating without scheme (https://github.com/rometools/rome-modules/issues/12).
 * 
 * @throws IOException if file not found or not accessible
 * @throws FeedException when the feed can't be parsed
 */
public void testParseRatingWithoutScheme() throws FeedException, IOException {
    final MediaEntryModule module = getFirstModuleFromFile("org/rometools/feed/module/mediarss/issue-12.xml");
    final Rating[] ratings = module.getMetadata().getRatings();

    assertThat(ratings, is(notNullValue()));
}
 
Example #29
Source File: MediaModuleTest.java    From rome with Apache License 2.0 5 votes vote down vote up
/**
 * tests parsing a decimal duration (https://github.com/rometools/rome-modules/issues/8).
 * 
 * @throws IOException if file not found or not accessible
 * @throws FeedException when the feed can't be parsed
 */
public void testParseDecimalDuration() throws FeedException, IOException {
    final MediaEntryModule module = getFirstModuleFromFile("org/rometools/feed/module/mediarss/issue-08.xml");
    final Thumbnail[] thumbnails = module.getMetadata().getThumbnail();

    assertThat(thumbnails, is(notNullValue()));
}
 
Example #30
Source File: MediaModuleTest.java    From rome with Apache License 2.0 5 votes vote down vote up
/**
 * tests parsing thubnails with empty dimensions (https://github.com/rometools/rome-modules/issues/7).
 * 
 * @throws IOException if file not found or not accessible
 * @throws FeedException when the feed can't be parsed
 */
public void testParseThumbnailWithEmptyDimensions() throws FeedException, IOException {
    final MediaEntryModule module = getFirstModuleFromFile("org/rometools/feed/module/mediarss/issue-07.xml");
    final Thumbnail[] thumbnails = module.getMetadata().getThumbnail();

    assertThat(thumbnails, is(notNullValue()));
}