1 16 17 package org.apache.jetspeed.portal.portlets; 18 19 import java.io.IOException ; 21 import java.io.Reader ; 22 import java.io.StringReader ; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 27 import org.apache.jetspeed.util.JetspeedClearElement; 29 import org.apache.ecs.ConcreteElement; 30 31 import org.apache.jetspeed.util.MimeType; 33 import org.apache.jetspeed.util.SimpleTransform; 34 import org.apache.jetspeed.cache.disk.JetspeedDiskCache; 35 import org.apache.jetspeed.portal.PortletException; 36 import org.apache.jetspeed.xml.JetspeedXMLEntityResolver; 37 import org.apache.jetspeed.capability.CapabilityMap; 38 import org.apache.jetspeed.capability.CapabilityMapFactory; 39 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 40 import org.apache.jetspeed.services.logging.JetspeedLogger; 41 import org.apache.jetspeed.services.rundata.JetspeedRunData; 42 43 import org.apache.turbine.util.RunData; 45 46 import javax.xml.parsers.DocumentBuilder ; 48 import javax.xml.parsers.DocumentBuilderFactory ; 49 50 import org.w3c.dom.Document ; 52 import org.w3c.dom.Node ; 53 import org.w3c.dom.NodeList ; 54 import org.xml.sax.InputSource ; 55 import org.xml.sax.SAXException ; 56 57 77 public class NewRSSPortlet extends FileWatchPortlet 78 { 79 80 83 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(NewRSSPortlet.class.getName()); 84 85 public final static String ERROR_NOT_VALID = "This does not appear to be an RSS document"; 86 public final static String INVALID_TYPE = "Unable to display for this browser"; 87 88 private Document document = null; 89 private Hashtable stylesheets = null; 90 private Hashtable params = null; 91 92 96 public void init( ) throws PortletException { 97 98 super.init(); 100 101 DocumentBuilder parser = null; 102 String url = null; 103 104 stylesheets = new Hashtable (); 106 params = new Hashtable (); 107 Iterator i = this.getPortletConfig().getInitParameterNames(); 108 while (i.hasNext()) { 109 String name = (String )i.next(); 110 String base = MimeType.HTML.toString(); 111 112 if (name.startsWith("stylesheet")) { 113 int idx=-1; 114 if ((idx=name.indexOf("."))>-1) { 115 base= name.substring(idx+1,name.length()); 116 } 117 stylesheets.put(base, getPortletConfig().getInitParameter(name)); 118 } else { 119 params.put(name.toLowerCase(), getPortletConfig().getInitParameter(name)); 120 } 121 } 122 123 try 125 { 126 final DocumentBuilderFactory docfactory = DocumentBuilderFactory.newInstance(); 127 docfactory.setValidating(false); 129 parser= docfactory.newDocumentBuilder(); 130 parser.setEntityResolver(new JetspeedXMLEntityResolver() ); 131 132 url = getPortletConfig().getURL(); 133 String content = JetspeedDiskCache.getInstance().getEntry( url ).getData(); 134 CapabilityMap xmap = 135 CapabilityMapFactory.getCapabilityMap(CapabilityMapFactory.AGENT_XML); 136 setContent( new JetspeedClearElement(content), xmap ); 137 InputSource isrc = new InputSource ( this.cleanse( content ) ); 138 isrc.setSystemId( url ); 139 isrc.setEncoding("UTF-8"); 140 this.document = parser.parse( isrc ); 141 this.setMetainfo(document); 142 143 } catch ( Throwable t ) 144 { 145 146 String message = "RSSPortlet: Couldn't parse out XML document -> " + 147 url; 148 149 logger.error( message, t ); 150 throw new PortletException( t.getMessage() ); 151 } 152 153 154 } 155 156 161 private void setMetainfo(Document document) throws PortletException 162 { 163 String title = null; 165 String description = null; 166 167 Node channel = null; 169 170 NodeList list = document.getElementsByTagName( "channel" ); 171 172 if ( list.getLength() != 1 ) { 173 throw new PortletException( ERROR_NOT_VALID ); 174 } 175 176 channel = list.item( 0 ); 177 178 Node tn = getNode( channel, "title" ); 179 180 if ( tn == null ) { 181 throw new PortletException( ERROR_NOT_VALID ); 182 } 183 else 184 { 185 Node fc = tn.getFirstChild(); 186 if (fc != null) 187 { 188 title = fc.getNodeValue(); 189 } 190 } 191 192 Node dn = getNode( channel, "description" ); 193 194 if ( dn != null ) 195 { 196 Node fc = dn.getFirstChild(); 197 if (fc != null) 198 { 199 description = fc.getNodeValue(); 200 } 201 } 202 203 this.setTitle( title ); 204 this.setDescription( description ); 205 } 206 207 214 public ConcreteElement getContent( RunData data ) 215 { 216 if (org.apache.jetspeed.util.PortletSessionState.getPortletConfigChanged(this, data)) 217 { 218 try 219 { 220 init(); 221 } 222 catch (PortletException pe) 223 { 224 logger.error("Exception", pe); 225 } 226 } 227 CapabilityMap map = ((JetspeedRunData)data).getCapability(); 228 String type = map.getPreferredType().toString(); 229 ConcreteElement content = new JetspeedClearElement(INVALID_TYPE); 230 String stylesheet = (String )stylesheets.get(type); 231 232 if (stylesheet != null) { 233 content = getContent( data, map ); 234 if ( content == null ) { 235 try { 236 content = new JetspeedClearElement( 237 SimpleTransform.transform( this.document, 238 stylesheet, 239 this.params ) ); 240 setContent( content, map ); 241 } catch ( SAXException e ) { 242 logger.error("Exception", e); 243 content = new JetspeedClearElement(e.getMessage()); 244 } 245 } 246 } 247 else 248 { 249 if (map.getPreferredType().equals(MimeType.XML)) 250 { 251 return getContent( data, map ); 252 } 253 } 254 255 return content; 256 } 257 258 267 public boolean supportsType( MimeType mimeType ) { 268 269 Enumeration en = stylesheets.keys(); 270 while(en.hasMoreElements()) { 271 String type = (String )en.nextElement(); 272 if (type.equals(mimeType.toString())) return true; 273 } 274 275 return false; 276 } 277 278 287 private final Node getNode( Node start, String name ) { 288 289 NodeList list = start.getChildNodes(); 290 291 for ( int i = 0; i < list.getLength(); ++i ) { 292 293 Node node = list.item( i ); 294 295 if ( node.getNodeName().equals( name ) ) { 296 return node; 297 } 298 } 299 return null; 300 } 301 302 314 private Reader cleanse( String content ) throws IOException { 315 316 String filtered = null; 317 318 String XMLDECL = "<?xml version="; 321 322 int start = content.indexOf( XMLDECL ); 323 324 if ( start <= 0 ) { 325 filtered = content; 326 } else { 327 filtered = content.substring( start, content.length() ); 328 } 329 330 return new StringReader ( filtered ); 331 } 332 333 } 334 335 | Popular Tags |