Esempio n. 1
0
  protected List<TemplateNode> getTemplateNodes(ThemeDisplay themeDisplay, Element element)
      throws Exception {

    List<TemplateNode> templateNodes = new ArrayList<TemplateNode>();

    Map<String, TemplateNode> prototypeTemplateNodes = new HashMap<String, TemplateNode>();

    List<Element> dynamicElementElements = element.elements("dynamic-element");

    for (Element dynamicElementElement : dynamicElementElements) {
      Element dynamicContentElement = dynamicElementElement.element("dynamic-content");

      String data = StringPool.BLANK;

      if (dynamicContentElement != null) {
        data = dynamicContentElement.getText();
      }

      String name = dynamicElementElement.attributeValue("name", StringPool.BLANK);

      if (name.length() == 0) {
        throw new TransformException("Element missing \"name\" attribute");
      }

      String type = dynamicElementElement.attributeValue("type", StringPool.BLANK);

      TemplateNode templateNode = new TemplateNode(themeDisplay, name, stripCDATA(data), type);

      if (dynamicElementElement.element("dynamic-element") != null) {
        templateNode.appendChildren(getTemplateNodes(themeDisplay, dynamicElementElement));
      } else if ((dynamicContentElement != null)
          && (dynamicContentElement.element("option") != null)) {

        List<Element> optionElements = dynamicContentElement.elements("option");

        for (Element optionElement : optionElements) {
          templateNode.appendOption(stripCDATA(optionElement.getText()));
        }
      }

      TemplateNode prototypeTemplateNode = prototypeTemplateNodes.get(name);

      if (prototypeTemplateNode == null) {
        prototypeTemplateNode = templateNode;

        prototypeTemplateNodes.put(name, prototypeTemplateNode);

        templateNodes.add(templateNode);
      }

      prototypeTemplateNode.appendSibling(templateNode);
    }

    return templateNodes;
  }
  protected String extractDynamicContent(Element rootElement) {
    StringBundler sb = new StringBundler();

    List<Element> dynamicElementElements = rootElement.elements("dynamic-element");

    for (Element dynamicElementElement : dynamicElementElements) {
      String type = dynamicElementElement.attributeValue("type", StringPool.BLANK);

      if (!type.equals("boolean")
          && !type.equals("document_library")
          && !type.equals("image")
          && !type.equals("list")
          && !type.equals("link_to_layout")
          && !type.equals("multi-list")
          && !type.equals("selection_break")) {

        Element dynamicContentElement = dynamicElementElement.element("dynamic-content");

        if (dynamicContentElement != null) {
          String dynamicContent = dynamicContentElement.getText();

          sb.append(dynamicContent);
          sb.append(StringPool.SPACE);
        }
      }

      sb.append(extractDynamicContent(dynamicElementElement));
    }

    return sb.toString();
  }
  protected void parseNotificationElements(
      List<Element> notificationElements, NotificationAware notificationAware) {

    if (notificationElements.isEmpty()) {
      return;
    }

    Set<Notification> notifications = new HashSet<Notification>(notificationElements.size());

    for (Element notificationElement : notificationElements) {
      String name = notificationElement.elementText("name");
      String description = notificationElement.elementText("description");
      String executionType = notificationElement.elementText("execution-type");
      String template = notificationElement.elementText("template");
      String templateLanguage = notificationElement.elementText("template-language");

      Notification notification =
          new Notification(name, description, executionType, template, templateLanguage);

      List<Element> notificationTypeElements = notificationElement.elements("notification-type");

      for (Element notificationTypeElement : notificationTypeElements) {
        notification.addNotificationType(notificationTypeElement.getText());
      }

      Element recipientsElement = notificationElement.element("recipients");

      parseRecipients(recipientsElement, notification);

      notifications.add(notification);
    }

    notificationAware.setNotifications(notifications);
  }
  protected List<String> readSpecialNamespaces(Element root) throws ImportFilesException {

    List<String> namespaces = new ArrayList<String>();

    Element siteinfoElement = root.element("siteinfo");

    if (siteinfoElement == null) {
      throw new ImportFilesException("Invalid pages XML file");
    }

    Element namespacesElement = siteinfoElement.element("namespaces");

    List<Element> namespaceElements = namespacesElement.elements("namespace");

    for (Element namespaceElement : namespaceElements) {
      Attribute attribute = namespaceElement.attribute("key");

      String value = attribute.getValue();

      if (!value.equals("0")) {
        namespaces.add(namespaceElement.getText());
      }
    }

    return namespaces;
  }
