1 package org.jahia.services.htmlparser; 14 15 import java.util.Iterator ; 16 import java.util.HashSet ; 17 import java.util.Hashtable ; 18 import java.util.Vector ; 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 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 markupDefinitions = null; 53 54 private Hashtable 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 try { 73 markupSettings = MarkupSettingPersistence 74 .getInstance().loadAllMarkupSettings(); 75 76 markupDefinitions = MarkupDefinitionPersistence 77 .getInstance().loadAllMarkupDefinitions(); 78 79 } catch ( Throwable t ){ 80 logger.debug("Error loading Markup Settings",t); 81 markupDefinitions = new Hashtable (); 82 markupSettings = new Hashtable (); 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 101 public SettingsBean getSettingsBean(){ 102 return this.jSettings; 103 } 104 105 110 public HtmlParser getParser(){ 111 return parser; 112 } 113 114 120 public HtmlParser getParser(int siteId){ 121 return parser; 122 } 123 124 134 public String parse(String inputString, Vector DOMVisitors){ 135 if ( parser == null ){ 136 return inputString; 137 } 138 return parser.parse(inputString, DOMVisitors); 139 } 140 141 151 public String parse(String inputString, Vector DOMVisitors, int siteId){ 152 if ( parser == null ){ 153 return inputString; 154 } 155 return parser.parse(inputString, DOMVisitors, siteId); 156 } 157 158 159 164 public Iterator getMarkupDefinitions(){ 165 return this.markupDefinitions.values().iterator(); 166 } 167 168 173 public MarkupDefinition getMarkupDefinition(int id){ 174 return (MarkupDefinition)this.markupDefinitions.get(new Integer (id)); 175 } 176 177 182 public MarkupDefinition getMarkupDefinition(String markupName){ 183 Iterator 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 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 (markupDefinition.getId()),markupDefinition); 207 } 208 209 214 public void removeMarkupDefinition(int id) 215 throws JahiaException { 216 MarkupDefinition markupDefinition = 217 (MarkupDefinition)this.markupDefinitions.get(new Integer (id)); 218 if ( markupDefinition != null ){ 219 MarkupDefinitionPersistence.getInstance().removeMarkupDefinition(markupDefinition); 220 } 221 this.markupDefinitions.remove(new Integer (markupDefinition.getId())); 222 } 223 224 229 public Iterator getMarkupSettings(){ 230 return this.markupSettings.values().iterator(); 231 } 232 233 240 public Iterator getMarkupSettings(int siteId){ 241 HashSet result = new HashSet (); 242 Iterator 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 260 public Iterator getMarkupSettings(String type){ 261 HashSet result = new HashSet (); 262 Iterator 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 278 public MarkupSetting getMarkupSetting(int id){ 279 return (MarkupSetting)this.markupSettings.get(new Integer (id)); 280 } 281 282 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 (markupSetting.getSettingId()),markupSetting); 295 } 296 297 302 public void removeMarkupSetting(int id) 303 throws JahiaException { 304 MarkupSetting mSetting = (MarkupSetting)this.markupSettings.get(new Integer (id)); 305 if ( mSetting != null ){ 306 MarkupSettingPersistence.getInstance().removeMarkupSetting(mSetting); 307 } 308 this.markupSettings.remove(new Integer (mSetting.getSettingId())); 309 } 310 311 317 public Vector getHtmlDOMVisitors(int siteId) 318 throws JahiaException { 319 Vector v = new Vector (); 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 338 public EngineMessages checkTextParsingErrors(String value){ 339 EngineMessages resultMessages = new EngineMessages(); 340 if ( value != null && value.indexOf(TidyHtmlParser.TIDYERRORS_TAG) != -1 ){ 341 String 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 (FormDataManager.getInstance().formEncode(errorMsg))); 346 resultMessages.add("htmlParser", errorMessage); 347 } else { 348 FormDataManager.getInstance().checkIntegrity(value,resultMessages); 349 } 350 return resultMessages; 351 } 352 353 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 className = jahiaConfig.getProperty("org.jahia.services.htmlparser.HtmlParser"); 366 if (className != null) { 367 try { 368 Class c = Class.forName(className); 369 parser = (HtmlParser) c.newInstance(); 370 if ( parser != null ){ 371 parser.init(this); 372 } 373 return parser; 374 } catch (ClassNotFoundException cnfe) { 375 logger.debug(cnfe); 376 } catch (InstantiationException ie) { 377 logger.debug(ie); 378 } catch (IllegalAccessException iae) { 379 logger.debug(iae); 380 } 381 } 382 return null; 383 } 384 385 } 386 387 | Popular Tags |