KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Locale JavaDoc;
23
24 import org.apache.avalon.framework.service.ServiceException;
25 import org.apache.avalon.framework.service.ServiceManager;
26 import org.apache.avalon.framework.service.Serviceable;
27 import org.apache.cocoon.forms.FormsConstants;
28 import org.apache.cocoon.forms.datatype.convertor.Convertor;
29 import org.apache.cocoon.forms.datatype.convertor.DefaultFormatCache;
30 import org.apache.cocoon.xml.AttributesImpl;
31 import org.apache.cocoon.xml.XMLUtils;
32 import org.xml.sax.ContentHandler JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34 import org.apache.excalibur.xml.sax.XMLizable;
35
36 /**
37  * Abstract implementation of a JavaSelectionList
38  */

39 public abstract class AbstractJavaSelectionList implements JavaSelectionList,
40                                                            Serviceable {
41
42     protected Datatype datatype;
43     protected ServiceManager manager;
44
45     private HashMap JavaDoc attributes;
46     private List JavaDoc items;
47     private boolean nullable;
48     private boolean rebuild;
49
50     public AbstractJavaSelectionList() {
51         this.items = new ArrayList JavaDoc();
52         this.rebuild = true;
53     }
54
55     public void service(ServiceManager manager) throws ServiceException {
56         this.manager = manager;
57     }
58
59     public String JavaDoc getAttribute(String JavaDoc name) {
60         if (this.attributes == null) {
61             return null;
62         }
63         return (String JavaDoc) this.attributes.get(name);
64     }
65
66     public void removeAttribute(String JavaDoc name) {
67         if (this.attributes != null) {
68             this.attributes.remove(name);
69         }
70     }
71
72     public void setAttribute(String JavaDoc name, String JavaDoc value) {
73         if (this.attributes == null) {
74             this.attributes = new HashMap JavaDoc();
75         }
76         this.attributes.put(name, value);
77     }
78
79     public boolean isNullable() {
80         return this.nullable;
81     }
82
83     public void setDatatype(Datatype datatype) {
84         this.datatype = datatype;
85     }
86
87     public void setNullable(boolean nullable) {
88         this.nullable = nullable;
89     }
90
91     public Datatype getDatatype() {
92         return this.datatype;
93     }
94
95     /**
96      * Enforce one rebuild on next usage of #generateSaxFragment(ContentHandler, Locale).
97      */

98     public void markForRebuild() {
99         this.rebuild = true;
100     }
101
102     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale)
103     throws SAXException JavaDoc {
104         if (this.rebuild) {
105             try {
106                 this.items.clear();
107                 this.rebuild = build();
108             } catch (Exception JavaDoc e) {
109                 e.printStackTrace();
110             }
111         }
112
113         Convertor.FormatCache formatCache = new DefaultFormatCache();
114         contentHandler.startElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES);
115
116         if (this.nullable) {
117             AttributesImpl voidAttrs = new AttributesImpl();
118             voidAttrs.addCDATAAttribute("value", "");
119             contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, voidAttrs);
120             contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
121         }
122
123         Iterator JavaDoc itemIt = items.iterator();
124         while (itemIt.hasNext()) {
125             SelectionListItem item = (SelectionListItem) itemIt.next();
126             item.generateSaxFragment(contentHandler, locale, formatCache);
127         }
128
129         contentHandler.endElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL);
130     }
131
132     /**
133      * Build the list of SelectionListItems using #addItem(Object, String).
134      * @return <code>true</code> if the list should be rebuild on each usage,
135      * <code>false</code> if it is static.
136      */

137     protected abstract boolean build() throws Exception JavaDoc;
138
139     /**
140      * Adds a new item to this selection list.
141      *
142      * @param value
143      * a value of the correct type (i.e. the type with which this
144      * selectionlist is associated)
145      * @param label
146      * string label, can be null.
147      */

148     protected void addItem(Object JavaDoc value, String JavaDoc label) {
149         this.items.add(new SelectionListItem(value, label));
150     }
151
152     /**
153      * Adds a new item to this selection list.
154      *
155      * @param value
156      * a value of the correct type (i.e. the type with which this
157      * selectionlist is associated)
158      * @param label
159      * a SAX-fragment such as a
160      * {@link org.apache.cocoon.xml.SaxBuffer}, can be null
161      */

162     protected void addItem(Object JavaDoc value, XMLizable label) {
163         this.items.add(new SelectionListItem(value, label));
164     }
165
166     protected List JavaDoc getItems() {
167         return this.items;
168     }
169
170     private final class SelectionListItem {
171         private final Object JavaDoc value;
172
173         private final Object JavaDoc label;
174
175         public SelectionListItem(Object JavaDoc value, String JavaDoc label) {
176             this.value = value;
177             this.label = label;
178         }
179
180         public SelectionListItem(Object JavaDoc value, XMLizable label) {
181             this.value = value;
182             this.label = label;
183         }
184
185         public Object JavaDoc getValue() {
186             return value;
187         }
188
189         public void generateSaxFragment(ContentHandler JavaDoc contentHandler,
190                                         Locale JavaDoc locale, Convertor.FormatCache formatCache)
191         throws SAXException JavaDoc {
192             String JavaDoc stringValue;
193             if (this.value == null) {
194                 stringValue = "";
195             } else {
196                 stringValue = datatype.getConvertor().convertToString(this.value, locale, formatCache);
197             }
198
199             AttributesImpl attrs = new AttributesImpl();
200             attrs.addCDATAAttribute("value", stringValue);
201             contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, attrs);
202             contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);
203             if (this.label == null) {
204                 contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length());
205             } else if (this.label instanceof XMLizable) {
206                 ((XMLizable) this.label).toSAX(contentHandler);
207             } else {
208                 String JavaDoc stringLabel = (String JavaDoc) this.label;
209                 contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length());
210             }
211             contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
212             contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
213         }
214     }
215 }
216
Popular Tags