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.woody.datatype;
018:
019: import java.lang.reflect.Field;
020: import java.lang.reflect.Modifier;
021: import java.util.Locale;
022:
023: import org.apache.cocoon.woody.Constants;
024: import org.apache.cocoon.xml.AttributesImpl;
025: import org.xml.sax.ContentHandler;
026: import org.xml.sax.SAXException;
027:
028: /**
029: * This type of selection list outputs a list of items
030: * corresponding to the possible instances of an {@link org.apache.cocoon.woody.datatype.typeimpl.EnumType}.
031: * <p>Example usage:</p>
032: * <pre>
033: * <wd:selection-list type="enum" class="com.example.Sex"/>
034: * </pre>
035: * <p>Produces the following output:</p>
036: * <pre>
037: * <wi:selection-list>
038: * <wi:item value=""/>
039: * <wi:item value="com.example.Sex.MALE">
040: * <wi:label>
041: * <i18n:text>com.example.Sex.MALE</i18n:text>
042: * </wi:label>
043: * </wi:item>
044: * <wi:item value="com.example.Sex.FEMALE">
045: * <wi:label>
046: * <i18n:text>com.example.Sex.FEMALE</i18n:text>
047: * </wi:label>
048: * </wi:item>
049: * </wi:selection-list>
050: * </pre>
051: * <p>If you don't want an initial null value, add a nullable="false"
052: * attribute to the wd:selection-list element.
053: *
054: * @version CVS $Id: EnumSelectionList.java 433543 2006-08-22 06:22:54Z crossley $
055: */
056: public class EnumSelectionList implements SelectionList {
057: public static final String I18N_NS = "http://apache.org/cocoon/i18n/2.1";
058: public static final String I18N_PREFIX_COLON = "i18n:";
059: public static final String TEXT_EL = "text";
060:
061: private Datatype datatype;
062: private Class clazz;
063: private boolean nullable;
064:
065: /**
066: * @param className
067: * @param datatype
068: */
069: public EnumSelectionList(String className, Datatype datatype,
070: boolean nullable) throws ClassNotFoundException {
071: this .datatype = datatype;
072: this .nullable = nullable;
073: this .clazz = Class.forName(className);
074: }
075:
076: /* (non-Javadoc)
077: * @see org.apache.cocoon.woody.datatype.SelectionList#getDatatype()
078: */
079: public Datatype getDatatype() {
080: return datatype;
081: }
082:
083: /* (non-Javadoc)
084: * @see org.apache.cocoon.woody.datatype.SelectionList#generateSaxFragment(org.xml.sax.ContentHandler, java.util.Locale)
085: */
086: public void generateSaxFragment(ContentHandler contentHandler,
087: Locale locale) throws SAXException {
088: try {
089: contentHandler.startElement(Constants.WI_NS,
090: SELECTION_LIST_EL, Constants.WI_PREFIX_COLON
091: + SELECTION_LIST_EL, Constants.EMPTY_ATTRS);
092: Field fields[] = clazz.getDeclaredFields();
093: // Create void element
094: if (nullable) {
095: AttributesImpl voidAttrs = new AttributesImpl();
096: voidAttrs.addCDATAAttribute("value", "");
097: contentHandler.startElement(Constants.WI_NS, ITEM_EL,
098: Constants.WI_PREFIX_COLON + ITEM_EL, voidAttrs);
099: contentHandler.endElement(Constants.WI_NS, ITEM_EL,
100: Constants.WI_PREFIX_COLON + ITEM_EL);
101: }
102: for (int i = 0; i < fields.length; ++i) {
103: int mods = fields[i].getModifiers();
104: if (Modifier.isPublic(mods) && Modifier.isStatic(mods)
105: && Modifier.isFinal(mods)
106: && fields[i].get(null).getClass().equals(clazz)) {
107: String stringValue = clazz.getName() + "."
108: + fields[i].getName();
109: // Output this item
110: AttributesImpl itemAttrs = new AttributesImpl();
111: itemAttrs.addCDATAAttribute("value", stringValue);
112: contentHandler.startElement(Constants.WI_NS,
113: ITEM_EL, Constants.WI_PREFIX_COLON
114: + ITEM_EL, itemAttrs);
115: contentHandler.startElement(Constants.WI_NS,
116: LABEL_EL, Constants.WI_PREFIX_COLON
117: + LABEL_EL, Constants.EMPTY_ATTRS);
118: // TODO: make i18n element optional
119: contentHandler.startElement(I18N_NS, TEXT_EL,
120: I18N_PREFIX_COLON + TEXT_EL,
121: Constants.EMPTY_ATTRS);
122: contentHandler.characters(
123: stringValue.toCharArray(), 0, stringValue
124: .length());
125: contentHandler.endElement(I18N_NS, TEXT_EL,
126: I18N_PREFIX_COLON + TEXT_EL);
127: contentHandler.endElement(Constants.WI_NS,
128: LABEL_EL, Constants.WI_PREFIX_COLON
129: + LABEL_EL);
130: contentHandler.endElement(Constants.WI_NS, ITEM_EL,
131: Constants.WI_PREFIX_COLON + ITEM_EL);
132: }
133: }
134: // End the selection-list
135: contentHandler.endElement(Constants.WI_NS,
136: SELECTION_LIST_EL, Constants.WI_PREFIX_COLON
137: + SELECTION_LIST_EL);
138: } catch (Exception e) {
139: throw new SAXException(
140: "Got exception trying to get enum's values", e);
141: }
142: }
143:
144: }
|