KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.xml.sax.ContentHandler JavaDoc;
19 import org.xml.sax.SAXException JavaDoc;
20 import org.apache.cocoon.forms.FormsConstants;
21 import org.apache.cocoon.forms.datatype.convertor.Convertor;
22 import org.apache.cocoon.forms.datatype.convertor.DefaultFormatCache;
23 import org.apache.cocoon.xml.AttributesImpl;
24 import org.apache.cocoon.xml.XMLUtils;
25 import org.apache.excalibur.xml.sax.XMLizable;
26
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Locale JavaDoc;
31
32 /**
33  * An implementation of a SelectionList. Create instances of this class by using
34  * the {@link SelectionListBuilder}. This implementation is called "Static" because
35  * the items in the list are build once from its source, and then list items are
36  * cached as part of this object. In contrast, the {@link DynamicSelectionList}
37  * will retrieve its content from its source each time it's needed.
38  *
39  * @version $Id: StaticSelectionList.java 326838 2005-10-20 06:26:53Z sylvain $
40  */

41 public class StaticSelectionList implements SelectionList {
42     /**
43      * The datatype to which this selection list belongs
44      */

45     private Datatype datatype;
46
47     /**
48      * List of SelectionListItems
49      */

50     private List JavaDoc items;
51
52     public StaticSelectionList(Datatype datatype) {
53         this.datatype = datatype;
54         this.items = new ArrayList JavaDoc();
55     }
56
57     public Datatype getDatatype() {
58         return datatype;
59     }
60
61     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
62         Convertor.FormatCache formatCache = new DefaultFormatCache();
63         contentHandler.startElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES);
64
65         Iterator JavaDoc itemIt = this.items.iterator();
66         while (itemIt.hasNext()) {
67             final SelectionListItem item = (SelectionListItem) itemIt.next();
68             item.generateSaxFragment(contentHandler, locale, formatCache);
69         }
70
71         contentHandler.endElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL);
72     }
73
74     public List JavaDoc getItems() {
75         return this.items;
76     }
77
78     /**
79      * Adds a new item to this selection list.
80      * @param value a value of the correct type (i.e. the type with which this selectionlist is associated)
81      * @param label a SAX-fragment such as a {@link org.apache.cocoon.xml.SaxBuffer}, can be null
82      */

83     public void addItem(Object JavaDoc value, XMLizable label) {
84         this.items.add(new SelectionListItem(value, label));
85     }
86
87     public final class SelectionListItem {
88         private final Object JavaDoc value;
89         private final XMLizable label;
90
91         public SelectionListItem(Object JavaDoc value, XMLizable label) {
92             this.value = value;
93             this.label = label;
94         }
95
96         public Object JavaDoc getValue() {
97             return value;
98         }
99
100         public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale, Convertor.FormatCache formatCache)
101         throws SAXException JavaDoc {
102             String JavaDoc stringValue;
103             if (this.value != null) {
104                 stringValue = datatype.getConvertor().convertToString(this.value, locale, formatCache);
105             } else {
106                 // Null value translates into the empty string
107
stringValue = "";
108             }
109
110             AttributesImpl attrs = new AttributesImpl();
111             attrs.addCDATAAttribute("value", stringValue);
112             contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, attrs);
113             contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);
114             if (this.label != null) {
115                 this.label.toSAX(contentHandler);
116             } else {
117                 contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length());
118             }
119             contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
120             contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
121         }
122     }
123 }
124
Popular Tags