KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.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.woody.Constants;
23 import org.apache.cocoon.xml.AttributesImpl;
24 import org.xml.sax.ContentHandler JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26
27 /**
28  * This type of selection list outputs a list of items
29  * corresponding to the possible instances of an {@link org.apache.cocoon.woody.datatype.typeimpl.EnumType}.
30  * <p>Example usage:</p>
31  * <pre>
32  * &lt;wd:selection-list type="enum" class="com.example.Sex"/>
33  * </pre>
34  * <p>Produces the following output:</p>
35  * <pre>
36  * &lt;wi:selection-list>
37  * &lt;wi:item value=""/>
38  * &lt;wi:item value="com.example.Sex.MALE">
39  * &lt;wi:label>
40  * &lt;i18n:text>com.example.Sex.MALE</i18n:text>
41  * &lt;/wi:label>
42  * &lt;/wi:item>
43  * &lt;wi:item value="com.example.Sex.FEMALE">
44  * &lt;wi:label>
45  * &lt;i18n:text>com.example.Sex.FEMALE</i18n:text>
46  * &lt;/wi:label>
47  * &lt;/wi:item>
48  * &lt;/wi:selection-list>
49  * </pre>
50  * <p>If you don't want an initial null value, add a nullable="false"
51  * attribute to the wd:selection-list element.
52  *
53  * @version CVS $Id: EnumSelectionList.java 30932 2004-07-29 17:35:38Z vgritsenko $
54  */

55 public class EnumSelectionList implements SelectionList {
56     public static final String JavaDoc I18N_NS = "http://apache.org/cocoon/i18n/2.1";
57     public static final String JavaDoc I18N_PREFIX_COLON = "i18n:";
58     public static final String JavaDoc TEXT_EL = "text";
59     
60     private Datatype datatype;
61     private Class JavaDoc clazz;
62     private boolean nullable;
63
64     /**
65      * @param className
66      * @param datatype
67      */

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

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

84     public void generateSaxFragment(
85         ContentHandler JavaDoc contentHandler,
86         Locale JavaDoc locale)
87         throws SAXException JavaDoc {
88         try {
89             contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL, Constants.WI_PREFIX_COLON + SELECTION_LIST_EL, Constants.EMPTY_ATTRS);
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(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL, voidAttrs);
96                 contentHandler.endElement(Constants.WI_NS, ITEM_EL, Constants.WI_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(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL, itemAttrs);
107                     contentHandler.startElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL, Constants.EMPTY_ATTRS);
108                     // TODO: make i18n element optional
109
contentHandler.startElement(I18N_NS, TEXT_EL, I18N_PREFIX_COLON + TEXT_EL, Constants.EMPTY_ATTRS);
110                     contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length());
111                     contentHandler.endElement(I18N_NS, TEXT_EL, I18N_PREFIX_COLON + TEXT_EL);
112                     contentHandler.endElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL);
113                     contentHandler.endElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL);
114                 }
115             }
116             // End the selection-list
117
contentHandler.endElement(Constants.WI_NS, SELECTION_LIST_EL, Constants.WI_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 }
124
Popular Tags