raphael 02/05/07 07:03:23 Modified: src/java/org/apache/jetspeed/portal/portlets RSSPortlet.java Log: fold the RSS Item class into the RSSPortlet and remove the rss.peer package Revision Changes Path 1.47 +113 -75 jakarta-jetspeed/src/java/org/apache/jetspeed/portal/portlets/RSSPortlet.java Index: RSSPortlet.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/portal/portlets/RSSPortlet.java,v retrieving revision 1.46 retrieving revision 1.47 diff -u -r1.46 -r1.47 --- RSSPortlet.java 5 Dec 2001 18:46:55 -0000 1.46 +++ RSSPortlet.java 7 May 2002 14:03:23 -0000 1.47 @@ -67,7 +67,6 @@ import org.apache.jetspeed.util.*; import org.apache.jetspeed.cache.disk.*; import org.apache.jetspeed.portal.*; -import org.apache.jetspeed.xml.peer.rss.*; import org.apache.jetspeed.xml.JetspeedXMLEntityResolver; import org.apache.jetspeed.capability.*; @@ -88,20 +87,20 @@ @author <A HREF="mailto:[EMAIL PROTECTED]">Kevin A. Burton</A> @author <A HREF="mailto:[EMAIL PROTECTED]">Santiago Gala</A> -@version $Id: RSSPortlet.java,v 1.46 2001/12/05 18:46:55 sgala Exp $ +@version $Id: RSSPortlet.java,v 1.47 2002/05/07 14:03:23 raphael Exp $ */ public class RSSPortlet extends FileWatchPortlet { - + public final static String ERROR_NOT_VALID = "This does not appear to be an RSS document"; /** - The + The */ private Item[] items = new Item[0]; - + /** @author <A HREF="mailto:[EMAIL PROTECTED]">Kevin A. Burton</A> - @version $Id: RSSPortlet.java,v 1.46 2001/12/05 18:46:55 sgala Exp $ + @version $Id: RSSPortlet.java,v 1.47 2002/05/07 14:03:23 raphael Exp $ */ public void init( ) throws PortletException { @@ -118,7 +117,7 @@ parser.setEntityResolver(new JetspeedXMLEntityResolver() ); url = this.getPortletConfig().getURL(); - + String content = JetspeedDiskCache.getInstance().getEntry( url ).getData(); InputSource is = new InputSource( this.cleanse( content ) ); @@ -126,14 +125,14 @@ is.setEncoding( "UTF8" ); is.setSystemId( url ); - //parser.setFeature( "http://apache.org/xml/features/allow-java-encodings", + //parser.setFeature( "http://apache.org/xml/features/allow-java-encodings", // true ); document = parser.parse( is ); } catch ( Throwable t ) { - String message = "RSSPortlet: Couldn't parse out XML document -> " + + String message = "RSSPortlet: Couldn't parse out XML document -> " + url; Log.error( message, t ); @@ -146,7 +145,7 @@ //now that we have the document set the items for this this.setItems( this.parseItems( document ) ); - + String title = null; String description = null; @@ -154,41 +153,41 @@ Node root = document.getFirstChild(); //now find the channel node. Node channel = null; - + NodeList list = document.getElementsByTagName( "channel" ); - + if ( list.getLength() != 1 ) { - throw new PortletException( ERROR_NOT_VALID ); + throw new PortletException( ERROR_NOT_VALID ); } - + channel = list.item( 0 ); - + Node tn = getNode( channel, "title" ); - + if ( tn == null ) { throw new PortletException( ERROR_NOT_VALID ); } else { title = tn.getFirstChild().getNodeValue(); } - + Node dn = getNode( channel, "description" ); - + if ( dn != null ) { description = dn.getFirstChild().getNodeValue(); - } + } this.setTitle( title ); this.setDescription( description ); - - + + //now that we have the DOM we should be able to do a transform here. - + String stylesheet = this.getPortletConfig().getInitParameter( "stylesheet" ); - + if ( stylesheet == null ) { throw new PortletException( "The 'stylesheet' parameter was not defined." ); } - + try { //Set encoding for the document to utf-8... String content = SimpleTransform.transform( document, @@ -203,71 +202,71 @@ throw new PortletException( e.getMessage() ); } } catch (Throwable t) { - String message = "RSSPortlet: Couldn't set items for XML document -> " + + String message = "RSSPortlet: Couldn't set items for XML document -> " + url; - + Log.error( message, t ); throw new PortletException( t.getMessage() ); } - - + + } - + /** - Given a base node... search this for a node with the given name and return + Given a base node... search this for a node with the given name and return it or null if it does not exist - */ + */ private static final Node getNode( Node start, String name ) { - + NodeList list = start.getChildNodes(); - + for ( int i = 0; i < list.getLength(); ++i ) { Node node = list.item( i ); - + if ( node.getNodeName().equals( name ) ) { return node; } } return null; } - + /** - Given a URL to some content, clean the content to Xerces can handle it + Given a URL to some content, clean the content to Xerces can handle it better. Right now this involves: <ul> <li> - If the document doesn't begin with "<?xml version=" truncate the + If the document doesn't begin with "<?xml version=" truncate the content until this is the first line </li> </ul> - + */ /** test it... */ private Reader cleanse( String content ) throws IOException, SAXException { - + String filtered = null; - + //specify the XML declaration to search for... this is just a subset //of the content but it will always exist. String XMLDECL = "<?xml version="; - + int start = content.indexOf( XMLDECL ); - + if ( start <= 0 ) { filtered = content; } else { filtered = content.substring( start, content.length() ); } - + return new StringReader( filtered ); - } + } /** Get the items that were defined by this XML content @@ -279,13 +278,13 @@ public void setItems( Item[] items ) { this.items = items; } - - - //Cacheable interface.. + + + //Cacheable interface.. /** @author <A HREF="mailto:[EMAIL PROTECTED]">Kevin A. Burton</A> - @version $Id: RSSPortlet.java,v 1.46 2001/12/05 18:46:55 sgala Exp $ + @version $Id: RSSPortlet.java,v 1.47 2002/05/07 14:03:23 raphael Exp $ */ public boolean isCacheable() { return true; @@ -295,32 +294,32 @@ Given a Document, find all the <item>'s and return them as peer's */ private Item[] parseItems( Document doc ) { - + String root = doc.getDocumentElement().getTagName(); - + if ( root.equals( "rdf:RDF" ) ) { - + NodeList list = doc.getElementsByTagName( "item" ); - + return getItems( list ); - - //parse out each nodelist item and create - + + //parse out each nodelist item and create + } else if ( root.equals( "rss" ) ) { - + NodeList list = doc.getElementsByTagName( "channel" ); - + if ( list.getLength() != 1 ) { //if there aren't any channels there can't be any items. return new Item[0]; } - + Node channel = list.item( 0 ); - + //how get the items as a nodelist - + return getItems( channel.getChildNodes() ); - + } else if ( root.equals( "xml" ) ) { @@ -334,9 +333,9 @@ //don't know what to do... return new Item[0]; } - + } - + /** After you find the nodelist for items parse it out and get some Items */ @@ -344,16 +343,16 @@ Vector v = new Vector(); - + for ( int i = 0; i < items.getLength(); ++i ) { - + Node node = items.item( i ); //just make sure this is an <item> element if ( node.getNodeName().equals( "item" ) == false ) { continue; } - + NodeList itemChildren = node.getChildNodes(); String title = null; @@ -361,7 +360,7 @@ String description = null; for ( int j = 0; j < itemChildren.getLength(); ++j ) { - + Node child = itemChildren.item( j ); @@ -380,19 +379,58 @@ description = child.getFirstChild().getNodeValue(); } } - - + + } - + v.addElement( new Item( title, link, description ) ); - + } - + Item[] foundItems = new Item[ v.size() ]; v.copyInto( foundItems ); return foundItems; - + } - -} + /** + Represents an RSS item. + */ + public static class Item { + + private String title; + private String link; + private String description; + + public Item( String title, + String link, + String description ) { + this( title, link ); + this.description = description; + + } + + public Item ( String title, + String link ) { + this.title = title; + this.link = link; + } + + public String getTitle() { + return this.title; + } + + public String getLink() { + return this.link; + } + + /** + Get the description for this item... it may be null. + */ + public String getDescription() { + return this.description; + } + + } + +}
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
