View Javadoc

1   /*
2    * $Id: EntryReceiver.java 22774 2011-08-27 17:31:44Z mariano.capurro $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.example.launcher.rss;
12  
13  import java.util.Iterator;
14  import java.util.List;
15  
16  import net.htmlparser.jericho.Element;
17  import net.htmlparser.jericho.HTMLElementName;
18  import net.htmlparser.jericho.Source;
19  import net.htmlparser.jericho.TagType;
20  
21  import org.apache.commons.lang.StringEscapeUtils;
22  import org.mule.api.annotations.param.Payload;
23  
24  import com.sun.syndication.feed.synd.SyndEnclosure;
25  import com.sun.syndication.feed.synd.SyndEntry;
26  import com.sun.syndication.feed.synd.SyndFeed;
27  
28  public class EntryReceiver
29  {
30      @SuppressWarnings("rawtypes")
31      public String processFeed(@Payload SyndFeed feed) throws Exception
32      {
33          StringBuilder feedXml = new StringBuilder();
34          feedXml.append("<muleforge-extensions>");
35  
36          for (Iterator it = feed.getEntries().iterator(); it.hasNext();)
37          {
38              SyndEntry entry = (SyndEntry) it.next();
39              processItem(feedXml, entry);
40          }
41          feedXml.append("</muleforge-extensions>");
42  
43          return feedXml.toString();
44      }
45  
46      private void processItem(StringBuilder feedXml, SyndEntry entry)
47      {
48          feedXml.append("<muleforge-extension>");
49          feedXml.append("<title>" + StringEscapeUtils.escapeXml(entry.getTitle()) + "</title>");
50          feedXml.append("<link>" + StringEscapeUtils.escapeXml(entry.getLink()) + "</link>");
51          if (!entry.getEnclosures().isEmpty())
52          {
53              feedXml.append("<image-url>"
54                             + StringEscapeUtils.escapeXml(((SyndEnclosure) entry.getEnclosures().get(0)).getUrl())
55                             + "</image-url>");
56          }
57          parseDescription(feedXml, entry);
58          feedXml.append("</muleforge-extension>");
59      }
60  
61      private void parseDescription(StringBuilder feedXml, SyndEntry entry)
62      {
63          Source source = new Source(entry.getDescription().getValue());
64  
65          List<Element> anchors = source.getAllElements(HTMLElementName.A);
66          if (anchors != null)
67          {
68              for (Element a : anchors)
69              {
70                  if ("documentation-url".equalsIgnoreCase(a.getAttributeValue("id")))
71                  {
72                      feedXml.append("<documentation-url>"
73                                     + StringEscapeUtils.escapeXml(a.getAttributeValue("href"))
74                                     + "</documentation-url>");
75                  }
76                  else if ("source-url".equalsIgnoreCase(a.getAttributeValue("id")))
77                  {
78                      feedXml.append("<source-url>" + StringEscapeUtils.escapeXml(a.getAttributeValue("href"))
79                                     + "</source-url>");
80                  }
81                  else if ("download-url".equalsIgnoreCase(a.getAttributeValue("id")))
82                  {
83                      feedXml.append("<download-url>"
84                                     + StringEscapeUtils.escapeXml(a.getAttributeValue("href"))
85                                     + "</download-url>");
86                  }
87              }
88          }
89      }
90  }