You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ju...@apache.org on 2011/06/14 15:58:28 UTC

svn commit: r1135581 - /tika/trunk/tika-parsers/src/main/java/org/apache/tika/parser/feed/FeedParser.java

Author: jukka
Date: Tue Jun 14 13:58:28 2011
New Revision: 1135581

URL: http://svn.apache.org/viewvc?rev=1135581&view=rev
Log:
TIKA-466: Feed Parser

Minor RSS parsing improvements

Modified:
    tika/trunk/tika-parsers/src/main/java/org/apache/tika/parser/feed/FeedParser.java

Modified: tika/trunk/tika-parsers/src/main/java/org/apache/tika/parser/feed/FeedParser.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-parsers/src/main/java/org/apache/tika/parser/feed/FeedParser.java?rev=1135581&r1=1135580&r2=1135581&view=diff
==============================================================================
--- tika/trunk/tika-parsers/src/main/java/org/apache/tika/parser/feed/FeedParser.java (original)
+++ tika/trunk/tika-parsers/src/main/java/org/apache/tika/parser/feed/FeedParser.java Tue Jun 14 13:58:28 2011
@@ -38,6 +38,7 @@ import org.xml.sax.SAXException;
 import com.sun.syndication.feed.synd.SyndContent;
 import com.sun.syndication.feed.synd.SyndEntry;
 import com.sun.syndication.feed.synd.SyndFeed;
+import com.sun.syndication.io.FeedException;
 import com.sun.syndication.io.SyndFeedInput;
 
 /**
@@ -57,48 +58,53 @@ public class FeedParser extends Abstract
         return SUPPORTED_TYPES;
     }
 
-    public void parse(InputStream stream, ContentHandler handler,
-            Metadata metadata, ParseContext context) throws IOException,
-            SAXException, TikaException {
-        SyndFeed feed = null;
+    public void parse(
+            InputStream stream, ContentHandler handler,
+            Metadata metadata, ParseContext context)
+            throws IOException, SAXException, TikaException {
         // set the encoding?
         try {
-            SyndFeedInput feedInput = new SyndFeedInput();
-            InputSource input = new InputSource(stream);
-            feed = feedInput.build(input);
-        } catch (Exception e) {
-            throw new TikaException(e.getMessage());
-        }
+            SyndFeed feed = new SyndFeedInput().build(
+                    new InputSource(stream));
 
-        String feedDesc = stripTags(feed.getDescriptionEx());
-        String feedTitle = stripTags(feed.getTitleEx());
+            String title = stripTags(feed.getTitleEx());
+            String description = stripTags(feed.getDescriptionEx());
 
-        metadata.set(Metadata.TITLE, feedTitle);
-        metadata.set(Metadata.DESCRIPTION, feedDesc);
-        // store the other fields in the metadata
-
-        XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
-        xhtml.startDocument();
-
-        List entries = feed.getEntries();
-        for (Iterator i = entries.iterator(); i.hasNext();) {
-            SyndEntry entry = (SyndEntry) i.next();
-            String link = entry.getLink();
-            if (link == null)
-                continue;
-            SyndContent description = entry.getDescription();
-
-            String title = stripTags(entry.getTitleEx());
-            xhtml.startElement("a", "href", link);
-            xhtml.characters(title);
-            xhtml.endElement("a");
-            xhtml.startElement("p");
-            if (description != null)
-                xhtml.characters(description.getValue());
-            xhtml.endElement("p");
+            metadata.set(Metadata.TITLE, title);
+            metadata.set(Metadata.DESCRIPTION, description);
+            // store the other fields in the metadata
+
+            XHTMLContentHandler xhtml =
+                new XHTMLContentHandler(handler, metadata);
+            xhtml.startDocument();
+
+            xhtml.element("h1", title);
+            xhtml.element("p", description);
+
+            xhtml.startElement("ul");
+            for (Object e : feed.getEntries()) {
+                SyndEntry entry = (SyndEntry) e;
+                String link = entry.getLink();
+                if (link != null) {
+                    xhtml.startElement("li");
+                    xhtml.startElement("a", "href", link);
+                    xhtml.characters(stripTags(entry.getTitleEx()));
+                    xhtml.endElement("a");
+                    SyndContent content = entry.getDescription();
+                    if (content != null) {
+                        xhtml.newline();
+                        xhtml.characters(content.getValue());
+                    }
+                    xhtml.endElement("li");
+                }
+            }
+            xhtml.endElement("ul");
+
+            xhtml.endDocument();
+        } catch (FeedException e) {
+            throw new TikaException("RSS parse error", e);
         }
 
-        xhtml.endDocument();
     }
 
     private static String stripTags(SyndContent c) {