Package javax.faces.convert

Examples of javax.faces.convert.Converter


   
    // if we have no local value, try to get the valueBinding.
    ValueBinding valueBinding = component.getValueBinding("value");

    Converter converter = null;
    Object result = null;
    // If there is a converter attribute, use it to to ask application
    // instance for a converter with this identifer.

    if (component instanceof ValueHolder) {
      converter = ((ValueHolder) component).getConverter();
    }

    if (null == converter && null != valueBinding) {
      Class converterType = valueBinding.getType(context);
      // if converterType is null, assume the modelType is "String".
      if (converterType == null || converterType == String.class
          || converterType == Object.class) {
        if (log.isDebugEnabled()) {
          log.debug("No conversion necessary for " + submittedValue
              + "and converterType " + converterType
              + " while decoding component " + component.getId());
        }
        return submittedValue;
      }
      // if getType returns a type for which we support a default
      // conversion, acquire an appropriate converter instance.

      try {
        Application application = context.getApplication();
        converter = application.createConverter(converterType);
        if (log.isDebugEnabled()) {
          log.debug("Created converter " + converter + "of type "
              + converterType + " while decoding component "
              + component.getId());
        }
      } catch (Exception e) {
        if (log.isDebugEnabled()) {
          log.debug("Converter could not be instantiated for "
              + converterType + " while " + "decoding component "
              + component.getId());         
        }
        if(submittedValue instanceof Object[]){
          if (log.isDebugEnabled()) {
            log.debug("Assume we have a String array here.");         
          }
          return submittedValue;
        }
        else {
          return (null)
        }       
      }
    } else if (converter == null && valueBinding == null) {
      // if there is no valueBinding and converter attribute set,
      // assume the modelType as "String" since we have no way of
      // figuring out the type. So for the selectOne and
      // selectMany, converter has to be set if there is no
      // valueBinding attribute set on the component.
      if (log.isDebugEnabled()) {
        log
            .debug("No conversion necessary for "
                + submittedValue
                + " while decoding component "
                + component.getId()
                + "since there is no explicitly registered converter and "
                + "component value is not bound to a model property ");
      }
      return submittedValue;
    }

    if (converter != null) {     
      if(submittedValue instanceof Object[]){
        String[] submittedValues = (String[]) submittedValue;
        Object[] results = new Object[submittedValues.length];
        for(int i=0; i < submittedValues.length;i++){
          results[i] = converter.getAsObject(context, component, submittedValues[i]);
        }
        return results;
      }
      else {
        result = converter.getAsObject(context, component, (String)submittedValue)
      }     
      return result;
    } else {
      if (log.isDebugEnabled()) {
        log.debug("Unexpected Converter exception "
View Full Code Here


    }
  }

  protected String getAsString(FacesContext facesContext,
      UIComponent component, Object value) {
    Converter converter = ((ValueHolder) component).getConverter();
    if (converter == null && value != null) {
      if (value instanceof String) {
        return (String) value;
      }

      try {
        converter = facesContext.getApplication().createConverter(
            value.getClass());
      } catch (FacesException e) {
        log.error("No converter for class "
            + value.getClass().getName() + " found (component id="
            + component.getId() + ").");
        // converter stays null
      }
    }

    if (converter == null) {
      if (value == null) {
        return null;
      } else {
        return value.toString();
      }
    } else {
      return converter.getAsString(facesContext, component, value);
    }
  }
View Full Code Here

      }

      Object value = ((ValueHolder) component).getValue();

      Converter converter = ((ValueHolder) component).getConverter();
      if (converter == null && value != null) {
        if (value instanceof String) {
          return (String) value;
        }

        try {
          converter = facesContext.getApplication().createConverter(
              value.getClass());
        } catch (FacesException e) {
          log.error("No converter for class "
              + value.getClass().getName()
              + " found (component id=" + component.getId()
              + ").");
          // converter stays null
        }
      }

      if (converter == null) {
        if (value == null) {
          return "";
        } else {
          return value.toString();
        }
      } else {
        return converter.getAsString(facesContext, component, value);
      }
    } catch (PropertyNotFoundException ex) {
      log.error("Property not found - called by component : ");
      throw ex;
    }
