public void importServicePortletPreferences(
      PortletDataContext portletDataContext, Element serviceElement) throws PortalException {

    long ownerId = GetterUtil.getLong(serviceElement.attributeValue("owner-id"));
    int ownerType = GetterUtil.getInteger(serviceElement.attributeValue("owner-type"));
    String serviceName = serviceElement.attributeValue("service-name");

    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
      ownerId = portletDataContext.getGroupId();
    } else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) {
      ownerId = portletDataContext.getCompanyId();
    }

    PortletPreferences portletPreferences =
        getPortletPreferences(
            portletDataContext.getCompanyId(),
            ownerId,
            ownerType,
            LayoutConstants.DEFAULT_PLID,
            serviceName);

    for (Attribute attribute : serviceElement.attributes()) {
      serviceElement.remove(attribute);
    }

    String xml = serviceElement.asXML();

    portletPreferences.setPreferences(xml);

    _portletPreferencesLocalService.updatePortletPreferences(portletPreferences);
  }
  protected void importServicePortletPreferences(
      PortletDataContext portletDataContext, Element serviceElement) throws PortalException {

    long ownerId = GetterUtil.getLong(serviceElement.attributeValue("owner-id"));
    int ownerType = GetterUtil.getInteger(serviceElement.attributeValue("owner-type"));
    String serviceName = serviceElement.attributeValue("service-name");

    PortletPreferences portletPreferences =
        getPortletPreferences(
            portletDataContext.getCompanyId(),
            ownerId,
            ownerType,
            LayoutConstants.DEFAULT_PLID,
            serviceName);

    for (Attribute attribute : serviceElement.attributes()) {
      serviceElement.remove(attribute);
    }

    String xml = serviceElement.asXML();

    portletPreferences.setPreferences(xml);

    _portletPreferencesLocalService.updatePortletPreferences(portletPreferences);
  }
  protected void validateTestCaseDocument(String fileName, Element rootElement) {

    if (!Validator.equals(rootElement.getName(), "definition")) {
      throwValidationException(1000, fileName, rootElement);
    }

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

    if (elements.isEmpty()) {
      throwValidationException(1001, fileName, rootElement, new String[] {"command"});
    }

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

      if (elementName.equals("command")) {
        String attributeValue = element.attributeValue("name");

        if (attributeValue == null) {
          throwValidationException(1003, fileName, element, "name");
        } else if (Validator.isNull(attributeValue)) {
          throwValidationException(1006, fileName, element, "name");
        }

        validateBlockElement(
            fileName,
            element,
            new String[] {"execute", "var"},
            new String[] {"action", "macro"},
            new String[] {"var"},
            new String[0]);
      } else if (elementName.equals("set-up") || elementName.equals("tear-down")) {

        List<Attribute> attributes = element.attributes();

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

          if (!attributeName.equals("line-number")) {
            throwValidationException(1005, fileName, element, attributeName);
          }
        }

        validateBlockElement(
            fileName,
            element,
            new String[] {"execute", "var"},
            new String[] {"action", "macro"},
            new String[] {"var"},
            new String[0]);
      } else if (elementName.equals("var")) {
        validateVarElement(fileName, element);
      } else {
        throwValidationException(1002, fileName, element, elementName);
      }
    }
  }
