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;
018:
019: import org.xml.sax.ContentHandler;
020: import org.xml.sax.SAXException;
021: import org.apache.cocoon.forms.FormsConstants;
022: import org.apache.cocoon.forms.datatype.convertor.Convertor;
023: import org.apache.cocoon.forms.datatype.convertor.DefaultFormatCache;
024: import org.apache.cocoon.xml.AttributesImpl;
025: import org.apache.cocoon.xml.XMLUtils;
026: import org.apache.excalibur.xml.sax.XMLizable;
027:
028: import java.util.List;
029: import java.util.ArrayList;
030: import java.util.Iterator;
031: import java.util.Locale;
032:
033: /**
034: * An implementation of a SelectionList. Create instances of this class by using
035: * the {@link SelectionListBuilder}. This implementation is called "Static" because
036: * the items in the list are build once from its source, and then list items are
037: * cached as part of this object. In contrast, the {@link DynamicSelectionList}
038: * will retrieve its content from its source each time it's needed.
039: *
040: * @version $Id: StaticSelectionList.java 449149 2006-09-23 03:58:05Z crossley $
041: */
042: public class StaticSelectionList implements SelectionList {
043: /**
044: * The datatype to which this selection list belongs
045: */
046: private Datatype datatype;
047:
048: /**
049: * List of SelectionListItems
050: */
051: private List items;
052:
053: public StaticSelectionList(Datatype datatype) {
054: this .datatype = datatype;
055: this .items = new ArrayList();
056: }
057:
058: public Datatype getDatatype() {
059: return datatype;
060: }
061:
062: public void generateSaxFragment(ContentHandler contentHandler,
063: Locale locale) throws SAXException {
064: Convertor.FormatCache formatCache = new DefaultFormatCache();
065: contentHandler.startElement(FormsConstants.INSTANCE_NS,
066: SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON
067: + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES);
068:
069: Iterator itemIt = this .items.iterator();
070: while (itemIt.hasNext()) {
071: final SelectionListItem item = (SelectionListItem) itemIt
072: .next();
073: item.generateSaxFragment(contentHandler, locale,
074: formatCache);
075: }
076:
077: contentHandler.endElement(FormsConstants.INSTANCE_NS,
078: SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON
079: + SELECTION_LIST_EL);
080: }
081:
082: public List getItems() {
083: return this .items;
084: }
085:
086: /**
087: * Adds a new item to this selection list.
088: * @param value a value of the correct type (i.e. the type with which this selectionlist is associated)
089: * @param label a SAX-fragment such as a {@link org.apache.cocoon.xml.SaxBuffer}, can be null
090: */
091: public void addItem(Object value, XMLizable label) {
092: this .items.add(new SelectionListItem(value, label));
093: }
094:
095: /**
096: * Adds a new item to this selection list.
097: * @param value a value of the correct type (i.e. the type with which this selectionlist is associated)
098: * @param label a String label, can be null
099: */
100: public void addItem(Object value, String label) {
101: this .items.add(new SelectionListItem(value, label));
102: }
103:
104: /**
105: * Adds a new item to this selection list, where the label is set to the toString()
106: * result of the value.
107: * @param value a value of the correct type (i.e. the type with which this selectionlist is associated)
108: */
109: public void addItem(Object value) {
110: this .items.add(new SelectionListItem(value));
111: }
112:
113: public final class SelectionListItem {
114: private final Object value;
115: private final Object label;
116:
117: public SelectionListItem(Object value, XMLizable label) {
118: this .value = value;
119: this .label = label;
120: }
121:
122: public SelectionListItem(Object value, String label) {
123: this .value = value;
124: this .label = label;
125: }
126:
127: public SelectionListItem(Object value) {
128: this .value = value;
129: this .label = null;
130: }
131:
132: public Object getValue() {
133: return value;
134: }
135:
136: public void generateSaxFragment(ContentHandler contentHandler,
137: Locale locale, Convertor.FormatCache formatCache)
138: throws SAXException {
139: String stringValue;
140: if (this .value != null) {
141: stringValue = getDatatype().getConvertor()
142: .convertToString(this .value, locale,
143: formatCache);
144: } else {
145: // Null value translates into the empty string
146: stringValue = "";
147: }
148:
149: AttributesImpl attrs = new AttributesImpl();
150: attrs.addCDATAAttribute("value", stringValue);
151: contentHandler.startElement(FormsConstants.INSTANCE_NS,
152: ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON
153: + ITEM_EL, attrs);
154: contentHandler.startElement(FormsConstants.INSTANCE_NS,
155: LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON
156: + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);
157: if (this .label == null) {
158: contentHandler.characters(stringValue.toCharArray(), 0,
159: stringValue.length());
160: } else if (this .label instanceof XMLizable) {
161: ((XMLizable) this .label).toSAX(contentHandler);
162: } else {
163: String stringLabel = (String) this .label;
164: contentHandler.characters(stringLabel.toCharArray(), 0,
165: stringLabel.length());
166: }
167: contentHandler.endElement(FormsConstants.INSTANCE_NS,
168: LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON
169: + LABEL_EL);
170: contentHandler.endElement(FormsConstants.INSTANCE_NS,
171: ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON
172: + ITEM_EL);
173: }
174: }
175: }
|