Esempio n. 5
0
  protected void read(ClassLoader classLoader, String source) throws Exception {

    InputStream is = classLoader.getResourceAsStream(source);

    if (is == null) {
      return;
    }

    if (_log.isDebugEnabled()) {
      _log.debug("Loading " + source);
    }

    Document document = SAXReaderUtil.read(is);

    Element rootElement = document.getRootElement();

    for (Element sqlElement : rootElement.elements("sql")) {
      String file = sqlElement.attributeValue("file");

      if (Validator.isNotNull(file)) {
        read(classLoader, file);
      } else {
        String id = sqlElement.attributeValue("id");
        String content = transform(sqlElement.getText());

        content = replaceIsNull(content);

        _sqlPool.put(id, content);
      }
    }
  }
  protected void validatePathDocument(String fileName, Element rootElement) {
    Element headElement = rootElement.element("head");

    Element titleElement = headElement.element("title");

    String title = titleElement.getText();

    int x = fileName.lastIndexOf(StringPool.SLASH);
    int y = fileName.lastIndexOf(CharPool.PERIOD);

    String shortFileName = fileName.substring(x + 1, y);

    if ((title == null) || !shortFileName.equals(title)) {
      throwValidationException(0, fileName);
    }

    Element bodyElement = rootElement.element("body");

    Element tableElement = bodyElement.element("table");

    Element theadElement = tableElement.element("thead");

    Element trElement = theadElement.element("tr");

    Element tdElement = trElement.element("td");

    String tdText = tdElement.getText();

    if ((tdText == null) || !shortFileName.equals(tdText)) {
      throwValidationException(0, fileName);
    }

    Element tbodyElement = tableElement.element("tbody");

    List<Element> elements = tbodyElement.elements();

    for (Element element : elements) {
      String elementName = element.getName();

      if (elementName.equals("tr")) {
        validatePathTrElement(fileName, element);
      } else {
        throwValidationException(1002, fileName, element, elementName);
      }
    }
  }
  protected void indexField(Document document, Element element, String elType, String elIndexType) {

    if (Validator.isNull(elIndexType)) {
      return;
    }

    com.liferay.portal.kernel.xml.Document structureDocument = element.getDocument();

    Element rootElement = structureDocument.getRootElement();

    String defaultLocale = GetterUtil.getString(rootElement.attributeValue("default-locale"));

    String name = encodeFieldName(element.attributeValue("name"));

    List<Element> dynamicContentElements = element.elements("dynamic-content");

    for (Element dynamicContentElement : dynamicContentElements) {
      String contentLocale =
          GetterUtil.getString(dynamicContentElement.attributeValue("language-id"));

      String[] value = new String[] {dynamicContentElement.getText()};

      if (elType.equals("multi-list")) {
        List<Element> optionElements = dynamicContentElement.elements("option");

        value = new String[optionElements.size()];

        for (int i = 0; i < optionElements.size(); i++) {
          value[i] = optionElements.get(i).getText();
        }
      }

      if (elIndexType.equals("keyword")) {
        if (Validator.isNull(contentLocale)) {
          document.addKeyword(name, value);
        } else {
          if (defaultLocale.equals(contentLocale)) {
            document.addKeyword(name, value);
          }

          document.addKeyword(name.concat(StringPool.UNDERLINE).concat(contentLocale), value);
        }
      } else if (elIndexType.equals("text")) {
        if (Validator.isNull(contentLocale)) {
          document.addText(name, StringUtil.merge(value, StringPool.SPACE));
        } else {
          if (defaultLocale.equals(contentLocale)) {
            document.addText(name, StringUtil.merge(value, StringPool.SPACE));
          }

          document.addText(
              name.concat(StringPool.UNDERLINE).concat(contentLocale),
              StringUtil.merge(value, StringPool.SPACE));
        }
      }
    }
  }
  protected void parseRecipients(Element recipientsElement, Notification notification) {

    if (recipientsElement == null) {
      return;
    }

    List<Element> addressRecipientElements = recipientsElement.elements("address");

    for (Element addressRecipientElement : addressRecipientElements) {
      AddressRecipient addressRecipient = new AddressRecipient(addressRecipientElement.getText());

      notification.addRecipients(addressRecipient);
    }

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

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

      for (Element roleReceipientElement : roleReceipientElements) {
        long roleId = GetterUtil.getLong(roleReceipientElement.elementText("role-id"));
        String roleType = roleReceipientElement.elementText("role-type");
        String name = roleReceipientElement.elementText("name");

        RoleRecipient roleRecipient = null;

        if (roleId > 0) {
          roleRecipient = new RoleRecipient(roleId, roleType);
        } else {
          roleRecipient = new RoleRecipient(name, roleType);

          boolean autoCreate =
              GetterUtil.getBoolean(roleReceipientElement.elementText("auto-create"), true);

          roleRecipient.setAutoCreate(autoCreate);
        }

        notification.addRecipients(roleRecipient);
      }
    }

    List<Element> userRecipientElements = recipientsElement.elements("user");

    for (Element userRecipientElement : userRecipientElements) {
      long userId = GetterUtil.getLong(userRecipientElement.elementText("user-id"));
      String screenName = userRecipientElement.elementText("screen-name");
      String emailAddress = userRecipientElement.elementText("email-address");

      UserRecipient userRecipient = new UserRecipient(userId, screenName, emailAddress);

      notification.addRecipients(userRecipient);
    }
  }
  protected void udpateFieldsMap(
      Element dynamicElementElement, Map<String, Map<Locale, List<String>>> fieldsMap) {

    List<Element> childrenDynamicElementElements =
        dynamicElementElement.elements("dynamic-element");

    for (Element childrenDynamicElementElement : childrenDynamicElementElements) {

      udpateFieldsMap(childrenDynamicElementElement, fieldsMap);
    }

    String name = dynamicElementElement.attributeValue("name");

    Map<Locale, List<String>> valuesMap = fieldsMap.get(name);

    if (valuesMap == null) {
      valuesMap = new HashMap<>();

      fieldsMap.put(name, valuesMap);
    }

    List<Element> dynamicContentElements = dynamicElementElement.elements("dynamic-content");

    for (Element dynamicContentElement : dynamicContentElements) {
      Locale locale =
          LocaleUtil.fromLanguageId(dynamicContentElement.attributeValue("language-id"));

      List<String> values = getValues(valuesMap, locale);

      List<Element> optionElements = dynamicContentElement.elements("option");

      if (!optionElements.isEmpty()) {
        for (Element optionElement : optionElements) {
          values.add(optionElement.getText());
        }
      } else {
        values.add(dynamicContentElement.getText());
      }
    }
  }
