Example #1
0
 @Test
 public void testEscapeXml() {
   assertEquals("abcd", StringUtils.escapeXml("abcd"));
   assertEquals(
       "a<table border="0" color='red'>b<c</table>d",
       StringUtils.escapeXml("a<table border=\"0\" color=\'red\'>b&lt;c</table>d"));
 }
Example #2
0
 @Test
 public void testCompressBlank() {
   assertEquals("12345678", StringUtils.clearBlank("12345678"));
   assertEquals("1 2 3 4 5 6 7 8", StringUtils.compressBlank("1\t2\n3\r4\b5\f6 \t7 \t\n\r\b\f8"));
   assertEquals(
       " 1 2 3 4 5 6 7 8 ", StringUtils.compressBlank(" 1\t2\n3\r4\b5\f6 \t7 \t\n\r\b\f8 "));
 }
Example #3
0
 @Test
 public void testUnescapeString() {
   assertEquals("''\"", StringUtils.unescapeString("'\\'\\\""));
   assertEquals(
       "a\"b\"c\'d\'e\\1\t2\n3\r4\b5\f6",
       StringUtils.unescapeString("a\\\"b\\\"c\\\'d\\\'e\\\\1\\t2\\n3\\r4\\b5\\f6"));
 }
Example #4
0
 public Class<?> getReturnType() throws ParseException {
   if (getName().startsWith("new ")) {
     String type = getName().substring(4);
     return ClassUtils.forName(getPackages(), type);
   } else if (StringUtils.isTyped(getName())) {
     String type = getName();
     return ClassUtils.forName(getPackages(), type);
   } else if (StringUtils.isFunction(getName())) {
     String name = getName().substring(1);
     Class<?> t = getParameterTypes().get(name);
     if (t != null && Expression.class.isAssignableFrom(t)) {
       return Object.class;
     } else {
       name = ClassUtils.filterJavaKeyword(name);
       Class<?>[] types = parameter.getReturnTypes();
       Collection<Class<?>> functions = getFunctions();
       if (functions != null && functions.size() > 0) {
         for (Class<?> function : functions) {
           try {
             Method method = ClassUtils.searchMethod(function, name, types);
             if (!Object.class.equals(method.getDeclaringClass())) {
               return method.getReturnType();
             }
           } catch (NoSuchMethodException e) {
           }
         }
       }
       throw new ParseException(
           "No such macro \""
               + name
               + "\" or import method \""
               + name
               + "\" with parameters "
               + Arrays.toString(types),
           getOffset());
     }
   } else if (getName().equals("[")) {
     Class<?>[] types = parameter.getReturnTypes();
     if (Map.Entry.class.isAssignableFrom(types[0])) {
       return Map.class;
     } else {
       Object array = Array.newInstance(types[0], 0);
       return array.getClass();
     }
   } else if (getName().equals("!")) {
     return boolean.class;
   } else {
     return parameter.getReturnType();
   }
 }
Example #5
0
 @Test
 public void testClearBlankLine() {
   assertEquals("12345678", StringUtils.clearBlankLine("12345678"));
   assertEquals(
       StringUtils.escapeString("1\t2\n3\r4\b5\f6 \t7 \t\n\r\b\f8\n"),
       StringUtils.escapeString(
           StringUtils.clearBlankLine("1\t2\n3\r4\b5\f6 \t7 \t\n\r\b\f8\n\t\r\b\f\n")));
   assertEquals(
       StringUtils.escapeString("1\t2\n3\r4\b5\f6 \t7 \t\n\r\b\f8\n"),
       StringUtils.escapeString(
           StringUtils.clearBlankLine(
               "\n\n1\t2\n3\r4\b5\f6 \t7 \t\n\t\n\r\b\f8\n\t\r\b\f\n\t\n\n")));
   assertEquals(
       StringUtils.escapeString("1\t2\n3\r4\b5\f6 \t7 \t\n\r\b\f8\n"),
       StringUtils.escapeString(
           StringUtils.clearBlankLine(
               " \t\n\n1\t2\n3\r4\b5\f6 \t7 \t\n\t\n\r\b\f8\n\t\r\b\f\n\t\n\n \t")));
 }
