Exemplo n.º 1
0
  public static boolean isCastableTo(short type, String strType, Object o) {
    switch (type) {
      case CFTypes.TYPE_ANY:
        return true;
      case CFTypes.TYPE_STRING:
        return isCastableToString(o);
      case CFTypes.TYPE_BOOLEAN:
        return isCastableToBoolean(o);
      case CFTypes.TYPE_NUMERIC:
        return isCastableToNumeric(o);
      case CFTypes.TYPE_STRUCT:
        return isCastableToStruct(o);
      case CFTypes.TYPE_ARRAY:
        return isCastableToArray(o);
      case CFTypes.TYPE_QUERY:
        return isQuery(o);
      case CFTypes.TYPE_DATETIME:
        return isDateAdvanced(o, true);
      case CFTypes.TYPE_VOID:
        return isVoid(o); // Caster.toVoid(o,Boolean.TRUE)!=Boolean.TRUE;
      case CFTypes.TYPE_BINARY:
        return isCastableToBinary(o, true);
      case CFTypes.TYPE_TIMESPAN:
        return Caster.toTimespan(o, null) != null;
      case CFTypes.TYPE_UUID:
        return isUUId(o);
      case CFTypes.TYPE_GUID:
        return isGUId(o);
      case CFTypes.TYPE_VARIABLE_NAME:
        return isVariableName(o);
      case CFTypes.TYPE_XML:
        return isXML(o);
    }

    if (o instanceof Component) {
      Component comp = ((Component) o);
      return comp.instanceOf(strType);
    }
    if (isArrayType(strType) && isArray(o)) {
      String t = strType.substring(0, strType.length() - 2);
      Array arr = Caster.toArray(o, null);
      if (arr != null) {
        Iterator it = arr.valueIterator();
        while (it.hasNext()) {
          if (!isCastableTo(type, t, it.next())) return false;
        }
        return true;
      }
    }

    return false;
  }
Exemplo n.º 2
0
 public static boolean isInteger(Object value, boolean alsoBooleans) {
   if (!alsoBooleans && value instanceof Boolean) return false;
   double dbl = Caster.toDoubleValue(value, Double.NaN);
   if (!Decision.isValid(dbl)) return false;
   int i = (int) dbl;
   return i == dbl;
 }
Exemplo n.º 3
0
  /**
   * returns if given object is a zip code
   *
   * @param value
   * @return
   */
  public static boolean isZipCode(Object value) {
    String str = Caster.toString(value, null);
    if (str == null) return false;

    if (zipPattern == null)
      zipPattern = Pattern.compile("([0-9]{5,5})|([0-9]{5,5}[- ]{1}[0-9]{4,4})");
    return zipPattern.matcher(str.trim()).matches();
  }
Exemplo n.º 4
0
  /**
   * returns if given object is a social security number (usa)
   *
   * @param value
   * @return
   */
  public static boolean isSSN(Object value) {
    String str = Caster.toString(value, null);
    if (str == null) return false;

    if (ssnPattern == null)
      ssnPattern = Pattern.compile("^[0-9]{3}[-|]{1}[0-9]{2}[-|]{1}[0-9]{4}$");

    return ssnPattern.matcher(str.trim()).matches();
  }
Exemplo n.º 5
0
  /**
   * returns if given object is a phone
   *
   * @param value
   * @return
   */
  public static boolean isPhone(Object value) {
    String str = Caster.toString(value, null);
    if (str == null) return false;

    if (phonePattern == null)
      phonePattern =
          Pattern.compile(
              "^(\\+?1?[ \\-\\.]?([\\(]?([1-9][0-9]{2})[\\)]?))?[ ,\\-,\\.]?([^0-1]){1}([0-9]){2}[ ,\\-,\\.]?([0-9]){4}(( )((x){0,1}([0-9]){1,5}){0,1})?$");
    return phonePattern.matcher(str.trim()).matches();
  }
Exemplo n.º 6
0
  /**
   * returns if given object is a URL
   *
   * @param value
   * @return
   */
  public static boolean isURL(Object value) {
    String str = Caster.toString(value, null);
    if (str == null) return false;

    if (urlPattern == null)
      urlPattern =
          Pattern.compile(
              "^((http|https|ftp|file)\\:\\/\\/([a-zA-Z0-0]*:[a-zA-Z0-0]*(@))?[a-zA-Z0-9-\\.]+(\\.[a-zA-Z]{2,3})?(:[a-zA-Z0-9]*)?\\/?([a-zA-Z0-9-\\._\\? \\,\\'\\/\\+&%\\$#\\=~])*)|((mailto)\\:[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\\.)+[a-zA-Z0-9]{2,7})|((news)\\: [a-zA-Z0-9\\.]*)$");
    return urlPattern.matcher(str.trim()).matches();
  }
