public static Object coerce()

in trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/share/expl/Coercions.java [52:203]


  public static Object coerce(ExpressionContext context,
                              String text,
                              Class<?> type) throws IllegalArgumentException
  {
    if (type != null)
    {
      if (type == Object.class)
      {
        return text;
      }
      // Turn the type into a Boolean
      else if ((type == Boolean.class) || (type == Boolean.TYPE))
      {
        return toBoolean(text);
      }
      else if ((type == Byte.class) || (type == Byte.TYPE))
      {
        return toByte(text);
      }
      else if ((type == Short.class) || (type == Short.TYPE))
      {
        return toShort(text);
      }
      else if ((type == Integer.class) || (type == Integer.TYPE))
      {
        return toInteger(text);
      }
      else if ((type == Long.class) || (type == Long.TYPE))
      {
        return toLong(text);
      }
      else if ((type == Float.class) || (type == Float.TYPE))
      {
        return toFloat(text);
      }
      else if ((type == Double.class) || (type == Double.TYPE))
      {
        return toDouble(text);
      }
      else if ((type == Number.class))
      {
        return toNumber(text);
      }
      else if ((type == Character.class) || (type == Character.TYPE))
      {
        Character c = toCharacter(text);

        if (text != null && c == null)
        {
          throw new IllegalArgumentException(_LOG.getMessage(
            "NOT_A_CHARACTER"));
        }

        return c;
      }
      else if (type == String.class)
      {
        return text;
      }
      else if ((type == int[].class))
      {
        String[] array = XMLUtils.parseNameTokens(text);
        int[] ints = new int[array.length];
        for (int i = 0; i < array.length; i++)
        {
          ints[i] = Integer.parseInt(array[i]);
        }

        return ints;
      }
      else if ((type == Integer[].class))
      {
        String[] array = XMLUtils.parseNameTokens(text);
        Integer[] ints = new Integer[array.length];
        for (int i = 0; i < array.length; i++)
        {
          ints[i] = Integer.parseInt(array[i]);
        }

        return ints;
      }
      else if ((type == String[].class))
      {
        return XMLUtils.parseNameTokens(text);
      }
      else if (type == List.class)
      {
        return XMLUtils.parseNameTokensAsList(text);
      }
      else if (type == Set.class)
      {
        return XMLUtils.parseNameTokensAsSet(text);
      }
      // For Dates, try to parse it as an ISO 8601 Date.
      // If that fails, simply let the string pass through;  for
      // DateFieldBean, this works, and it really should for
      // all date-accepting components.
      else if (type == Date.class)
      {
        try
        {
          return _getDateFormat().parse(text);
        }
        catch (ParseException pe)
        {
          return text;
        }
      }
      // For Colors, try to parse it as "#FFFFFF" format.
      // If that fails, simply let the string pass through;  for
      // ColorFieldBean, this works, and it really should for
      // all color-accepting components.
      else if (type == Color.class)
      {
        return toColor(text);
      }
      else if ((type == NamespaceURI.class))
      {
        return NamespaceURI.create(context, text, "");
      }
      else if (type == CoreStyle.class)
      {
        return CSSUtils.parseStyle(text);
      }
      else if (type == Class.class)
      {
        try
        {
          return ClassLoaderUtils.loadClass(text);
        }
        catch (ClassNotFoundException cnfe)
        {
          throw new IllegalArgumentException(_LOG.getMessage(
            "CANNOT_FIND_CLASS", text));
        }
      }
      else if (type == Object.class)
      {
        return text;
      }
      else if (Enum.class.isAssignableFrom(type))
      {
        return Enum.valueOf((Class<? extends Enum>) type, text);
      }

      throw new IllegalArgumentException(_LOG.getMessage(
        "CANNOT_BE_PARSED", new Object[]{text, type.getName()}));
    }

    throw new NullPointerException(_LOG.getMessage(
      "NULL_TYPE"));
  }