Example #6
0
 private String findMessageByLocale(String key, Locale locale) {
   String file = messageBasename + (locale == null ? "" : "_" + locale) + messageSuffix;
   EncodingProperties properties = messageCache.get(file);
   if ((properties == null || reloadable) && engine.hasResource(file)) {
     if (properties == null) {
       properties = new EncodingProperties();
       EncodingProperties old = messageCache.putIfAbsent(file, properties);
       if (old != null) {
         properties = old;
       }
     }
     try {
       Resource resource = engine.getResource(file);
       if (properties.getLastModified() < resource.getLastModified()) {
         String encoding = (StringUtils.isEmpty(messageEncoding) ? "UTF-8" : messageEncoding);
         properties.load(resource.openStream(), encoding, resource.getLastModified());
       }
     } catch (IOException e) {
       if (logger != null && logger.isErrorEnabled()) {
         logger.error(
             "Failed to load httl message file "
                 + file
                 + " with locale "
                 + locale
                 + ", cause: "
                 + e.getMessage(),
             e);
       }
     }
   }
   if (properties != null) {
     String value = properties.getProperty(key);
     if (StringUtils.isNotEmpty(value)) {
       return value;
     }
   }
   if (locale != null) {
     return findMessageByLocale(key, LocaleUtils.getParentLocale(locale));
   }
   return null;
 }
Example #7
0
 public String message(String key, Locale locale, Object[] args) {
   if (StringUtils.isEmpty(key) || messageBasename == null) {
     return key;
   }
   if (locale == null) {
     locale = getLocale();
   }
   String value = findMessageByLocale(key, locale);
   if (StringUtils.isNotEmpty(value)) {
     if (args != null && args.length > 0) {
       if ("string".equals(messageFormat)) {
         return String.format(value, args);
       } else {
         return MessageFormat.format(value, args);
       }
     } else {
       return value;
     }
   }
   return key;
 }
 public String filter(String key, String value) {
   boolean left = false;
   boolean right = false;
   String trim = value.trim();
   if (trim.startsWith(commentRight)) {
     value = value.substring(value.indexOf(commentRight) + commentRight.length());
     left = true;
   }
   if (trim.endsWith(commentLeft)) {
     value = value.substring(0, value.lastIndexOf(commentLeft));
     right = true;
   }
   if (removeDirectiveBlankLine && (left || right)) {
     value = StringUtils.trimBlankLine(value, left, right);
   }
   return value;
 }
Example #9
0
  @Test
  public void testEscapeXmlPerformance() {
    String html = "a<table border=\"0\" color=\'red\'>b&lt;c</table>d";
    // char[] html = "a<table border=\"0\" color=\'red\'>b&lt;c</table>d".toCharArray();

    int count = profile ? 10 * 1000 * 1000 : 1000;
    long start = System.nanoTime();
    for (int i = 0; i < count; i++) {
      StringUtils.escapeXml(html);
    }
    long elapsed = System.nanoTime() - start;
    DecimalFormat format = new DecimalFormat("###,##0.###");
    System.out.println(
        "elapsed: "
            + format.format(elapsed)
            + "ns, tps: "
            + format.format(1000L * 1000L * 1000L * (long) count / elapsed));
  }
Example #10
0
 protected String format(Object value) {
   if (value == null) return nullValue;
   if (formatter != null) return formatter.format(value);
   return StringUtils.toString(value);
 }