Esempio n. 10
0
  protected void transformDateFieldValue(Element dynamicContentElement) {
    String value = dynamicContentElement.getText();

    if (!Validator.isNumber(value)) {
      return;
    }

    Date date = new Date(GetterUtil.getLong(value));

    dynamicContentElement.clearContent();

    dynamicContentElement.addCDATA(_dateFormat.format(date));
  }
  private Map<String, String> _getField(Element element, String locale) {
    Map<String, String> field = new HashMap<String, String>();

    List<String> availableLocales = getAvailableLanguageIds();

    if ((locale != null) && !availableLocales.contains(locale)) {
      locale = getDefaultLanguageId();
    }

    locale = HtmlUtil.escapeXPathAttribute(locale);

    String xPathExpression = "meta-data[@locale=".concat(locale).concat("]");

    XPath xPathSelector = SAXReaderUtil.createXPath(xPathExpression);

    Node node = xPathSelector.selectSingleNode(element);

    Element metaDataElement = (Element) node.asXPathResult(node.getParent());

    if (metaDataElement != null) {
      List<Element> childMetaDataElements = metaDataElement.elements();

      for (Element childMetaDataElement : childMetaDataElements) {
        String name = childMetaDataElement.attributeValue("name");
        String value = childMetaDataElement.getText();

        field.put(name, value);
      }
    }

    for (Attribute attribute : element.attributes()) {
      field.put(attribute.getName(), attribute.getValue());
    }

    Element parentElement = element.getParent();

    if (parentElement != null) {
      String parentName = parentElement.attributeValue("name");

      if (Validator.isNotNull(parentName)) {
        field.put(_getPrivateAttributeKey("parentName"), parentName);
      }
    }

    return field;
  }
