001: /*
002: * Copyright 2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package javax.faces.convert;
018:
019: import javax.faces.component.StateHolder;
020: import javax.faces.component.UIComponent;
021: import javax.faces.context.FacesContext;
022:
023: /**
024: * see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
025: *
026: * @author Stan Silvert
027: */
028: public class EnumConverter implements Converter, StateHolder {
029:
030: public static final String CONVERTER_ID = "javax.faces.Enum";
031: public static final String ENUM_ID = "javax.faces.converter.EnumConverter.ENUM";
032: public static final String ENUM_NO_CLASS_ID = "javax.faces.converter.EnumConverter.ENUM_NO_CLASS";
033:
034: private Class targetClass;
035:
036: private boolean isTransient = false;
037:
038: /** Creates a new instance of EnumConverter */
039: public EnumConverter() {
040: }
041:
042: public EnumConverter(Class targetClass) {
043: if (!targetClass.isEnum())
044: throw new IllegalArgumentException(
045: "targetClass for EnumConverter must be an Enum");
046: this .targetClass = targetClass;
047: }
048:
049: public String getAsString(FacesContext facesContext,
050: UIComponent uiComponent, Object value)
051: throws ConverterException {
052: if (facesContext == null)
053: throw new NullPointerException(
054: "facesContext can not be null");
055: if (uiComponent == null)
056: throw new NullPointerException(
057: "uiComponent can not be null");
058: if (value == null)
059: return "";
060:
061: for (Object enumConstant : targetClass.getEnumConstants()) {
062: if (enumConstant == value)
063: return enumConstant.toString();
064: }
065:
066: return value.toString();
067: }
068:
069: public Object getAsObject(FacesContext facesContext,
070: UIComponent uiComponent, String value)
071: throws ConverterException {
072: if (facesContext == null)
073: throw new NullPointerException("facesContext");
074: if (uiComponent == null)
075: throw new NullPointerException("uiComponent");
076: if (value == null)
077: return null;
078: if (targetClass == null) {
079: Object[] params = new Object[] { value,
080: _MessageUtils.getLabel(facesContext, uiComponent) };
081: throw new ConverterException(_MessageUtils.getErrorMessage(
082: facesContext, ENUM_NO_CLASS_ID, params));
083: }
084:
085: value = value.trim();
086: if (value.length() == 0)
087: return null;
088:
089: // we know targetClass and value can't be null, so we can use Enum.valueOf
090: // instead of the hokey looping called for in the javadoc
091: try {
092: return Enum.valueOf(targetClass, value);
093: } catch (IllegalArgumentException e) {
094: Object[] params = new Object[] { value,
095: firstConstantOfEnum(),
096: _MessageUtils.getLabel(facesContext, uiComponent) };
097:
098: throw new ConverterException(_MessageUtils.getErrorMessage(
099: facesContext, ENUM_ID, params));
100: }
101: }
102:
103: // find the first constant value of the targetClass and return as a String
104: private String firstConstantOfEnum() {
105: Object[] enumConstants = targetClass.getEnumConstants();
106:
107: if (enumConstants.length != 0)
108: return enumConstants[0].toString();
109:
110: return ""; // if empty Enum
111: }
112:
113: public void restoreState(FacesContext context, Object state) {
114: targetClass = (Class) state;
115: }
116:
117: public Object saveState(FacesContext context) {
118: return targetClass;
119: }
120:
121: public void setTransient(boolean newTransientValue) {
122: isTransient = newTransientValue;
123: }
124:
125: public boolean isTransient() {
126: return isTransient;
127: }
128:
129: }
|