KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > htmlparser > HtmlParserBaseService


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
package org.jahia.services.htmlparser;
14
15 import java.util.Iterator JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Vector JavaDoc;
19
20 import org.jahia.data.FormDataManager;
21 import org.jahia.engines.EngineMessage;
22 import org.jahia.engines.EngineMessages;
23 import org.jahia.exceptions.JahiaException;
24 import org.jahia.exceptions.JahiaInitializationException;
25 import org.jahia.registries.ServicesRegistry;
26 import org.jahia.services.sites.JahiaSite;
27 import org.jahia.settings.SettingsBean;
28 import org.jahia.utils.properties.PropertiesManager;
29 import org.springframework.beans.factory.BeanFactory;
30 import org.jahia.bin.JahiaConfig;
31
32
33 /**
34  *
35  * <p>Title: Html parsing service , implementation based on Tidy </p>
36  * <p>Description: </p>
37  * <p>Copyright: Copyright (c) 2002</p>
38  * <p>Company: </p>
39  * @author Khue Nguyen
40  * @version 1.0
41  */

42 public class HtmlParserBaseService extends HtmlParserService
43 {
44
45     private static org.apache.log4j.Logger logger =
46             org.apache.log4j.Logger.getLogger(HtmlParserBaseService.class);
47
48     static private HtmlParserBaseService instance = null;
49
50     private PropertiesManager tidyConfigFile = null;
51
52     private Hashtable JavaDoc markupDefinitions = null;
53
54     private Hashtable JavaDoc markupSettings = null;
55
56     private SettingsBean jSettings;
57
58     private HtmlParser parser = null;
59
60     protected HtmlParserBaseService() {
61         logger.info("***** Starting the HtmlParserBaseService *****" );
62     }
63
64     public void init( SettingsBean jSettings )
65     throws JahiaInitializationException {
66
67         this.jSettings = jSettings;
68
69         this.parser = loadHtmlParser();
70
71         // load all markup settings
72
try {
73             markupSettings = MarkupSettingPersistence
74                            .getInstance().loadAllMarkupSettings();
75
76             markupDefinitions = MarkupDefinitionPersistence
77                            .getInstance().loadAllMarkupDefinitions();
78
79         } catch ( Throwable JavaDoc t ){
80             logger.debug("Error loading Markup Settings",t);
81             markupDefinitions = new Hashtable JavaDoc();
82             markupSettings = new Hashtable JavaDoc();
83         }
84     }
85
86     public static HtmlParserBaseService getInstance() {
87         if (instance == null) {
88             synchronized ( HtmlParserBaseService.class ){
89                 if (instance == null) {
90                     instance = new HtmlParserBaseService();
91                 }
92             }
93         }
94         return instance;
95     }
96
97     /**
98      *
99      * @return SettingsBean
100      */

101     public SettingsBean getSettingsBean(){
102         return this.jSettings;
103     }
104
105     /**
106      * Return a parser no regard to a specific site.
107      *
108      * @return
109      */

110     public HtmlParser getParser(){
111         return parser;
112     }
113
114     /**
115      * Return a parser for a given site.
116      *
117      * @param siteId
118      * @return
119      */

120     public HtmlParser getParser(int siteId){
121         return parser;
122     }
123
124     /**
125      * Parse an input document, checkout html markups integrity,
126      * remove unwanted markups.
127      *
128      * Using default parser and default setting
129      *
130      * @param inputString
131      * @param DOMVisitors
132      * @return
133      */

134     public String JavaDoc parse(String JavaDoc inputString, Vector JavaDoc DOMVisitors){
135         if ( parser == null ){
136             return inputString;
137         }
138        return parser.parse(inputString, DOMVisitors);
139     }
140
141     /**
142      * Parse an input document, checkout html markups integrity,
143      * remove unwanted markups.
144      * Using parser defined and configured for the given site
145      *
146      * @param inputString
147      * @param DOMVisitors
148      * @param siteId
149      * @return
150      */

151     public String JavaDoc parse(String JavaDoc inputString, Vector JavaDoc DOMVisitors, int siteId){
152         if ( parser == null ){
153             return inputString;
154         }
155         return parser.parse(inputString, DOMVisitors, siteId);
156     }
157
158
159     /**
160      * Return all markup definitions
161      *
162      * @return
163      */

164     public Iterator JavaDoc getMarkupDefinitions(){
165         return this.markupDefinitions.values().iterator();
166     }
167
168     /**
169      * Return a markup definition
170      *
171      * @return
172      */

173     public MarkupDefinition getMarkupDefinition(int id){
174         return (MarkupDefinition)this.markupDefinitions.get(new Integer JavaDoc(id));
175     }
176
177     /**
178      * Return a markup definition looking at the name
179      *
180      * @return
181      */

182     public MarkupDefinition getMarkupDefinition(String JavaDoc markupName){
183         Iterator JavaDoc iterator = this.markupDefinitions.values().iterator();
184         MarkupDefinition def = null;
185         while ( iterator.hasNext() ){
186             def = (MarkupDefinition)iterator.next();
187             if ( def.getName().equals(markupName) ){
188                 return def;
189             }
190         }
191         return null;
192     }
193
194     /**
195      * Save the state of an markup definition
196      *
197      * @param markupDefinition
198      */

199     public void saveMarkupDefinition(MarkupDefinition markupDefinition)
200     throws JahiaException {
201         if ( markupDefinition == null ){
202             return;
203         }
204         MarkupDefinitionPersistence.getInstance()
205                 .createMarkupDefinition(markupDefinition);
206         this.markupDefinitions.put(new Integer JavaDoc(markupDefinition.getId()),markupDefinition);
207     }
208
209     /**
210      * Remove a markup definition
211      *
212      * @param id
213      */

214     public void removeMarkupDefinition(int id)
215     throws JahiaException {
216         MarkupDefinition markupDefinition =
217                 (MarkupDefinition)this.markupDefinitions.get(new Integer JavaDoc(id));
218         if ( markupDefinition != null ){
219             MarkupDefinitionPersistence.getInstance().removeMarkupDefinition(markupDefinition);
220         }
221         this.markupDefinitions.remove(new Integer JavaDoc(markupDefinition.getId()));
222     }
223
224     /**
225      * Return all markup settings
226      *
227      * @return
228      */

229     public Iterator JavaDoc getMarkupSettings(){
230         return this.markupSettings.values().iterator();
231     }
232
233     /**
234      * Return the markup settings for a given site inclusing those defined for all
235      * sites.
236      *
237      * @param siteId
238      * @return
239      */

240     public Iterator JavaDoc getMarkupSettings(int siteId){
241         HashSet JavaDoc result = new HashSet JavaDoc();
242         Iterator JavaDoc iterator = this.markupSettings.values().iterator();
243         MarkupSetting mSetting = null;
244         while ( iterator.hasNext() ){
245             mSetting = (MarkupSetting)iterator.next();
246             if ( mSetting.applyToAllSite()
247                  || mSetting.containSite(siteId) ){
248                 result.add(mSetting);
249             }
250         }
251         return result.iterator();
252     }
253
254     /**
255      * Return the markup settings of a given type
256      *
257      * @param type
258      * @return
259      */

260     public Iterator JavaDoc getMarkupSettings(String JavaDoc type){
261         HashSet JavaDoc result = new HashSet JavaDoc();
262         Iterator JavaDoc iterator = this.markupSettings.values().iterator();
263         MarkupSetting mSetting = null;
264         while ( iterator.hasNext() ){
265             mSetting = (MarkupSetting)iterator.next();
266             if ( mSetting.getSettingType().equals(type) ){
267                 result.add(mSetting);
268             }
269         }
270         return result.iterator();
271     }
272
273     /**
274      * Return a markup setting
275      *
276      * @return
277      */

278     public MarkupSetting getMarkupSetting(int id){
279         return (MarkupSetting)this.markupSettings.get(new Integer JavaDoc(id));
280     }
281
282     /**
283      * Save the state of an HtmlMarkupSetting
284      *
285      * @param markupSetting
286      */

287     public void saveMarkupSetting(MarkupSetting markupSetting)
288     throws JahiaException {
289         if ( markupSetting == null ){
290             return;
291         }
292         MarkupSettingPersistence.getInstance()
293                 .createMarkupSetting(markupSetting);
294         this.markupSettings.put(new Integer JavaDoc(markupSetting.getSettingId()),markupSetting);
295     }
296
297     /**
298      * Remove a markup setting
299      *
300      * @param id
301      */

302     public void removeMarkupSetting(int id)
303     throws JahiaException {
304         MarkupSetting mSetting = (MarkupSetting)this.markupSettings.get(new Integer JavaDoc(id));
305         if ( mSetting != null ){
306             MarkupSettingPersistence.getInstance().removeMarkupSetting(mSetting);
307         }
308         this.markupSettings.remove(new Integer JavaDoc(mSetting.getSettingId()));
309     }
310
311     /**
312      * Return a vector of registered HtmlDOMVisitors
313      *
314      * @param siteKey
315      * @return
316      */

317     public Vector JavaDoc getHtmlDOMVisitors(int siteId)
318     throws JahiaException {
319         Vector JavaDoc v = new Vector JavaDoc();
320         JahiaSite site = ServicesRegistry.getInstance()
321                        .getJahiaSitesService().getSite(siteId);
322         if ( site.getHtmlMarkupFiltering()==1 ){
323             RemoveUnwantedMarkupVisitor visitor
324                     = new RemoveUnwantedMarkupVisitor();
325             visitor.init(siteId);
326             v.add(visitor);
327         }
328         return v;
329     }
330
331     /**
332      * Check if there is any html parsing errors
333      * If there is any error, store an MessageError with property key = "htmlParser"
334      *
335      * @param value String, the result of a previous Html parsing
336      * @return EngineMessages
337      */

338     public EngineMessages checkTextParsingErrors(String JavaDoc value){
339         EngineMessages resultMessages = new EngineMessages();
340         if ( value != null && value.indexOf(TidyHtmlParser.TIDYERRORS_TAG) != -1 ){
341             String JavaDoc errorMsg = value.substring(value.indexOf(TidyHtmlParser.TIDYERRORS_TAG)
342                                                    +TidyHtmlParser.TIDYERRORS_TAG.length()+1,
343                                                    value.length()-TidyHtmlParser.TIDYERRORS_TAG.length()-3);
344             EngineMessage errorMessage = new EngineMessage("org.jahia.data.HtmlParser.htmlParserError",
345                 new String JavaDoc(FormDataManager.getInstance().formEncode(errorMsg)));
346             resultMessages.add("htmlParser", errorMessage);
347         } else {
348             FormDataManager.getInstance().checkIntegrity(value,resultMessages);
349         }
350         return resultMessages;
351     }
352
353     /**
354      *
355      * @return HtmlParser
356      * @throws JahiaException
357      */

358     private HtmlParser loadHtmlParser() {
359         HtmlParser parser = null;
360         BeanFactory bf = org.jahia.bin.Jahia.getConfigBeanFactory();
361         if ( bf == null ){
362             return null;
363         }
364         JahiaConfig jahiaConfig = (JahiaConfig)bf.getBean("jahiaConfigBean");
365         String JavaDoc className = jahiaConfig.getProperty("org.jahia.services.htmlparser.HtmlParser");
366         if (className != null) {
367             try {
368                 Class JavaDoc c = Class.forName(className);
369                 parser = (HtmlParser) c.newInstance();
370                 if ( parser != null ){
371                     parser.init(this);
372                 }
373                 return parser;
374             } catch (ClassNotFoundException JavaDoc cnfe) {
375                 logger.debug(cnfe);
376             } catch (InstantiationException JavaDoc ie) {
377                 logger.debug(ie);
378             } catch (IllegalAccessException JavaDoc iae) {
379                 logger.debug(iae);
380             }
381         }
382         return null;
383     }
384
385 }
386
387
Popular Tags