ugo 2003/11/09 01:21:20
Modified: src/blocks/woody/samples/messages OtherMessages.xml
src/blocks/woody/java/org/apache/cocoon/woody/samples
Sex.java
src/blocks/woody/samples/forms form2_jx.xml
src/blocks/woody/java/org/apache/cocoon/woody/datatype
EnumSelectionList.java
src/blocks/woody/java/org/apache/cocoon/woody/datatype/convertor
EnumConvertor.java
src/blocks/woody/samples sitemap.xmap
Added: src/blocks/woody/samples/messages OtherMessages_it_IT.xml
Removed: src/blocks/woody/java/org/apache/cocoon/woody/datatype
Enum.java
Log:
Removed the Enum interface and the need for enumerated types to implement it.
Rewrote most of the EnumConvertor and EnumSelectionList to use reflection.
Revision Changes Path
1.2 +2 -0
cocoon-2.1/src/blocks/woody/samples/messages/OtherMessages.xml
Index: OtherMessages.xml
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/samples/messages/OtherMessages.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- OtherMessages.xml 22 Apr 2003 12:04:22 -0000 1.1
+++ OtherMessages.xml 9 Nov 2003 09:21:19 -0000 1.2
@@ -1,3 +1,5 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalogue>
+ <message key="org.apache.cocoon.woody.samples.Sex.FEMALE">femmina</message>
+ <message key="org.apache.cocoon.woody.samples.Sex.MALE">maschio</message>
</catalogue>
1.1
cocoon-2.1/src/blocks/woody/samples/messages/OtherMessages_it_IT.xml
Index: OtherMessages_it_IT.xml
===================================================================
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalogue>
<message key="org.apache.cocoon.woody.samples.Sex.FEMALE">female</message>
<message key="org.apache.cocoon.woody.samples.Sex.MALE">male</message>
</catalogue>
1.5 +4 -30
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/samples/Sex.java
Index: Sex.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/samples/Sex.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Sex.java 8 Nov 2003 14:27:03 -0000 1.4
+++ Sex.java 9 Nov 2003 09:21:19 -0000 1.5
@@ -50,17 +50,12 @@
*/
package org.apache.cocoon.woody.samples;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Locale;
-
-import org.apache.cocoon.woody.datatype.Enum;
/**
* Description of Sex.
* @version CVS $Id$
*/
-public class Sex implements Enum {
+public class Sex {
public static final Sex MALE = new Sex("M");
public static final Sex FEMALE = new Sex("F");
@@ -71,30 +66,9 @@
public String toString() {
// Will probably have some i18n support here
switch(code.charAt(0)) {
- case 'M' : return "male";
- case 'F' : return "female";
+ case 'M' : return this.getClass().getName() + ".MALE";
+ case 'F' : return this.getClass().getName() + ".FEMALE";
default : return "unknown"; // Should never happen
}
}
-
- public static Sex fromString(String value, Locale locale) {
- if (value.equals("male")) return Sex.MALE;
- if (value.equals("female")) return Sex.FEMALE;
- return null;
- }
-
- /* (non-Javadoc)
- * @see
org.apache.cocoon.woody.datatype.Enum#convertToString(java.lang.Object,
java.util.Locale)
- */
- public String convertToString(Locale locale) {
- return toString();
- }
-
- public static Collection listValues() {
- Collection values = new ArrayList(2);
- values.add(Sex.FEMALE);
- values.add(Sex.MALE);
- return values;
- }
-
}
1.6 +1 -1 cocoon-2.1/src/blocks/woody/samples/forms/form2_jx.xml
Index: form2_jx.xml
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/samples/forms/form2_jx.xml,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- form2_jx.xml 6 Nov 2003 22:58:36 -0000 1.5
+++ form2_jx.xml 9 Nov 2003 09:21:19 -0000 1.6
@@ -13,7 +13,7 @@
<br/>
I choose: ${form2bean.choose}
<br/>
- Sex: ${form2bean.sex}
+ Sex: <i18n:text
xmlns:i18n="http://apache.org/cocoon/i18n/2.1">${form2bean.sex}</i18n:text>
<table border="1">
<tr>
1.2 +21 -19
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/EnumSelectionList.java
Index: EnumSelectionList.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/EnumSelectionList.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- EnumSelectionList.java 7 Nov 2003 22:04:38 -0000 1.1
+++ EnumSelectionList.java 9 Nov 2003 09:21:19 -0000 1.2
@@ -50,9 +50,8 @@
*/
package org.apache.cocoon.woody.datatype;
-import java.lang.reflect.Method;
-import java.util.Collection;
-import java.util.Iterator;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
import java.util.Locale;
import org.apache.cocoon.woody.Constants;
@@ -98,22 +97,25 @@
Locale locale)
throws SAXException {
try {
- Method method = clazz.
- getMethod("listValues", new Class[] {});
- Iterator iter = ((Collection) method.invoke(null,
null)).iterator();
+ Field fields[] = clazz.getDeclaredFields();
contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL,
Constants.WI_PREFIX_COLON + SELECTION_LIST_EL, Constants.EMPTY_ATTRS);
- while(iter.hasNext()) {
- String stringValue = ((Enum)
iter.next()).convertToString(locale);
- // Output this item
- AttributesImpl itemAttrs = new AttributesImpl();
- itemAttrs.addCDATAAttribute("value", stringValue);
- contentHandler.startElement(Constants.WI_NS, ITEM_EL,
Constants.WI_PREFIX_COLON + ITEM_EL, itemAttrs);
- contentHandler.startElement(Constants.WI_NS, LABEL_EL,
Constants.WI_PREFIX_COLON + LABEL_EL, Constants.EMPTY_ATTRS);
- contentHandler.startElement(I18N_NS, TEXT_EL,
I18N_PREFIX_COLON + TEXT_EL, Constants.EMPTY_ATTRS);
- contentHandler.characters(stringValue.toCharArray(), 0,
stringValue.length());
- contentHandler.endElement(I18N_NS, TEXT_EL,
I18N_PREFIX_COLON + TEXT_EL);
- contentHandler.endElement(Constants.WI_NS, LABEL_EL,
Constants.WI_PREFIX_COLON + LABEL_EL);
- contentHandler.endElement(Constants.WI_NS, ITEM_EL,
Constants.WI_PREFIX_COLON + ITEM_EL);
+ for (int i = 0 ; i < fields.length ; ++i) {
+ int mods = fields[i].getModifiers();
+ if (Modifier.isPublic(mods) && Modifier.isStatic(mods)
+ && Modifier.isFinal(mods) &&
fields[i].get(null).getClass().equals(clazz)) {
+ String stringValue = clazz.getName() + "." +
fields[i].getName();
+ // Output this item
+ AttributesImpl itemAttrs = new AttributesImpl();
+ itemAttrs.addCDATAAttribute("value", stringValue);
+ contentHandler.startElement(Constants.WI_NS, ITEM_EL,
Constants.WI_PREFIX_COLON + ITEM_EL, itemAttrs);
+ contentHandler.startElement(Constants.WI_NS, LABEL_EL,
Constants.WI_PREFIX_COLON + LABEL_EL, Constants.EMPTY_ATTRS);
+ // TODO: make i18n element optional
+ contentHandler.startElement(I18N_NS, TEXT_EL,
I18N_PREFIX_COLON + TEXT_EL, Constants.EMPTY_ATTRS);
+ contentHandler.characters(stringValue.toCharArray(), 0,
stringValue.length());
+ contentHandler.endElement(I18N_NS, TEXT_EL,
I18N_PREFIX_COLON + TEXT_EL);
+ contentHandler.endElement(Constants.WI_NS, LABEL_EL,
Constants.WI_PREFIX_COLON + LABEL_EL);
+ contentHandler.endElement(Constants.WI_NS, ITEM_EL,
Constants.WI_PREFIX_COLON + ITEM_EL);
+ }
}
// End the selection-list
contentHandler.endElement(Constants.WI_NS, SELECTION_LIST_EL,
Constants.WI_PREFIX_COLON + SELECTION_LIST_EL);
1.4 +38 -20
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/convertor/EnumConvertor.java
Index: EnumConvertor.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/convertor/EnumConvertor.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- EnumConvertor.java 7 Nov 2003 14:28:05 -0000 1.3
+++ EnumConvertor.java 9 Nov 2003 09:21:19 -0000 1.4
@@ -50,11 +50,11 @@
*/
package org.apache.cocoon.woody.datatype.convertor;
+import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Locale;
import org.apache.avalon.framework.CascadingRuntimeException;
-import org.apache.cocoon.woody.datatype.Enum;
/**
* Description of EnumConvertor.
@@ -71,18 +71,28 @@
/* (non-Javadoc)
* @see
org.apache.cocoon.woody.datatype.convertor.Convertor#convertFromString(java.lang.String,
java.util.Locale,
org.apache.cocoon.woody.datatype.convertor.Convertor.FormatCache)
*/
- public Object convertFromString(
- String value,
- Locale locale,
- FormatCache formatCache) {
+ public Object convertFromString(String value,
+ Locale
locale,
+
FormatCache formatCache) {
try {
- Method method = getTypeClass().
- getMethod("fromString", new Class[] { String.class,
Locale.class});
- return method.invoke(null, new Object[] { value, locale});
+ // If the enum provides a "fromString" method, use it
+ try {
+ Method method = getTypeClass().
+ getMethod("fromString", new Class[] { String.class,
Locale.class});
+ return method.invoke(null, new Object[] { value, locale});
+ } catch(NoSuchMethodException nsme) {
+ // fromString method was not found, try to convert
+ // the value to a field via reflection.
+ // Strip the class name
+ int pos = value.lastIndexOf('.');
+ if (pos >= 0) {
+ value = value.substring(pos + 1);
+ }
+ Class clazz = getTypeClass();
+ Field field = clazz.getField(value);
+ return field.get(null);
+ }
} catch (Exception e) {
- // FIXME: I'd like to throw a o.a.c.ProcessingException here,
- // but unfortunately it's a checked exception.
- // Checked exceptions are evil, aren't they?
throw new CascadingRuntimeException("Got exception trying to
convert " + value, e);
}
}
@@ -90,11 +100,22 @@
/* (non-Javadoc)
* @see
org.apache.cocoon.woody.datatype.convertor.Convertor#convertToString(java.lang.Object,
java.util.Locale,
org.apache.cocoon.woody.datatype.convertor.Convertor.FormatCache)
*/
- public String convertToString(
- Object value,
- Locale locale,
- FormatCache formatCache) {
- return ((Enum) value).convertToString(locale);
+ public String convertToString(Object value,
+ Locale locale,
+ FormatCache
formatCache) {
+ Class clazz = getTypeClass();
+ Field fields[] = clazz.getDeclaredFields();
+ for (int i = 0 ; i < fields.length ; ++i) {
+ try {
+ if (fields[i].get(null).equals(value)) {
+ return clazz.getName() + "." + fields[i].getName();
+ }
+ } catch (Exception e) {
+ throw new CascadingRuntimeException("Got exception trying to
get value of field " + fields[i], e);
+ }
+ }
+ // Fall back on toString
+ return value.toString();
}
/* (non-Javadoc)
@@ -106,9 +127,6 @@
return Class.forName(className);
}
catch (ClassNotFoundException e) {
- // FIXME: I'd like to throw a o.a.c.ProcessingException here,
- // but unfortunately it's a checked exception.
- // Checked exceptions are evil, aren't they?
throw new CascadingRuntimeException("Class " + className + " not
found", e);
}
}
1.23 +6 -0 cocoon-2.1/src/blocks/woody/samples/sitemap.xmap
Index: sitemap.xmap
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/samples/sitemap.xmap,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- sitemap.xmap 3 Nov 2003 23:17:41 -0000 1.22
+++ sitemap.xmap 9 Nov 2003 09:21:20 -0000 1.23
@@ -214,11 +214,17 @@
<map:match pattern="form2-success-pipeline">
<map:generate src="forms/form2_data-result.xml"/>
+ <map:transform type="i18n">
+ <map:parameter name="locale" value="en-US"/>
+ </map:transform>
<map:serialize type="xml"/>
</map:match>
<map:match pattern="form2bean-success-pipeline">
<map:generate type="jx" src="forms/form2_jx.xml"/>
+ <map:transform type="i18n">
+ <map:parameter name="locale" value="en-US"/>
+ </map:transform>
<map:serialize/>
</map:match>