Example #11
0
 public Class<?> getReturnType() throws ParseException {
   String name = getName();
   if (">".equals(name)
       || ">=".equals(name)
       || "<".equals(name)
       || "<=".equals(name)
       || "gt".equals(name)
       || "ge".equals(name)
       || "lt".equals(name)
       || "le".equals(name)
       || "==".equals(name)
       || "!=".equals(name)
       || "&&".equals(name)
       || "||".equals(name)) {
     return boolean.class;
   }
   Map<String, Class<?>> types = getParameterTypes();
   Class<?> leftType = leftParameter.getReturnType();
   if (StringUtils.isFunction(name)) {
     name = name.substring(1);
     if ("to".equals(name)
         && rightParameter instanceof Constant
         && rightParameter.getReturnType() == String.class) {
       String rightCode = rightParameter.getCode();
       if (rightCode.length() > 2 && rightCode.startsWith("\"") && rightCode.endsWith("\"")) {
         return ClassUtils.forName(getPackages(), rightCode.substring(1, rightCode.length() - 1));
       }
     } else if ("class".equals(name)) {
       return Class.class;
     }
     if (Map.Entry.class.isAssignableFrom(leftType)
         && ("key".equals(name) || "value".equals(name))) {
       String var = getGenericVariableName(leftParameter);
       if (var != null) {
         Class<?> keyType = types.get(var + ":0"); // Map<K,V>第一个泛型
         Class<?> valueType = types.get(var + ":1"); // Map<K,V>第二个泛型
         if ("key".equals(name) && keyType != null) {
           return keyType;
         } else if ("value".equals(name) && valueType != null) {
           return valueType;
         }
       }
     }
     Class<?> rightType = rightParameter.getReturnType();
     if (Map.class.isAssignableFrom(leftType)
         && "get".equals(name)
         && String.class.equals(rightType)) {
       String var = getGenericVariableName(leftParameter);
       if (var != null) {
         Class<?> type = types.get(var + ":1"); // Map<K,V>第二个泛型
         if (type != null) {
           return type;
         }
       }
     }
     if (List.class.isAssignableFrom(leftType)
         && "get".equals(name)
         && int.class.equals(rightType)) {
       String var = getGenericVariableName(leftParameter);
       if (var != null) {
         Class<?> type = types.get(var + ":0"); // List<T>第一个泛型
         if (type != null) {
           return type;
         }
       }
     }
     Class<?>[] rightTypes = rightParameter.getReturnTypes();
     Collection<Class<?>> functions = getFunctions();
     if (functions != null && functions.size() > 0) {
       Class<?>[] allTypes;
       if (rightTypes == null || rightTypes.length == 0) {
         if (leftType == null) {
           allTypes = new Class<?>[0];
         } else {
           allTypes = new Class<?>[] {leftType};
         }
       } else {
         allTypes = new Class<?>[rightTypes.length + 1];
         allTypes[0] = leftType;
         System.arraycopy(rightTypes, 0, allTypes, 1, rightTypes.length);
       }
       for (Class<?> function : functions) {
         try {
           Method method = ClassUtils.searchMethod(function, name, allTypes);
           if (Object.class.equals(method.getDeclaringClass())) {
             break;
           }
           return method.getReturnType();
         } catch (NoSuchMethodException e) {
         }
       }
     }
     return getReturnType(leftType, name, rightTypes);
   } else if ("..".equals(name)) {
     if (leftType == int.class
         || leftType == Integer.class
         || leftType == short.class
         || leftType == Short.class
         || leftType == long.class
         || leftType == Long.class) {
       return IntegerSequence.class;
     } else if (leftType == char.class || leftType == Character.class) {
       return CharacterSequence.class;
     } else if (leftType == String.class) {
       return StringSequence.class;
     } else {
       throw new ParseException(
           "The operator \"..\" unsupported parameter type " + leftType, getOffset());
     }
   } else if ("[".equals(name)) {
     if (Map.class.isAssignableFrom(leftType)) {
       String var = getGenericVariableName(leftParameter);
       if (var != null) {
         Class<?> t = types.get(var + ":1"); // Map<K,V>第二个泛型
         if (t != null) {
           return t;
         }
       }
       return Object.class;
     }
     Class<?> rightType = rightParameter.getReturnType();
     if (List.class.isAssignableFrom(leftType)) {
       if (IntegerSequence.class.equals(rightType) || int[].class == rightType) {
         return List.class;
       } else if (int.class.equals(rightType)) {
         String var = getGenericVariableName(leftParameter);
         if (var != null) {
           Class<?> t = types.get(var + ":0"); // List<T>第一个泛型
           if (t != null) {
             return t;
           }
         }
         return Object.class;
       } else {
         throw new ParseException(
             "The \"[]\" index type: " + rightType.getName() + " must be int!", getOffset());
       }
     } else if (leftType.isArray()) {
       if (IntegerSequence.class.equals(rightType) || int[].class == rightType) {
         return leftType;
       } else if (int.class.equals(rightType)) {
         return leftType.getComponentType();
       } else {
         throw new ParseException(
             "The \"[]\" index type: " + rightType.getName() + " must be int!", getOffset());
       }
     }
     throw new ParseException(
         "Unsuptorted \"[]\" for non-array type: " + leftType.getName(), getOffset());
   } else if ("?".equals(name)) {
     return rightParameter.getReturnType();
   } else if (":".equals(name)
       && !(leftParameter instanceof BinaryOperator
           && "?".equals(((BinaryOperator) leftParameter).getName()))) {
     return Map.Entry.class;
   } else if ("+".equals(name)) {
     Class<?> rightType = rightParameter.getReturnType();
     if ((Collection.class.isAssignableFrom(leftType) || leftType.isArray())
             && (Collection.class.isAssignableFrom(rightType) || rightType.isArray())
         || Map.class.isAssignableFrom(leftType) && Map.class.isAssignableFrom(rightType)) {
       return leftType;
     } else if (rightType.isPrimitive() && rightType != boolean.class) {
       return rightType;
     } else if (leftType.isPrimitive() && leftType != boolean.class) {
       return leftType;
     } else {
       return String.class;
     }
   } else if ("-".equals(name) || "*".equals(name) || "/".equals(name) || "%".equals(name)) {
     Class<?> rightType = rightParameter.getReturnType();
     if (rightType.isPrimitive() && rightType != boolean.class) {
       return rightType;
     } else if (leftType.isPrimitive() && leftType != boolean.class) {
       return leftType;
     } else {
       return int.class;
     }
   } else {
     return leftType;
   }
 }
