1 31 32 package org.opencms.workplace.editors; 33 34 import org.opencms.main.CmsLog; 35 import org.opencms.util.CmsStringUtil; 36 import org.opencms.xml.CmsXmlException; 37 import org.opencms.xml.CmsXmlUtils; 38 39 import java.util.ArrayList ; 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Map ; 44 import java.util.regex.Pattern ; 45 import java.util.regex.PatternSyntaxException ; 46 47 import org.apache.commons.logging.Log; 48 49 import org.dom4j.Document; 50 import org.dom4j.Element; 51 52 66 public class CmsWorkplaceEditorConfiguration { 67 68 69 public static final String DOCUMENT_NODE = "editor"; 70 71 72 protected static final String N_AGENT = "agent"; 73 74 75 protected static final String N_CLASS = "class"; 76 77 78 protected static final String N_LABEL = "label"; 79 80 81 protected static final String N_MAPTO = "mapto"; 82 83 84 protected static final String N_NAME = "name"; 85 86 87 protected static final String N_RANKING = "ranking"; 88 89 90 protected static final String N_RESOURCETYPES = "resourcetypes"; 91 92 93 protected static final String N_TYPE = "type"; 94 95 96 protected static final String N_USERAGENTS = "useragents"; 97 98 99 protected static final String N_WIDGETEDITOR = "widgeteditor"; 100 101 102 private static final Log LOG = CmsLog.getLog(CmsWorkplaceEditorConfiguration.class); 103 104 private List m_browserPattern; 105 private String m_editorLabel; 106 private String m_editorUri; 107 private Map m_resTypes; 108 private List m_userAgentsRegEx; 109 private boolean m_validConfiguration; 110 private String m_widgetEditor; 111 112 118 public CmsWorkplaceEditorConfiguration(byte[] xmlData, String editorUri) { 119 120 setValidConfiguration(true); 121 try { 122 initialize(CmsXmlUtils.unmarshalHelper(xmlData, null), editorUri); 123 } catch (CmsXmlException e) { 124 logConfigurationError(Messages.get().getBundle().key(Messages.ERR_XML_PARSE_0), e); 126 } 127 } 128 129 134 public List getBrowserPattern() { 135 136 return m_browserPattern; 137 } 138 139 144 public String getEditorLabel() { 145 146 return m_editorLabel; 147 } 148 149 154 public String getEditorUri() { 155 156 return m_editorUri; 157 } 158 159 165 public String getMappingForResourceType(String resourceType) { 166 167 String [] resourceTypeParams = (String [])getResourceTypes().get(resourceType); 168 if (resourceTypeParams == null) { 169 return null; 170 } else { 171 return resourceTypeParams[1]; 172 } 173 } 174 175 181 public float getRankingForResourceType(String resourceType) { 182 183 String [] resourceTypeParams = (String [])getResourceTypes().get(resourceType); 184 if (resourceTypeParams == null) { 185 return -1.0f; 186 } else { 187 return Float.parseFloat(resourceTypeParams[0]); 188 } 189 } 190 191 203 public Map getResourceTypes() { 204 205 return m_resTypes; 206 } 207 208 213 public List getUserAgentsRegEx() { 214 215 return m_userAgentsRegEx; 216 } 217 218 223 public String getWidgetEditor() { 224 225 return m_widgetEditor; 226 } 227 228 233 public boolean isValidConfiguration() { 234 235 return m_validConfiguration; 236 } 237 238 243 public boolean isWidgetEditor() { 244 245 return CmsStringUtil.isNotEmpty(m_widgetEditor); 246 } 247 248 254 public boolean matchesBrowser(String currentBrowser) { 255 256 if (currentBrowser == null) { 257 return false; 258 } 259 for (int i = 0; i < getBrowserPattern().size(); i++) { 260 boolean matches = ((Pattern )getBrowserPattern().get(i)).matcher(currentBrowser.trim()).matches(); 261 if (matches) { 262 if (LOG.isDebugEnabled()) { 263 LOG.debug(Messages.get().getBundle().key(Messages.LOG_BROWSER_MATCHES_CONFIG_1, currentBrowser)); 264 } 265 return true; 266 } 267 } 268 return false; 269 } 270 271 277 public boolean matchesResourceType(String resourceType) { 278 279 return m_resTypes.containsKey(resourceType); 280 } 281 282 288 private void initialize(Document document, String editorUri) { 289 290 Element rootElement = document.getRootElement(); 292 293 setEditorLabel(rootElement.elementText(N_LABEL)); 295 296 String widgetClass = rootElement.elementText(N_WIDGETEDITOR); 298 if (CmsStringUtil.isNotEmpty(widgetClass)) { 299 setWidgetEditor(widgetClass); 300 } 301 302 setEditorUri(editorUri); 304 305 Iterator i = rootElement.element(N_RESOURCETYPES).elementIterator(N_TYPE); 307 Map resTypes = new HashMap (); 308 while (i.hasNext()) { 309 Element currentType = (Element)i.next(); 310 float ranking; 311 String name = currentType.elementText(N_NAME); 312 if (CmsStringUtil.isEmpty(name)) { 313 logConfigurationError(Messages.get().getBundle().key(Messages.ERR_INVALID_RESTYPE_NAME_0), null); 314 continue; 315 } 316 try { 317 ranking = Float.parseFloat(currentType.elementText(N_RANKING)); 318 } catch (Throwable t) { 319 logConfigurationError(Messages.get().getBundle().key(Messages.ERR_INVALID_RESTYPE_RANKING_1, name), t); 320 continue; 321 } 322 String mapTo = currentType.elementText(N_MAPTO); 323 if (CmsStringUtil.isEmpty(mapTo)) { 324 mapTo = null; 325 } 326 resTypes.put(name, new String [] {"" + ranking, mapTo}); 327 } 328 i = rootElement.element(N_RESOURCETYPES).elementIterator(N_CLASS); 330 while (i.hasNext()) { 331 Element currentClass = (Element)i.next(); 332 String name = currentClass.elementText(N_NAME); 333 List assignedTypes = new ArrayList (); 334 try { 335 I_CmsEditorTypeMatcher matcher = (I_CmsEditorTypeMatcher)Class.forName(name).newInstance(); 337 assignedTypes = matcher.getAdditionalResourceTypes(); 338 } catch (Throwable t) { 339 logConfigurationError(Messages.get().getBundle().key(Messages.ERR_INVALID_RESTYPE_CLASS_1, name), t); 340 continue; 341 } 342 float ranking; 343 try { 344 ranking = Float.parseFloat(currentClass.elementText(N_RANKING)); 345 } catch (Throwable t) { 346 logConfigurationError(Messages.get().getBundle().key(Messages.ERR_INVALID_RESTYPE_RANKING_1, name), t); 347 continue; 348 } 349 String mapTo = currentClass.elementText(N_MAPTO); 350 if ("".equals(mapTo)) { 351 mapTo = null; 352 } 353 Iterator j = assignedTypes.iterator(); 355 while (j.hasNext()) { 356 String typeName = (String )j.next(); 357 resTypes.put(typeName, new String [] {"" + ranking, mapTo}); 358 } 359 } 360 361 setResourceTypes(resTypes); 362 363 i = document.getRootElement().element(N_USERAGENTS).elementIterator(N_AGENT); 365 List pattern = new ArrayList (); 366 List userAgents = new ArrayList (); 367 while (i.hasNext()) { 368 Element currentAgent = (Element)i.next(); 369 String agentName = currentAgent.getText(); 370 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(agentName)) { 371 userAgents.add(agentName); 372 try { 373 pattern.add(Pattern.compile(agentName)); 374 } catch (PatternSyntaxException e) { 375 logConfigurationError( 376 Messages.get().getBundle().key(Messages.ERR_COMPILE_EDITOR_REGEX_1, agentName), 377 e); 378 } 379 } else { 380 logConfigurationError(Messages.get().getBundle().key(Messages.ERR_INVALID_USERAGENT_DEF_0), null); 381 } 382 } 383 setBrowserPattern(pattern); 384 setUserAgentsRegEx(userAgents); 385 } 386 387 393 private void logConfigurationError(String message, Throwable t) { 394 395 setValidConfiguration(false); 396 if (LOG.isErrorEnabled()) { 397 if (t == null) { 398 LOG.error(Messages.get().getBundle().key(Messages.LOG_EDITOR_CONFIG_ERROR_1, message)); 399 } else { 400 LOG.error(Messages.get().getBundle().key(Messages.LOG_EDITOR_CONFIG_ERROR_1, message), t); 401 } 402 } 403 } 404 405 410 private void setBrowserPattern(List pattern) { 411 412 if (pattern == null || pattern.size() == 0) { 413 setValidConfiguration(false); 414 LOG.error(Messages.get().getBundle().key(Messages.LOG_EDITOR_CONFIG_NO_PATTERN_0)); 415 } 416 m_browserPattern = pattern; 417 } 418 419 424 private void setEditorLabel(String label) { 425 426 if (CmsStringUtil.isEmptyOrWhitespaceOnly(label)) { 427 setValidConfiguration(false); 428 LOG.error(Messages.get().getBundle().key(Messages.LOG_EDITOR_CONFIG_NO_LABEL_0)); 429 } 430 m_editorLabel = label; 431 } 432 433 437 private void setEditorUri(String uri) { 438 439 if (CmsStringUtil.isEmptyOrWhitespaceOnly(uri)) { 440 setValidConfiguration(false); 441 LOG.error(Messages.get().getBundle().key(Messages.LOG_EDITOR_CONFIG_NO_URI_0)); 442 } 443 m_editorUri = uri; 444 } 445 446 451 private void setResourceTypes(Map types) { 452 453 if (types == null || types.size() == 0) { 454 setValidConfiguration(false); 455 LOG.error(Messages.get().getBundle().key(Messages.LOG_NO_RESOURCE_TYPES_0)); 456 } 457 m_resTypes = types; 458 } 459 460 465 private void setUserAgentsRegEx(List agents) { 466 467 if (agents == null || agents.size() == 0) { 468 setValidConfiguration(false); 469 LOG.error(Messages.get().getBundle().key(Messages.LOG_NO_USER_AGENTS_0)); 470 } 471 m_userAgentsRegEx = agents; 472 } 473 474 479 private void setValidConfiguration(boolean isValid) { 480 481 m_validConfiguration = isValid; 482 } 483 484 489 private void setWidgetEditor(String widgetEditor) { 490 491 m_widgetEditor = widgetEditor; 492 } 493 494 } 495 | Popular Tags |