View Javadoc
1 package net.sf.flock.support; 2 3 import java.net.MalformedURLException; 4 import java.net.URL; 5 import java.util.ArrayList; 6 import java.util.Iterator; 7 import java.util.List; 8 9 import net.sf.flock.FeedInfoI; 10 import net.sf.flock.FlockResourceException; 11 import net.sf.flock.MetaData; 12 import net.sf.flock.SubscriptionInfoI; 13 import org.apache.log4j.LogManager; 14 import org.apache.log4j.Logger; 15 import org.jdom.Document; 16 import org.jdom.Element; 17 18 /*** 19 * Imports subscriptions from an Radio/AmphetaDesk-style OPML. 20 * 21 * @version $Revision: 1.5 $ 22 * @author $Author: phraktle $ 23 */ 24 public class OpmlSubscriptionImport { 25 26 private final static Logger LOGGER = LogManager.getLogger( OpmlSubscriptionImport.class ); 27 28 private final static String[] KNOWN_ATTRIBUTES = { "path" }; 29 30 private final MetaData metaDataTemplate; 31 32 public OpmlSubscriptionImport() { 33 this(new MetaData()); 34 } 35 36 public OpmlSubscriptionImport(MetaData metaDataTemplate) { 37 this.metaDataTemplate = metaDataTemplate; 38 } 39 40 public boolean isSuitable(Document doc) { 41 Element root = doc.getRootElement(); 42 if (!"opml".equals(root.getName())) { 43 return false; 44 } 45 // @consider more opml checks 46 return true; 47 } 48 49 /*** 50 * @return Collection of SubscriptionI 51 */ 52 public SubscriptionInfoI[] load(Document opml) throws FlockResourceException { 53 Element body = opml.getRootElement().getChild("body"); 54 55 List subscriptions = new ArrayList(); 56 57 for (Iterator i = body.getChildren("outline").iterator(); i.hasNext(); ) { 58 Element outline = (Element)i.next(); 59 final URL feedURL; 60 try { 61 String atr = outline.getAttributeValue("xmlUrl", outline.getAttributeValue("xmlurl")); 62 feedURL = new URL( atr ); 63 } catch (MalformedURLException e) { 64 throw new FlockResourceException(e); 65 } 66 67 final MetaData metaData = new MetaData( this.metaDataTemplate ); 68 69 for (int a=0; a<KNOWN_ATTRIBUTES.length; a++) { 70 String value = outline.getAttributeValue( KNOWN_ATTRIBUTES[a] ); 71 if (value!=null) { 72 metaData.set( KNOWN_ATTRIBUTES[a], value); 73 } 74 } 75 76 final String title = outline.getAttributeValue("title", "New feed"); 77 78 String htmlUrl = outline.getAttributeValue("htmlurl", outline.getAttributeValue("htmlUrl")); 79 URL siteURL = null; 80 if (htmlUrl!=null) { 81 try { 82 siteURL = new URL(htmlUrl); 83 } catch (MalformedURLException e) { 84 LOGGER.warn("Invalid htmlUrl", e); 85 } 86 } 87 final URL site = siteURL; 88 89 90 subscriptions.add( new SubscriptionInfoI() { 91 public URL getLocation() { 92 return feedURL; 93 } 94 95 public MetaData getMetaData() { 96 return metaData; 97 } 98 99 public FeedInfoI getFeedInfo() { 100 return new FeedInfoI() { 101 public String getTitle() { 102 return title; 103 } 104 public URL getSite() { 105 return site; 106 } 107 public SubscriptionInfoI getSubscriptionInfo() { 108 return null; 109 } 110 }; 111 } 112 113 } ); 114 } 115 116 return (SubscriptionInfoI[])subscriptions.toArray(new SubscriptionInfoI[subscriptions.size()]); 117 } 118 119 }

This page was automatically generated by Maven