Esempio n. 12
0
  protected Map<String, Object> insertRequestVariables(Element element) {
    Map<String, Object> map = new HashMap<>();

    if (element == null) {
      return map;
    }

    for (Element childElement : element.elements()) {
      String name = childElement.getName();

      if (name.equals("attribute")) {
        Element nameElement = childElement.element("name");
        Element valueElement = childElement.element("value");

        map.put(nameElement.getText(), valueElement.getText());
      } else if (name.equals("parameter")) {
        Element nameElement = childElement.element("name");

        List<Element> valueElements = childElement.elements("value");

        if (valueElements.size() == 1) {
          Element valueElement = valueElements.get(0);

          map.put(nameElement.getText(), valueElement.getText());
        } else {
          List<String> values = new ArrayList<>();

          for (Element valueElement : valueElements) {
            values.add(valueElement.getText());
          }

          map.put(nameElement.getText(), values);
        }
      } else {
        List<Element> elements = childElement.elements();

        if (!elements.isEmpty()) {
          map.put(name, insertRequestVariables(childElement));
        } else {
          map.put(name, childElement.getText());
        }
      }
    }

    return map;
  }
Esempio n. 13
0
  protected String convertStaticContentToDynamic(String content) throws Exception {

    Document document = SAXReaderUtil.read(content);

    Document newDocument = SAXReaderUtil.createDocument();

    Element rootElement = document.getRootElement();

    String availableLocales = rootElement.attributeValue("available-locales");
    String defaultLocale = rootElement.attributeValue("default-locale");

    Element newRootElement = SAXReaderUtil.createElement("root");

    newRootElement.addAttribute("available-locales", availableLocales);
    newRootElement.addAttribute("default-locale", defaultLocale);

    newDocument.add(newRootElement);

    Element dynamicElementElement = SAXReaderUtil.createElement("dynamic-element");

    dynamicElementElement.addAttribute("name", "content");
    dynamicElementElement.addAttribute("type", "text_area");
    dynamicElementElement.addAttribute("index-type", "keyword");
    dynamicElementElement.addAttribute("index", String.valueOf(0));

    newRootElement.add(dynamicElementElement);

    List<Element> staticContentElements = rootElement.elements("static-content");

    for (Element staticContentElement : staticContentElements) {
      String languageId = staticContentElement.attributeValue("language-id");
      String text = staticContentElement.getText();

      Element dynamicContentElement = SAXReaderUtil.createElement("dynamic-content");

      dynamicContentElement.addAttribute("language-id", languageId);
      dynamicContentElement.addCDATA(text);

      dynamicElementElement.add(dynamicContentElement);
    }

    return XMLUtil.formatXML(newDocument);
  }
  private void _addDocletTags(Element parentElement, String name, String indent, StringBuilder sb) {

    List<Element> elements = parentElement.elements(name);

    for (Element element : elements) {
      sb.append(indent);
      sb.append(" * @");
      sb.append(name);
      sb.append(" ");

      Element commentElement = element.element("comment");

      if (commentElement != null) {
        sb.append(element.elementText("name"));
        sb.append(" ");
        sb.append(_getCDATA(element.elementText("comment")));
      } else {
        sb.append(_getCDATA(element.getText()));
      }

      sb.append("\n");
    }
  }
  protected void upgradeToAssetEntryUuidElement(Element rootElement) throws Exception {

    Element assetEntryIdElement = rootElement.element("assetEntryId");

    long assetEntryId = GetterUtil.getLong(assetEntryIdElement.getText());

    try (PreparedStatement ps =
        connection.prepareStatement("select classUuid from AssetEntry where entryId = ?")) {

      ps.setLong(1, assetEntryId);

      try (ResultSet rs = ps.executeQuery()) {
        if (rs.next()) {
          String classUuid = rs.getString("classUuid");

          Element assetEntryUuidElement = rootElement.addElement("assetEntryUuid");

          assetEntryUuidElement.addText(classUuid);

          rootElement.remove(assetEntryIdElement);
        }
      }
    }
  }
  protected void validateVarElement(String fileName, Element element) {
    List<Attribute> attributes = element.attributes();

    Map<String, String> attributeMap = new HashMap<String, String>();

    for (Attribute attribute : attributes) {
      String attributeName = attribute.getName();
      String attributeValue = attribute.getValue();

      if (!attributeName.equals("value") && Validator.isNull(attributeValue)) {

        throwValidationException(1006, fileName, element, attributeName);
      }

      if (!_allowedVarAttributes.contains(attributeName)) {
        throwValidationException(1005, fileName, element, attributeName);
      }

      attributeMap.put(attributeName, attributeValue);
    }

    if (!attributeMap.containsKey("name")) {
      throwValidationException(1004, fileName, element, new String[] {"name"});
    } else {
      String nameValue = attributeMap.get("name");

      if (Validator.isNull(nameValue)) {
        throwValidationException(1006, fileName, element, "name");
      }
    }

    String varText = element.getText();

    if (Validator.isNull(varText)
        && !attributeMap.containsKey("locator-key")
        && !attributeMap.containsKey("path")
        && !attributeMap.containsKey("value")) {

      throwValidationException(1004, fileName, element, new String[] {"value"});
    }

    if (!attributeMap.containsKey("value")) {
      String locatorKeyValue = attributeMap.get("locator-key");
      String pathValue = attributeMap.get("path");

      if (Validator.isNull(locatorKeyValue) && Validator.isNotNull(pathValue)) {

        throwValidationException(1004, fileName, element, new String[] {"locator-key"});
      }

      if (Validator.isNotNull(locatorKeyValue) && Validator.isNull(pathValue)) {

        throwValidationException(1004, fileName, element, new String[] {"path"});
      }
    } else {
      String varValue = attributeMap.get("value");

      Pattern pattern = Pattern.compile("\\$\\{([^\\}]*?)\\}");

      Matcher matcher = pattern.matcher(varValue);

      while (matcher.find()) {
        String statement = matcher.group(1);

        Pattern statementPattern = Pattern.compile("(.*)\\?(.*)\\(([^\\)]*?)\\)");

        Matcher statementMatcher = statementPattern.matcher(statement);

        if (statementMatcher.find()) {
          String operand = statementMatcher.group(1);

          String method = statementMatcher.group(2);

          if (operand.equals("") || method.equals("")) {
            throwValidationException(1006, fileName, element, "value");
          }

          if (!_methodNames.contains(method)) {
            throwValidationException(1013, fileName, element, method);
          }
        } else {
          if (statement.matches(".*[\\?\\(\\)\\}\\{].*")) {
            throwValidationException(1006, fileName, element, "value");
          }
        }
      }
    }

    List<Element> childElements = element.elements();

    if (!childElements.isEmpty()) {
      Element childElement = childElements.get(0);

      String childElementName = childElement.getName();

      throwValidationException(1002, fileName, childElement, childElementName);
    }
  }
