/** {@inheritDoc} */
  @Override
  protected List<Node> getMarkupSubstitutes(final Arguments arguments, final Element element) {
    final List<Node> nodes = new ArrayList<>();

    final Configuration configuration = arguments.getConfiguration();

    // Obtain the attribute value
    final IWebContext context = (IWebContext) arguments.getContext();
    final String contextPath =
        StringUtils.defaultIfEmpty(
            element.getAttributeValue(SpringHrefAttrProcessor.ATTR_NAME),
            context.getServletContext().getContextPath());

    // Obtain the Thymeleaf Standard Expression parser
    final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);
    // Parse the attribute value as a Thymeleaf Standard Expression
    final String pageAttrValue = element.getAttributeValue(ATTR_PAGE);
    final IStandardExpression expression =
        parser.parseExpression(configuration, arguments, pageAttrValue);
    Page<?> page = (Page<?>) expression.execute(configuration, arguments);

    final Element container = new Element("ul");
    container.setAttribute("class", "pagination");

    container.addChild(getFirstElement(page, contextPath));
    container.addChild(getPreviousElement(page, contextPath));
    addStepElements(page, contextPath, container);
    container.addChild(getNextElement(page, contextPath));
    container.addChild(getLastElement(page, contextPath));

    nodes.add(container);

    return nodes;
  }
  FragmentSelection parseFragmentSelection(
      final Arguments arguments, final String input, final boolean preprocess) {

    final String trimmedInput = input.trim();

    final String preprocessedInput =
        (preprocess ? preprocess(arguments, trimmedInput) : trimmedInput);

    final FragmentSelection cachedFragmentSelection =
        CACHE.getFragmentSelectionFromCache(arguments.getConfiguration(), preprocessedInput);
    if (cachedFragmentSelection != null) {
      return cachedFragmentSelection;
    }

    final FragmentSelection fragmentSelection = FragmentSelection.parse(preprocessedInput);

    if (fragmentSelection == null) {
      throw new TemplateProcessingException(
          "Could not parse as fragment selection: \"" + input + "\"");
    }

    CACHE.putFragmentSelectionIntoCache(
        arguments.getConfiguration(), preprocessedInput, fragmentSelection);

    return fragmentSelection;
  }
  ExpressionSequence parseExpressionSequence(
      final Arguments arguments, final String input, final boolean preprocess) {

    final String trimmedInput = input.trim();

    final String preprocessedInput =
        (preprocess ? preprocess(arguments, trimmedInput) : trimmedInput);

    final ExpressionSequence cachedExpressionSequence =
        CACHE.getExpressionSequenceFromCache(arguments.getConfiguration(), preprocessedInput);
    if (cachedExpressionSequence != null) {
      return cachedExpressionSequence;
    }

    final ExpressionSequence expressionSequence = ExpressionSequence.parse(preprocessedInput);

    if (expressionSequence == null) {
      throw new TemplateProcessingException(
          "Could not parse as expression sequence: \"" + input + "\"");
    }

    CACHE.putExpressionSequenceIntoCache(
        arguments.getConfiguration(), preprocessedInput, expressionSequence);

    return expressionSequence;
  }
  AssignationSequence parseAssignationSequence(
      final Arguments arguments,
      final String input,
      final boolean preprocess,
      final boolean allowParametersWithoutValue) {

    final String trimmedInput = input.trim();

    final String preprocessedInput =
        (preprocess ? preprocess(arguments, trimmedInput) : trimmedInput);

    final AssignationSequence cachedAssignationSequence =
        CACHE.getAssignationSequenceFromCache(arguments.getConfiguration(), preprocessedInput);
    if (cachedAssignationSequence != null) {
      return cachedAssignationSequence;
    }

    final AssignationSequence assignationSequence =
        AssignationSequence.parse(preprocessedInput, allowParametersWithoutValue);

    if (assignationSequence == null) {
      throw new TemplateProcessingException(
          "Could not parse as assignation sequence: \"" + input + "\"");
    }

    CACHE.putAssignationSequenceIntoCache(
        arguments.getConfiguration(), preprocessedInput, assignationSequence);

    return assignationSequence;
  }
  Each parseEach(final Arguments arguments, final String input, final boolean preprocess) {

    final String trimmedInput = input.trim();

    final String preprocessedInput =
        (preprocess ? preprocess(arguments, trimmedInput) : trimmedInput);

    final Each cachedEach = CACHE.getEachFromCache(arguments.getConfiguration(), preprocessedInput);
    if (cachedEach != null) {
      return cachedEach;
    }

    final Each each = Each.parse(preprocessedInput);

    if (each == null) {
      throw new TemplateProcessingException("Could not parse as each: \"" + input + "\"");
    }

    CACHE.putEachIntoCache(arguments.getConfiguration(), preprocessedInput, each);

    return each;
  }
Esempio n. 6
0
  /**
   * Parses the playframework action expressions. The string inside "()" is evaluated by OGNL in the
   * current context.
   *
   * @param arguments
   * @param attributeValue e.g. "Application.show(obj.id)"
   * @return parsed action path
   */
  @SuppressWarnings("unchecked")
  static String toActionString(final Arguments arguments, String attributeValue) {
    Matcher matcher = PARAM_PATTERN.matcher(attributeValue);
    if (!matcher.matches()) {
      return Router.reverse(attributeValue).toString();
    }

    String exp = matcher.group(1);
    if (StringUtils.isBlank(exp)) {
      return Router.reverse(attributeValue).toString();
    }

    Object obj =
        PlayOgnlVariableExpressionEvaluator.INSTANCE.evaluate(
            arguments.getConfiguration(), arguments, exp, false);
    if (obj instanceof Map) {
      return Router.reverse(attributeValue, (Map<String, Object>) obj).toString();
    }

    List<?> list = obj instanceof List ? (List<?>) obj : Arrays.asList(obj);

    Map<String, Object> paramMap = new HashMap<String, Object>();

    String extracted = StringUtils.substringBefore(attributeValue, "(");
    if (!extracted.contains(".")) {
      extracted = Request.current().controller + "." + extracted;
    }
    Object[] actionMethods = ActionInvoker.getActionMethod(extracted);
    String[] paramNames = null;
    try {
      paramNames = Java.parameterNames((Method) actionMethods[1]);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    if (paramNames.length < list.size()) {
      Logger.warn("param length unmatched. %s", Arrays.toString(paramNames));
      throw new ActionNotFoundException(attributeValue, null);
    }

    for (int i = 0; i < list.size(); i++) {
      paramMap.put(paramNames[i], list.get(i));
    }

    return Router.reverse(extracted, paramMap).toString();
  }