Package org.mvel2.util

Examples of org.mvel2.util.StringAppender


  public CharSequence getCodeNearError() {
    return showCodeNearError(expr, cursor);
  }

  private String generateErrorMessage() {
    StringAppender appender = new StringAppender().append("[Error: " + super.getMessage() + "]\n");

    int offset = appender.length();

    appender.append("[Near : {... ");

    offset = appender.length() - offset;

    appender.append(showCodeNearError(expr, cursor))
        .append(" ....}]\n")
        .append(repeatChar(' ', offset));

    if (msgOffset < 0) msgOffset = 0;

    appender.append(repeatChar(' ', msgOffset)).append('^');

    calcRowAndColumn();

    if (lineNumber != -1) {
      appender.append('\n')
          .append("[Line: " + lineNumber + ", Column: " + (column) + "]");
    }
    return appender.toString();
  }
View Full Code Here


  }

  public char[] parse(char[] input) {
    setExpression(input);

    StringAppender appender = new StringAppender();

    int start;
    boolean macroArmed = true;
    String token;

    for (; cursor < length; cursor++) {
      start = cursor;
      while (cursor < length && isIdentifierPart(expr[cursor])) cursor++;
      if (cursor > start) {
        if (macros.containsKey(token = new String(expr, start, cursor - start)) && macroArmed) {
          appender.append(macros.get(token).doMacro());
        }
        else {
          appender.append(token);
        }
      }

      if (cursor < length) {
        switch (expr[cursor]) {
          case '\\':
            cursor++;
            break;
          case '/':
            start = cursor;

            if (cursor + 1 != length) {
              switch (expr[cursor + 1]) {
                case '/':
                  while (cursor != length && expr[cursor] != '\n') cursor++;
                  break;
                case '*':
                  int len = length - 1;
                  while (cursor != len && !(expr[cursor] == '*' && expr[cursor + 1] == '/')) cursor++;
                  cursor += 2;
                  break;
              }
            }

            if (cursor < length) cursor++;

            appender.append(new String(expr, start, cursor - start));

            if (cursor < length) cursor--;
            break;

          case '"':
          case '\'':
            appender.append(new String(expr, (start = cursor),
                (cursor = captureStringLiteral(expr[cursor], expr, cursor, length)) - start));

            if (cursor >= length) break;
            else if (isIdentifierPart(expr[cursor])) cursor--;

          default:
            switch (expr[cursor]) {
              case '.':
                macroArmed = false;
                break;
              case ';':
              case '{':
              case '(':
                macroArmed = true;
                break;
            }

            appender.append(expr[cursor]);
        }
      }
    }

    return appender.toChars();
  }
View Full Code Here

  public static void eval(String template, Object ctx, VariableResolverFactory vars, TemplateRegistry registry, TemplateOutputStream stream) {
    execute(compileTemplate(template), ctx, vars, registry, stream);
  }

  public static Object execute(CompiledTemplate compiled) {
    return execute(compiled.getRoot(), compiled.getTemplate(), new StringAppender(), null, new ImmutableDefaultFactory(), null);
  }
View Full Code Here

  public static void execute(CompiledTemplate compiled, OutputStream stream) {
    execute(compiled.getRoot(), compiled.getTemplate(), new StandardOutputStream(stream), null, new ImmutableDefaultFactory(), null);
  }

  public static Object execute(CompiledTemplate compiled, Object context) {
    return execute(compiled.getRoot(), compiled.getTemplate(), new StringAppender(), context, new ImmutableDefaultFactory(), null);
  }
