You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mm...@apache.org on 2006/09/21 15:42:14 UTC
svn commit: r448551 -
/myfaces/core/trunk/api/src/main/java/javax/faces/convert/
Author: mmarinschek
Date: Thu Sep 21 06:42:13 2006
New Revision: 448551
URL: http://svn.apache.org/viewvc?view=rev&rev=448551
Log:
fix for MYFACES-1419 : javax.faces.convert - refactor common behaviour + DateTimeConverter changes. Thanks to Nikolay Petrov for providing this patch.
Added:
myfaces/core/trunk/api/src/main/java/javax/faces/convert/AbstractConverter.java
Modified:
myfaces/core/trunk/api/src/main/java/javax/faces/convert/BigDecimalConverter.java
myfaces/core/trunk/api/src/main/java/javax/faces/convert/BigIntegerConverter.java
myfaces/core/trunk/api/src/main/java/javax/faces/convert/BooleanConverter.java
myfaces/core/trunk/api/src/main/java/javax/faces/convert/ByteConverter.java
myfaces/core/trunk/api/src/main/java/javax/faces/convert/CharacterConverter.java
myfaces/core/trunk/api/src/main/java/javax/faces/convert/DateTimeConverter.java
myfaces/core/trunk/api/src/main/java/javax/faces/convert/DoubleConverter.java
myfaces/core/trunk/api/src/main/java/javax/faces/convert/FloatConverter.java
myfaces/core/trunk/api/src/main/java/javax/faces/convert/IntegerConverter.java
myfaces/core/trunk/api/src/main/java/javax/faces/convert/LongConverter.java
myfaces/core/trunk/api/src/main/java/javax/faces/convert/ShortConverter.java
Added: myfaces/core/trunk/api/src/main/java/javax/faces/convert/AbstractConverter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/convert/AbstractConverter.java?view=auto&rev=448551
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/convert/AbstractConverter.java (added)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/convert/AbstractConverter.java Thu Sep 21 06:42:13 2006
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package javax.faces.convert;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+
+/**
+ * An abstract base class for all basic converters
+ *
+ * @author Nikolay Petrov
+ * @version $Revision$ $Date$
+ */
+abstract class AbstractConverter implements Converter {
+ public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
+ if (context == null) {
+ throw new NullPointerException("facesContext");
+ }
+ if (component == null) {
+ throw new NullPointerException("uiComponent");
+ }
+
+ if (value == null) {
+ return null;
+ }
+
+ String trimmedValue = value.trim();
+ if (trimmedValue.length() == 0) {
+ return null;
+ }
+
+ try {
+ return getAsObject(trimmedValue);
+ } catch (Exception e) {
+ throw new ConverterException(_MessageUtils.getErrorMessage(context, getConversionMessageId(),
+ getMessageArguments(component, value)), e);
+ }
+ }
+
+ public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
+ if (context == null) {
+ throw new NullPointerException("facesContext");
+ }
+ if (component == null) {
+ throw new NullPointerException("uiComponent");
+ }
+
+ if (value == null) {
+ return "";
+ }
+ if (value instanceof String) {
+ return (String) value;
+ }
+
+ try {
+ return getAsString(value);
+ } catch (Exception e) {
+ throw new ConverterException(e);
+ }
+ }
+
+ protected Object[] getMessageArguments(UIComponent component, String value) {
+ return new Object[] {component.getId(), value};
+ }
+
+ protected abstract Object getAsObject(String value) throws Exception;
+
+ protected abstract String getAsString(Object value) throws Exception;
+
+ protected abstract String getConversionMessageId();
+}
Modified: myfaces/core/trunk/api/src/main/java/javax/faces/convert/BigDecimalConverter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/convert/BigDecimalConverter.java?view=diff&rev=448551&r1=448550&r2=448551
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/convert/BigDecimalConverter.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/convert/BigDecimalConverter.java Thu Sep 21 06:42:13 2006
@@ -15,8 +15,6 @@
*/
package javax.faces.convert;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
import java.math.BigDecimal;
/**
@@ -25,67 +23,21 @@
* @author Thomas Spiegl (latest modification by $Author$)
* @version $Revision$ $Date$
*/
-public class BigDecimalConverter
- implements Converter
-{
- private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.BigDecimalConverter.CONVERSION";
+public class BigDecimalConverter extends AbstractConverter {
+ private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.BigDecimalConverter.CONVERSION";
- // FIELDS
- public static final String CONVERTER_ID = "javax.faces.BigDecimal";
+ public static final String CONVERTER_ID = "javax.faces.BigDecimal";
- // CONSTRUCTORS
- public BigDecimalConverter()
- {
- }
-
- // METHODS
- public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value != null)
- {
- {
- value = value.trim();
- if (value.length() > 0)
- {
- try
- {
- return new BigDecimal(value.trim());
- }
- catch (NumberFormatException e)
- {
- throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
- CONVERSION_MESSAGE_ID,
- new Object[]{uiComponent.getId(),value}), e);
- }
- }
- }
- }
- return null;
- }
-
- public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value == null)
- {
- return "";
- }
- if (value instanceof String)
- {
- return (String)value;
- }
- try
- {
- return ((BigDecimal)value).toString();
- }
- catch (Exception e)
- {
- throw new ConverterException(e);
- }
- }
+ // Methods implementation
+ protected Object getAsObject(String value) {
+ return new BigDecimal(value);
+ }
+
+ protected String getAsString(Object value) {
+ return ((BigDecimal) value).toString();
+ }
+
+ protected String getConversionMessageId() {
+ return CONVERSION_MESSAGE_ID;
+ }
}
Modified: myfaces/core/trunk/api/src/main/java/javax/faces/convert/BigIntegerConverter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/convert/BigIntegerConverter.java?view=diff&rev=448551&r1=448550&r2=448551
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/convert/BigIntegerConverter.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/convert/BigIntegerConverter.java Thu Sep 21 06:42:13 2006
@@ -15,8 +15,6 @@
*/
package javax.faces.convert;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
import java.math.BigInteger;
/**
@@ -25,66 +23,21 @@
* @author Thomas Spiegl (latest modification by $Author$)
* @version $Revision$ $Date$
*/
-public class BigIntegerConverter
- implements Converter
-{
- private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.BigIntegerConverter.CONVERSION";
+public class BigIntegerConverter extends AbstractConverter {
+ private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.BigIntegerConverter.CONVERSION";
- // FIELDS
- public static final String CONVERTER_ID = "javax.faces.BigInteger";
-
- // CONSTRUCTORS
- public BigIntegerConverter()
- {
- }
-
- // METHODS
- public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value != null)
- {
- value = value.trim();
- if (value.length() > 0)
- {
- try
- {
- return new BigInteger(value.trim());
- }
- catch (NumberFormatException e)
- {
- throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
- CONVERSION_MESSAGE_ID,
- new Object[]{uiComponent.getId(),value}), e);
- }
- }
- }
- return null;
- }
-
- public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value == null)
- {
- return "";
- }
- if (value instanceof String)
- {
- return (String)value;
- }
- try
- {
- return ((BigInteger)value).toString();
- }
- catch (Exception e)
- {
- throw new ConverterException(e);
- }
- }
+ public static final String CONVERTER_ID = "javax.faces.BigInteger";
+ // Methods implementation
+ protected Object getAsObject(String value) {
+ return new BigInteger(value);
+ }
+
+ protected String getAsString(Object value) {
+ return ((BigInteger) value).toString();
+ }
+
+ protected String getConversionMessageId() {
+ return CONVERSION_MESSAGE_ID;
+ }
}
Modified: myfaces/core/trunk/api/src/main/java/javax/faces/convert/BooleanConverter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/convert/BooleanConverter.java?view=diff&rev=448551&r1=448550&r2=448551
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/convert/BooleanConverter.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/convert/BooleanConverter.java Thu Sep 21 06:42:13 2006
@@ -15,74 +15,27 @@
*/
package javax.faces.convert;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-
/**
* see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
*
* @author Thomas Spiegl (latest modification by $Author$)
* @version $Revision$ $Date$
*/
-public class BooleanConverter
- implements Converter
-{
- private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.BooleanConverter.CONVERSION";
-
- // FIELDS
- public static final String CONVERTER_ID = "javax.faces.Boolean";
-
- // CONSTRUCTORS
- public BooleanConverter()
- {
- }
-
- // METHODS
- public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value != null)
- {
- value = value.trim();
- if (value.length() > 0)
- {
- try
- {
- return Boolean.valueOf(value);
- }
- catch (Exception e)
- {
- throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
- CONVERSION_MESSAGE_ID,
- new Object[]{value,uiComponent.getId()}), e);
- }
- }
- }
- return null;
- }
+public class BooleanConverter extends AbstractConverter {
+ private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.BooleanConverter.CONVERSION";
- public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
+ public static final String CONVERTER_ID = "javax.faces.Boolean";
- if (value == null)
- {
- return "";
- }
- if (value instanceof String)
- {
- return (String)value;
- }
- try
- {
- return ((Boolean)value).toString();
- }
- catch (Exception e)
- {
- throw new ConverterException(e);
- }
- }
+ // Methods implementation
+ protected Object getAsObject(String value) {
+ return Boolean.valueOf(value);
+ }
+
+ protected String getAsString(Object value) {
+ return ((Boolean) value).toString();
+ }
+
+ protected String getConversionMessageId() {
+ return CONVERSION_MESSAGE_ID;
+ }
}
Modified: myfaces/core/trunk/api/src/main/java/javax/faces/convert/ByteConverter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/convert/ByteConverter.java?view=diff&rev=448551&r1=448550&r2=448551
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/convert/ByteConverter.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/convert/ByteConverter.java Thu Sep 21 06:42:13 2006
@@ -15,75 +15,27 @@
*/
package javax.faces.convert;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-
/**
* see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
*
* @author Thomas Spiegl (latest modification by $Author$)
* @version $Revision$ $Date$
*/
-public class ByteConverter
- implements Converter
-{
- private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.ByteConverter.CONVERSION";
-
- // FIELDS
- public static final String CONVERTER_ID = "javax.faces.Byte";
-
- // CONSTRUCTORS
- public ByteConverter()
- {
- }
-
- // METHODS
- public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value != null)
- {
- value = value.trim();
- if (value.length() > 0)
- {
- try
- {
- return Byte.valueOf(value);
- }
- catch (NumberFormatException e)
- {
- throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
- CONVERSION_MESSAGE_ID,
- new Object[]{value,uiComponent.getId()}), e);
- }
- }
- }
- return null;
- }
-
- public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
+public class ByteConverter extends AbstractConverter {
+ private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.ByteConverter.CONVERSION";
- if (value == null)
- {
- return "";
- }
- if (value instanceof String)
- {
- return (String)value;
- }
- try
- {
- return Byte.toString(((Number)value).byteValue());
- }
- catch (Exception e)
- {
- throw new ConverterException(e);
- }
+ public static final String CONVERTER_ID = "javax.faces.Byte";
- }
+ // Methods implementation
+ protected Object getAsObject(String value) {
+ return Byte.valueOf(value);
+ }
+
+ protected String getAsString(Object value) {
+ return Byte.toString(((Number) value).byteValue());
+ }
+
+ protected String getConversionMessageId() {
+ return CONVERSION_MESSAGE_ID;
+ }
}
Modified: myfaces/core/trunk/api/src/main/java/javax/faces/convert/CharacterConverter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/convert/CharacterConverter.java?view=diff&rev=448551&r1=448550&r2=448551
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/convert/CharacterConverter.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/convert/CharacterConverter.java Thu Sep 21 06:42:13 2006
@@ -15,64 +15,26 @@
*/
package javax.faces.convert;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-
/**
* see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
*
* @author Thomas Spiegl (latest modification by $Author$)
* @version $Revision$ $Date$
*/
-public class CharacterConverter
- implements Converter
-{
- // FIELDS
- public static final String CONVERTER_ID = "javax.faces.Character";
-
- // CONSTRUCTORS
- public CharacterConverter()
- {
- }
-
- // METHODS
- public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value != null)
- {
- value = value.trim();
- if (value.length() > 0)
- {
- return new Character(value.charAt(0));
- }
- }
- return null;
- }
-
- public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value == null)
- {
- return "";
- }
- if (value instanceof String)
- {
- return (String)value;
- }
- try
- {
- return ((Character)value).toString();
- }
- catch (Exception e)
- {
- throw new ConverterException(e);
- }
- }
-
+public class CharacterConverter extends AbstractConverter {
+ // FIELDS
+ public static final String CONVERTER_ID = "javax.faces.Character";
+
+ // Methods implementation
+ protected Object getAsObject(String value) {
+ return new Character(value.charAt(0));
+ }
+
+ protected String getAsString(Object value) {
+ return ((Character) value).toString();
+ }
+
+ protected String getConversionMessageId() {
+ return null;
+ }
}
Modified: myfaces/core/trunk/api/src/main/java/javax/faces/convert/DateTimeConverter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/convert/DateTimeConverter.java?view=diff&rev=448551&r1=448550&r2=448551
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/convert/DateTimeConverter.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/convert/DateTimeConverter.java Thu Sep 21 06:42:13 2006
@@ -15,275 +15,254 @@
*/
package javax.faces.convert;
-import javax.faces.component.UIComponent;
-import javax.faces.component.StateHolder;
-import javax.faces.context.FacesContext;
import java.text.DateFormat;
-import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
+import javax.faces.component.StateHolder;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+
/**
* see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
*
* @author Thomas Spiegl (latest modification by $Author$)
* @version $Revision$ $Date$
*/
-public class DateTimeConverter
- implements Converter, StateHolder
-{
-
- // API field
- public static final String CONVERTER_ID = "javax.faces.DateTime";
-
- // internal constants
- private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.DateTimeConverter.CONVERSION";
- private static final String TYPE_DATE = "date";
- private static final String TYPE_TIME = "time";
- private static final String TYPE_BOTH = "both";
- private static final String STYLE_DEFAULT = "default";
- private static final String STYLE_MEDIUM = "medium";
- private static final String STYLE_SHORT = "short";
- private static final String STYLE_LONG = "long";
- private static final String STYLE_FULL = "full";
- private static final TimeZone TIMEZONE_DEFAULT = TimeZone.getTimeZone("GMT");
-
- private String _dateStyle;
- private Locale _locale;
- private String _pattern;
- private String _timeStyle;
- private TimeZone _timeZone;
- private String _type;
- private boolean _transient;
-
- // CONSTRUCTORS
- public DateTimeConverter()
- {
- }
-
- // METHODS
- public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value != null)
- {
- value = value.trim();
- if (value.length() > 0)
- {
- DateFormat format = getDateFormat();
- TimeZone tz = getTimeZone();
- if( tz != null )
- format.setTimeZone( tz );
- try
- {
- return format.parse(value);
- }
- catch (ParseException e)
- {
- throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
- CONVERSION_MESSAGE_ID,
- new Object[]{value,uiComponent.getId()}), e);
- }
- }
- }
- return null;
- }
-
- public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value == null)
- {
- return "";
- }
- if (value instanceof String)
- {
- return (String)value;
- }
-
- DateFormat format = getDateFormat();
- TimeZone tz = getTimeZone();
- if (tz != null)
- {
- format.setTimeZone(tz);
- }
- try
- {
- return format.format(value);
- }
- catch (Exception e)
- {
- throw new ConverterException("Cannot convert value '" + value + "'");
- }
- }
-
- private DateFormat getDateFormat()
- {
- String type = getType();
- DateFormat format;
- if (_pattern != null)
- {
- try
- {
- format = new SimpleDateFormat(_pattern, getLocale());
- }
- catch (IllegalArgumentException iae)
- {
- throw new ConverterException("Invalid pattern", iae);
- }
- }
- else if (type.equals(TYPE_DATE))
- {
- format = DateFormat.getDateInstance(calcStyle(getDateStyle()), getLocale());
- }
- else if (type.equals(TYPE_TIME))
- {
- format = DateFormat.getTimeInstance(calcStyle(getTimeStyle()), getLocale());
- }
- else if (type.equals(TYPE_BOTH))
- {
- format = DateFormat.getDateTimeInstance(calcStyle(getDateStyle()),
- calcStyle(getTimeStyle()),
- getLocale());
- }
- else
- {
- throw new ConverterException("invalid type '" + _type + "'");
- }
-
- // format cannot be lenient (JSR-127)
- format.setLenient(false);
- return format;
- }
-
- private int calcStyle(String name)
- {
- if (name.equals(STYLE_DEFAULT))
- {
- return DateFormat.DEFAULT;
- }
- if (name.equals(STYLE_MEDIUM))
- {
- return DateFormat.MEDIUM;
- }
- if (name.equals(STYLE_SHORT))
- {
- return DateFormat.SHORT;
- }
- if (name.equals(STYLE_LONG))
- {
- return DateFormat.LONG;
- }
- if (name.equals(STYLE_FULL))
- {
- return DateFormat.FULL;
- }
-
- throw new ConverterException("invalid style '" + name + "'");
- }
-
- // STATE SAVE/RESTORE
- public void restoreState(FacesContext facesContext, Object state)
- {
- Object[] values = (Object[])state;
- _dateStyle = (String)values[0];
- _locale = (Locale)values[1];
- _pattern = (String)values[2];
- _timeStyle = (String)values[3];
- _timeZone = (TimeZone)values[4];
- _type = (String)values[5];
- }
-
- public Object saveState(FacesContext facesContext)
- {
- Object[] values = new Object[6];
- values[0] = _dateStyle;
- values[1] = _locale;
- values[2] = _pattern;
- values[3] = _timeStyle;
- values[4] = _timeZone;
- values[5] = _type;
- return values;
- }
-
- // GETTER & SETTER
- public String getDateStyle()
- {
- return _dateStyle != null ? _dateStyle : STYLE_DEFAULT;
- }
-
- public void setDateStyle(String dateStyle)
- {
- //TODO: validate timeStyle
- _dateStyle = dateStyle;
- }
-
- public Locale getLocale()
- {
- if (_locale != null) return _locale;
- FacesContext context = FacesContext.getCurrentInstance();
- return context.getViewRoot().getLocale();
- }
-
- public void setLocale(Locale locale)
- {
- _locale = locale;
- }
-
- public String getPattern()
- {
- return _pattern;
- }
-
- public void setPattern(String pattern)
- {
- _pattern = pattern;
- }
-
- public String getTimeStyle()
- {
- return _timeStyle != null ? _timeStyle : STYLE_DEFAULT;
- }
-
- public void setTimeStyle(String timeStyle)
- {
- //TODO: validate timeStyle
- _timeStyle = timeStyle;
- }
-
- public TimeZone getTimeZone()
- {
- return _timeZone != null ? _timeZone : TIMEZONE_DEFAULT;
- }
-
- public void setTimeZone(TimeZone timeZone)
- {
- _timeZone = timeZone;
- }
-
- public boolean isTransient()
- {
- return _transient;
- }
-
- public void setTransient(boolean aTransient)
- {
- _transient = aTransient;
- }
-
- public String getType()
- {
- return _type != null ? _type : TYPE_DATE;
- }
-
- public void setType(String type)
- {
- //TODO: validate type
- _type = type;
- }
+public class DateTimeConverter extends AbstractConverter implements StateHolder {
+ // API field
+ public static final String CONVERTER_ID = "javax.faces.DateTime";
+
+ // internal constants
+ private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.DateTimeConverter.CONVERSION";
+
+ private static final TimeZone TIMEZONE_DEFAULT = TimeZone.getTimeZone("GMT");
+
+ private String _dateStyle;
+ private Locale _locale;
+ private String _pattern;
+ private String _timeStyle;
+ private TimeZone _timeZone;
+ private String _type;
+ private boolean _transient;
+
+ // Methods implementation
+ protected Object getAsObject(String value) throws Exception {
+ return prepareDateFormat().parse(value);
+ }
+
+ protected String getAsString(Object value) {
+ return prepareDateFormat().format(value);
+ }
+
+ protected String getConversionMessageId() {
+ return CONVERSION_MESSAGE_ID;
+ }
+
+ protected Object[] getMessageArguments(UIComponent component, String value) {
+ return new Object[] {value, component.getId()};
+ }
+
+ private DateFormat prepareDateFormat() {
+ DateFormat format = getDateFormat();
+ // format cannot be lenient (JSR-127)
+ format.setLenient(false);
+
+ TimeZone tz = getTimeZone();
+ if (tz != null) {
+ format.setTimeZone(tz);
+ }
+
+ return format;
+ }
+
+ private DateFormat getDateFormat() {
+ if(_pattern != null) {
+ try {
+ return new SimpleDateFormat(_pattern, getLocale());
+ } catch (IllegalArgumentException iae) {
+ throw new ConverterException("Invalid pattern", iae);
+ }
+ }
+
+ return Type.getType(getType()).getFormatter(calcDateStyle(), calcTimeStyle(), getLocale());
+ }
+
+ private int calcDateStyle() {
+ return Style.getStyleFormat(getDateStyle());
+ }
+
+ private int calcTimeStyle() {
+ return Style.getStyleFormat(getTimeStyle());
+ }
+
+ // STATE SAVE/RESTORE
+ public void restoreState(FacesContext facesContext, Object state) {
+ Object[] values = (Object[]) state;
+ _dateStyle = (String) values[0];
+ _locale = (Locale) values[1];
+ _pattern = (String) values[2];
+ _timeStyle = (String) values[3];
+ _timeZone = (TimeZone) values[4];
+ _type = (String) values[5];
+ }
+
+ public Object saveState(FacesContext facesContext) {
+ Object[] values = new Object[6];
+ values[0] = _dateStyle;
+ values[1] = _locale;
+ values[2] = _pattern;
+ values[3] = _timeStyle;
+ values[4] = _timeZone;
+ values[5] = _type;
+ return values;
+ }
+
+ // GETTER & SETTER
+ public String getDateStyle() {
+ return _dateStyle != null ? _dateStyle : Style.DEFAULT.getName();
+ }
+
+ public void setDateStyle(String dateStyle) {
+ //TODO: validate timeStyle
+ _dateStyle = dateStyle;
+ }
+
+ public Locale getLocale() {
+ if (_locale != null)
+ return _locale;
+ FacesContext context = FacesContext.getCurrentInstance();
+ return context.getViewRoot().getLocale();
+ }
+
+ public void setLocale(Locale locale) {
+ _locale = locale;
+ }
+
+ public String getPattern() {
+ return _pattern;
+ }
+
+ public void setPattern(String pattern) {
+ _pattern = pattern;
+ }
+
+ public String getTimeStyle() {
+ return _timeStyle != null ? _timeStyle : Style.DEFAULT.getName();
+ }
+
+ public void setTimeStyle(String timeStyle) {
+ //TODO: validate timeStyle
+ _timeStyle = timeStyle;
+ }
+
+ public TimeZone getTimeZone() {
+ return _timeZone != null ? _timeZone : TIMEZONE_DEFAULT;
+ }
+
+ public void setTimeZone(TimeZone timeZone) {
+ _timeZone = timeZone;
+ }
+
+ public boolean isTransient() {
+ return _transient;
+ }
+
+ public void setTransient(boolean aTransient) {
+ _transient = aTransient;
+ }
+
+ public String getType() {
+ return _type != null ? _type : Type.DATE.getName();
+ }
+
+ public void setType(String type) {
+ //TODO: validate type
+ _type = type;
+ }
+
+ private static class Style {
+
+ private static final Style DEFAULT = new Style("default", DateFormat.DEFAULT);
+ private static final Style MEDIUM = new Style("medium", DateFormat.MEDIUM);
+ private static final Style SHORT = new Style("short", DateFormat.SHORT);
+ private static final Style LONG = new Style("long", DateFormat.LONG);
+ private static final Style FULL = new Style("full", DateFormat.FULL);
+
+ private static final Style[] values = new Style[] {DEFAULT, MEDIUM, SHORT, LONG, FULL};
+
+ public static Style getStyle(String name) {
+ for(int i = 0;i < values.length;i++) {
+ if(values[i]._name.equals(name)) {
+ return values[i];
+ }
+ }
+
+ throw new ConverterException("invalid style '" + name + "'");
+ }
+
+ private static int getStyleFormat(String name) {
+ return getStyle(name).getFormat();
+ }
+
+ private String _name;
+ private int _format;
+
+ private Style(String name, int format) {
+ this._name = name;
+ this._format = format;
+ }
+
+ public String getName() {
+ return _name;
+ }
+
+ public int getFormat() {
+ return _format;
+ }
+ }
+
+ private abstract static class Type {
+
+ private static final Type DATE = new Type("date") {
+ public DateFormat getFormatter(int dateStyle, int timeStyle, Locale locale) {
+ return DateFormat.getDateInstance(dateStyle, locale);
+ }
+ };
+ private static final Type TIME = new Type("time") {
+ public DateFormat getFormatter(int dateStyle, int timeStyle, Locale locale) {
+ return DateFormat.getDateInstance(timeStyle, locale);
+ }
+ };
+ private static final Type BOTH = new Type("both") {
+ public DateFormat getFormatter(int dateStyle, int timeStyle, Locale locale) {
+ return DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
+ }
+ };
+
+ private static final Type[] values = new Type[] {DATE, TIME, BOTH};
+
+ public static Type getType(String name) {
+ for(int i = 0;i < values.length;i++) {
+ if(values[i]._name.equals(name)) {
+ return values[i];
+ }
+ }
+
+ throw new ConverterException("invalid type '" + name + "'");
+ }
+
+ private String _name;
+
+ private Type(String name) {
+ this._name = name;
+ }
+
+ public String getName() {
+ return _name;
+ }
+
+ public abstract DateFormat getFormatter(int dateStyle, int timeStyle, Locale locale);
+ }
}
Modified: myfaces/core/trunk/api/src/main/java/javax/faces/convert/DoubleConverter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/convert/DoubleConverter.java?view=diff&rev=448551&r1=448550&r2=448551
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/convert/DoubleConverter.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/convert/DoubleConverter.java Thu Sep 21 06:42:13 2006
@@ -15,77 +15,28 @@
*/
package javax.faces.convert;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-
/**
* see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
*
* @author Thomas Spiegl (latest modification by $Author$)
* @version $Revision$ $Date$
*/
-public class DoubleConverter
- implements Converter
-{
- // API FIELDS
+public class DoubleConverter extends AbstractConverter {
// the wrong string javax.faces.DoubleTime is required to be compatible with JSF RI and its TCK tests
- public static final String CONVERTER_ID = "javax.faces.DoubleTime";
-
-
- private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.DoubleConverter.CONVERSION";
-
-
- // CONSTRUCTORS
- public DoubleConverter()
- {
- }
-
- // METHODS
- public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value != null)
- {
- value = value.trim();
- if (value.length() > 0)
- {
- try
- {
- return Double.valueOf(value);
- }
- catch (NumberFormatException e)
- {
- throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
- CONVERSION_MESSAGE_ID,
- new Object[]{uiComponent.getId(),value}), e);
- }
- }
- }
- return null;
- }
+ public static final String CONVERTER_ID = "javax.faces.DoubleTime";
- public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
+ private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.DoubleConverter.CONVERSION";
- if (value == null)
- {
- return "";
- }
- if (value instanceof String)
- {
- return (String)value;
- }
- try
- {
- return Double.toString(((Number)value).doubleValue());
- }
- catch (Exception e)
- {
- throw new ConverterException(e);
- }
- }
+ // Methods implementation
+ protected Object getAsObject(String value) {
+ return Double.valueOf(value);
+ }
+
+ protected String getAsString(Object value) {
+ return Double.toString(((Number) value).doubleValue());
+ }
+
+ protected String getConversionMessageId() {
+ return CONVERSION_MESSAGE_ID;
+ }
}
Modified: myfaces/core/trunk/api/src/main/java/javax/faces/convert/FloatConverter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/convert/FloatConverter.java?view=diff&rev=448551&r1=448550&r2=448551
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/convert/FloatConverter.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/convert/FloatConverter.java Thu Sep 21 06:42:13 2006
@@ -15,74 +15,27 @@
*/
package javax.faces.convert;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-
/**
* see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
*
* @author Thomas Spiegl (latest modification by $Author$)
* @version $Revision$ $Date$
*/
-public class FloatConverter
- implements Converter
-{
- private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.FloatConverter.CONVERSION";
-
- // FIELDS
- public static final String CONVERTER_ID = "javax.faces.Float";
-
- // CONSTRUCTORS
- public FloatConverter()
- {
- }
-
- // METHODS
- public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value != null)
- {
- value = value.trim();
- if (value.length() > 0)
- {
- try
- {
- return Float.valueOf(value);
- }
- catch (NumberFormatException e)
- {
- throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
- CONVERSION_MESSAGE_ID,
- new Object[]{uiComponent.getId(),value}), e);
- }
- }
- }
- return null;
- }
+public class FloatConverter extends AbstractConverter {
+ private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.FloatConverter.CONVERSION";
- public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
+ public static final String CONVERTER_ID = "javax.faces.Float";
- if (value == null)
- {
- return "";
- }
- if (value instanceof String)
- {
- return (String)value;
- }
- try
- {
- return Float.toString(((Number)value).floatValue());
- }
- catch (Exception e)
- {
- throw new ConverterException(e);
- }
- }
+ // Methods implementation
+ protected Object getAsObject(String value) {
+ return Float.valueOf(value);
+ }
+
+ protected String getAsString(Object value) {
+ return Float.toString(((Number) value).floatValue());
+ }
+
+ protected String getConversionMessageId() {
+ return CONVERSION_MESSAGE_ID;
+ }
}
Modified: myfaces/core/trunk/api/src/main/java/javax/faces/convert/IntegerConverter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/convert/IntegerConverter.java?view=diff&rev=448551&r1=448550&r2=448551
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/convert/IntegerConverter.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/convert/IntegerConverter.java Thu Sep 21 06:42:13 2006
@@ -15,74 +15,27 @@
*/
package javax.faces.convert;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-
/**
* see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
*
* @author Thomas Spiegl (latest modification by $Author$)
* @version $Revision$ $Date$
*/
-public class IntegerConverter
- implements Converter
-{
- private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.IntegerConverter.CONVERSION";
-
- // FIELDS
- public static final String CONVERTER_ID = "javax.faces.Integer";
-
- // CONSTRUCTORS
- public IntegerConverter()
- {
- }
-
- // METHODS
- public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value != null)
- {
- value = value.trim();
- if (value.length() > 0)
- {
- try
- {
- return Integer.valueOf(value);
- }
- catch (NumberFormatException e)
- {
- throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
- CONVERSION_MESSAGE_ID,
- new Object[]{uiComponent.getId(),value}), e);
- }
- }
- }
- return null;
- }
+public class IntegerConverter extends AbstractConverter {
+ private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.IntegerConverter.CONVERSION";
- public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
+ public static final String CONVERTER_ID = "javax.faces.Integer";
- if (value == null)
- {
- return "";
- }
- if (value instanceof String)
- {
- return (String)value;
- }
- try
- {
- return Integer.toString(((Number)value).intValue());
- }
- catch (Exception e)
- {
- throw new ConverterException(e);
- }
- }
+ // Methods implementation
+ protected Object getAsObject(String value) {
+ return Integer.valueOf(value);
+ }
+
+ protected String getAsString(Object value) {
+ return Integer.toString(((Number) value).intValue());
+ }
+
+ protected String getConversionMessageId() {
+ return CONVERSION_MESSAGE_ID;
+ }
}
Modified: myfaces/core/trunk/api/src/main/java/javax/faces/convert/LongConverter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/convert/LongConverter.java?view=diff&rev=448551&r1=448550&r2=448551
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/convert/LongConverter.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/convert/LongConverter.java Thu Sep 21 06:42:13 2006
@@ -15,74 +15,27 @@
*/
package javax.faces.convert;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-
/**
* see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
- *
+ *
* @author Thomas Spiegl (latest modification by $Author$)
* @version $Revision$ $Date$
*/
-public class LongConverter
- implements Converter
-{
- private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.LongConverter.CONVERSION";
-
- // FIELDS
- public static final String CONVERTER_ID = "javax.faces.Long";
-
- // CONSTRUCTORS
- public LongConverter()
- {
- }
-
- // METHODS
- public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value != null)
- {
- value = value.trim();
- if (value.length() > 0)
- {
- try
- {
- return Long.valueOf(value);
- }
- catch (NumberFormatException e)
- {
- throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
- CONVERSION_MESSAGE_ID,
- new Object[]{uiComponent.getId(),value}), e);
- }
- }
- }
- return null;
- }
+public class LongConverter extends AbstractConverter {
+ private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.LongConverter.CONVERSION";
- public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
+ public static final String CONVERTER_ID = "javax.faces.Long";
- if (value == null)
- {
- return "";
- }
- if (value instanceof String)
- {
- return (String)value;
- }
- try
- {
- return Long.toString(((Number)value).longValue());
- }
- catch (Exception e)
- {
- throw new ConverterException(e);
- }
- }
+ // Methods implementation
+ protected Object getAsObject(String value) {
+ return Long.valueOf(value);
+ }
+
+ protected String getAsString(Object value) {
+ return Long.toString(((Number) value).longValue());
+ }
+
+ protected String getConversionMessageId() {
+ return CONVERSION_MESSAGE_ID;
+ }
}
Modified: myfaces/core/trunk/api/src/main/java/javax/faces/convert/ShortConverter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/convert/ShortConverter.java?view=diff&rev=448551&r1=448550&r2=448551
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/convert/ShortConverter.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/convert/ShortConverter.java Thu Sep 21 06:42:13 2006
@@ -15,74 +15,27 @@
*/
package javax.faces.convert;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-
/**
* see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
*
* @author Thomas Spiegl (latest modification by $Author$)
* @version $Revision$ $Date$
*/
-public class ShortConverter
- implements Converter
-{
- private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.ShortConverter.CONVERSION";
-
- // FIELDS
- public static final String CONVERTER_ID = "javax.faces.Short";
-
- // CONSTRUCTORS
- public ShortConverter()
- {
- }
-
- // METHODS
- public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
-
- if (value != null)
- {
- value = value.trim();
- if (value.length() > 0)
- {
- try
- {
- return Short.valueOf(value);
- }
- catch (NumberFormatException e)
- {
- throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
- CONVERSION_MESSAGE_ID,
- new Object[]{uiComponent.getId(),value}), e);
- }
- }
- }
- return null;
- }
+public class ShortConverter extends AbstractConverter {
+ private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.ShortConverter.CONVERSION";
- public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
- {
- if (facesContext == null) throw new NullPointerException("facesContext");
- if (uiComponent == null) throw new NullPointerException("uiComponent");
+ public static final String CONVERTER_ID = "javax.faces.Short";
- if (value == null)
- {
- return "";
- }
- if (value instanceof String)
- {
- return (String)value;
- }
- try
- {
- return Short.toString(((Number)value).shortValue());
- }
- catch (Exception e)
- {
- throw new ConverterException(e);
- }
- }
+ // Methods implementation
+ protected Object getAsObject(String value) {
+ return Short.valueOf(value);
+ }
+
+ protected String getAsString(Object value) {
+ return Short.toString(((Number) value).shortValue());
+ }
+
+ protected String getConversionMessageId() {
+ return CONVERSION_MESSAGE_ID;
+ }
}