View Full Code Here

    if (value != null) {
      if (value instanceof String) {
        return value.toString();
      }
      try {
        Converter converter = context.getApplication().createConverter(value.getClass());
        if (converter != null) {
          return converter.getAsString(context, component, value);
        }

      } catch (FacesException e) {
        return value.toString();
      }
View Full Code Here

                result = currentValue.toString();
            }
            return result;
        }

        Converter converter = null;

        // If there is a converter attribute, use it to to ask application
        // instance for a converter with this identifer.

        if (component instanceof ValueHolder) {
            converter = ((ValueHolder) component).getConverter();
        }

        // if value is null and no converter attribute is specified, then
        // return a zero length String.
        if (converter == null && currentValue == null) {
            return "";
        }

        if (converter == null) {
            // Do not look for "by-type" converters for Strings
            if (currentValue instanceof String) {
                return (String) currentValue;
            }

            // if converter attribute set, try to acquire a converter
            // using its class type.

            Class converterType = currentValue.getClass();
            converter = null;//Util.getConverterForClass(converterType);

            // if there is no default converter available for this identifier,
            // assume the model type to be String.
            if (converter == null && currentValue != null) {
                result = currentValue.toString();
                return result;
            }
        }

        if (converter != null) {
            result = converter.getAsString(context, component, currentValue);

            return result;
        } else {
            // throw converter exception if no converter can be
            // identified        
View Full Code Here

  private static final Log log = LogFactory.getLog(OutputRenderer.class);

  protected String getValueAsString(FacesContext facesContext,
      UIOutput uiOutput) {
    String value = null;
    Converter converter = uiOutput.getConverter();
    Object rawValue = uiOutput.getValue();
    if (converter == null) {
      if (rawValue != null) {       
        value = rawValue.toString();
      }
    } else {
      value = converter.getAsString(facesContext, uiOutput, rawValue);
    }
    if (log.isDebugEnabled()) {
      String mesg = "Component " + uiOutput.getClass().getName()
          + " " + uiOutput.getId();
      log.debug(mesg + " ,converted value is " + value);
View Full Code Here

  {
    FacesContext context = FacesContext.getCurrentInstance();

    Application app = context.getApplication();

    Converter converter = null;

    ELContext elContext = context.getELContext();
     
    if (_bindingExpr != null)
      converter = (Converter) _bindingExpr.getValue(elContext);
View Full Code Here

                ValueBinding vb = context.getApplication().createValueBinding(_converter);
                comp.setValueBinding("converter", vb);
            }
            else
            {
                Converter converter = getFacesContext().getApplication().createConverter(_converter);
                comp.setConverter(converter);
            }
        }
        if (_value != null)
        {
View Full Code Here

        super(criteria);
    }

    @Test
    public void testConvert() throws Exception {
        Converter converter = createConverter();
        try {
            Object convertedValue = converter.getAsObject(facesEnvironment.getFacesContext(), input, criteria.getValue()
                .toString());
            Object jsConvertedValue = convertOnClient(converter);
            if (null != convertedValue || null != jsConvertedValue) {
                compareResult(convertedValue, jsConvertedValue);
            }
View Full Code Here

      {
         return null;
      }
      else
      {
         Converter converter = null;
         try
         {
            converter = getConverter();
         }
         catch (RuntimeException re)
         {
            //YUCK! due to bad JSF/MyFaces error handling
            log.warn("could not create converter for: " + name, re);
            return null;
         }
        
         return converter==null ?
               value.toString() :
               converter.getAsString( facesContext, facesContext.getViewRoot(), value );
      }
   }
View Full Code Here

TOP

Related Classes of javax.faces.convert.Converter

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.