KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.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
25 /**
26  * A {@link org.apache.cocoon.woody.datatype.convertor.Convertor Convertor}
27  * implementation for types implementing Joshua Bloch's
28  * <a HREF="http://developer.java.sun.com/developer/Books/shiftintojava/page1.html#replaceenums">
29  * typesafe enum</a> pattern.
30  *
31  * @see org.apache.cocoon.woody.datatype.typeimpl.EnumType
32  * @version CVS $Id: EnumConvertor.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

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

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

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

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

109     public Class JavaDoc getTypeClass() {
110         return clazz;
111     }
112 }
113
Popular Tags