Esempio 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;
  }
Esempio n. 2
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;
  }