KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > EnumSelectionList


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;
17
18 import java.lang.reflect.Field JavaDoc;
19 import java.lang.reflect.Modifier JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import org.apache.cocoon.forms.FormsConstants;
23 import org.apache.cocoon.xml.AttributesImpl;
24 import org.apache.cocoon.xml.XMLUtils;
25 import org.xml.sax.ContentHandler JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27
28 /**
29  * This type of selection list outputs a list of items
30  * corresponding to the possible instances of an {@link org.apache.cocoon.forms.datatype.typeimpl.EnumType}.
31  * <p>Example usage:</p>
32  * <pre>
33  * &lt;fd:selection-list type="enum" class="com.example.Sex"/&gt;
34  * </pre>
35  * <p>Produces the following output:</p>
36  * <pre>
37  * &lt;fi:selection-list&gt;
38  * &lt;fi:item value=""/&gt;
39  * &lt;fi:item value="com.example.Sex.MALE"&gt;
40  * &lt;fi:label&gt;
41  * &lt;i18n:text&gt;com.example.Sex.MALE&lt;/i18n:text&gt;
42  * &lt;/fi:label&gt;
43  * &lt;/fi:item&gt;
44  * &lt;fi:item value="com.example.Sex.FEMALE"&gt;
45  * &lt;fi:label&gt;
46  * &lt;i18n:text&gt;com.example.Sex.FEMALE&lt;/i18n:text&gt;
47  * &lt;/fi:label&gt;
48  * &lt;/fi:item&gt;
49  * &lt;/fi:selection-list&gt;
50  * </pre>
51  *
52  * <p>If you don't want an initial null value, add a
53  * <code>nullable="false"</code> attribute to the
54  * <code>fd:selection-list</code> element.</p>
55  *
56  * @version $Id: EnumSelectionList.java 326838 2005-10-20 06:26:53Z sylvain $
57  */

58 public class EnumSelectionList implements SelectionList {
59     public static final String JavaDoc TEXT_EL = "text";
60
61     private Datatype datatype;
62     private Class JavaDoc clazz;
63     private boolean nullable;
64
65     /**
66      * @param className
67      * @param datatype
68      */

69     public EnumSelectionList(String JavaDoc className, Datatype datatype, boolean nullable) throws ClassNotFoundException JavaDoc {
70         this.datatype = datatype;
71         this.nullable = nullable;
72         this.clazz = Class.forName(className);
73     }
74
75     /* (non-Javadoc)
76      * @see org.apache.cocoon.forms.datatype.SelectionList#getDatatype()
77      */

78     public Datatype getDatatype() {
79         return datatype;
80     }
81
82     /* (non-Javadoc)
83      * @see org.apache.cocoon.forms.datatype.SelectionList#generateSaxFragment(org.xml.sax.ContentHandler, java.util.Locale)
84      */

85     public void generateSaxFragment(ContentHandler JavaDoc contentHandler,
86                                     Locale JavaDoc locale)
87     throws SAXException JavaDoc {
88         try {
89             contentHandler.startElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES);
90             Field JavaDoc fields[] = clazz.getDeclaredFields();
91             // Create void element
92
if (nullable) {
93                 AttributesImpl voidAttrs = new AttributesImpl();
94                 voidAttrs.addCDATAAttribute("value", "");
95                 contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, voidAttrs);
96                 contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
97             }
98             for (int i = 0 ; i < fields.length ; ++i) {
99                 int mods = fields[i].getModifiers();
100                 if (Modifier.isPublic(mods) && Modifier.isStatic(mods)
101                         && Modifier.isFinal(mods) && fields[i].get(null).getClass().equals(clazz)) {
102                     String JavaDoc stringValue = clazz.getName() + "." + fields[i].getName();
103                     // Output this item
104
AttributesImpl itemAttrs = new AttributesImpl();
105                     itemAttrs.addCDATAAttribute("value", stringValue);
106                     contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, itemAttrs);
107                     contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);
108                     // TODO: make i18n element optional
109
contentHandler.startElement(FormsConstants.I18N_NS, TEXT_EL, FormsConstants.I18N_PREFIX_COLON + TEXT_EL, XMLUtils.EMPTY_ATTRIBUTES);
110                     contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length());
111                     contentHandler.endElement(FormsConstants.I18N_NS, TEXT_EL, FormsConstants.I18N_PREFIX_COLON + TEXT_EL);
112                     contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
113                     contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
114                 }
115             }
116             // End the selection-list
117
contentHandler.endElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL);
118         } catch (Exception JavaDoc e) {
119             throw new SAXException JavaDoc("Got exception trying to get enum's values", e);
120         }
121     }
122 }
123
Popular Tags