Exemplo n.º 7
0
  /**
   * tests if object is catable to a binary
   *
   * @param object
   * @return boolean
   */
  public static boolean isCastableToBinary(Object object, boolean checkBase64String) {
    if (isBinary(object)) return true;
    if (object instanceof InputStream) return true;
    if (object instanceof ByteArrayOutputStream) return true;
    if (object instanceof Blob) return true;

    // Base64 String
    if (!checkBase64String) return false;
    String str = Caster.toString(object, null);
    if (str == null) return false;
    return Base64Util.isBase64(str);
  }
Exemplo n.º 8
0
  /**
   * returns if given object is a email
   *
   * @param value
   * @return
   */
  public static boolean isEmail(Object value) {
    String str = Caster.toString(value, null);
    if (str == null) return false;

    if (emailPattern == null) {
      String prefix = "\\%\\+a-zA-Z_0-9-'~";
      emailPattern =
          Pattern.compile(
              "^[" + prefix + "]+(\\.[" + prefix + "]+)*@([a-zA-Z_0-9-]+\\.)+[a-zA-Z]{2,7}$");
    }
    return emailPattern.matcher(str).matches();
  }
Exemplo n.º 9
0
  public static boolean isGUIdSimple(Object obj) {
    String str = Caster.toString(obj, null);
    if (str == null) return false;

    // GUID
    if (str.length() == 36) {
      return str.charAt(8) == '-'
          && str.charAt(13) == '-'
          && str.charAt(18) == '-'
          && str.charAt(23) == '-';
    }
    return false;
  }
Exemplo n.º 10
0
  private static boolean isUSorEuroDateEuro(String str, boolean isEuro) {
    if (StringUtil.isEmpty(str)) return false;

    for (int i = 0; i < DATE_DEL.length; i++) {
      Array arr = railo.runtime.type.List.listToArrayRemoveEmpty(str, DATE_DEL[i]);
      if (arr.size() != 3) continue;

      int month =
          Caster.toIntValue(arr.get(isEuro ? 2 : 1, Constants.INTEGER_0), Integer.MIN_VALUE);
      int day = Caster.toIntValue(arr.get(isEuro ? 1 : 2, Constants.INTEGER_0), Integer.MIN_VALUE);
      int year = Caster.toIntValue(arr.get(3, Constants.INTEGER_0), Integer.MIN_VALUE);

      if (month == Integer.MIN_VALUE) continue;
      if (month > 12) continue;
      if (day == Integer.MIN_VALUE) continue;
      if (day > 31) continue;
      if (year == Integer.MIN_VALUE) continue;
      if (DateTimeUtil.getInstance().toTime(null, year, month, day, 0, 0, 0, 0, Long.MIN_VALUE)
          == Long.MIN_VALUE) continue;
      return true;
    }
    return false;
  }
Exemplo n.º 11
0
  /**
   * tests if object is a WDDX Object
   *
   * @param o Object to check
   * @return boolean
   */
  public static boolean isWddx(Object o) {
    if (!(o instanceof String)) return false;
    String str = o.toString();
    if (!(str.indexOf("wddxPacket") > 0)) return false;

    // wrong timezone but this isent importend because date will not be used
    WDDXConverter converter = new WDDXConverter(TimeZone.getDefault(), false);
    try {
      converter.deserialize(Caster.toString(o), true);
    } catch (Exception e) {
      return false;
    }
    return true;
  }
Exemplo n.º 12
0
  /**
   * tests if String value is UUID Value
   *
   * @param str value to test
   * @return is value numeric
   */
  public static boolean isUUId(Object obj) {
    String str = Caster.toString(obj, null);
    if (str == null) return false;

    if (str.length() == 35) {
      return Decision.isHex(str.substring(0, 8))
          && str.charAt(8) == '-'
          && Decision.isHex(str.substring(9, 13))
          && str.charAt(13) == '-'
          && Decision.isHex(str.substring(14, 18))
          && str.charAt(18) == '-'
          && Decision.isHex(str.substring(19));
    }
    return false;
  }