Example #12
0
 public String getCode() throws ParseException {
   Class<?> leftType = leftParameter.getReturnType();
   String leftCode = leftParameter.getCode();
   if (leftParameter instanceof Operator
       && ((Operator) leftParameter).getPriority() < getPriority()) {
     leftCode = "(" + leftCode + ")";
   }
   String name = getName();
   Map<String, Class<?>> types = getParameterTypes();
   String rightCode = rightParameter.getCode();
   if (StringUtils.isFunction(name)) {
     name = name.substring(1);
     if ("to".equals(name)
         && rightParameter instanceof Constant
         && rightParameter.getReturnType() == String.class
         && rightCode.length() > 2
         && rightCode.startsWith("\"")
         && rightCode.endsWith("\"")) {
       return "((" + rightCode.substring(1, rightCode.length() - 1) + ")" + leftCode + ")";
     } else if ("class".equals(name)) {
       if (leftType.isPrimitive()) {
         return leftType.getCanonicalName() + ".class";
       } else {
         return getNotNullCode(leftCode, leftCode + ".getClass()");
       }
     }
     if (Map.Entry.class.isAssignableFrom(leftType)
         && ("key".equals(name) || "value".equals(name))) {
       String var = getGenericVariableName(leftParameter);
       if (var != null) {
         Class<?> keyType = types.get(var + ":0"); // Map<K,V>第一个泛型
         Class<?> valueType = types.get(var + ":1"); // Map<K,V>第二个泛型
         if ("key".equals(name) && keyType != null) {
           return getNotNullCode(
               leftCode,
               "((" + keyType.getCanonicalName() + ")" + leftCode + ".getKey(" + rightCode + "))");
         } else if ("value".equals(name) && valueType != null) {
           return getNotNullCode(
               leftCode,
               "(("
                   + valueType.getCanonicalName()
                   + ")"
                   + leftCode
                   + ".getValue("
                   + rightCode
                   + "))");
         }
       }
     }
     Class<?> rightType = rightParameter.getReturnType();
     if (Map.class.isAssignableFrom(leftType)
         && "get".equals(name)
         && String.class.equals(rightType)) {
       String var = getGenericVariableName(leftParameter);
       if (var != null) {
         Class<?> type = types.get(var + ":1"); // Map<K,V>第二个泛型
         if (type != null) {
           return getNotNullCode(
               leftCode,
               "((" + type.getCanonicalName() + ")" + leftCode + ".get(" + rightCode + "))");
         }
       }
     }
     if (List.class.isAssignableFrom(leftType)
         && "get".equals(name)
         && int.class.equals(rightType)) {
       String var = getGenericVariableName(leftParameter);
       if (var != null) {
         Class<?> type = types.get(var + ":0"); // List<T>第一个泛型
         if (type != null) {
           return getNotNullCode(
               leftCode,
               "((" + type.getCanonicalName() + ")" + leftCode + ".get(" + rightCode + "))");
         }
       }
     }
     Class<?>[] rightTypes = rightParameter.getReturnTypes();
     Collection<Class<?>> functions = getFunctions();
     if (functions != null && functions.size() > 0) {
       Class<?>[] allTypes;
       String allCode;
       if (rightTypes == null || rightTypes.length == 0) {
         allTypes = new Class<?>[] {leftType};
         allCode = leftCode;
       } else {
         allTypes = new Class<?>[rightTypes.length + 1];
         allTypes[0] = leftType;
         System.arraycopy(rightTypes, 0, allTypes, 1, rightTypes.length);
         allCode = leftCode + ", " + rightCode;
       }
       for (Class<?> function : functions) {
         try {
           Method method = ClassUtils.searchMethod(function, name, allTypes);
           if (!Object.class.equals(method.getDeclaringClass())) {
             if (Modifier.isStatic(method.getModifiers())) {
               return function.getName() + "." + method.getName() + "(" + allCode + ")";
             }
             return "_"
                 + function.getName().replace('.', '_')
                 + "."
                 + method.getName()
                 + "("
                 + allCode
                 + ")";
           }
         } catch (NoSuchMethodException e) {
         }
       }
     }
     return getNotNullCode(
         leftCode, getMethodName(leftType, name, rightTypes, leftCode, rightCode));
   } else if (name.equals("[")) {
     if (Map.class.isAssignableFrom(leftType)) {
       String var = getGenericVariableName(leftParameter);
       if (var != null) {
         Class<?> t = types.get(var + ":1"); // Map<K,V>第二个泛型
         if (t != null) {
           return getNotNullCode(
               leftCode,
               "((" + t.getCanonicalName() + ")" + leftCode + ".get(" + rightCode + "))");
         }
       }
       return getNotNullCode(leftCode, leftCode + ".get(" + rightCode + ")");
     }
     Class<?> rightType = rightParameter.getReturnType();
     if (List.class.isAssignableFrom(leftType)) {
       if (IntegerSequence.class.equals(rightType) || int[].class == rightType) {
         return ClassUtils.class.getName() + ".subList(" + leftCode + ", " + rightCode + ")";
       } else if (int.class.equals(rightType)) {
         if (leftParameter instanceof Variable) {
           String var = ((Variable) leftParameter).getName();
           Class<?> t = types.get(var + ":0"); // List<T>第一个泛型
           if (t != null) {
             return getNotNullCode(
                 leftCode,
                 "((" + t.getCanonicalName() + ")" + leftCode + ".get(" + rightCode + "))");
           }
         }
         return getNotNullCode(leftCode, leftCode + ".get(" + rightCode + ")");
       } else {
         throw new ParseException(
             "The \"[]\" index type: " + rightType + " must be int!", getOffset());
       }
     } else if (leftType.isArray()) {
       if (IntegerSequence.class.equals(rightType) || int[].class == rightType) {
         return ClassUtils.class.getName() + ".subArray(" + leftCode + ", " + rightCode + ")";
       } else if (int.class.equals(rightType)) {
         return getNotNullCode(leftCode, leftCode + "[" + rightCode + "]");
       } else {
         throw new ParseException(
             "The \"[]\" index type: " + rightType + " must be int!", getOffset());
       }
     }
     throw new ParseException("Unsuptorted \"[]\" for non-array type: " + leftType, getOffset());
   } else if (name.equals("..")) {
     if (leftType == int.class
         || leftType == Integer.class
         || leftType == short.class
         || leftType == Short.class
         || leftType == long.class
         || leftType == Long.class) {
       return "new " + IntegerSequence.class.getName() + "(" + leftCode + ", " + rightCode + ")";
     } else if (leftType == char.class || leftType == Character.class) {
       return "new " + CharacterSequence.class.getName() + "(" + leftCode + ", " + rightCode + ")";
     } else if (leftType == String.class
         && leftCode.length() >= 2
         && leftCode.startsWith("\"")
         && leftCode.endsWith("\"")
         && rightCode.length() >= 2
         && rightCode.startsWith("\"")
         && rightCode.endsWith("\"")) {
       StringBuilder buf = new StringBuilder();
       for (String s :
           getSequence(
               leftCode.substring(1, leftCode.length() - 1),
               rightCode.substring(1, rightCode.length() - 1))) {
         if (buf.length() > 0) {
           buf.append(",");
         }
         buf.append("\"");
         buf.append(s);
         buf.append("\"");
       }
       return "new String[] {" + buf.toString() + "}";
     } else {
       throw new ParseException(
           "The operator \"..\" unsupported parameter type " + leftType, getOffset());
     }
   } else if ("==".equals(name)
       && !"null".equals(leftCode)
       && !"null".equals(rightCode)
       && !leftType.isPrimitive()
       && !rightParameter.getReturnType().isPrimitive()) {
     return getNotNullCode(leftCode, leftCode + ".equals(" + rightCode + ")");
   } else if ("!=".equals(name)
       && !"null".equals(leftCode)
       && !"null".equals(rightCode)
       && !leftType.isPrimitive()
       && !rightParameter.getReturnType().isPrimitive()) {
     return getNotNullCode(leftCode, "(! " + leftCode + ".equals(" + rightCode + "))");
   } else if ("&&".equals(name) || "||".equals(name)) {
     if (rightParameter instanceof Operator
         && ((Operator) rightParameter).getPriority() < getPriority()) {
       rightCode = "(" + rightCode + ")";
     }
     leftCode = StringUtils.getConditionCode(leftType, leftCode);
     rightCode = StringUtils.getConditionCode(rightParameter.getReturnType(), rightCode);
     return leftCode + " " + name + " " + rightCode;
   } else if (name.equals("?")) {
     if (rightParameter instanceof Operator
         && ((Operator) rightParameter).getPriority() < getPriority()) {
       rightCode = "(" + rightCode + ")";
     }
     leftCode = StringUtils.getConditionCode(leftType, leftCode);
     return leftCode + " " + name + " " + rightCode;
   } else if ("|".equals(name)
       && !leftType.isPrimitive()
       && !Number.class.isAssignableFrom(leftType)
       && !Boolean.class.isAssignableFrom(leftType)) {
     return "("
         + ClassUtils.class.getName()
         + ".isNotEmpty("
         + leftParameter.getCode()
         + ") ? ("
         + leftCode
         + ") : ("
         + rightCode
         + "))";
   } else if (":".equals(name)
       && !(leftParameter instanceof BinaryOperator
           && "?".equals(((BinaryOperator) leftParameter).getName()))) {
     return "new " + MapEntry.class.getName() + "(" + leftCode + ", " + rightCode + ")";
   } else {
     if ("lt".equals(name)) {
       name = "<";
     } else if ("le".equals(name)) {
       name = "<=";
     } else if ("gt".equals(name)) {
       name = ">";
     } else if ("ge".equals(name)) {
       name = ">=";
     }
     if (leftType != null && Comparable.class.isAssignableFrom(leftType)) {
       if (">".equals(name)) {
         return getNotNullCode(leftCode, leftCode + ".compareTo(" + rightCode + ") > 0");
       } else if (">=".equals(name)) {
         return getNotNullCode(leftCode, leftCode + ".compareTo(" + rightCode + ") >= 0");
       } else if ("<".equals(name)) {
         return getNotNullCode(leftCode, leftCode + ".compareTo(" + rightCode + ") < 0");
       } else if ("<=".equals(name)) {
         return getNotNullCode(leftCode, leftCode + ".compareTo(" + rightCode + ") <= 0");
       }
     }
     if ("+".equals(name)) {
       Class<?> rightType = rightParameter.getReturnType();
       if ((Collection.class.isAssignableFrom(leftType) || leftType.isArray())
               && (Collection.class.isAssignableFrom(rightType) || rightType.isArray())
           || Map.class.isAssignableFrom(leftType) && Map.class.isAssignableFrom(rightType)) {
         return CollectionUtils.class.getName() + ".merge(" + leftCode + ", " + rightCode + ")";
       }
       Class<?> type;
       if (rightType.isPrimitive() && rightType != boolean.class) {
         type = rightType;
       } else if (leftType.isPrimitive() && leftType != boolean.class) {
         type = leftType;
       } else {
         type = String.class;
       }
       if (type != String.class) {
         String typeName = type.getCanonicalName();
         typeName = typeName.substring(0, 1).toUpperCase() + typeName.substring(1);
         if (!(leftType.isPrimitive() && leftType != boolean.class)) {
           leftCode = DefaultMethod.class.getName() + ".to" + typeName + "(" + leftCode + ")";
         }
         if (!(rightType.isPrimitive() && leftType != boolean.class)) {
           rightCode = DefaultMethod.class.getName() + ".to" + typeName + "(" + rightCode + ")";
         }
       }
     } else if ("-".equals(name) || "*".equals(name) || "/".equals(name) || "%".equals(name)) {
       Class<?> rightType = rightParameter.getReturnType();
       Class<?> type;
       if (rightType.isPrimitive() && rightType != boolean.class) {
         type = rightType;
       } else if (leftType.isPrimitive() && leftType != boolean.class) {
         type = leftType;
       } else {
         type = int.class;
       }
       String typeName = type.getCanonicalName();
       typeName = typeName.substring(0, 1).toUpperCase() + typeName.substring(1);
       if (!(leftType.isPrimitive() && leftType != boolean.class)) {
         leftCode = DefaultMethod.class.getName() + ".to" + typeName + "(" + leftCode + ")";
       }
       if (!(rightType.isPrimitive() && leftType != boolean.class)) {
         rightCode = DefaultMethod.class.getName() + ".to" + typeName + "(" + rightCode + ")";
       }
     }
     if (rightParameter instanceof Operator
         && ((Operator) rightParameter).getPriority() < getPriority()) {
       rightCode = "(" + rightCode + ")";
     }
     return leftCode + " " + name + " " + rightCode;
   }
 }
