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.comarquage.modules.solr.utils.parsers;
35
36 import java.io.IOException;
37 import java.text.ParseException;
38 import java.text.SimpleDateFormat;
39 import java.util.ArrayList;
40 import java.util.Date;
41 import java.util.List;
42 import java.util.Locale;
43
44 import javax.xml.parsers.ParserConfigurationException;
45 import javax.xml.parsers.SAXParser;
46 import javax.xml.parsers.SAXParserFactory;
47
48 import org.xml.sax.Attributes;
49 import org.xml.sax.SAXException;
50 import org.xml.sax.helpers.DefaultHandler;
51
52 import fr.paris.lutece.plugins.search.solr.indexer.SolrItem;
53 import fr.paris.lutece.plugins.search.solr.util.SolrConstants;
54 import fr.paris.lutece.portal.service.content.XPageAppService;
55 import fr.paris.lutece.portal.service.util.AppLogService;
56 import fr.paris.lutece.portal.service.util.AppPathService;
57 import fr.paris.lutece.portal.service.util.AppPropertiesService;
58 import fr.paris.lutece.util.url.UrlItem;
59
60
61
62
63
64 public class CoMarquageSolrPublicParser extends DefaultHandler
65 {
66
67
68
69
70 private static final String PROPERTY_PLUGIN_NAME = "comarquage.plugin.name";
71
72
73 private static final String PROPERTY_INDEXING_XML_BASE_VAR = "comarquage.path.xml";
74 private static final String PROPERTY_INDEXING_FRAGMENT = "comarquage.indexing.";
75 private static final String PROPERTY_LIST_CDC_INDEX_KEYS_FRAGMENT = "listCdcIndexKeys";
76
77
78 private static final String PROPERTY_XPATH_CARD = "comarquage.parser.xpath.public.card";
79 private static final String PROPERTY_XPATH_URL = "comarquage.parser.xpath.public.url";
80 private static final String PROPERTY_XPATH_DATE = "comarquage.parser.xpath.public.date";
81 private static final String PROPERTY_XPATH_TITLE = "comarquage.parser.xpath.public.title";
82 private static final String PROPERTY_XPATH_THEME = "comarquage.parser.xpath.public.theme";
83 private static final String PROPERTY_XPATH_KEYWORDS = "comarquage.parser.xpath.public.keywords";
84
85
86 private static final String PROPERTY_INDEXING_TYPE = "comarquage-solr.indexing.publicType";
87
88
89 private static final String PROPERTY_SITE = "lutece.name";
90 private static final String PROPERTY_PROD_URL = "lutece.prod.url";
91
92
93 private static final String PROPERTY_PATH_ID = "comarquage.parser.path.id";
94
95
96 private static final String PROPERTY_URL_DELIMITER = "comarquage.parser.url.public.delimiter";
97
98
99 private static final String STRING_EMPTY = "";
100 private static final String STRING_EQUAL = "=";
101 private static final String STRING_SLASH = "/";
102 private static final String STRING_SPACE = " ";
103 private static final String SHORT_NAME = "comgepub";
104
105
106
107
108
109 private List<SolrItem> _listSolrItems;
110
111
112 private String _strXPath;
113
114
115 private String _strUrl;
116 private String _strDate;
117 private String _strType;
118 private String _strTitle;
119 private String _strSite;
120 private String _strProdUrl;
121 private String _strTheme;
122 private String _strKeywords;
123
124
125
126
127 public CoMarquageSolrPublicParser( )
128 {
129
130 String strCdcIndexKeys = AppPropertiesService.getProperty( PROPERTY_INDEXING_FRAGMENT +
131 PROPERTY_LIST_CDC_INDEX_KEYS_FRAGMENT );
132
133
134 _listSolrItems = new ArrayList<SolrItem>( );
135
136
137 _strType = AppPropertiesService.getProperty( PROPERTY_INDEXING_TYPE );
138
139
140 _strSite = AppPropertiesService.getProperty( PROPERTY_SITE );
141
142
143 _strProdUrl = AppPropertiesService.getProperty( PROPERTY_PROD_URL );
144
145 if ( !_strProdUrl.endsWith( "/" ) )
146 {
147 _strProdUrl = _strProdUrl + "/";
148 }
149
150 try
151 {
152
153 SAXParserFactory factory = SAXParserFactory.newInstance( );
154 SAXParser parser = factory.newSAXParser( );
155
156
157 String[] splitKeys = strCdcIndexKeys.split( "," );
158
159 for ( int i = 0; i < splitKeys.length; i++ )
160 {
161
162 String strXmlFile = AppPropertiesService.getProperty( PROPERTY_INDEXING_FRAGMENT + splitKeys[i] );
163 String strXmlPath = AppPathService.getPath( PROPERTY_INDEXING_XML_BASE_VAR, strXmlFile );
164
165
166 parser.parse( strXmlPath, this );
167 }
168 }
169 catch ( ParserConfigurationException e )
170 {
171 AppLogService.error( e.getMessage( ), e );
172 }
173 catch ( SAXException e )
174 {
175 AppLogService.error( e.getMessage( ), e );
176 }
177 catch ( IOException e )
178 {
179 AppLogService.error( e.getMessage( ), e );
180 }
181 }
182
183
184
185
186
187
188 public void startDocument( ) throws SAXException
189 {
190
191 _strXPath = STRING_EMPTY;
192
193
194 _strUrl = STRING_EMPTY;
195 _strDate = STRING_EMPTY;
196 _strTitle = STRING_EMPTY;
197 _strTheme = STRING_EMPTY;
198 _strKeywords = STRING_EMPTY;
199 }
200
201
202
203
204
205
206 public void endDocument( ) throws SAXException
207 {
208
209 }
210
211
212
213
214
215
216
217
218
219
220
221 public void startElement( String uri, String localName, String qName, Attributes atts )
222 throws SAXException
223 {
224
225 _strXPath += ( STRING_SLASH + qName );
226
227
228 String strXPathCard = AppPropertiesService.getProperty( PROPERTY_XPATH_CARD );
229
230 if ( ( _strXPath != null ) && _strXPath.equals( strXPathCard ) )
231 {
232 _strUrl = STRING_EMPTY;
233 _strTitle = STRING_EMPTY;
234 _strTheme = STRING_EMPTY;
235 _strKeywords = STRING_EMPTY;
236 }
237 }
238
239
240
241
242
243
244
245
246
247
248 public void endElement( String uri, String localName, String qName )
249 throws SAXException
250 {
251
252 String strXPathCard = AppPropertiesService.getProperty( PROPERTY_XPATH_CARD );
253
254 if ( ( _strXPath != null ) && _strXPath.equals( strXPathCard ) )
255 {
256
257 String strDelimiter = AppPropertiesService.getProperty( PROPERTY_URL_DELIMITER ) + STRING_EQUAL;
258 String strPath = _strUrl.split( strDelimiter )[1];
259
260
261 UrlItem url = new UrlItem( _strProdUrl );
262 url.addParameter( XPageAppService.PARAM_XPAGE_APP, AppPropertiesService.getProperty( PROPERTY_PLUGIN_NAME ) );
263 url.addParameter( AppPropertiesService.getProperty( PROPERTY_PATH_ID ), strPath );
264
265
266 String strContents = _strTitle + STRING_SPACE + _strKeywords + STRING_SPACE + _strTheme;
267
268
269 Locale locale = Locale.FRENCH;
270 Date dateUpdate = null;
271
272 try
273 {
274 SimpleDateFormat dateFormat = new SimpleDateFormat( "dd MMMMM yyyy", locale );
275 dateUpdate = dateFormat.parse( _strDate );
276
277 dateFormat.applyPattern( "yyyyMMdd" );
278 }
279 catch ( ParseException e )
280 {
281 dateUpdate = null;
282 }
283
284
285 SolrItem item = new SolrItem( );
286
287
288
289
290
291
292
293
294 item.setUrl( url.getUrl( ) );
295 item.setDate( dateUpdate );
296 item.setUid( strPath + SolrConstants.CONSTANT_UNDERSCORE + SHORT_NAME );
297 item.setContent( strContents );
298 item.setTitle( _strTitle );
299 item.setType( _strType );
300 item.setSite( _strSite );
301
302
303 _listSolrItems.add( item );
304 }
305
306
307 _strXPath = _strXPath.substring( 0, _strXPath.lastIndexOf( STRING_SLASH ) );
308 }
309
310
311
312
313
314
315
316
317
318
319 public void characters( char[] ch, int start, int length )
320 throws SAXException
321 {
322
323 String strXPathUrl = AppPropertiesService.getProperty( PROPERTY_XPATH_URL );
324 String strXPathDate = AppPropertiesService.getProperty( PROPERTY_XPATH_DATE );
325 String strXPathTitle = AppPropertiesService.getProperty( PROPERTY_XPATH_TITLE );
326 String strXPathTheme = AppPropertiesService.getProperty( PROPERTY_XPATH_THEME );
327 String strXPathKeywords = AppPropertiesService.getProperty( PROPERTY_XPATH_KEYWORDS );
328
329
330 if ( ( _strXPath != null ) && _strXPath.equals( strXPathUrl ) )
331 {
332 _strUrl += new String( ch, start, length );
333 }
334
335
336 else if ( ( _strXPath != null ) && _strXPath.equals( strXPathDate ) )
337 {
338 _strDate += new String( ch, start, length );
339 }
340
341
342 else if ( ( _strXPath != null ) && _strXPath.equals( strXPathTitle ) )
343 {
344 _strTitle += new String( ch, start, length );
345 }
346
347
348 else if ( ( _strXPath != null ) && _strXPath.equals( strXPathTheme ) )
349 {
350 if ( ( _strTheme != null ) && !_strTheme.equals( STRING_EMPTY ) )
351 {
352 _strTheme += ( STRING_SPACE + new String( ch, start, length ) );
353 }
354 else
355 {
356 _strTheme += new String( ch, start, length );
357 }
358 }
359
360
361 else if ( ( _strXPath != null ) && _strXPath.equals( strXPathKeywords ) )
362 {
363 _strKeywords += new String( ch, start, length );
364 }
365 }
366
367
368
369
370
371
372 public List<SolrItem> getPublicSolrItems( )
373 {
374 return _listSolrItems;
375 }
376 }