Esempio n. 17
0
  @Override
  public Fields getFields(DDMStructure structure, XPath xPath, String xml, List<String> fieldNames)
      throws PortalException {

    Document document = null;

    try {
      document = SAXReaderUtil.read(xml);
    } catch (DocumentException de) {
      return null;
    }

    if ((xPath != null) && !xPath.booleanValueOf(document)) {
      return null;
    }

    Fields fields = new Fields();

    Element rootElement = document.getRootElement();

    List<Element> dynamicElementElements = rootElement.elements("dynamic-element");

    for (Element dynamicElementElement : dynamicElementElements) {
      String fieldName = dynamicElementElement.attributeValue("name");

      if (!structure.hasField(fieldName)
          || ((fieldNames != null) && !fieldNames.contains(fieldName))) {

        continue;
      }

      String fieldDataType = structure.getFieldDataType(fieldName);

      List<Element> dynamicContentElements = dynamicElementElement.elements("dynamic-content");

      for (Element dynamicContentElement : dynamicContentElements) {
        String fieldValue = dynamicContentElement.getText();

        String languageId = dynamicContentElement.attributeValue("language-id");

        Locale locale = LocaleUtil.fromLanguageId(languageId);

        Serializable fieldValueSerializable =
            FieldConstants.getSerializable(fieldDataType, fieldValue);

        Field field = fields.get(fieldName);

        if (field == null) {
          field = new Field();

          String defaultLanguageId = dynamicElementElement.attributeValue("default-language-id");

          Locale defaultLocale = LocaleUtil.fromLanguageId(defaultLanguageId);

          field.setDefaultLocale(defaultLocale);

          field.setDDMStructureId(structure.getStructureId());
          field.setName(fieldName);
          field.setValue(locale, fieldValueSerializable);

          fields.put(field);
        } else {
          field.addValue(locale, fieldValueSerializable);
        }
      }
    }

    return fields;
  }
  protected Set<Assignment> parseAssignments(Element assignmentsElement) {
    if (assignmentsElement == null) {
      return Collections.emptySet();
    }

    Set<Assignment> assignments = new HashSet<Assignment>();

    Element resourceActionsElement = assignmentsElement.element("resource-actions");

    if (resourceActionsElement != null) {
      List<Element> resourceActionElements = resourceActionsElement.elements("resource-action");

      for (Element resourceActionElement : resourceActionElements) {
        String actionId = resourceActionElement.getText();

        if (Validator.isNotNull(actionId)) {
          ResourceActionAssignment resourceActionAssignment =
              new ResourceActionAssignment(actionId);

          assignments.add(resourceActionAssignment);
        }
      }
    }

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

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

      for (Element roleAssignmentElement : roleAssignmentElements) {
        long roleId = GetterUtil.getLong(roleAssignmentElement.elementText("role-id"));
        String roleType = roleAssignmentElement.elementText("role-type");
        String name = roleAssignmentElement.elementText("name");

        RoleAssignment roleAssignment = null;

        if (Validator.isNotNull(name)) {
          roleAssignment = new RoleAssignment(name, roleType);

          boolean autoCreate =
              GetterUtil.getBoolean(roleAssignmentElement.elementText("auto-create"), true);

          roleAssignment.setAutoCreate(autoCreate);
        } else {
          roleAssignment = new RoleAssignment(roleId);
        }

        assignments.add(roleAssignment);
      }
    }

    List<Element> scriptedAssignmentElements = assignmentsElement.elements("scripted-assignment");

    for (Element scriptedAssignmentElement : scriptedAssignmentElements) {
      String script = scriptedAssignmentElement.elementText("script");
      String scriptLanguage = scriptedAssignmentElement.elementText("script-language");
      String scriptRequiredContexts =
          scriptedAssignmentElement.elementText("script-required-contexts");

      ScriptAssignment scriptAssignment =
          new ScriptAssignment(script, scriptLanguage, scriptRequiredContexts);

      assignments.add(scriptAssignment);
    }

    List<Element> userAssignmentElements = assignmentsElement.elements("user");

    for (Element userAssignmentElement : userAssignmentElements) {
      long userId = GetterUtil.getLong(userAssignmentElement.elementText("user-id"));
      String screenName = userAssignmentElement.elementText("screen-name");
      String emailAddress = userAssignmentElement.elementText("email-address");

      UserAssignment userAssignment = new UserAssignment(userId, screenName, emailAddress);

      assignments.add(userAssignment);
    }

    return assignments;
  }
  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 format(
      String oldCompanyId,
      long newCompanyId,
      long groupId,
      String articleId,
      double version,
      Element root)
      throws Exception {

    Iterator<Element> itr = root.elements().iterator();

    while (itr.hasNext()) {
      Element el = itr.next();

      Element dynamicContent = el.element("dynamic-content");

      String elInstanceId = StringPool.BLANK;
      String elName = el.attributeValue("name", StringPool.BLANK);
      String elType = el.attributeValue("type", StringPool.BLANK);
      String elLanguage = StringPool.BLANK;

      if (dynamicContent != null) {
        elLanguage = dynamicContent.attributeValue("language-id", StringPool.BLANK);

        if (!elLanguage.equals(StringPool.BLANK)) {
          elLanguage = "_" + elLanguage;
        }
      }

      if (elType.equals("image") || elType.equals("text")) {
        String oldImageId = dynamicContent.getText();

        if (oldImageId.startsWith(_IMG_ID_PATH)
            || oldImageId.startsWith("@portal_url@" + _IMG_ID_PATH)
            || oldImageId.startsWith("http://@portal_url@" + _IMG_ID_PATH)
            || oldImageId.startsWith("https://@portal_url@" + _IMG_ID_PATH)) {

          int pos = oldImageId.indexOf(_IMG_ID_PATH);

          String preOldImageId = oldImageId.substring(0, pos);

          oldImageId = oldImageId.substring(pos + _IMG_ID_PATH.length(), oldImageId.length());

          String newImageId = getNewImageId(oldCompanyId, oldImageId);

          dynamicContent.setText(preOldImageId + _IMG_ID_PATH + newImageId);

          if (elType.equals("image")) {
            dynamicContent.addAttribute("id", newImageId);

            long articleImageId = GetterUtil.getLong(newImageId);

            JournalArticleImageLocalServiceUtil.addArticleImageId(
                articleImageId, groupId, articleId, version, elInstanceId, elName, elLanguage);
          }
        }
      }

      format(oldCompanyId, newCompanyId, groupId, articleId, version, el);
    }
  }