Ejemplo n.º 4
0
  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;
  }
  protected void validateSimpleElement(
      String fileName, Element element, String[] neededAttributes) {

    Map<String, Boolean> hasNeededAttributes = new HashMap<String, Boolean>();

    for (String neededAttribute : neededAttributes) {
      hasNeededAttributes.put(neededAttribute, false);
    }

    List<Attribute> attributes = element.attributes();

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

      if (Validator.isNull(attributeValue)) {
        throwValidationException(1006, fileName, element, attributeName);
      }

      if (hasNeededAttributes.containsKey(attributeName)) {
        hasNeededAttributes.put(attributeName, true);
      }

      if (!attributeName.equals("line-number") && !hasNeededAttributes.containsKey(attributeName)) {

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

    for (String neededAttribute : neededAttributes) {
      if (!hasNeededAttributes.get(neededAttribute)) {
        throwValidationException(1004, fileName, element, neededAttributes);
      }
    }

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

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

      String childElementName = childElement.getName();

      throwValidationException(1002, fileName, childElement, childElementName);
    }
  }
  protected void validateExecuteElement(
      String fileName,
      Element executeElement,
      String[] allowedExecuteAttributeNames,
      String allowedExecuteAttributeValuesRegex,
      String[] allowedExecuteChildElementNames) {

    boolean hasAllowedAttributeName = false;

    List<Attribute> attributes = executeElement.attributes();

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

      if (ArrayUtil.contains(allowedExecuteAttributeNames, attributeName)) {

        hasAllowedAttributeName = true;

        break;
      }
    }

    if (!hasAllowedAttributeName) {
      throwValidationException(1004, fileName, executeElement, allowedExecuteAttributeNames);
    }

    String action = executeElement.attributeValue("action");
    String function = executeElement.attributeValue("function");
    String macro = executeElement.attributeValue("macro");
    String selenium = executeElement.attributeValue("selenium");
    String testCase = executeElement.attributeValue("test-case");
    String testClass = executeElement.attributeValue("test-class");
    String testSuite = executeElement.attributeValue("test-suite");

    if (action != null) {
      if (Validator.isNull(action) || !action.matches(allowedExecuteAttributeValuesRegex)) {

        throwValidationException(1006, fileName, executeElement, "action");
      }

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

        if (!attributeName.equals("action")
            && !attributeName.equals("line-number")
            && !attributeName.startsWith("locator")
            && !attributeName.startsWith("locator-key")
            && !attributeName.startsWith("value")) {

          throwValidationException(1005, fileName, executeElement, attributeName);
        }

        if (attributeName.equals("locator")
            || attributeName.equals("locator-key")
            || attributeName.equals("value")) {

          throwValidationException(1005, fileName, executeElement, attributeName);
        }
      }
    } else if (function != null) {
      if (Validator.isNull(function) || !function.matches(allowedExecuteAttributeValuesRegex)) {

        throwValidationException(1006, fileName, executeElement, "function");
      }

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

        if (!attributeName.equals("function")
            && !attributeName.equals("line-number")
            && !attributeName.startsWith("locator")
            && !attributeName.startsWith("value")) {

          throwValidationException(1005, fileName, executeElement, attributeName);
        }

        if (attributeName.equals("locator") || attributeName.equals("value")) {

          throwValidationException(1005, fileName, executeElement, attributeName);
        }
      }
    } else if (macro != null) {
      if (Validator.isNull(macro) || !macro.matches(allowedExecuteAttributeValuesRegex)) {

        throwValidationException(1006, fileName, executeElement, "macro");
      }

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

        if (!attributeName.equals("macro") && !attributeName.equals("line-number")) {

          throwValidationException(1005, fileName, executeElement, attributeName);
        }
      }
    } else if (selenium != null) {
      if (Validator.isNull(selenium) || !selenium.matches(allowedExecuteAttributeValuesRegex)) {

        throwValidationException(1006, fileName, executeElement, "selenium");
      }

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

        if (!attributeName.equals("argument1")
            && !attributeName.equals("argument2")
            && !attributeName.equals("line-number")
            && !attributeName.equals("selenium")) {

          throwValidationException(1005, fileName, executeElement, attributeName);
        }
      }
    } else if (testCase != null) {
      if (Validator.isNull(testCase) || !testCase.matches(allowedExecuteAttributeValuesRegex)) {

        throwValidationException(1006, fileName, executeElement, "test-case");
      }

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

        if (!attributeName.equals("line-number") && !attributeName.equals("test-case")) {

          throwValidationException(1005, fileName, executeElement, attributeName);
        }
      }
    } else if (testClass != null) {
      if (Validator.isNull(testClass) || !testClass.matches(allowedExecuteAttributeValuesRegex)) {

        throwValidationException(1006, fileName, executeElement, "test-class");
      }

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

        if (!attributeName.equals("line-number") && !attributeName.equals("test-class")) {

          throwValidationException(1005, fileName, executeElement, attributeName);
        }
      }
    } else if (testSuite != null) {
      if (Validator.isNull(testSuite) || !testSuite.matches(allowedExecuteAttributeValuesRegex)) {

        throwValidationException(1006, fileName, executeElement, "test-suite");
      }

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

        if (!attributeName.equals("line-number") && !attributeName.equals("test-suite")) {

          throwValidationException(1005, fileName, executeElement, attributeName);
        }
      }
    } else {
      throwValidationException(0, fileName);
    }

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

    if (allowedExecuteChildElementNames.length == 0) {
      if (!elements.isEmpty()) {
        Element element = elements.get(0);

        String elementName = element.getName();

        throwValidationException(1002, fileName, element, elementName);
      }
    } else {
      String executeElementName = executeElement.getName();

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

        if (executeElementName.equals("condition")) {
          throwValidationException(1002, fileName, element, elementName);
        }

        if (elementName.equals("var")) {
          validateVarElement(fileName, element);
        } else {
          throwValidationException(1002, fileName, element, elementName);
        }
      }
    }
  }
  protected void validateActionCommandElement(
      String fileName,
      Element commandElement,
      String[] allowedBlockChildElementNames,
      String[] allowedExecuteAttributeNames,
      String[] allowedExecuteChildElementNames) {

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

    if (elements.isEmpty()) {
      throwValidationException(1001, fileName, commandElement, new String[] {"case", "default"});
    }

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

      String elementName = element.getName();

      if (childElements.size() > 1) {
        throwValidationException(2000, fileName, childElements.get(1), elementName);
      }

      if (elementName.equals("case")) {
        List<Attribute> attributes = element.attributes();

        boolean hasNeededAttributeName = false;

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

          if (attributeName.equals("comparator")) {
            String attributeValue = attribute.getValue();

            if (!attributeValue.equals("contains")
                && !attributeValue.equals("endsWith")
                && !attributeValue.equals("equals")
                && !attributeValue.equals("startsWith")) {

              throwValidationException(1006, fileName, element, attributeName);
            }
          } else if (attributeName.startsWith("locator")
              || attributeName.startsWith("locator-key")) {

            String attributeValue = attribute.getValue();

            if (Validator.isNull(attributeValue)) {
              throwValidationException(1006, fileName, element, attributeName);
            }

            hasNeededAttributeName = true;
          }

          if (!attributeName.equals("comparator")
              && !attributeName.equals("line-number")
              && !attributeName.startsWith("locator")
              && !attributeName.startsWith("locator-key")) {

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

          if (attributeName.equals("locator") || attributeName.equals("locator-key")) {

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

        if (!hasNeededAttributeName) {
          throwValidationException(
              1004, fileName, element, new String[] {"locator1", "locator-key1"});
        }

        validateBlockElement(
            fileName,
            element,
            new String[] {"execute"},
            new String[] {"function"},
            new String[0],
            new String[0]);
      } else if (elementName.equals("default")) {
        List<Attribute> attributes = element.attributes();

        if (attributes.size() != 1) {
          Attribute attribute = attributes.get(1);

          String attributeName = attribute.getName();

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

        validateBlockElement(
            fileName,
            element,
            new String[] {"execute"},
            new String[] {"function"},
            new String[0],
            new String[0]);
      } else {
        throwValidationException(1002, fileName, element, elementName);
      }
    }
  }
  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);
    }
  }
Ejemplo n.º 9
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;
  }