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