Esempio n. 21
0
  private String _addDocletTags(
      Element parentElement, String[] tagNames, String indent, boolean publicAccess) {

    List<String> allTagNames = new ArrayList<String>();
    List<String> customTagNames = new ArrayList<String>();
    List<String> requiredTagNames = new ArrayList<String>();

    for (String tagName : tagNames) {
      List<Element> elements = parentElement.elements(tagName);

      for (Element element : elements) {
        Element commentElement = element.element("comment");

        String comment = null;

        // Get comment by comment element's text or the element's text

        if (commentElement != null) {
          comment = commentElement.getText();
        } else {
          comment = element.getText();
        }

        if (tagName.equals("param") || tagName.equals("return") || tagName.equals("throws")) {

          if (Validator.isNotNull(comment)) {
            requiredTagNames.add(tagName);
          } else if (tagName.equals("param")) {
            if (GetterUtil.getBoolean(element.elementText("required"))) {

              requiredTagNames.add(tagName);
            }
          } else if (tagName.equals("throws")) {
            if (GetterUtil.getBoolean(element.elementText("required"))) {

              requiredTagNames.add(tagName);
            }
          }
        } else {
          customTagNames.add(tagName);
        }

        allTagNames.add(tagName);
      }
    }

    int maxTagNameLength = 0;

    List<String> maxTagNameLengthTags = new ArrayList<String>();

    if (_initializeMissingJavadocs) {
      maxTagNameLengthTags.addAll(allTagNames);
    } else if (_updateJavadocs) {
      if (!requiredTagNames.isEmpty()) {
        maxTagNameLengthTags.addAll(allTagNames);
      } else {
        maxTagNameLengthTags.addAll(customTagNames);
        maxTagNameLengthTags.addAll(requiredTagNames);
      }
    } else {
      maxTagNameLengthTags.addAll(customTagNames);
      maxTagNameLengthTags.addAll(requiredTagNames);
    }

    for (String name : maxTagNameLengthTags) {
      if (name.length() > maxTagNameLength) {
        maxTagNameLength = name.length();
      }
    }

    // There should be an @ sign before the tag and a space after it

    maxTagNameLength += 2;

    String tagNameIndent = _getSpacesIndent(maxTagNameLength);

    StringBundler sb = new StringBundler();

    for (String tagName : tagNames) {
      List<Element> elements = parentElement.elements(tagName);

      for (Element element : elements) {
        Element commentElement = element.element("comment");

        String comment = null;

        if (commentElement != null) {
          comment = commentElement.getText();
        } else {
          comment = element.getText();
        }

        String elementName = element.elementText("name");

        if (Validator.isNotNull(comment)) {
          comment = _assembleTagComment(tagName, elementName, comment, indent, tagNameIndent);

          sb.append(comment);
        } else {
          if (_initializeMissingJavadocs && publicAccess) {

            // Write out all tags

            comment = _assembleTagComment(tagName, elementName, comment, indent, tagNameIndent);

            sb.append(comment);
          } else if (_updateJavadocs && publicAccess) {
            if (!tagName.equals("param")
                && !tagName.equals("return")
                && !tagName.equals("throws")) {

              // Write out custom tag

              comment = _assembleTagComment(tagName, elementName, comment, indent, tagNameIndent);

              sb.append(comment);
            } else if (!requiredTagNames.isEmpty()) {

              // Write out all tags

              comment = _assembleTagComment(tagName, elementName, comment, indent, tagNameIndent);

              sb.append(comment);
            } else {

              // Skip empty common tag

            }
          } else {
            if (!tagName.equals("param")
                && !tagName.equals("return")
                && !tagName.equals("throws")) {

              // Write out custom tag

              comment = _assembleTagComment(tagName, elementName, comment, indent, tagNameIndent);

              sb.append(comment);
            } else if (tagName.equals("param")
                || tagName.equals("return")
                || tagName.equals("throws")) {

              if (GetterUtil.getBoolean(element.elementText("required"))) {

                elementName = element.elementText("name");

                comment = _assembleTagComment(tagName, elementName, comment, indent, tagNameIndent);

                sb.append(comment);
              }
            } else {

              // Skip empty common tag

            }
          }
        }
      }
    }

    return sb.toString();
  }
