1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.rss.web;
35
36 import fr.paris.lutece.plugins.rss.business.RssFeed;
37 import fr.paris.lutece.plugins.rss.business.RssFeedHome;
38 import fr.paris.lutece.plugins.rss.service.RssContent;
39 import fr.paris.lutece.plugins.rss.service.RssContentService;
40 import fr.paris.lutece.plugins.rss.service.RssPlugin;
41 import fr.paris.lutece.portal.business.style.ModeHome;
42 import fr.paris.lutece.portal.business.style.Style;
43 import fr.paris.lutece.portal.business.style.StyleHome;
44 import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
45 import fr.paris.lutece.portal.service.content.PageData;
46 import fr.paris.lutece.portal.service.html.XmlTransformerService;
47 import fr.paris.lutece.portal.service.i18n.I18nService;
48 import fr.paris.lutece.portal.service.includes.PageInclude;
49 import fr.paris.lutece.portal.service.plugin.Plugin;
50 import fr.paris.lutece.portal.service.plugin.PluginService;
51 import fr.paris.lutece.portal.service.template.AppTemplateService;
52 import fr.paris.lutece.portal.service.util.AppLogService;
53 import fr.paris.lutece.portal.service.util.AppPathService;
54 import fr.paris.lutece.portal.service.util.AppPropertiesService;
55 import fr.paris.lutece.util.html.HtmlTemplate;
56
57 import java.io.File;
58 import java.io.StringReader;
59
60 import java.util.Collection;
61 import java.util.HashMap;
62 import java.util.List;
63 import java.util.Locale;
64 import java.util.Map;
65
66 import javax.servlet.http.HttpServletRequest;
67
68 import javax.xml.transform.stream.StreamSource;
69
70
71
72
73
74 public class RssFeedInclude implements PageInclude
75 {
76
77
78
79 private static final String MARK_RSS = "rss";
80 private static final String MARK_PLUGIN = "plugin";
81 private static final String MARK_LOCALE = "locale";
82
83
84
85
86 private static final String MARK_RSS_FILES_LIST = "rss_list";
87
88
89
90
91 private static final String TEMPLATE_RSS_INCLUDE = "skin/plugins/rss/rss_feed_include.html";
92
93
94 private static final String PROPERTY_RSS_MARKER_PREFIX = "rss.include.marker.prefix";
95 private static final String PROPERTY_PATH_XSL = "path.stylesheet";
96
97
98 private static final String MARK_DEFAULT_RSS_MARKER_PREFIX = "rss_";
99
100
101 private static final int CONSTANTE_RSS_STATUT_OK = 0;
102
103
104
105
106
107
108
109
110 public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
111 {
112 Plugin plugin = PluginService.getPlugin( RssPlugin.PLUGIN_NAME );
113 Locale locale = getLocale( request );
114
115 HashMap<String, Collection<RssFeed>> model = new HashMap<String, Collection<RssFeed>>( );
116 model.put( MARK_RSS_FILES_LIST, RssFeedHome.getRssFeeds( ) );
117
118 List<RssFeed> listRss = RssFeedHome.getRssFeeds( );
119
120 for ( RssFeed rssFeed : listRss )
121 {
122 if ( rssFeed.getLastFetchStatus( ) == CONSTANTE_RSS_STATUT_OK )
123 {
124 rootModel.put( getRssMarkerPrefix( ) + String.valueOf( rssFeed.getId( ) ),
125 getTemplateHtmlForRss( rssFeed, plugin, locale ) );
126 }
127 }
128 }
129
130 public static String getRssMarkerPrefix( )
131 {
132 return AppPropertiesService.getProperty( PROPERTY_RSS_MARKER_PREFIX, MARK_DEFAULT_RSS_MARKER_PREFIX );
133 }
134
135
136
137
138
139
140
141
142 private String getTemplateHtmlForRss( RssFeed rss, Plugin plugin, Locale locale )
143 {
144 HashMap<String, Object> model = new HashMap<String, Object>( );
145
146 RssContent rssContent = RssContentService.getInstance( ).getRssContent( String.valueOf( rss.getId( ) ) );
147
148 String strHtml = "";
149
150 if ( rss.getIdIncludeStyle( ) != -1 )
151 {
152 Style rssStyle = StyleHome.findByPrimaryKey( rss.getIdIncludeStyle( ) );
153 Collection<StyleSheet> rssStyleSheetList = StyleHome.getStyleSheetList( rssStyle.getId( ) );
154 String strXslDirectory = null;
155 String strXslFileName = null;
156
157 for ( StyleSheet rssStyleSheet : rssStyleSheetList )
158 {
159 strXslDirectory = AppPathService.getPath( PROPERTY_PATH_XSL ) +
160 ModeHome.findByPrimaryKey( rssStyleSheet.getModeId( ) ).getPath( );
161 strXslFileName = rssStyleSheet.getFile( );
162 }
163
164 String strXslPath = strXslDirectory + strXslFileName;
165
166 File fileXsl = new File( strXslPath );
167 StreamSource sourceStyleSheet = new StreamSource( fileXsl );
168 StringReader srInput = new StringReader( rssContent.getContent( ) );
169 StreamSource sourceDocument = new StreamSource( srInput );
170
171 try
172 {
173 XmlTransformerService xmlTransformerService = new XmlTransformerService( );
174 strHtml = xmlTransformerService.transformBySourceWithXslCache( sourceDocument, sourceStyleSheet,
175 strXslPath, null, null );
176 }
177 catch ( Exception e )
178 {
179 AppLogService.error( e );
180 }
181 }
182 else
183 {
184 strHtml = rssContent.getContent( );
185 }
186
187 model.put( MARK_RSS, strHtml );
188
189 model.put( MARK_PLUGIN, plugin );
190 model.put( MARK_LOCALE, locale );
191
192 HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_RSS_INCLUDE, Locale.getDefault( ), model );
193
194 return templateList.getHtml( );
195 }
196
197
198
199
200
201
202 private Locale getLocale( HttpServletRequest request )
203 {
204 Locale locale = null;
205
206 if ( request != null )
207 {
208 locale = request.getLocale( );
209 }
210 else
211 {
212 locale = I18nService.getDefaultLocale( );
213 }
214
215 return locale;
216 }
217 }