KickJava   Java API By Example, From Geeks To Geeks.

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


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.w3c.dom.Document JavaDoc;
19 import org.w3c.dom.Element JavaDoc;
20 import org.w3c.dom.NodeList JavaDoc;
21 import org.w3c.dom.Node JavaDoc;
22 import org.apache.avalon.framework.context.Context;
23 import org.apache.avalon.framework.context.ContextException;
24 import org.apache.avalon.framework.context.Contextualizable;
25 import org.apache.avalon.framework.service.ServiceException;
26 import org.apache.avalon.framework.service.ServiceManager;
27 import org.apache.avalon.framework.service.Serviceable;
28 import org.apache.cocoon.forms.FormsConstants;
29 import org.apache.cocoon.forms.datatype.convertor.Convertor;
30 import org.apache.cocoon.forms.datatype.convertor.DefaultFormatCache;
31 import org.apache.cocoon.forms.datatype.convertor.ConversionResult;
32 import org.apache.cocoon.forms.util.DomHelper;
33 import org.apache.cocoon.util.Deprecation;
34 import org.apache.cocoon.util.location.LocationAttributes;
35 import org.apache.excalibur.source.Source;
36 import org.apache.excalibur.source.SourceResolver;
37 import org.apache.excalibur.xml.sax.XMLizable;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41 import java.util.Locale JavaDoc;
42
43 /**
44  * Builds {@link SelectionList}s from an XML description or an URL.
45  *
46  * <p>Note: the class {@link DynamicSelectionList} also interprets the same
47  * <code>fd:selection-list</code> XML, so if anything changes here to how
48  * that XML is interpreted, it also needs to change over there and vice
49  * versa.</p>
50  *
51  * @version $Id: DefaultSelectionListBuilder.java 328353 2005-10-25 12:59:42Z sylvain $
52  */

53 public class DefaultSelectionListBuilder implements SelectionListBuilder, Serviceable, Contextualizable {
54
55     private ServiceManager serviceManager;
56     private Context context;
57
58     public void contextualize(Context context) throws ContextException {
59         this.context = context;
60     }
61
62     public void service(ServiceManager manager) throws ServiceException {
63         this.serviceManager = manager;
64     }
65
66     public SelectionList build(Element JavaDoc selectionListElement, Datatype datatype) throws Exception JavaDoc {
67         SelectionList selectionList;
68         String JavaDoc src = selectionListElement.getAttribute("src");
69         if (src.length() > 0) {
70             boolean dynamic = false;
71             boolean usePerRequestCache = false;
72             String JavaDoc cacheType = DomHelper.getAttribute(selectionListElement, "cache", null);
73
74             // Read @cache
75
if ("request".equals(cacheType)) { // Dynamic SelectionList cached per request
76
dynamic = true;
77                 usePerRequestCache = true;
78             } else if ("none".equals(cacheType)){ // Dynamic SelectionList non cached
79
dynamic = true;
80             } else if ("static".equals(cacheType)) {
81                 // Static SelectionList (default values)
82
} else { // Checking for deprecated @dynamic
83
if (DomHelper.getAttribute(selectionListElement, "dynamic", null) != null) {
84                     Deprecation.logger.warn("'@dynamic' is deprecated in <fd:selection-list> and replaced by '@cache' at " + DomHelper.getLocation(selectionListElement));
85                 }
86                 dynamic = DomHelper.getAttributeAsBoolean(selectionListElement, "dynamic", false);
87             }
88             // Create SelectionList
89
if (dynamic) {
90                 selectionList = new DynamicSelectionList(datatype, src, usePerRequestCache, serviceManager, context);
91             } else {
92                 selectionListElement = readSelectionList(src);
93                 selectionList = buildStaticList(selectionListElement, datatype);
94             }
95         } else {
96             // selection list is defined inline
97
selectionList = buildStaticList(selectionListElement, datatype);
98         }
99         return selectionList;
100     }
101
102     private SelectionList buildStaticList(Element JavaDoc selectionListElement, Datatype datatype) throws Exception JavaDoc {
103         StaticSelectionList selectionList = new StaticSelectionList(datatype);
104         Convertor convertor = null;
105         Convertor.FormatCache formatCache = new DefaultFormatCache();
106
107         // Remove location attributes from the selection list
108
LocationAttributes.remove(selectionListElement, true);
109         
110         NodeList JavaDoc children = selectionListElement.getChildNodes();
111         for (int i = 0; children.item(i) != null; i++) {
112             Node JavaDoc node = children.item(i);
113             if (convertor == null && node instanceof Element JavaDoc && FormsConstants.DEFINITION_NS.equals(node.getNamespaceURI()) && "convertor".equals(node.getLocalName())) {
114                 Element JavaDoc convertorConfigElement = (Element JavaDoc)node;
115                 try {
116                     convertor = datatype.getBuilder().buildConvertor(convertorConfigElement);
117                 } catch (Exception JavaDoc e) {
118                     throw new SAXException JavaDoc("Error building convertor from convertor configuration embedded in selection list XML.", e);
119                 }
120             } else if (node instanceof Element JavaDoc && FormsConstants.DEFINITION_NS.equals(node.getNamespaceURI()) && "item".equals(node.getLocalName())) {
121                 if (convertor == null) {
122                     convertor = datatype.getConvertor();
123                 }
124                 Element JavaDoc element = (Element JavaDoc)node;
125                 String JavaDoc stringValue = element.getAttribute("value");
126                 Object JavaDoc value;
127                 if ("".equals(stringValue)) {
128                     // Empty value translates into the null object
129
value = null;
130                 } else {
131                     ConversionResult conversionResult = convertor.convertFromString(stringValue, Locale.US, formatCache);
132                     if (!conversionResult.isSuccessful()) {
133                         throw new Exception JavaDoc("Could not convert the value \"" + stringValue +
134                                             "\" to the type " + datatype.getDescriptiveName() +
135                                             ", defined at " + DomHelper.getLocation(element));
136                     }
137                     value = conversionResult.getResult();
138                 }
139
140                 XMLizable label = null;
141                 Element JavaDoc labelEl = DomHelper.getChildElement(element, FormsConstants.DEFINITION_NS, "label");
142                 if (labelEl != null) {
143                     label = DomHelper.compileElementContent(labelEl);
144                 }
145                 selectionList.addItem(value, label);
146             }
147         }
148
149         return selectionList;
150     }
151
152     private Element JavaDoc readSelectionList(String JavaDoc src) throws Exception JavaDoc {
153         SourceResolver resolver = null;
154         Source source = null;
155         try {
156             resolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
157             source = resolver.resolveURI(src);
158             InputSource JavaDoc inputSource = new InputSource JavaDoc(source.getInputStream());
159             inputSource.setSystemId(source.getURI());
160             Document JavaDoc document = DomHelper.parse(inputSource, this.serviceManager);
161             Element JavaDoc selectionListElement = document.getDocumentElement();
162             if (!FormsConstants.DEFINITION_NS.equals(selectionListElement.getNamespaceURI()) ||
163                     !"selection-list".equals(selectionListElement.getLocalName())) {
164                 throw new Exception JavaDoc("Expected a fd:selection-list element at " +
165                                     DomHelper.getLocation(selectionListElement));
166             }
167
168             return selectionListElement;
169         } finally {
170             if (resolver != null) {
171                 if (source != null) {
172                     resolver.release(source);
173                 }
174                 serviceManager.release(resolver);
175             }
176         }
177     }
178 }
179
Popular Tags