Esempio n. 22
0
  protected List<TemplateNode> getTemplateNodes(
      ThemeDisplay themeDisplay, Element element, long ddmStructureId) throws Exception {

    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(ddmStructureId);

    DDMForm ddmForm = ddmStructure.getDDMForm();

    Map<String, DDMFormField> ddmFormFieldsMap = ddmForm.getDDMFormFieldsMap(true);

    List<TemplateNode> templateNodes = new ArrayList<>();

    Map<String, TemplateNode> prototypeTemplateNodes = new HashMap<>();

    List<Element> dynamicElementElements = element.elements("dynamic-element");

    for (Element dynamicElementElement : dynamicElementElements) {
      Element dynamicContentElement = dynamicElementElement.element("dynamic-content");

      String data = StringPool.BLANK;

      if (dynamicContentElement != null) {
        data = dynamicContentElement.getText();
      }

      String name = dynamicElementElement.attributeValue("name", StringPool.BLANK);

      if (name.length() == 0) {
        throw new TransformException("Element missing \"name\" attribute");
      }

      String type = dynamicElementElement.attributeValue("type", StringPool.BLANK);

      Map<String, String> attributes = new HashMap<>();

      if (dynamicContentElement != null) {
        for (Attribute attribute : dynamicContentElement.attributes()) {
          attributes.put(attribute.getName(), attribute.getValue());
        }
      }

      TemplateNode templateNode =
          new TemplateNode(themeDisplay, name, StringUtil.stripCDATA(data), type, attributes);

      if (dynamicElementElement.element("dynamic-element") != null) {
        templateNode.appendChildren(
            getTemplateNodes(themeDisplay, dynamicElementElement, ddmStructureId));
      } else if ((dynamicContentElement != null)
          && (dynamicContentElement.element("option") != null)) {

        List<Element> optionElements = dynamicContentElement.elements("option");

        for (Element optionElement : optionElements) {
          templateNode.appendOption(StringUtil.stripCDATA(optionElement.getText()));
        }
      }

      DDMFormField ddmFormField = ddmFormFieldsMap.get(name);

      if (ddmFormField != null) {
        DDMFormFieldOptions ddmFormFieldOptions = ddmFormField.getDDMFormFieldOptions();

        Map<String, LocalizedValue> options = ddmFormFieldOptions.getOptions();

        for (Entry<String, LocalizedValue> entry : options.entrySet()) {
          String optionValue = StringUtil.stripCDATA(entry.getKey());

          LocalizedValue localizedLabel = entry.getValue();

          String optionLabel = localizedLabel.getString(themeDisplay.getLocale());

          templateNode.appendOptionMap(optionValue, optionLabel);
        }
      }

      TemplateNode prototypeTemplateNode = prototypeTemplateNodes.get(name);

      if (prototypeTemplateNode == null) {
        prototypeTemplateNode = templateNode;

        prototypeTemplateNodes.put(name, prototypeTemplateNode);

        templateNodes.add(templateNode);
      }

      prototypeTemplateNode.appendSibling(templateNode);
    }

    return templateNodes;
  }