KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > convertor > EnumConvertor


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.datatype.convertor;
17
18 import java.lang.reflect.Field JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.lang.reflect.Modifier JavaDoc;
21 import java.util.Locale JavaDoc;
22
23 import org.apache.avalon.framework.CascadingRuntimeException;
24 import org.xml.sax.ContentHandler JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26
27 /**
28  * A {@link org.apache.cocoon.forms.datatype.convertor.Convertor Convertor}
29  * implementation for types implementing Joshua Bloch's
30  * <a HREF="http://developer.java.sun.com/developer/Books/shiftintojava/page1.html#replaceenums">
31  * typesafe enum</a> pattern.
32  *
33  * @see org.apache.cocoon.forms.datatype.typeimpl.EnumType
34  * @version $Id: EnumConvertor.java 151179 2005-02-03 16:55:16Z tim $
35  */

36 public class EnumConvertor implements Convertor {
37
38     private Class JavaDoc clazz;
39     
40     /**
41      * Construct a new EnumConvertor for a class
42      * @param className The package-qualified name of the class implementing
43      * the typesafe enum pattern.
44      */

45     public EnumConvertor(String JavaDoc className) {
46         try {
47             clazz = Class.forName(className);
48         }
49         catch (ClassNotFoundException JavaDoc e) {
50             throw new CascadingRuntimeException("Class " + className + " not found", e);
51         }
52     }
53     
54     /* (non-Javadoc)
55      * @see org.apache.cocoon.forms.datatype.convertor.Convertor#convertFromString(java.lang.String, java.util.Locale, org.apache.cocoon.forms.datatype.convertor.Convertor.FormatCache)
56      */

57     public ConversionResult convertFromString(String JavaDoc value,
58                                     Locale JavaDoc locale,
59                                     FormatCache formatCache) {
60         try {
61             // If the enum provides a "fromString" method, use it
62
try {
63                 Method JavaDoc method = getTypeClass().
64                     getMethod("fromString", new Class JavaDoc[] { String JavaDoc.class, Locale JavaDoc.class});
65                 return new ConversionResult(method.invoke(null, new Object JavaDoc[] { value, locale}));
66             } catch(NoSuchMethodException JavaDoc nsme) {
67                 // fromString method was not found, try to convert
68
// the value to a field via reflection.
69
// Strip the class name
70
int pos = value.lastIndexOf('.');
71                 if (pos >= 0) {
72                     value = value.substring(pos + 1);
73                 }
74                 Class JavaDoc clazz = getTypeClass();
75                 Field JavaDoc field = clazz.getField(value);
76                 return new ConversionResult(field.get(null));
77             }
78         } catch (Exception JavaDoc e) {
79             throw new CascadingRuntimeException("Got exception trying to convert " + value, e);
80         }
81     }
82
83     /* (non-Javadoc)
84      * @see org.apache.cocoon.forms.datatype.convertor.Convertor#convertToString(java.lang.Object, java.util.Locale, org.apache.cocoon.forms.datatype.convertor.Convertor.FormatCache)
85      */

86     public String JavaDoc convertToString(Object JavaDoc value,
87                                   Locale JavaDoc locale,
88                                   FormatCache formatCache) {
89         Class JavaDoc clazz = getTypeClass();
90         Field JavaDoc fields[] = clazz.getDeclaredFields();
91         for (int i = 0 ; i < fields.length ; ++i) {
92             try {
93                 int mods = fields[i].getModifiers();
94                 if (Modifier.isPublic(mods)
95                         && Modifier.isStatic(mods)
96                         && Modifier.isFinal(mods)
97                         && fields[i].get(null).equals(value)) {
98                     return clazz.getName() + "." + fields[i].getName();
99                 }
100             } catch (Exception JavaDoc e) {
101                 throw new CascadingRuntimeException("Got exception trying to get value of field " + fields[i], e);
102             }
103         }
104         // Fall back on toString
105
return value.toString();
106     }
107
108     /* (non-Javadoc)
109      * @see org.apache.cocoon.forms.datatype.convertor.Convertor#getTypeClass()
110      */

111     public Class JavaDoc getTypeClass() {
112         return clazz;
113     }
114
115     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
116         // intentionally empty
117
}
118 }
119
Popular Tags