View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.plugin.basic;
17  
18  import java.io.IOException;
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.seasar.tuigwaa.cms.core.CmsConstants;
27  import org.seasar.tuigwaa.cms.core.CmsRequest;
28  import org.seasar.tuigwaa.cms.core.CmsResponse;
29  import org.seasar.tuigwaa.plugin.AbstractPlugin;
30  import org.seasar.tuigwaa.plugin.PluginException;
31  import org.seasar.tuigwaa.plugin.PluginRequest;
32  
33  import com.isenshi.util.HtmlBuffer;
34  import com.sun.syndication.feed.synd.SyndEntry;
35  import com.sun.syndication.feed.synd.SyndFeed;
36  import com.sun.syndication.io.FeedException;
37  import com.sun.syndication.io.SyndFeedInput;
38  import com.sun.syndication.io.XmlReader;
39  
40  /***
41   * @author someda
42   */
43  public class RssreaderPlugin extends AbstractPlugin {
44  
45  	private static final String DISPLAY_TYPES_LIST = "list";
46  	private static final String DISPLAY_TYPES_TABLE = "table";
47  	
48  	private static final String DEFAULT_DISPLAY_TYPES = DISPLAY_TYPES_TABLE;
49  	
50  	private static final Map colmap = buildColmap();	
51  		
52  	public String doHTMLView(CmsRequest request, CmsResponse response,
53  			PluginRequest prequest) throws PluginException {
54  				
55  		String[] args = prequest.getArgs();
56  		HtmlBuffer buf = null;
57  		String type = DEFAULT_DISPLAY_TYPES;
58  		
59  		if(args == null || args.length < 1) 
60  			throw new PluginException("feed url required.");
61  		
62  		if(args.length >= 2 && args[1] !=null) 
63  			type = args[1];
64  						
65  		try{
66  			URL url = new URL(args[0]);
67  			SyndFeedInput input = new SyndFeedInput();
68  			SyndFeed feed = input.build(new XmlReader(url));
69  			
70  			buf = new HtmlBuffer();
71  			buf.appendHeading(4,feed.getTitle());
72  			
73  			if(type.equalsIgnoreCase(DISPLAY_TYPES_TABLE)){
74  				buildTableHTML(buf,feed);
75  			}else if(type.equalsIgnoreCase(DISPLAY_TYPES_LIST)){
76  				buildListHTML(buf,feed);
77  			}else{
78  				buildListHTML(buf,feed);
79  			}
80  						
81  		}catch(MalformedURLException mue){
82  			buf = new HtmlBuffer();
83  			buf.appendBody(getMessage("rssreader.malformedurl"));
84  			buf.appendBr();
85  		}catch(IOException ioe){
86  			buf = new HtmlBuffer();
87  			buf.appendBody(getMessage("rssreader.notfound"));
88  			buf.appendBr();			
89  		}catch(FeedException fe){
90  			throw new PluginException(fe);
91  		}				
92  		return buf.toString();
93  	}
94  	
95  	private void buildListHTML(HtmlBuffer buf, SyndFeed feed){
96  		List entries = feed.getEntries();
97  		
98  		if(entries != null){
99  			buf.appendStartTag("dl");
100 			for(Iterator i = entries.iterator();i.hasNext();){
101 				SyndEntry entry = (SyndEntry) i.next();
102 				buf.appendStartTag("dt");
103 				buf.setNewline(false);
104 				buf.appendAnchor(entry.getLink(),entry.getTitle());				
105 				if(entry.getPublishedDate() != null)
106 					buf.appendBody(HtmlBuffer.ENTITY_SPACE + CmsConstants.DATEFORMAT.format(entry.getPublishedDate()));					
107 				
108 				buf.setNewline(true);				
109 				buf.endTag();
110 				buf.appendStartTag("dd");
111 				buf.appendBody(entry.getDescription().getValue());				
112 				buf.endTag(); //tr
113 			}
114 			buf.endTag();
115 		}
116 		
117 		
118 	}
119 	
120 	private void buildTableHTML(HtmlBuffer buf, SyndFeed feed){
121 
122 		List entries = feed.getEntries();
123 		
124 		if(entries != null){
125 			buf.appendStartTag(HtmlBuffer.TAG_TABLE);
126 			for(Iterator i = entries.iterator();i.hasNext();){
127 				SyndEntry entry = (SyndEntry) i.next();
128 				buf.appendStartTag(HtmlBuffer.TAG_TR);
129 				buf.appendStartTag(HtmlBuffer.TAG_TD);
130 				buf.setNewline(false);
131 				buf.appendAnchor(entry.getLink(),entry.getTitle());
132 				buf.setNewline(true);
133 				buf.endTag();
134 				if(entry.getPublishedDate() != null){
135 					buf.appendTd(CmsConstants.DATEFORMAT.format(entry.getPublishedDate()));
136 				}else{
137 					buf.appendTd(HtmlBuffer.ENTITY_SPACE);
138 				}
139 				buf.endTag();
140 				buf.appendStartTag(HtmlBuffer.TAG_TR);
141 				buf.appendTd(entry.getDescription().getValue(),colmap,false);
142 				buf.endTag(); //tr
143 			}
144 			buf.endTag();
145 		}
146 	}	
147 
148 	private static Map buildColmap(){
149 		Map map = new HashMap();
150 		map.put("colspan","2");
151 		return map;
152 	}
153 
154 }