KickJava   Java API By Example, From Geeks To Geeks.

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

40 public class StaticSelectionList implements SelectionList {
41     /** The datatype to which this selection list belongs */
42     private Datatype datatype;
43     private List JavaDoc items = new ArrayList JavaDoc();
44
45     public StaticSelectionList(Datatype datatype) {
46         this.datatype = datatype;
47     }
48
49     public Datatype getDatatype() {
50         return datatype;
51     }
52
53     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
54         Convertor.FormatCache formatCache = new DefaultFormatCache();
55         contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL, Constants.WI_PREFIX_COLON + SELECTION_LIST_EL, Constants.EMPTY_ATTRS);
56         Iterator JavaDoc itemIt = items.iterator();
57         while (itemIt.hasNext()) {
58             SelectionListItem item = (SelectionListItem)itemIt.next();
59             item.generateSaxFragment(contentHandler, locale, formatCache);
60         }
61         contentHandler.endElement(Constants.WI_NS, SELECTION_LIST_EL, Constants.WI_PREFIX_COLON + SELECTION_LIST_EL);
62     }
63
64     public List JavaDoc getItems() {
65         return items;
66     }
67
68     /**
69      * Adds a new item to this selection list.
70      * @param value a value of the correct type (i.e. the type with which this selectionlist is associated)
71      * @param label a SAX-fragment such as a {@link org.apache.cocoon.xml.SaxBuffer}, can be null
72      */

73     public void addItem(Object JavaDoc value, XMLizable label) {
74         items.add(new SelectionListItem(value, label));
75     }
76
77     public final class SelectionListItem {
78         private final Object JavaDoc value;
79         private final XMLizable label;
80
81         public SelectionListItem(Object JavaDoc value, XMLizable label) {
82             this.value = value;
83             this.label = label;
84         }
85
86         public Object JavaDoc getValue() {
87             return value;
88         }
89
90         public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale, Convertor.FormatCache formatCache)
91                 throws SAXException JavaDoc
92         {
93             AttributesImpl itemAttrs = new AttributesImpl();
94             String JavaDoc stringValue;
95             if (this.value == null) {
96                 // Null value translates into the empty string
97
stringValue = "";
98             } else {
99                 stringValue = datatype.getConvertor().convertToString(value, locale, formatCache);
100             }
101             itemAttrs.addCDATAAttribute("value", stringValue);
102             contentHandler.startElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL, itemAttrs);
103             contentHandler.startElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL, Constants.EMPTY_ATTRS);
104             if (label != null) {
105                 label.toSAX(contentHandler);
106             } else {
107                 contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length());
108             }
109             contentHandler.endElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL);
110             contentHandler.endElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL);
111         }
112     }
113 }
114
Popular Tags