Example #13
0
 public byte[] filter(String key, byte[] value) {
   return StringUtils.escapeXml(value);
 }
Example #14
0
 public char[] filter(String key, char[] value) {
   return StringUtils.escapeXml(value);
 }
Example #15
0
 public String filter(String key, String value) {
   return StringUtils.escapeXml(value);
 }
Example #16
0
 @Test
 public void escapeXmlBytes() {
   assertEquals("中&lt;文&gt;字&quot;符", new String(StringUtils.escapeXml("中<文>字\"符".getBytes())));
 }
Example #17
0
  @Test
  public void testTrimBlankLine() {
    assertEquals("12345678", StringUtils.trimBlankLine("12345678"));
    assertEquals("", StringUtils.escapeString(StringUtils.trimBlankLine("\t\r\b\f ")));
    assertEquals("", StringUtils.escapeString(StringUtils.trimBlankLine("\n\t\b\f ")));
    assertEquals("", StringUtils.escapeString(StringUtils.trimBlankLine("\t\b\f \n")));
    assertEquals(
        StringUtils.escapeString("\t\r"),
        StringUtils.escapeString(StringUtils.trimBlankLine("\n\t\r\b\f ")));
    assertEquals(
        StringUtils.escapeString("\b\f \n"),
        StringUtils.escapeString(StringUtils.trimBlankLine("\t\r\b\f \n")));
    assertEquals(
        StringUtils.escapeString("\b\f \n\t\n"),
        StringUtils.escapeString(StringUtils.trimBlankLine("\t\r\b\f \n\t\n")));
    assertEquals(
        StringUtils.escapeString("1\t2\n3\r4\b5\f6 \t7 \t\n\r\b\f8\n\t\r\b\f\n"),
        StringUtils.escapeString(
            StringUtils.trimBlankLine("1\t2\n3\r4\b5\f6 \t7 \t\n\r\b\f8\n\t\r\b\f\n")));
    assertEquals(
        StringUtils.escapeString("\b\f\n1\t2\n3\r4\b5\f6 \t7 \t\n\t\n\r\b\f8\n\t\r"),
        StringUtils.escapeString(
            StringUtils.trimBlankLine(
                " \t\r\b\f\n1\t2\n3\r4\b5\f6 \t7 \t\n\t\n\r\b\f8\n\t\r\b\f ")));
    assertEquals(
        StringUtils.escapeString("\t\r\b\f\n1\t2\n3\r4\b5\f6 \t7 \t\n\t\n\r\b\f8\n\t\r\b\f\n"),
        StringUtils.escapeString(
            StringUtils.trimBlankLine(
                " \t\n\t\r\b\f\n1\t2\n3\r4\b5\f6 \t7 \t\n\t\n\r\b\f8\n\t\r\b\f\n\t ")));

    // LF
    assertEquals("", StringUtils.trimBlankLine("\n"));
    assertEquals("\n", StringUtils.trimBlankLine("\n\n"));

    // CRLF
    assertEquals("", StringUtils.trimBlankLine("\r\n"));
    assertEquals("\r\n", StringUtils.trimBlankLine("\r\n\r\n"));

    // CR
    assertEquals("", StringUtils.trimBlankLine("\r"));
    assertEquals("\r", StringUtils.trimBlankLine("\r\r"));

    // Mix
    assertEquals("\r\n", StringUtils.trimBlankLine("\r\r\n"));

    // with blanks
    assertEquals("", StringUtils.trimBlankLine(" \t  \n    \t      "));
    assertEquals("\n", StringUtils.trimBlankLine("  \t  \n\n   \t  "));
  }