Exemplo n.º 13
0
 /**
  * can this type be casted to a array
  *
  * @param o
  * @return
  * @throws PageException
  */
 public static boolean isCastableToArray(Object o) {
   if (isArray(o)) return true;
   // else if(o instanceof XMLStruct) return true;
   else if (o instanceof Struct) {
     Struct sct = (Struct) o;
     Collection.Key[] keys = sct.keys();
     try {
       for (int i = 0; i < keys.length; i++) Caster.toIntValue(keys[i].toString());
       return true;
     } catch (Exception e) {
       return false;
     }
   }
   return false;
 }
Exemplo n.º 14
0
  public static boolean isCastableTo(String type, Object o, boolean alsoPattern) {

    type = StringUtil.toLowerCase(type).trim();
    if (type.length() > 2) {
      char first = type.charAt(0);
      switch (first) {
        case 'a':
          if (type.equals("any")) {
            return true;
          } else if (type.equals("array")) {
            return isCastableToArray(o);
          }
          break;
        case 'b':
          if (type.equals("boolean") || type.equals("bool")) {
            return isCastableToBoolean(o);
          } else if (type.equals("binary")) {
            return isCastableToBinary(o, true);
          } else if (type.equals("base64")) {
            return Caster.toBase64(o, null) != null;
          }
          break;
        case 'c':
          if (alsoPattern && type.equals("creditcard")) {
            return Caster.toCreditCard(o, null) != null;
          }
          break;
        case 'd':
          if (type.equals("date")) {
            return isDateAdvanced(o, true);
          } else if (type.equals("datetime")) {
            return isDateAdvanced(o, true);
          } else if (type.equals("double")) {
            return isCastableToNumeric(o);
          } else if (type.equals("decimal")) {
            return Caster.toDecimal(o, null) != null;
          }
          break;
        case 'e':
          if (type.equals("eurodate")) {
            return isDateAdvanced(o, true);
          } else if (alsoPattern && type.equals("email")) {
            return Caster.toEmail(o, null) != null;
          }
          break;
        case 'f':
          if (type.equals("float")) {
            return isCastableToNumeric(o);
          }
          break;
        case 'g':
          if (type.equals("guid")) {
            return isGUId(o);
          }
          break;
        case 'i':
          if (type.equals("integer") || type.equals("int")) {
            return isCastableToNumeric(o);
          }
          break;
        case 'l':
          if (type.equals("long")) {
            return isCastableToNumeric(o);
          }
          break;
        case 'n':
          if (type.equals("numeric")) {
            return isCastableToNumeric(o);
          } else if (type.equals("number")) {
            return isCastableToNumeric(o);
          }
          break;
        case 'o':
          if (type.equals("object")) {
            return true;
          } else if (type.equals("other")) {
            return true;
          }
          break;
        case 'p':
          if (alsoPattern && type.equals("phone")) {
            return Caster.toPhone(o, null) != null;
          }
          break;
        case 'q':
          if (type.equals("query")) {
            return isQuery(o);
          }
          break;
        case 's':
          if (type.equals("string")) {
            return isCastableToString(o);
          } else if (type.equals("struct")) {
            return isCastableToStruct(o);
          } else if (type.equals("short")) {
            return isCastableToNumeric(o);
          } else if (alsoPattern && (type.equals("ssn") || type.equals("social_security_number"))) {
            return Caster.toSSN(o, null) != null;
          }
          break;
        case 't':
          if (type.equals("timespan")) {
            return Caster.toTimespan(o, null) != null;
          }
          if (type.equals("time")) {
            return isDateAdvanced(o, true);
          }
          if (alsoPattern && type.equals("telephone")) {
            return Caster.toPhone(o, null) != null;
          }
        case 'u':
          if (type.equals("uuid")) {
            return isUUId(o);
          }
          if (type.equals("usdate")) {
            return isDateAdvanced(o, true);
            // return DateCaster.toDate(o,pc.getTimeZone());
          }
          if (alsoPattern && type.equals("url")) {
            return Caster.toURL(o, null) != null;
          }
          break;
        case 'v':
          if (type.equals("variablename")) {
            return isVariableName(o);
          } else if (type.equals("void")) {
            return isVoid(o); // Caster.toVoid(o,Boolean.TRUE)!=Boolean.TRUE;
          } else if (type.equals("variable_name")) {
            return isVariableName(o);
          } else if (type.equals("variable-name")) {
            return isVariableName(o);
          }
          break;
        case 'x':
          if (type.equals("xml")) {
            return isXML(o);
          }
          break;
        case 'z':
          if (alsoPattern && (type.equals("zip") || type.equals("zipcode"))) {
            return Caster.toZip(o, null) != null;
          }
          break;
      }
    }
    if (o instanceof Component) {
      Component comp = ((Component) o);
      return comp.instanceOf(type);
    }
    if (isArrayType(type) && isArray(o)) {
      String t = type.substring(0, type.length() - 2);
      Array arr = Caster.toArray(o, null);
      if (arr != null) {
        Iterator it = arr.valueIterator();
        while (it.hasNext()) {
          if (!isCastableTo(t, it.next(), alsoPattern)) return false;
        }
        return true;
      }
    }
    return false;
  }
