/**
  * Processes the Dojo configuration from the supplied {@link Element}
  *
  * @param configElement WebFrameworkConfigElement
  * @param elem Element
  */
 public static void processDojoConfiguration(
     WebFrameworkConfigElement configElement, Element elem) {
   Element dojoConfig = elem.element(DOJO_CONFIG);
   if (dojoConfig != null) {
     String dojoEnabled = dojoConfig.elementTextTrim(DOJO_ENABLED);
     if (dojoEnabled != null) {
       configElement.dojoEnabled = Boolean.valueOf(dojoEnabled);
     }
     String loaderTraceEnabled = dojoConfig.elementTextTrim(DOJO_LOADER_TRACE_ENABLED);
     if (loaderTraceEnabled != null) {
       configElement.dojoLoaderTraceEnabled = Boolean.valueOf(loaderTraceEnabled);
     }
     String bootstrapFile = dojoConfig.elementTextTrim(DOJO_BOOTSTRAP_FILE);
     if (bootstrapFile != null) {
       configElement.dojoBootstrapFile = bootstrapFile;
     }
     String pageWidget = dojoConfig.elementTextTrim(DOJO_PAGE_WIDGETS);
     if (pageWidget != null) {
       configElement.dojoPageWidget = pageWidget;
     }
     String baseUrl = dojoConfig.elementTextTrim(DOJO_BASE_URL);
     if (baseUrl != null) {
       configElement.dojoBaseUrl = baseUrl;
     }
     String messagesObject = dojoConfig.elementTextTrim(DOJO_MESSAGES_OBJECT);
     if (messagesObject != null) {
       configElement.dojoMessagesObject = messagesObject;
     }
     String messagesDefaultScope = dojoConfig.elementTextTrim(DOJO_MESSAGES_DEFAULT_SCOPE);
     if (messagesDefaultScope != null) {
       configElement.dojoMessagesDefaultScope = messagesDefaultScope;
     }
     String defaultLessConfig = dojoConfig.elementTextTrim(DOJO_DEFAULT_LESS_CONFIG);
     if (defaultLessConfig != null) {
       configElement.dojoDefaultLessConfig = defaultLessConfig;
     }
     String aikauVersion = dojoConfig.elementTextTrim(AIKAU_VERSION);
     if (aikauVersion != null) {
       configElement.aikauVersion = aikauVersion;
     }
     Element packages = dojoConfig.element(DOJO_PACKAGES);
     if (packages != null) {
       @SuppressWarnings("unchecked")
       List<Element> packageList = packages.elements(DOJO_PACKAGE);
       if (packageList != null) {
         for (Element packageEntry : packageList) {
           String name = packageEntry.attributeValue(DOJO_PACKAGE_NAME);
           String location = packageEntry.attributeValue(DOJO_PACKAGE_LOCATION);
           if (name != null && location != null) {
             configElement.dojoPackages.put(name, location);
             String main = packageEntry.attributeValue(DOJO_PACKAGE_MAIN);
             if (main != null) {
               configElement.dojoPackagesMain.put(name, main);
             }
           }
         }
       }
     }
   }
 }
  @SuppressWarnings("unchecked")
  protected static WebFrameworkConfigElement newInstance(Element elem) {
    WebFrameworkConfigElement configElement = new WebFrameworkConfigElement();

    // formats
    List<Element> formats = elem.elements("format");
    for (Element el : formats) {
      FormatDescriptor descriptor = new FormatDescriptor(el);
      configElement.formats.put(descriptor.getId(), descriptor);
    }

    // error handlers
    List<Element> errorHandlers = elem.elements("error-handler");
    for (Element el : errorHandlers) {
      ErrorHandlerDescriptor descriptor = new ErrorHandlerDescriptor(el);
      configElement.errorHandlers.put(descriptor.getId(), descriptor);
    }

    // system pages
    List<Element> systemPages = elem.elements("system-page");
    for (Element el : systemPages) {
      SystemPageDescriptor descriptor = new SystemPageDescriptor(el);
      configElement.systemPages.put(descriptor.getId(), descriptor);
    }

    // tag libraries
    List<Element> tagLibraries = elem.elements("tag-library");
    for (Element el : tagLibraries) {
      TagLibraryDescriptor descriptor = new TagLibraryDescriptor(el);
      configElement.tagLibraries.put(descriptor.getId(), descriptor);
    }

    // defaults
    Element defaults = elem.element("defaults");
    if (defaults != null) {
      /** SERVICES * */
      String _requestContextFactoryId = defaults.elementTextTrim("request-context-factory");
      if (_requestContextFactoryId != null) {
        configElement.defaultRequestContextFactoryId = _requestContextFactoryId;
      }
      String _linkBuilderFactoryId = defaults.elementTextTrim("link-builder-factory");
      if (_linkBuilderFactoryId != null) {
        configElement.defaultLinkBuilderFactoryId = _linkBuilderFactoryId;
      }
      String _userFactoryId = defaults.elementTextTrim("user-factory");
      if (_userFactoryId != null) {
        configElement.defaultUserFactoryId = _userFactoryId;
      }

      /** SETTINGS * */
      String _format = defaults.elementTextTrim("format");
      if (_format != null) {
        configElement.defaultFormatId = _format;
      }
      String _regionChrome = defaults.elementTextTrim("region-chrome");
      if (_regionChrome != null) {
        configElement.defaultRegionChrome = _regionChrome;
      }
      String _componentChrome = defaults.elementTextTrim("component-chrome");
      if (_componentChrome != null) {
        configElement.defaultComponentChrome = _componentChrome;
      }

      String _subComponentChrome = defaults.elementTextTrim("sub-component-chrome");
      if (_subComponentChrome != null) {
        configElement.defaultSubComponentChrome = _subComponentChrome;
      }

      String _surfBug = defaults.elementTextTrim("surfbug");
      if (_surfBug != null) {
        configElement.surfBug = _surfBug;
      }

      String _loginCookiesEnabled = defaults.elementTextTrim("login-cookies-enabled");
      if (_loginCookiesEnabled != null) {
        configElement.loginCookiesEnabled = Boolean.valueOf(_loginCookiesEnabled);
      }

      String _theme = defaults.elementTextTrim("theme");
      if (_theme != null && _theme.length() != 0) {
        configElement.defaultTheme = _theme;
      }
      List<Element> pageTypes = defaults.elements("page-type");
      for (Element pageType : pageTypes) {
        String pageTypeId = pageType.elementTextTrim("id");
        String pageTypeInstanceId = pageType.elementTextTrim("page-instance-id");
        configElement.pageTypes.put(pageTypeId, pageTypeInstanceId);
      }
      String _siteConfiguration = defaults.elementTextTrim("site-configuration");
      if (_siteConfiguration != null) {
        configElement.defaultSiteConfiguration = _siteConfiguration;
      }

      /** DEFAULT PERSISTER SETTING * */
      String _defaultPersisterId = defaults.elementText("persister");
      if (_defaultPersisterId != null) {
        configElement.defaultPersisterId = _defaultPersisterId;
      }
    }

    //////////////////////////////////////////////////////
    // Debug Timer
    //////////////////////////////////////////////////////

    Element debugElement = elem.element("debug");
    if (debugElement != null) {
      String _isTimerEnabled = debugElement.elementTextTrim("timer");
      if (_isTimerEnabled != null) {
        configElement.isTimerEnabled = Boolean.parseBoolean(_isTimerEnabled);
      }
    }

    //////////////////////////////////////////////////////
    // Type Specific Things
    //////////////////////////////////////////////////////

    List<Element> objectTypes = elem.elements("object-type");
    for (Element el : objectTypes) {
      TypeDescriptor descriptor = new TypeDescriptor(el);
      if (descriptor.useDefaultPerister() && configElement.getDefaultPersisterId() != null) {
        descriptor.setPersisterId(configElement.getDefaultPersisterId());
      }

      configElement.types.put(descriptor.getId(), descriptor);
    }

    //////////////////////////////////////////////////////
    // Resource Loaders
    //////////////////////////////////////////////////////

    List<Element> loaders = elem.elements("resource-loader");
    for (Element el : loaders) {
      ResourceLoaderDescriptor descriptor = new ResourceLoaderDescriptor(el);
      configElement.resourceLoaders.put(descriptor.getId(), descriptor);
    }

    //////////////////////////////////////////////////////
    // Resource Resolvers
    //////////////////////////////////////////////////////

    List<Element> resolvers = elem.elements("resource-resolver");
    for (Element el : resolvers) {
      ResourceResolverDescriptor descriptor = new ResourceResolverDescriptor(el);
      configElement.resourceResolvers.put(descriptor.getId(), descriptor);
    }

    //////////////////////////////////////////////////////
    // Runtime Configuration
    //////////////////////////////////////////////////////
    List<Element> runtimeConfigElements = elem.elements("runtime-config");
    for (Element el : runtimeConfigElements) {
      RuntimeConfigDescriptor descriptor = new RuntimeConfigDescriptor(el);
      configElement.runtimeConfigs.put(descriptor.getId(), descriptor);
    }

    //////////////////////////////////////////////////////
    // Autowire Configuration
    //////////////////////////////////////////////////////
    Element autowireConfigElement = elem.element("autowire");
    if (autowireConfigElement != null) {
      String _autowireModeId = autowireConfigElement.elementTextTrim("mode");
      if (_autowireModeId != null) {
        configElement.autowireModeId = _autowireModeId;
      }
      String _autowireRuntimeId = autowireConfigElement.elementTextTrim("runtime");
      if (_autowireRuntimeId != null) {
        configElement.autowireRuntimeId = _autowireRuntimeId;
      }
    }

    //////////////////////////////////////////////////////
    // Persister Config Descriptor
    //////////////////////////////////////////////////////
    Element persisterConfigElement = elem.element("persisters");
    if (persisterConfigElement != null) {
      configElement.persisterConfigDescriptor =
          new PersisterConfigDescriptor(persisterConfigElement);
    }

    // Module Deployment mode...
    Element moduleDeploymentElement = elem.element("module-deployment");
    if (moduleDeploymentElement != null) {
      String _moduleDeploymentMode = moduleDeploymentElement.elementTextTrim("mode");
      if (_moduleDeploymentMode != null) {
        configElement.moduleDeploymentMode = _moduleDeploymentMode;
      }

      String _enableAutoDeployModules =
          moduleDeploymentElement.elementTextTrim("enable-auto-deploy-modules");
      if (_enableAutoDeployModules != null) {
        configElement.enableAutoDeployModules = Boolean.valueOf(_enableAutoDeployModules);
      }
    }

    // MNT-12724 case, externally specify paths that should be denied by ResourceController
    Element denyAccessPathsElement = elem.element("deny-access-resource-paths");
    if (denyAccessPathsElement != null) {
      List<Element> paths = denyAccessPathsElement.elements("resource-path-pattern");

      for (Element path : paths) {
        configElement.resourcesDeniedPaths.add(Pattern.compile(path.getTextTrim()));
      }
    }

    // When "use-checksum-dependencies" is set to true the JavaScriptDependencyDirective and
    // CssDependencyDirectives will be made available to FreeMarker templates...
    String useChecksumDependencies = elem.elementTextTrim("use-checksum-dependencies");
    if (useChecksumDependencies != null) {
      configElement.useChecksumDependencies = Boolean.valueOf(useChecksumDependencies);
    }

    String generateCssDataImages = elem.elementTextTrim("generate-css-data-images");
    if (generateCssDataImages != null) {
      configElement.generateCssDataImages = Boolean.valueOf(generateCssDataImages);
    }

    String aggregateDependencies = elem.elementTextTrim("aggregate-dependencies");
    if (aggregateDependencies != null) {
      configElement.aggregateDependencies = Boolean.valueOf(aggregateDependencies);
    }

    String calculateWebScriptDependencies =
        elem.elementTextTrim("calculate-webscript-dependencies");
    if (calculateWebScriptDependencies != null) {
      configElement.calculateWebScriptDependencies =
          Boolean.valueOf(calculateWebScriptDependencies);
    }

    String enableRemoteResources = elem.elementTextTrim("enable-remote-resource-resolving");
    if (enableRemoteResources != null) {
      configElement.enableRemoteResourceHandling = Boolean.valueOf(enableRemoteResources);
    }

    String enableGuestPageExtensionModules =
        elem.elementTextTrim("enable-guest-page-extension-modules");
    if (enableGuestPageExtensionModules != null) {
      configElement.enableExtensionModulesOnGuestPages =
          Boolean.valueOf(enableGuestPageExtensionModules);
    }

    String enableDynamicExtensionModules = elem.elementTextTrim("enable-dynamic-extension-modules");
    if (enableDynamicExtensionModules != null) {
      configElement.enableDynamicExtensions = Boolean.valueOf(enableDynamicExtensionModules);
    }

    String disableResourceCaching = elem.elementTextTrim("disable-resource-caching");
    if (disableResourceCaching != null) {
      configElement.disableResourceCaching = Boolean.valueOf(disableResourceCaching);
    }

    // Process any Dojo configuration...
    processDojoConfiguration(configElement, elem);

    return configElement;
  }
  /* (non-Javadoc)
   * @see org.alfresco.config.element.GenericConfigElement#combine(org.alfresco.config.ConfigElement)
   */
  public ConfigElement combine(ConfigElement element) {
    WebFrameworkConfigElement configElement = (WebFrameworkConfigElement) element;

    // new combined element
    WebFrameworkConfigElement combinedElement = new WebFrameworkConfigElement();

    // copy in our things
    combinedElement.formats.putAll(this.formats);
    combinedElement.errorHandlers.putAll(this.errorHandlers);
    combinedElement.systemPages.putAll(this.systemPages);
    combinedElement.tagLibraries.putAll(this.tagLibraries);
    combinedElement.types.putAll(this.types);
    combinedElement.pageTypes.putAll(this.pageTypes);
    combinedElement.resourceLoaders.putAll(this.resourceLoaders);
    combinedElement.resourceResolvers.putAll(this.resourceResolvers);
    combinedElement.resourcesDeniedPaths.addAll(this.resourcesDeniedPaths);
    combinedElement.runtimeConfigs.putAll(this.runtimeConfigs);

    // override with things from the merging object
    combinedElement.formats.putAll(configElement.formats);
    combinedElement.errorHandlers.putAll(configElement.errorHandlers);
    combinedElement.systemPages.putAll(configElement.systemPages);
    combinedElement.tagLibraries.putAll(configElement.tagLibraries);
    combinedElement.types.putAll(configElement.types);
    combinedElement.pageTypes.putAll(configElement.pageTypes);
    combinedElement.resourceLoaders.putAll(configElement.resourceLoaders);
    combinedElement.resourceResolvers.putAll(configElement.resourceResolvers);
    combinedElement.resourcesDeniedPaths.addAll(configElement.resourcesDeniedPaths);
    combinedElement.runtimeConfigs.putAll(configElement.runtimeConfigs);

    // other properties
    combinedElement.isTimerEnabled = this.isTimerEnabled;
    if (configElement.isTimerEnabled) {
      combinedElement.isTimerEnabled = configElement.isTimerEnabled;
    }

    // default services
    combinedElement.defaultLinkBuilderFactoryId = this.defaultLinkBuilderFactoryId;
    if (configElement.defaultLinkBuilderFactoryId != null) {
      combinedElement.defaultLinkBuilderFactoryId = configElement.defaultLinkBuilderFactoryId;
    }
    combinedElement.defaultRequestContextFactoryId = this.defaultRequestContextFactoryId;
    if (configElement.defaultRequestContextFactoryId != null) {
      combinedElement.defaultRequestContextFactoryId = configElement.defaultRequestContextFactoryId;
    }
    combinedElement.defaultUserFactoryId = this.defaultUserFactoryId;
    if (configElement.defaultUserFactoryId != null) {
      combinedElement.defaultUserFactoryId = configElement.defaultUserFactoryId;
    }

    // default application settings
    combinedElement.defaultFormatId = this.defaultFormatId;
    if (configElement.defaultFormatId != null) {
      combinedElement.defaultFormatId = configElement.defaultFormatId;
    }
    combinedElement.defaultRegionChrome = this.defaultRegionChrome;
    if (configElement.defaultRegionChrome != null) {
      combinedElement.defaultRegionChrome = configElement.defaultRegionChrome;
    }
    combinedElement.defaultComponentChrome = this.defaultComponentChrome;
    if (configElement.defaultComponentChrome != null) {
      combinedElement.defaultComponentChrome = configElement.defaultComponentChrome;
    }
    combinedElement.defaultSubComponentChrome = this.defaultSubComponentChrome;
    if (configElement.defaultSubComponentChrome != null) {
      combinedElement.defaultSubComponentChrome = configElement.defaultSubComponentChrome;
    }
    combinedElement.defaultTheme = this.defaultTheme;
    if (configElement.defaultTheme != null) {
      combinedElement.defaultTheme = configElement.defaultTheme;
    }

    combinedElement.surfBug = this.surfBug;
    if (configElement.surfBug != null) {
      combinedElement.surfBug = configElement.surfBug;
    }

    combinedElement.loginCookiesEnabled = this.loginCookiesEnabled;
    if (configElement.loginCookiesEnabled != null) {
      combinedElement.loginCookiesEnabled = configElement.loginCookiesEnabled;
    }

    combinedElement.defaultSiteConfiguration = this.defaultSiteConfiguration;
    if (configElement.defaultSiteConfiguration != null) {
      combinedElement.defaultSiteConfiguration = configElement.defaultSiteConfiguration;
    }

    // default persister setting
    combinedElement.defaultPersisterId = this.defaultPersisterId;
    if (configElement.defaultPersisterId != null) {
      combinedElement.defaultPersisterId = configElement.defaultPersisterId;
    }

    // Whenever two config elements are merged it is important to ensure that the
    // currently configured types are updated with any overridden default persisters.
    // A type is marked as using the default persister if a persister id is not
    // explicitly set. It will be set with the default persister configured in the
    // same file as the type is declared, but the "useDefaultPersister" flag will
    // be set so that it can be overridden if the default persister is updated.
    for (TypeDescriptor type : combinedElement.getTypes()) {
      if (type.useDefaultPerister()) {
        type.setPersisterId(combinedElement.defaultPersisterId);
      }
    }

    // AutoWire Settings
    combinedElement.autowireModeId = this.autowireModeId;
    if (configElement.autowireModeId != null) {
      combinedElement.autowireModeId = configElement.autowireModeId;
    }
    combinedElement.autowireRuntimeId = this.autowireRuntimeId;
    if (configElement.autowireRuntimeId != null) {
      combinedElement.autowireRuntimeId = configElement.autowireRuntimeId;
    }

    // persister config
    combinedElement.persisterConfigDescriptor = this.persisterConfigDescriptor;
    if (configElement.persisterConfigDescriptor != null) {
      combinedElement.persisterConfigDescriptor = configElement.persisterConfigDescriptor;
    }

    // Module deployment settings...
    combinedElement.moduleDeploymentMode = this.moduleDeploymentMode;
    if (configElement.moduleDeploymentMode != null) {
      combinedElement.moduleDeploymentMode = configElement.moduleDeploymentMode;
    }

    combinedElement.enableAutoDeployModules = this.enableAutoDeployModules;
    if (configElement.enableAutoDeployModules != null) {
      combinedElement.enableAutoDeployModules = configElement.enableAutoDeployModules;
    }

    combinedElement.useChecksumDependencies = this.useChecksumDependencies;
    if (configElement.useChecksumDependencies != null) {
      combinedElement.useChecksumDependencies = configElement.useChecksumDependencies;
    }

    combinedElement.generateCssDataImages = this.generateCssDataImages;
    if (configElement.generateCssDataImages != null) {
      combinedElement.generateCssDataImages = configElement.generateCssDataImages;
    }

    combinedElement.aggregateDependencies = this.aggregateDependencies;
    if (configElement.aggregateDependencies != null) {
      combinedElement.aggregateDependencies = configElement.aggregateDependencies;
    }

    combinedElement.calculateWebScriptDependencies = this.calculateWebScriptDependencies;
    if (configElement.calculateWebScriptDependencies != null) {
      combinedElement.calculateWebScriptDependencies = configElement.calculateWebScriptDependencies;
    }

    combinedElement.enableRemoteResourceHandling = this.enableRemoteResourceHandling;
    if (configElement.enableRemoteResourceHandling != null) {
      combinedElement.enableRemoteResourceHandling = configElement.enableRemoteResourceHandling;
    }

    combinedElement.enableExtensionModulesOnGuestPages = this.enableExtensionModulesOnGuestPages;
    if (configElement.enableExtensionModulesOnGuestPages != null) {
      combinedElement.enableExtensionModulesOnGuestPages =
          configElement.enableExtensionModulesOnGuestPages;
    }

    combinedElement.enableDynamicExtensions = this.enableDynamicExtensions;
    if (configElement.enableDynamicExtensions != null) {
      combinedElement.enableDynamicExtensions = configElement.enableDynamicExtensions;
    }

    combinedElement.disableResourceCaching = this.disableResourceCaching;
    if (configElement.disableResourceCaching != null) {
      combinedElement.disableResourceCaching = configElement.disableResourceCaching;
    }

    // Combine any Dojo configurations...
    combineDojoConfiguration(configElement, combinedElement);

    return combinedElement;
  }
  /**
   * Combines Dojo configuration from the configElement into the combinedElement.
   *
   * @param configElement WebFrameworkConfigElement
   * @param combinedElement WebFrameworkConfigElement
   */
  public void combineDojoConfiguration(
      WebFrameworkConfigElement configElement, WebFrameworkConfigElement combinedElement) {
    combinedElement.dojoEnabled = this.dojoEnabled;
    if (configElement.dojoEnabled != null) {
      combinedElement.dojoEnabled = configElement.dojoEnabled;
    }
    combinedElement.dojoLoaderTraceEnabled = this.dojoLoaderTraceEnabled;
    if (configElement.dojoLoaderTraceEnabled != null) {
      combinedElement.dojoLoaderTraceEnabled = configElement.dojoLoaderTraceEnabled;
    }
    combinedElement.dojoBootstrapFile = this.dojoBootstrapFile;
    if (configElement.dojoBootstrapFile != null) {
      combinedElement.dojoBootstrapFile = configElement.dojoBootstrapFile;
    }
    combinedElement.dojoPageWidget = this.dojoPageWidget;
    if (configElement.dojoPageWidget != null) {
      combinedElement.dojoPageWidget = configElement.dojoPageWidget;
    }
    combinedElement.dojoBaseUrl = this.dojoBaseUrl;
    if (configElement.dojoBaseUrl != null) {
      combinedElement.dojoBaseUrl = configElement.dojoBaseUrl;
    }
    combinedElement.dojoMessagesObject = this.dojoMessagesObject;
    if (configElement.dojoMessagesObject != null) {
      combinedElement.dojoMessagesObject = configElement.dojoMessagesObject;
    }
    combinedElement.dojoMessagesDefaultScope = this.dojoMessagesDefaultScope;
    if (configElement.dojoMessagesDefaultScope != null) {
      combinedElement.dojoMessagesDefaultScope = configElement.dojoMessagesDefaultScope;
    }
    combinedElement.dojoDefaultLessConfig = this.dojoDefaultLessConfig;
    if (configElement.dojoDefaultLessConfig != null) {
      combinedElement.dojoDefaultLessConfig = configElement.dojoDefaultLessConfig;
    }
    combinedElement.dojoPackages = this.dojoPackages;
    if (configElement.dojoPackages != null) {
      combinedElement.dojoPackages.putAll(configElement.dojoPackages);
    }

    combinedElement.dojoPackagesMain = this.dojoPackagesMain;
    if (configElement.dojoPackagesMain != null) {
      combinedElement.dojoPackagesMain.putAll(configElement.dojoPackagesMain);
    }
    combinedElement.aikauVersion = this.aikauVersion;
    if (configElement.aikauVersion != null) {
      combinedElement.aikauVersion = configElement.aikauVersion;
    }
  }