1 31 32 package org.opencms.module; 33 34 import org.opencms.configuration.CmsVfsConfiguration; 35 import org.opencms.configuration.CmsWorkplaceConfiguration; 36 import org.opencms.configuration.I_CmsConfigurationParameterHandler; 37 import org.opencms.configuration.I_CmsXmlConfiguration; 38 import org.opencms.db.CmsExportPoint; 39 import org.opencms.file.types.I_CmsResourceType; 40 import org.opencms.main.CmsLog; 41 import org.opencms.util.CmsDateUtil; 42 import org.opencms.util.CmsStringUtil; 43 import org.opencms.workplace.CmsWorkplace; 44 import org.opencms.workplace.explorer.CmsExplorerTypeSettings; 45 46 import java.text.ParseException ; 47 import java.util.ArrayList ; 48 import java.util.Collections ; 49 import java.util.HashMap ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 import java.util.Map ; 53 import java.util.SortedMap ; 54 55 import org.apache.commons.digester.Digester; 56 import org.apache.commons.logging.Log; 57 58 import org.dom4j.Document; 59 import org.dom4j.DocumentHelper; 60 import org.dom4j.Element; 61 62 71 public class CmsModuleXmlHandler { 72 73 74 public static final String A_NAME = "name"; 75 76 77 public static final String A_VERSION = "version"; 78 79 80 public static final String N_AUTHOREMAIL = "authoremail"; 81 82 83 public static final String N_AUTHORNAME = "authorname"; 84 85 86 public static final String N_CLASS = "class"; 87 88 89 public static final String N_DATECREATED = "datecreated"; 90 91 92 public static final String N_DATEINSTALLED = "dateinstalled"; 93 94 95 public static final String N_DEPENDENCIES = "dependencies"; 96 97 98 public static final String N_DEPENDENCY = "dependency"; 99 100 101 public static final String N_DESCRIPTION = "description"; 102 103 104 public static final String N_GROUP = "group"; 105 106 107 public static final String N_MODULE = "module"; 108 109 110 public static final String N_NAME = "name"; 111 112 113 public static final String N_NICENAME = "nicename"; 114 115 116 public static final String N_PARAM = "param"; 117 118 119 public static final String N_PARAMETERS = "parameters"; 120 121 122 public static final String N_RESOURCES = "resources"; 123 124 125 public static final String N_USERINSTALLED = "userinstalled"; 126 127 128 public static final String N_VERSION = "version"; 129 130 131 private static final Log LOG = CmsLog.getLog(CmsModuleXmlHandler.class); 132 133 134 private List m_dependencies; 135 136 137 private List m_explorerTypeSettings; 138 139 140 private List m_exportPoints; 141 142 143 private CmsModule m_module; 144 145 146 private boolean m_oldModule; 147 148 149 private Map m_parameters; 150 151 152 private List m_resources; 153 154 155 private List m_resourceTypes; 156 157 160 public CmsModuleXmlHandler() { 161 162 m_exportPoints = new ArrayList (); 163 m_dependencies = new ArrayList (); 164 m_resources = new ArrayList (); 165 m_parameters = new HashMap (); 166 m_resourceTypes = new ArrayList (); 167 m_explorerTypeSettings = new ArrayList (); 168 } 169 170 175 public static void addXmlDigesterRules(Digester digester) { 176 177 digester.addObjectCreate("*/" + N_MODULE, CmsModuleXmlHandler.class); 179 digester.addSetNext("*/" + N_MODULE, "setModule"); 180 181 digester.addCallMethod("*/" + N_MODULE, "createdModule", 11); 183 digester.addCallParam("*/" + N_MODULE + "/" + N_NAME, 0); 184 digester.addCallParam("*/" + N_MODULE + "/" + N_NICENAME, 1); 185 digester.addCallParam("*/" + N_MODULE + "/" + N_GROUP, 2); 186 digester.addCallParam("*/" + N_MODULE + "/" + N_CLASS, 3); 187 digester.addCallParam("*/" + N_MODULE + "/" + N_DESCRIPTION, 4); 188 digester.addCallParam("*/" + N_MODULE + "/" + N_VERSION, 5); 189 digester.addCallParam("*/" + N_MODULE + "/" + N_AUTHORNAME, 6); 190 digester.addCallParam("*/" + N_MODULE + "/" + N_AUTHOREMAIL, 7); 191 digester.addCallParam("*/" + N_MODULE + "/" + N_DATECREATED, 8); 192 digester.addCallParam("*/" + N_MODULE + "/" + N_USERINSTALLED, 9); 193 digester.addCallParam("*/" + N_MODULE + "/" + N_DATEINSTALLED, 10); 194 195 digester.addCallMethod("*/" + N_MODULE + "/" + N_DEPENDENCIES + "/" + N_DEPENDENCY, "addDependency", 2); 197 digester.addCallParam( 198 "*/" + N_MODULE + "/" + N_DEPENDENCIES + "/" + N_DEPENDENCY, 199 0, 200 I_CmsXmlConfiguration.A_NAME); 201 digester.addCallParam("*/" + N_MODULE + "/" + N_DEPENDENCIES + "/" + N_DEPENDENCY, 1, A_VERSION); 202 203 digester.addCallMethod("*/" 205 + N_MODULE 206 + "/" 207 + I_CmsXmlConfiguration.N_EXPORTPOINTS 208 + "/" 209 + I_CmsXmlConfiguration.N_EXPORTPOINT, "addExportPoint", 2); 210 digester.addCallParam("*/" 211 + N_MODULE 212 + "/" 213 + I_CmsXmlConfiguration.N_EXPORTPOINTS 214 + "/" 215 + I_CmsXmlConfiguration.N_EXPORTPOINT, 0, I_CmsXmlConfiguration.A_URI); 216 digester.addCallParam("*/" 217 + N_MODULE 218 + "/" 219 + I_CmsXmlConfiguration.N_EXPORTPOINTS 220 + "/" 221 + I_CmsXmlConfiguration.N_EXPORTPOINT, 1, I_CmsXmlConfiguration.A_DESTINATION); 222 223 digester.addCallMethod( 225 "*/" + N_MODULE + "/" + N_RESOURCES + "/" + I_CmsXmlConfiguration.N_RESOURCE, 226 "addResource", 227 1); 228 digester.addCallParam( 229 "*/" + N_MODULE + "/" + N_RESOURCES + "/" + I_CmsXmlConfiguration.N_RESOURCE, 230 0, 231 I_CmsXmlConfiguration.A_URI); 232 233 digester.addCallMethod( 235 "*/" + N_MODULE + "/" + N_PARAMETERS + "/" + I_CmsXmlConfiguration.N_PARAM, 236 "addParameter", 237 2); 238 digester.addCallParam( 239 "*/" + N_MODULE + "/" + N_PARAMETERS + "/" + I_CmsXmlConfiguration.N_PARAM, 240 0, 241 I_CmsXmlConfiguration.A_NAME); 242 digester.addCallParam("*/" + N_MODULE + "/" + N_PARAMETERS + "/" + I_CmsXmlConfiguration.N_PARAM, 1); 243 244 digester.addCallMethod( 246 "*/" + I_CmsXmlConfiguration.N_PARAM, 247 I_CmsConfigurationParameterHandler.ADD_PARAMETER_METHOD, 248 2); 249 digester.addCallParam("*/" + I_CmsXmlConfiguration.N_PARAM, 0, I_CmsXmlConfiguration.A_NAME); 250 digester.addCallParam("*/" + I_CmsXmlConfiguration.N_PARAM, 1); 251 252 CmsVfsConfiguration.addResourceTypeXmlRules(digester); 254 255 CmsWorkplaceConfiguration.addExplorerTypeXmlRules(digester); 257 258 addXmlDigesterRulesForVersion5Modules(digester); 260 } 261 262 268 public static Element generateXml(CmsModule module) { 269 270 Document doc = DocumentHelper.createDocument(); 271 272 Element moduleElement = doc.addElement(N_MODULE); 273 274 moduleElement.addElement(N_NAME).setText(module.getName()); 275 if (!module.getName().equals(module.getNiceName())) { 276 moduleElement.addElement(N_NICENAME).addCDATA(module.getNiceName()); 277 } else { 278 moduleElement.addElement(N_NICENAME); 279 } 280 if (CmsStringUtil.isNotEmpty(module.getGroup())) { 281 moduleElement.addElement(N_GROUP).setText(module.getGroup()); 282 } 283 if (CmsStringUtil.isNotEmpty(module.getActionClass())) { 284 moduleElement.addElement(N_CLASS).setText(module.getActionClass()); 285 } else { 286 moduleElement.addElement(N_CLASS); 287 } 288 if (CmsStringUtil.isNotEmpty(module.getDescription())) { 289 moduleElement.addElement(N_DESCRIPTION).addCDATA(module.getDescription()); 290 } else { 291 moduleElement.addElement(N_DESCRIPTION); 292 } 293 moduleElement.addElement(N_VERSION).setText(module.getVersion().toString()); 294 if (CmsStringUtil.isNotEmpty(module.getAuthorName())) { 295 moduleElement.addElement(N_AUTHORNAME).addCDATA(module.getAuthorName()); 296 } else { 297 moduleElement.addElement(N_AUTHORNAME); 298 } 299 if (CmsStringUtil.isNotEmpty(module.getAuthorEmail())) { 300 moduleElement.addElement(N_AUTHOREMAIL).addCDATA(module.getAuthorEmail()); 301 } else { 302 moduleElement.addElement(N_AUTHOREMAIL); 303 } 304 if (module.getDateCreated() != CmsModule.DEFAULT_DATE) { 305 moduleElement.addElement(N_DATECREATED).setText(CmsDateUtil.getHeaderDate(module.getDateCreated())); 306 } else { 307 moduleElement.addElement(N_DATECREATED); 308 } 309 310 if (CmsStringUtil.isNotEmpty(module.getUserInstalled())) { 311 moduleElement.addElement(N_USERINSTALLED).setText(module.getUserInstalled()); 312 } else { 313 moduleElement.addElement(N_USERINSTALLED); 314 } 315 if (module.getDateInstalled() != CmsModule.DEFAULT_DATE) { 316 moduleElement.addElement(N_DATEINSTALLED).setText(CmsDateUtil.getHeaderDate(module.getDateInstalled())); 317 } else { 318 moduleElement.addElement(N_DATEINSTALLED); 319 } 320 Element dependenciesElement = moduleElement.addElement(N_DEPENDENCIES); 321 for (int i = 0; i < module.getDependencies().size(); i++) { 322 CmsModuleDependency dependency = (CmsModuleDependency)module.getDependencies().get(i); 323 dependenciesElement.addElement(N_DEPENDENCY).addAttribute( 324 I_CmsXmlConfiguration.A_NAME, 325 dependency.getName()).addAttribute(A_VERSION, dependency.getVersion().toString()); 326 } 327 Element exportpointsElement = moduleElement.addElement(I_CmsXmlConfiguration.N_EXPORTPOINTS); 328 for (int i = 0; i < module.getExportPoints().size(); i++) { 329 CmsExportPoint point = (CmsExportPoint)module.getExportPoints().get(i); 330 exportpointsElement.addElement(I_CmsXmlConfiguration.N_EXPORTPOINT).addAttribute( 331 I_CmsXmlConfiguration.A_URI, 332 point.getUri()).addAttribute(I_CmsXmlConfiguration.A_DESTINATION, point.getConfiguredDestination()); 333 } 334 Element resourcesElement = moduleElement.addElement(N_RESOURCES); 335 for (int i = 0; i < module.getResources().size(); i++) { 336 String resource = (String )module.getResources().get(i); 337 resourcesElement.addElement(I_CmsXmlConfiguration.N_RESOURCE).addAttribute( 338 I_CmsXmlConfiguration.A_URI, 339 resource); 340 } 341 Element parametersElement = moduleElement.addElement(N_PARAMETERS); 342 SortedMap parameters = module.getParameters(); 343 if (parameters != null) { 344 List parameterList = new ArrayList (parameters.keySet()); 345 Collections.sort(parameterList); 346 Iterator it = parameterList.iterator(); 347 while (it.hasNext()) { 348 String name = (String )it.next(); 349 String value = parameters.get(name).toString(); 350 Element paramNode = parametersElement.addElement(I_CmsXmlConfiguration.N_PARAM); 351 paramNode.addAttribute(I_CmsXmlConfiguration.A_NAME, name); 352 paramNode.addText(value); 353 } 354 } 355 356 List resourceTypes = module.getResourceTypes(); 358 if (resourceTypes.size() > 0) { 359 Element resourcetypesElement = moduleElement.addElement(CmsVfsConfiguration.N_RESOURCETYPES); 360 CmsVfsConfiguration.generateResourceTypeXml(resourcetypesElement, resourceTypes, true); 361 } 362 363 List explorerTypes = module.getExplorerTypes(); 364 if (explorerTypes.size() > 0) { 365 Element explorerTypesElement = moduleElement.addElement(CmsWorkplaceConfiguration.N_EXPLORERTYPES); 366 CmsWorkplaceConfiguration.generateExplorerTypesXml(explorerTypesElement, explorerTypes, true); 367 } 368 369 moduleElement.detach(); 371 return moduleElement; 372 } 373 374 384 public static String makeValidJavaClassName(String className) { 385 386 StringBuffer result = new StringBuffer (className.length()); 387 int length = className.length(); 388 boolean nodot = true; 389 for (int i = 0; i < length; i++) { 390 char ch = className.charAt(i); 391 if (nodot) { 392 if (ch == '.') { 393 } else if (Character.isJavaIdentifierStart(ch)) { 395 nodot = false; 396 result.append(ch); 397 } else { 398 result.append('_'); 399 } 400 } else { 401 if (ch == '.') { 402 nodot = true; 403 result.append(ch); 404 } else if (Character.isJavaIdentifierPart(ch)) { 405 nodot = false; 406 result.append(ch); 407 } else { 408 result.append('_'); 409 } 410 } 411 } 412 return result.toString(); 413 } 414 415 420 private static void addXmlDigesterRulesForVersion5Modules(Digester digester) { 421 422 digester.addCallMethod("*/" + N_MODULE + "/author", "setOldModule"); 424 425 digester.addCallParam("*/" + N_MODULE + "/author", 6); 427 digester.addCallParam("*/" + N_MODULE + "/email", 7); 428 digester.addCallParam("*/" + N_MODULE + "/creationdate", 8); 429 430 digester.addCallParam("*/" + N_MODULE + "/dependencies/dependency/name", 0); 432 digester.addCallParam("*/" + N_MODULE + "/dependencies/dependency/minversion", 1); 433 434 digester.addCallMethod("*/" + N_MODULE + "/exportpoint", "addExportPoint", 2); 436 digester.addCallParam("*/" + N_MODULE + "/exportpoint/source", 0); 437 digester.addCallParam("*/" + N_MODULE + "/exportpoint/destination", 1); 438 439 digester.addCallMethod("*/" + N_MODULE + "/parameters/para", "addParameter", 2); 441 digester.addCallParam("*/" + N_MODULE + "/parameters/para/name", 0); 442 digester.addCallParam("*/" + N_MODULE + "/parameters/para/value", 1); 443 } 444 445 451 public void addDependency(String name, String version) { 452 453 CmsModuleVersion moduleVersion = new CmsModuleVersion(version); 454 455 CmsModuleDependency dependency = new CmsModuleDependency(name, moduleVersion); 456 m_dependencies.add(dependency); 457 458 if (LOG.isDebugEnabled()) { 459 LOG.debug(Messages.get().getBundle().key(Messages.LOG_ADD_MOD_DEPENDENCY_2, name, version)); 460 } 461 } 462 463 470 public void addExplorerTypeSetting(CmsExplorerTypeSettings settings) { 471 472 settings.setAddititionalModuleExplorerType(true); 473 m_explorerTypeSettings.add(settings); 474 } 475 476 482 public void addExportPoint(String uri, String destination) { 483 484 CmsExportPoint point = new CmsExportPoint(uri, destination); 485 m_exportPoints.add(point); 486 if (CmsLog.INIT.isInfoEnabled() && (point.getDestinationPath() != null)) { 487 CmsLog.INIT.info(Messages.get().getBundle().key( 488 Messages.INIT_ADD_EXPORT_POINT_2, 489 point.getUri(), 490 point.getDestinationPath())); 491 } 492 } 493 494 500 public void addParameter(String key, String value) { 501 502 if (CmsStringUtil.isNotEmpty(key)) { 503 key = key.trim(); 504 } 505 if (CmsStringUtil.isNotEmpty(value)) { 506 value = value.trim(); 507 } 508 m_parameters.put(key, value); 509 if (LOG.isDebugEnabled()) { 510 LOG.debug(Messages.get().getBundle().key(Messages.LOG_ADD_MOD_PARAM_KEY_2, key, value)); 511 } 512 } 513 514 519 public void addResource(String resource) { 520 521 if (LOG.isDebugEnabled()) { 522 LOG.debug(Messages.get().getBundle().key(Messages.LOG_ADD_MOD_RESOURCE_1, resource)); 523 } 524 m_resources.add(resource); 525 } 526 527 534 public void addResourceType(I_CmsResourceType resourceType) { 535 536 resourceType.setAdditionalModuleResourceType(true); 537 m_resourceTypes.add(resourceType); 538 } 539 540 555 public void createdModule( 556 String name, 557 String niceName, 558 String group, 559 String actionClass, 560 String description, 561 String version, 562 String authorName, 563 String authorEmail, 564 String dateCreated, 565 String userInstalled, 566 String dateInstalled) { 567 568 String moduleName; 569 570 if (!CmsStringUtil.isValidJavaClassName(name)) { 571 LOG.error(Messages.get().getBundle().key(Messages.LOG_INVALID_MOD_NAME_IMPORTED_1, name)); 573 moduleName = makeValidJavaClassName(name); 574 LOG.error(Messages.get().getBundle().key(Messages.LOG_CORRECTED_MOD_NAME_1, moduleName)); 575 } else { 576 moduleName = name; 577 } 578 579 CmsModuleVersion moduleVersion = new CmsModuleVersion(version); 581 582 long moduleDateCreated = CmsModule.DEFAULT_DATE; 584 if (dateCreated != null) { 585 try { 586 moduleDateCreated = CmsDateUtil.parseHeaderDate(dateCreated); 587 } catch (ParseException e) { 588 } 590 } 591 592 long moduleDateInstalled = CmsModule.DEFAULT_DATE; 594 if (dateInstalled != null) { 595 try { 596 moduleDateInstalled = CmsDateUtil.parseHeaderDate(dateInstalled); 597 } catch (ParseException e1) { 598 } 600 } 601 602 if (m_oldModule) { 603 String modulePath = CmsWorkplace.VFS_PATH_MODULES + name + "/"; 605 m_resources.add(modulePath); 606 } 607 608 m_module = new CmsModule( 610 moduleName, 611 niceName, 612 group, 613 actionClass, 614 description, 615 moduleVersion, 616 authorName, 617 authorEmail, 618 moduleDateCreated, 619 userInstalled, 620 moduleDateInstalled, 621 m_dependencies, 622 m_exportPoints, 623 m_resources, 624 m_parameters); 625 626 m_module.setResourceTypes(m_resourceTypes); 628 629 m_module.setExplorerTypes(m_explorerTypeSettings); 631 } 632 633 638 public CmsModule getModule() { 639 640 return m_module; 641 } 642 643 646 public void setOldModule() { 647 648 m_oldModule = true; 649 if (LOG.isDebugEnabled()) { 650 LOG.debug(Messages.get().getBundle().key(Messages.LOG_OLD_MODULE_IMPORTED_0)); 651 } 652 } 653 } | Popular Tags |