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 org.xml.sax.ContentHandler;
020: import org.xml.sax.SAXException;
021: import org.apache.cocoon.woody.Constants;
022: import org.apache.cocoon.woody.datatype.convertor.Convertor;
023: import org.apache.cocoon.woody.datatype.convertor.DefaultFormatCache;
024: import org.apache.cocoon.xml.AttributesImpl;
025: import org.apache.excalibur.xml.sax.XMLizable;
026:
027: import java.util.List;
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.Locale;
031:
032: /**
033: * An implementation of a SelectionList. Create instances of this class by using
034: * the {@link SelectionListBuilder}. This implementation is called "Static" because
035: * the items in the list are build once from its source, and then list items are
036: * cached as part of this object. In contrast, the {@link DynamicSelectionList}
037: * will retrieve its content from its source each time it's needed.
038: *
039: * @version $Id: StaticSelectionList.java 433543 2006-08-22 06:22:54Z crossley $
040: */
041: public class StaticSelectionList implements SelectionList {
042: /** The datatype to which this selection list belongs */
043: private Datatype datatype;
044: private List items = new ArrayList();
045:
046: public StaticSelectionList(Datatype datatype) {
047: this .datatype = datatype;
048: }
049:
050: public Datatype getDatatype() {
051: return datatype;
052: }
053:
054: public void generateSaxFragment(ContentHandler contentHandler,
055: Locale locale) throws SAXException {
056: Convertor.FormatCache formatCache = new DefaultFormatCache();
057: contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL,
058: Constants.WI_PREFIX_COLON + SELECTION_LIST_EL,
059: Constants.EMPTY_ATTRS);
060: Iterator itemIt = items.iterator();
061: while (itemIt.hasNext()) {
062: SelectionListItem item = (SelectionListItem) itemIt.next();
063: item.generateSaxFragment(contentHandler, locale,
064: formatCache);
065: }
066: contentHandler.endElement(Constants.WI_NS, SELECTION_LIST_EL,
067: Constants.WI_PREFIX_COLON + SELECTION_LIST_EL);
068: }
069:
070: public List getItems() {
071: return items;
072: }
073:
074: /**
075: * Adds a new item to this selection list.
076: * @param value a value of the correct type (i.e. the type with which this selectionlist is associated)
077: * @param label a SAX-fragment such as a {@link org.apache.cocoon.xml.SaxBuffer}, can be null
078: */
079: public void addItem(Object value, XMLizable label) {
080: items.add(new SelectionListItem(value, label));
081: }
082:
083: public final class SelectionListItem {
084: private final Object value;
085: private final XMLizable label;
086:
087: public SelectionListItem(Object value, XMLizable label) {
088: this .value = value;
089: this .label = label;
090: }
091:
092: public Object getValue() {
093: return value;
094: }
095:
096: public void generateSaxFragment(ContentHandler contentHandler,
097: Locale locale, Convertor.FormatCache formatCache)
098: throws SAXException {
099: AttributesImpl itemAttrs = new AttributesImpl();
100: String stringValue;
101: if (this .value == null) {
102: // Null value translates into the empty string
103: stringValue = "";
104: } else {
105: stringValue = datatype.getConvertor().convertToString(
106: value, locale, formatCache);
107: }
108: itemAttrs.addCDATAAttribute("value", stringValue);
109: contentHandler.startElement(Constants.WI_NS, ITEM_EL,
110: Constants.WI_PREFIX_COLON + ITEM_EL, itemAttrs);
111: contentHandler.startElement(Constants.WI_NS, LABEL_EL,
112: Constants.WI_PREFIX_COLON + LABEL_EL,
113: Constants.EMPTY_ATTRS);
114: if (label != null) {
115: label.toSAX(contentHandler);
116: } else {
117: contentHandler.characters(stringValue.toCharArray(), 0,
118: stringValue.length());
119: }
120: contentHandler.endElement(Constants.WI_NS, LABEL_EL,
121: Constants.WI_PREFIX_COLON + LABEL_EL);
122: contentHandler.endElement(Constants.WI_NS, ITEM_EL,
123: Constants.WI_PREFIX_COLON + ITEM_EL);
124: }
125: }
126: }
|