private Set<String> _readThemes(
      String servletContextName,
      ServletContext servletContext,
      String themesPath,
      boolean loadFromServletContext,
      String xml,
      PluginPackage pluginPackage)
      throws Exception {

    Set<String> themeIds = new HashSet<String>();

    if (xml == null) {
      return themeIds;
    }

    Document document = SAXReaderUtil.read(xml, true);

    Element rootElement = document.getRootElement();

    Version portalVersion = _getVersion(ReleaseInfo.getVersion());

    boolean compatible = false;

    Element compatibilityElement = rootElement.element("compatibility");

    if (compatibilityElement != null) {
      List<Element> versionElements = compatibilityElement.elements("version");

      for (Element versionElement : versionElements) {
        Version version = _getVersion(versionElement.getTextTrim());

        if (version.includes(portalVersion)) {
          compatible = true;

          break;
        }
      }
    }

    if (!compatible) {
      _log.error("Themes in this WAR are not compatible with " + ReleaseInfo.getServerInfo());

      return themeIds;
    }

    ThemeCompanyLimit companyLimit = null;

    Element companyLimitElement = rootElement.element("company-limit");

    if (companyLimitElement != null) {
      companyLimit = new ThemeCompanyLimit();

      Element companyIncludesElement = companyLimitElement.element("company-includes");

      if (companyIncludesElement != null) {
        companyLimit.setIncludes(_getCompanyLimitIncludes(companyIncludesElement));
      }

      Element companyExcludesElement = companyLimitElement.element("company-excludes");

      if (companyExcludesElement != null) {
        companyLimit.setExcludes(_getCompanyLimitExcludes(companyExcludesElement));
      }
    }

    ThemeGroupLimit groupLimit = null;

    Element groupLimitElement = rootElement.element("group-limit");

    if (groupLimitElement != null) {
      groupLimit = new ThemeGroupLimit();

      Element groupIncludesElement = groupLimitElement.element("group-includes");

      if (groupIncludesElement != null) {
        groupLimit.setIncludes(_getGroupLimitIncludes(groupIncludesElement));
      }

      Element groupExcludesElement = groupLimitElement.element("group-excludes");

      if (groupExcludesElement != null) {
        groupLimit.setExcludes(_getGroupLimitExcludes(groupExcludesElement));
      }
    }

    long timestamp = ServletContextUtil.getLastModified(servletContext);

    List<Element> themeElements = rootElement.elements("theme");

    for (Element themeElement : themeElements) {
      ContextReplace themeContextReplace = new ContextReplace();

      themeContextReplace.addValue("themes-path", themesPath);

      String themeId = themeElement.attributeValue("id");

      if (servletContextName != null) {
        themeId = themeId + PortletConstants.WAR_SEPARATOR + servletContextName;
      }

      themeId = PortalUtil.getJsSafePortletId(themeId);

      themeContextReplace.addValue("theme-id", themeId);

      themeIds.add(themeId);

      Theme theme = _themes.get(themeId);

      if (theme == null) {
        theme = new ThemeImpl(themeId);
      }

      theme.setTimestamp(timestamp);

      PluginSetting pluginSetting = pluginSettingLocalService.getDefaultPluginSetting();

      theme.setPluginPackage(pluginPackage);
      theme.setDefaultPluginSetting(pluginSetting);

      theme.setThemeCompanyLimit(companyLimit);
      theme.setThemeGroupLimit(groupLimit);

      if (servletContextName != null) {
        theme.setServletContextName(servletContextName);
      }

      theme.setLoadFromServletContext(loadFromServletContext);

      String name = GetterUtil.getString(themeElement.attributeValue("name"), theme.getName());

      String rootPath =
          GetterUtil.getString(themeElement.elementText("root-path"), theme.getRootPath());

      rootPath = themeContextReplace.replace(rootPath);

      themeContextReplace.addValue("root-path", rootPath);

      String templatesPath =
          GetterUtil.getString(
              themeElement.elementText("templates-path"), theme.getTemplatesPath());

      templatesPath = themeContextReplace.replace(templatesPath);
      templatesPath = StringUtil.safePath(templatesPath);

      themeContextReplace.addValue("templates-path", templatesPath);

      String cssPath =
          GetterUtil.getString(themeElement.elementText("css-path"), theme.getCssPath());

      cssPath = themeContextReplace.replace(cssPath);
      cssPath = StringUtil.safePath(cssPath);

      themeContextReplace.addValue("css-path", cssPath);

      String imagesPath =
          GetterUtil.getString(themeElement.elementText("images-path"), theme.getImagesPath());

      imagesPath = themeContextReplace.replace(imagesPath);
      imagesPath = StringUtil.safePath(imagesPath);

      themeContextReplace.addValue("images-path", imagesPath);

      String javaScriptPath =
          GetterUtil.getString(
              themeElement.elementText("javascript-path"), theme.getJavaScriptPath());

      javaScriptPath = themeContextReplace.replace(javaScriptPath);
      javaScriptPath = StringUtil.safePath(javaScriptPath);

      themeContextReplace.addValue("javascript-path", javaScriptPath);

      String virtualPath =
          GetterUtil.getString(themeElement.elementText("virtual-path"), theme.getVirtualPath());

      String templateExtension =
          GetterUtil.getString(
              themeElement.elementText("template-extension"), theme.getTemplateExtension());

      theme.setName(name);
      theme.setRootPath(rootPath);
      theme.setTemplatesPath(templatesPath);
      theme.setCssPath(cssPath);
      theme.setImagesPath(imagesPath);
      theme.setJavaScriptPath(javaScriptPath);
      theme.setVirtualPath(virtualPath);
      theme.setTemplateExtension(templateExtension);

      Element settingsElement = themeElement.element("settings");

      if (settingsElement != null) {
        List<Element> settingElements = settingsElement.elements("setting");

        for (Element settingElement : settingElements) {
          boolean configurable =
              GetterUtil.getBoolean(settingElement.attributeValue("configurable"));
          String key = settingElement.attributeValue("key");
          String[] options = StringUtil.split(settingElement.attributeValue("options"));
          String type = settingElement.attributeValue("type");
          String value = settingElement.attributeValue("value");
          String script = settingElement.getTextTrim();

          theme.addSetting(key, value, configurable, type, options, script);
        }
      }

      theme.setWapTheme(
          GetterUtil.getBoolean(themeElement.elementText("wap-theme"), theme.isWapTheme()));

      Element rolesElement = themeElement.element("roles");

      if (rolesElement != null) {
        List<Element> roleNameElements = rolesElement.elements("role-name");

        for (Element roleNameElement : roleNameElements) {
          pluginSetting.addRole(roleNameElement.getText());
        }
      }

      _readColorSchemes(themeElement, theme.getColorSchemesMap(), themeContextReplace);
      _readColorSchemes(themeElement, theme.getColorSchemesMap(), themeContextReplace);

      Element layoutTemplatesElement = themeElement.element("layout-templates");

      if (layoutTemplatesElement != null) {
        Element standardElement = layoutTemplatesElement.element("standard");

        if (standardElement != null) {
          layoutTemplateLocalService.readLayoutTemplate(
              servletContextName,
              servletContext,
              null,
              standardElement,
              true,
              themeId,
              pluginPackage);
        }

        Element customElement = layoutTemplatesElement.element("custom");

        if (customElement != null) {
          layoutTemplateLocalService.readLayoutTemplate(
              servletContextName,
              servletContext,
              null,
              customElement,
              false,
              themeId,
              pluginPackage);
        }
      }

      if (!theme.isWapTheme()) {
        _setSpriteImages(servletContext, theme, imagesPath);
      }

      if (!_themes.containsKey(themeId)) {
        _themes.put(themeId, theme);
      }
    }

    return themeIds;
  }
  protected void exportTheme(LayoutSet layoutSet, ZipWriter zipWriter) throws Exception {

    Theme theme = layoutSet.getTheme();

    String lookAndFeelXML =
        ContentUtil.get("com/liferay/portal/dependencies/liferay-look-and-feel.xml.tmpl");

    lookAndFeelXML =
        StringUtil.replace(
            lookAndFeelXML,
            new String[] {"[$TEMPLATE_EXTENSION$]", "[$VIRTUAL_PATH$]"},
            new String[] {theme.getTemplateExtension(), theme.getVirtualPath()});

    String servletContextName = theme.getServletContextName();

    ServletContext servletContext = ServletContextPool.get(servletContextName);

    if (servletContext == null) {
      if (_log.isWarnEnabled()) {
        _log.warn("Servlet context not found for theme " + theme.getThemeId());
      }

      return;
    }

    File themeZip = new File(zipWriter.getPath() + "/theme.zip");

    ZipWriter themeZipWriter = ZipWriterFactoryUtil.getZipWriter(themeZip);

    themeZipWriter.addEntry("liferay-look-and-feel.xml", lookAndFeelXML);

    File cssPath = null;
    File imagesPath = null;
    File javaScriptPath = null;
    File templatesPath = null;

    if (!theme.isLoadFromServletContext()) {
      ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(servletContextName);

      if (themeLoader == null) {
        _log.error(servletContextName + " does not map to a theme loader");
      } else {
        String realPath =
            themeLoader.getFileStorage().getPath() + StringPool.SLASH + theme.getName();

        cssPath = new File(realPath + "/css");
        imagesPath = new File(realPath + "/images");
        javaScriptPath = new File(realPath + "/javascript");
        templatesPath = new File(realPath + "/templates");
      }
    } else {
      cssPath = new File(servletContext.getRealPath(theme.getCssPath()));
      imagesPath = new File(servletContext.getRealPath(theme.getImagesPath()));
      javaScriptPath = new File(servletContext.getRealPath(theme.getJavaScriptPath()));
      templatesPath = new File(servletContext.getRealPath(theme.getTemplatesPath()));
    }

    exportThemeFiles("css", cssPath, themeZipWriter);
    exportThemeFiles("images", imagesPath, themeZipWriter);
    exportThemeFiles("javascript", javaScriptPath, themeZipWriter);
    exportThemeFiles("templates", templatesPath, themeZipWriter);
  }