Exemplo n.º 15
0
 public static boolean isValid(String type, Object value) throws ExpressionException {
   type = StringUtil.toLowerCase(type.trim());
   char first = type.charAt(0);
   switch (first) {
     case 'a':
       if ("any".equals(type)) return true; // isSimpleValue(value);
       if ("array".equals(type)) return isArray(value);
       break;
     case 'b':
       if ("binary".equals(type)) return isBinary(value);
       if ("boolean".equals(type)) return isBoolean(value, true);
       break;
     case 'c':
       if ("creditcard".equals(type)) return isCreditCard(value);
       if ("component".equals(type)) return isComponent(value);
       if ("cfc".equals(type)) return isComponent(value);
       break;
     case 'd':
       if ("date".equals(type))
         return isDateAdvanced(value, true); // ist zwar nicht logisch aber ident. zu Neo
       if ("double".equals(type)) return isCastableToNumeric(value);
       break;
     case 'e':
       if ("eurodate".equals(type)) return isEuroDate(value);
       if ("email".equals(type)) return isEmail(value);
       break;
     case 'f':
       if ("float".equals(type)) return isNumeric(value, true);
       break;
     case 'g':
       if ("guid".equals(type)) return isGUId(value);
       break;
     case 'i':
       if ("integer".equals(type)) return isInteger(value, false);
       break;
     case 'n':
       if ("numeric".equals(type)) return isCastableToNumeric(value);
       if ("number".equals(type)) return isCastableToNumeric(value);
       if ("node".equals(type)) return isXML(value);
       break;
     case 'p':
       if ("phone".equals(type)) return isPhone(value);
       break;
     case 'q':
       if ("query".equals(type)) return isQuery(value);
       break;
     case 's':
       if ("simple".equals(type)) return isSimpleValue(value);
       if ("struct".equals(type)) return isStruct(value);
       if ("ssn".equals(type)) return isSSN(value);
       if ("social_security_number".equals(type)) return isSSN(value);
       if ("string".equals(type)) return isString(value);
       break;
     case 't':
       if ("telephone".equals(type)) return isPhone(value);
       if ("time".equals(type)) return isDateAdvanced(value, false);
       break;
     case 'u':
       if ("usdate".equals(type)) return isUSDate(value);
       if ("uuid".equals(type)) return isUUId(value);
       if ("url".equals(type)) return isURL(value);
       break;
     case 'v':
       if ("variablename".equals(type)) return isVariableName(Caster.toString(value, ""));
       break;
     case 'x':
       if ("xml".equals(type)) return isXML(value); // DIFF 23
       break;
     case 'z':
       if ("zip".equals(type)) return isZipCode(value);
       if ("zipcode".equals(type)) return isZipCode(value);
       break;
   }
   throw new ExpressionException(
       "invalid type ["
           + type
           + "], valid types are [any,array,binary,boolean,component,creditcard,date,time,email,eurodate,float,numeric,guid,integer,query,simple,ssn,string,struct,telephone,URL,UUID,USdate,variableName,zipcode]");
 }
Exemplo n.º 16
0
 public static boolean isEuroDate(Object value) {
   String str = Caster.toString(value, "");
   return isUSorEuroDateEuro(str, true);
 }
Exemplo n.º 17
0
 /**
  * returns if a value is a credit card
  *
  * @param value
  * @return is credit card
  */
 public static boolean isCreditCard(Object value) {
   return ValidateCreditCard.isValid(Caster.toString(value, "0"));
 }