View Full Code Here

     * If we have not cached the method then we need to go ahead and try to resolve it.
     */

    if ((m = getBestCandidate(args, name, ctx, ctx.getMethods(), pCtx.isStrongTyping())) == null) {
      if ((m = getBestCandidate(args, name, ctx, ctx.getDeclaredMethods(), pCtx.isStrongTyping())) == null) {
        StringAppender errorBuild = new StringAppender();
        for (int i = 0; i < args.length; i++) {
          errorBuild.append(args[i] != null ? args[i].getName() : null);
          if (i < args.length - 1) errorBuild.append(", ");
        }

        if (("size".equals(name) || "length".equals(name)) && args.length == 0 && ctx.isArray()) {
          return Integer.class;
        }

        if (pCtx.isStrictTypeEnforcement()) {
          throw new CompileException("unable to resolve method using strict-mode: "
              + ctx.getName() + "." + name + "(" + errorBuild.toString() + ")", expr, tkStart);
        }

        return Object.class;
      }
    }

    /**
     * If we're in strict mode, we look for generic type information.
     */
    if (pCtx.isStrictTypeEnforcement() && m.getGenericReturnType() != null) {
      Map<String, Class> typeArgs = new HashMap<String, Class>();

      Type[] gpt = m.getGenericParameterTypes();
      Class z;
      ParameterizedType pt;

      for (int i = 0; i < gpt.length; i++) {
        if (gpt[i] instanceof ParameterizedType) {
          pt = (ParameterizedType) gpt[i];
          if ((z = pCtx.getImport(new String(subtokens.get(i)))) != null) {
            /**
             * We record the value of the type parameter to our typeArgs Map.
             */
            if (pt.getRawType().equals(Class.class)) {
              /**
               * If this is an instance of Class, we deal with the special parameterization case.
               */
              typeArgs.put(pt.getActualTypeArguments()[0].toString(), z);
            }
            else {
              typeArgs.put(gpt[i].toString(), z);
            }
          }
        }
      }

      if (pCtx.isStrictTypeEnforcement() && ctx.getTypeParameters().length != 0 && pCtx.getLastTypeParameters() !=
          null && pCtx.getLastTypeParameters().length == ctx.getTypeParameters().length) {

        TypeVariable[] typeVariables = ctx.getTypeParameters();
        for (int i = 0; i < typeVariables.length; i++) {
          Type typeArg = pCtx.getLastTypeParameters()[i];
          typeArgs.put(typeVariables[i].getName(), typeArg instanceof Class ? (Class) pCtx.getLastTypeParameters()[i] : Object.class);
        }
      }

      /**
       * Get the return type argument
       */
      Type parametricReturnType = m.getGenericReturnType();
      String returnTypeArg = parametricReturnType.toString();

      //push return type parameters onto parser context, only if this is a parametric type
      if (parametricReturnType instanceof ParameterizedType) {
        pCtx.setLastTypeParameters(((ParameterizedType) parametricReturnType).getActualTypeArguments());
      }

      if (paramTypes != null && paramTypes.containsKey(returnTypeArg)) {
        /**
         * If the paramTypes Map contains the known type, return that type.
         */
        return (Class) paramTypes.get(returnTypeArg);
      }
      else if (typeArgs.containsKey(returnTypeArg)) {
        /**
         * If the generic type was declared as part of the method, it will be in this
         * Map.
         */
        return typeArgs.get(returnTypeArg);
      }
    }

    if (!Modifier.isPublic(m.getModifiers()) && pCtx.isStrictTypeEnforcement()) {
      StringAppender errorBuild = new StringAppender();
      for (int i = 0; i < args.length; i++) {
        errorBuild.append(args[i] != null ? args[i].getName() : null);
        if (i < args.length - 1) errorBuild.append(", ");
      }

      String scope = Modifier.toString(m.getModifiers());
      if (scope.trim().equals("")) scope = "<package local>";

      addFatalError("the referenced method is not accessible: "
          + ctx.getName() + "." + name + "(" + errorBuild.toString() + ")"
          + " (scope: " + scope + "; required: public", this.tkStart);
    }

    return getReturnType(ctx, m);
  }
