001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.datatype.convertor;
018:
019: import java.lang.reflect.Field;
020: import java.lang.reflect.Method;
021: import java.lang.reflect.Modifier;
022: import java.util.Locale;
023:
024: import org.apache.avalon.framework.CascadingRuntimeException;
025: import org.xml.sax.ContentHandler;
026: import org.xml.sax.SAXException;
027:
028: /**
029: * A {@link org.apache.cocoon.forms.datatype.convertor.Convertor Convertor}
030: * implementation for types implementing Joshua Bloch's
031: * <a href="http://developer.java.sun.com/developer/Books/shiftintojava/page1.html#replaceenums">
032: * typesafe enum</a> pattern.
033: *
034: * @see org.apache.cocoon.forms.datatype.typeimpl.EnumType
035: * @version $Id: EnumConvertor.java 449149 2006-09-23 03:58:05Z crossley $
036: */
037: public class EnumConvertor implements Convertor {
038:
039: private Class clazz;
040:
041: /**
042: * Construct a new EnumConvertor for a class
043: * @param className The package-qualified name of the class implementing
044: * the typesafe enum pattern.
045: */
046: public EnumConvertor(String className) {
047: try {
048: clazz = Class.forName(className);
049: } catch (ClassNotFoundException e) {
050: throw new CascadingRuntimeException("Class " + className
051: + " not found", e);
052: }
053: }
054:
055: /* (non-Javadoc)
056: * @see org.apache.cocoon.forms.datatype.convertor.Convertor#convertFromString(java.lang.String, java.util.Locale, org.apache.cocoon.forms.datatype.convertor.Convertor.FormatCache)
057: */
058: public ConversionResult convertFromString(String value,
059: Locale locale, FormatCache formatCache) {
060: try {
061: // If the enum provides a "fromString" method, use it
062: try {
063: Method method = getTypeClass().getMethod("fromString",
064: new Class[] { String.class, Locale.class });
065: return new ConversionResult(method.invoke(null,
066: new Object[] { value, locale }));
067: } catch (NoSuchMethodException nsme) {
068: // fromString method was not found, try to convert
069: // the value to a field via reflection.
070: // Strip the class name
071: int pos = value.lastIndexOf('.');
072: if (pos >= 0) {
073: value = value.substring(pos + 1);
074: }
075: Class clazz = getTypeClass();
076: Field field = clazz.getField(value);
077: return new ConversionResult(field.get(null));
078: }
079: } catch (Exception e) {
080: throw new CascadingRuntimeException(
081: "Got exception trying to convert " + value, e);
082: }
083: }
084:
085: /* (non-Javadoc)
086: * @see org.apache.cocoon.forms.datatype.convertor.Convertor#convertToString(java.lang.Object, java.util.Locale, org.apache.cocoon.forms.datatype.convertor.Convertor.FormatCache)
087: */
088: public String convertToString(Object value, Locale locale,
089: FormatCache formatCache) {
090: Class clazz = getTypeClass();
091: Field fields[] = clazz.getDeclaredFields();
092: for (int i = 0; i < fields.length; ++i) {
093: try {
094: int mods = fields[i].getModifiers();
095: if (Modifier.isPublic(mods) && Modifier.isStatic(mods)
096: && Modifier.isFinal(mods)
097: && fields[i].get(null).equals(value)) {
098: return clazz.getName() + "." + fields[i].getName();
099: }
100: } catch (Exception e) {
101: throw new CascadingRuntimeException(
102: "Got exception trying to get value of field "
103: + fields[i], e);
104: }
105: }
106: // Fall back on toString
107: return value.toString();
108: }
109:
110: /* (non-Javadoc)
111: * @see org.apache.cocoon.forms.datatype.convertor.Convertor#getTypeClass()
112: */
113: public Class getTypeClass() {
114: return clazz;
115: }
116:
117: public void generateSaxFragment(ContentHandler contentHandler,
118: Locale locale) throws SAXException {
119: // intentionally empty
120: }
121: }
|