Example #18
0
 public String getCode() throws ParseException {
   Class<?>[] types = parameter.getReturnTypes();
   if (getName().startsWith("new ")) {
     return getName() + "(" + parameter.getCode() + ")";
   } else if (StringUtils.isTyped(getName())) {
     return "(" + getName() + ")(" + parameter.getCode() + ")";
   } else if (StringUtils.isFunction(getName())) {
     String name = getName().substring(1);
     Class<?> t = getParameterTypes().get(name);
     if (t != null && Expression.class.isAssignableFrom(t)) {
       return "("
           + name
           + " == null ? null : "
           + name
           + ".evaluate("
           + ClassUtils.class.getName()
           + ".toMap("
           + name
           + ".getParameterTypes().keySet(), new Object"
           + (parameter.getCode().length() == 0 ? "[0]" : "[] { " + parameter.getCode() + " }")
           + " )))";
     } else {
       name = ClassUtils.filterJavaKeyword(name);
       Collection<Class<?>> functions = getFunctions();
       if (functions != null && functions.size() > 0) {
         for (Class<?> function : functions) {
           try {
             Method method = ClassUtils.searchMethod(function, name, types);
             if (Object.class.equals(method.getDeclaringClass())) {
               break;
             }
             if (Modifier.isStatic(method.getModifiers())) {
               return function.getName()
                   + "."
                   + method.getName()
                   + "("
                   + parameter.getCode()
                   + ")";
             }
             return "_"
                 + function.getName().replace('.', '_')
                 + "."
                 + method.getName()
                 + "("
                 + parameter.getCode()
                 + ")";
           } catch (NoSuchMethodException e) {
           }
         }
       }
       throw new ParseException(
           "No such macro \""
               + name
               + "\" or import method \""
               + name
               + "\" with parameters "
               + Arrays.toString(types),
           getOffset());
     }
   } else if (getName().equals("[")) {
     if (Map.Entry.class.isAssignableFrom(types[0])) {
       return ClassUtils.class.getName()
           + ".toMap(new "
           + Map.Entry.class.getCanonicalName()
           + "[] {"
           + parameter.getCode()
           + "})";
     } else {
       return "new " + types[0].getCanonicalName() + "[] {" + parameter.getCode() + "}";
     }
   } else if (getName().equals("!") && !boolean.class.equals(types[0])) {
     return "! (" + StringUtils.getConditionCode(types[0], parameter.getCode()) + ")";
   } else {
     if (parameter instanceof Operator && ((Operator) parameter).getPriority() < getPriority()) {
       return getName() + " (" + parameter.getCode() + ")";
     }
     return getName() + " " + parameter.getCode();
   }
 }