View Full Code Here

        }
      }
    }

    if (m == null) {
      StringAppender errorBuild = new StringAppender();
      for (int i = 0; i < args.length; i++) {
        errorBuild.append(args[i] != null ? args[i].getClass().getName() : null);
        if (i < args.length - 1) errorBuild.append(", ");
      }

      if ("size".equals(name) && args.length == 0 && cls.isArray()) {
        return getLength(ctx);
      }

      System.out.println("{ " + new String(property) + " }");


      throw new PropertyAccessException("unable to resolve method: "
          + cls.getName() + "." + name + "(" + errorBuild.toString() + ") [arglength=" + args.length + "]"
          , property, st);
    }
    else {
      for (int i = 0; i < args.length; i++) {
        args[i] = convert(args[i], parameterTypes[i]);
View Full Code Here

     * If we have not cached the method then we need to go ahead and try to resolve it.
     */

    if ((m = getBestCandidate(args, name, ctx, ctx.getMethods(), pCtx.isStrongTyping())) == null) {
      if ((m = getBestCandidate(args, name, ctx, ctx.getDeclaredMethods(), pCtx.isStrongTyping())) == null) {
        StringAppender errorBuild = new StringAppender();
        for (int i = 0; i < args.length; i++) {
          errorBuild.append(args[i] != null ? args[i].getName() : null);
          if (i < args.length - 1) errorBuild.append(", ");
        }

        if (("size".equals(name) || "length".equals(name)) && args.length == 0 && ctx.isArray()) {
          return Integer.class;
        }

        if (pCtx.isStrictTypeEnforcement()) {
          throw new CompileException("unable to resolve method using strict-mode: "
              + ctx.getName() + "." + name + "(" + errorBuild.toString() + ")", expr, tkStart);
        }

        return Object.class;
      }
    }

    /**
     * If we're in strict mode, we look for generic type information.
     */
    if (pCtx.isStrictTypeEnforcement() && m.getGenericReturnType() != null) {
      Map<String, Class> typeArgs = new HashMap<String, Class>();

      Type[] gpt = m.getGenericParameterTypes();
      Class z;
      ParameterizedType pt;

      for (int i = 0; i < gpt.length; i++) {
        if (gpt[i] instanceof ParameterizedType) {
          pt = (ParameterizedType) gpt[i];
          if ((z = pCtx.getImport(new String(subtokens.get(i)))) != null) {
            /**
             * We record the value of the type parameter to our typeArgs Map.
             */
            if (pt.getRawType().equals(Class.class)) {
              /**
               * If this is an instance of Class, we deal with the special parameterization case.
               */
              typeArgs.put(pt.getActualTypeArguments()[0].toString(), z);
            }
            else {
              typeArgs.put(gpt[i].toString(), z);
            }
          }
        }
      }

      if (pCtx.isStrictTypeEnforcement() && ctx.getTypeParameters().length != 0 && pCtx.getLastTypeParameters() !=
              null && pCtx.getLastTypeParameters().length == ctx.getTypeParameters().length) {

        TypeVariable[] typeVariables = ctx.getTypeParameters();
        for (int i = 0; i < typeVariables.length; i++) {
          typeArgs.put(typeVariables[i].getName(), (Class) pCtx.getLastTypeParameters()[i]);
        }
      }

      /**
       * Get the return type argument
       */
      Type parametricReturnType = m.getGenericReturnType();
      String returnTypeArg = parametricReturnType.toString();

      //push return type parameters onto parser context, only if this is a parametric type
      if (parametricReturnType instanceof ParameterizedType) {
        pCtx.setLastTypeParameters(((ParameterizedType) parametricReturnType).getActualTypeArguments());
      }

      if (paramTypes != null && paramTypes.containsKey(returnTypeArg)) {
        /**
         * If the paramTypes Map contains the known type, return that type.
         */
        return (Class) paramTypes.get(returnTypeArg);
      }
      else if (typeArgs.containsKey(returnTypeArg)) {
        /**
         * If the generic type was declared as part of the method, it will be in this
         * Map.
         */
        return typeArgs.get(returnTypeArg);
      }
    }

    if (!Modifier.isPublic(m.getModifiers())) {
      StringAppender errorBuild = new StringAppender();
      for (int i = 0; i < args.length; i++) {
        errorBuild.append(args[i] != null ? args[i].getName() : null);
        if (i < args.length - 1) errorBuild.append(", ");
      }

      String scope = Modifier.toString(m.getModifiers());
      if (scope.trim().equals("")) scope = "<package local>";

      addFatalError("the referenced method is not accessible: "
          + ctx.getName() + "." + name + "(" + errorBuild.toString() + ")"
          + " (scope: " + scope + "; required: public", this.tkStart);
    }

    return m.getReturnType();
  }
View Full Code Here

  public static void eval(String template, Object ctx, VariableResolverFactory vars, TemplateRegistry registry, TemplateOutputStream stream) {
    execute(compileTemplate(template), ctx, vars, registry, stream);
  }

  public static Object execute(CompiledTemplate compiled) {
    return execute(compiled.getRoot(), compiled.getTemplate(), new StringAppender(), null, null, null);
  }
View Full Code Here

  public static void execute(CompiledTemplate compiled, OutputStream stream) {
    execute(compiled.getRoot(), compiled.getTemplate(), new StandardOutputStream(stream), null, null, null);
  }

  public static Object execute(CompiledTemplate compiled, Object context) {
    return execute(compiled.getRoot(), compiled.getTemplate(), new StringAppender(), context, null, null);
  }
View Full Code Here

    }

    public static String renderMessage(Object value) {

        if (value instanceof Message) {
            StringAppender appender = new StringAppender();
            Map<String, Object> vars = ((Message) value).getParts();

            boolean first = true;
            for (Map.Entry<String, Object> entry : vars.entrySet()) {
                if (first) {
                    first = false;
                }
                else {
                    appender.append(", ");
                }

                appender.append(entry.getKey()).append('=').append(entry.getValue());
            }

            return appender.toString();
        }
        return null;
    }
View Full Code Here

TOP

Related Classes of org.mvel2.util.StringAppender

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.