KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.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.service.ServiceException;
23 import org.apache.avalon.framework.service.ServiceManager;
24 import org.apache.avalon.framework.service.Serviceable;
25 import org.apache.cocoon.woody.Constants;
26 import org.apache.cocoon.woody.datatype.convertor.Convertor;
27 import org.apache.cocoon.woody.datatype.convertor.DefaultFormatCache;
28 import org.apache.cocoon.woody.util.DomHelper;
29 import org.apache.excalibur.source.Source;
30 import org.apache.excalibur.source.SourceResolver;
31 import org.apache.excalibur.xml.sax.XMLizable;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 import java.util.Locale JavaDoc;
36
37 /**
38  * Builds {@link SelectionList}s from an XML description or an URL.
39  *
40  * <p>Note: the class {@link DynamicSelectionList} also interprets the same wd:selection-list XML, so if
41  * anything changes here to how that XML is interpreted, it also needs to change over there and vice versa.
42  *
43  * @version CVS $Id: DefaultSelectionListBuilder.java 30932 2004-07-29 17:35:38Z vgritsenko $
44  */

45 public class DefaultSelectionListBuilder implements SelectionListBuilder, Serviceable {
46     
47     private ServiceManager serviceManager;
48
49     public void service(ServiceManager manager) throws ServiceException {
50         this.serviceManager = manager;
51     }
52     
53     public SelectionList build(Element JavaDoc selectionListElement, Datatype datatype) throws Exception JavaDoc {
54         SelectionList selectionList;
55         String JavaDoc src = selectionListElement.getAttribute("src");
56         if (src.length() > 0) {
57             boolean dynamic = DomHelper.getAttributeAsBoolean(selectionListElement, "dynamic", false);
58             if (!dynamic) {
59                 selectionListElement = readSelectionList(src);
60                 selectionList = buildStaticList(selectionListElement, datatype);
61             } else {
62                 selectionList = new DynamicSelectionList(datatype, src, serviceManager);
63             }
64         } else {
65             // selection list is defined inline
66
selectionList = buildStaticList(selectionListElement, datatype);
67         }
68         
69         return selectionList;
70     }
71    
72     private SelectionList buildStaticList(Element JavaDoc selectionListElement, Datatype datatype) throws Exception JavaDoc {
73         StaticSelectionList selectionList = new StaticSelectionList(datatype);
74         Convertor convertor = null;
75         Convertor.FormatCache formatCache = new DefaultFormatCache();
76
77         NodeList JavaDoc children = selectionListElement.getChildNodes();
78         for (int i = 0; children.item(i) != null; i++) {
79             Node JavaDoc node = children.item(i);
80             if (convertor == null && node instanceof Element JavaDoc && Constants.WD_NS.equals(node.getNamespaceURI()) && "convertor".equals(node.getLocalName())) {
81                 Element JavaDoc convertorConfigElement = (Element JavaDoc)node;
82                 try {
83                     convertor = datatype.getBuilder().buildConvertor(convertorConfigElement);
84                 } catch (Exception JavaDoc e) {
85                     throw new SAXException JavaDoc("Error building convertor from convertor configuration embedded in selection list XML.", e);
86                 }
87             } else if (node instanceof Element JavaDoc && Constants.WD_NS.equals(node.getNamespaceURI()) && "item".equals(node.getLocalName())) {
88                 if (convertor == null) {
89                     convertor = datatype.getConvertor();
90                 }
91                 Element JavaDoc element = (Element JavaDoc)node;
92                 String JavaDoc stringValue = element.getAttribute("value");
93                 Object JavaDoc value;
94                 if ("".equals(stringValue)) {
95                     // Empty value translates into the null object
96
value = null;
97                 } else {
98                     value = convertor.convertFromString(stringValue, Locale.US, formatCache);
99                     if (value == null) {
100                         throw new Exception JavaDoc("Could not convert the value \"" + stringValue +
101                                             "\" to the type " + datatype.getDescriptiveName() +
102                                             ", defined at " + DomHelper.getLocation(element));
103                     }
104                 }
105
106                 XMLizable label = null;
107                 Element JavaDoc labelEl = DomHelper.getChildElement(element, Constants.WD_NS, "label");
108                 if (labelEl != null) {
109                     label = DomHelper.compileElementContent(labelEl);
110                 }
111                 selectionList.addItem(value, label);
112             }
113         }
114
115         return selectionList;
116     }
117
118     private Element JavaDoc readSelectionList(String JavaDoc src) throws Exception JavaDoc {
119         SourceResolver resolver = null;
120         Source source = null;
121         try {
122             resolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
123             source = resolver.resolveURI(src);
124             InputSource JavaDoc inputSource = new InputSource JavaDoc(source.getInputStream());
125             inputSource.setSystemId(source.getURI());
126             Document JavaDoc document = DomHelper.parse(inputSource);
127             Element JavaDoc selectionListElement = document.getDocumentElement();
128             if (!Constants.WD_NS.equals(selectionListElement.getNamespaceURI()) || !"selection-list".equals(selectionListElement.getLocalName()))
129                 throw new Exception JavaDoc("Excepted a wd:selection-list element at " + DomHelper.getLocation(selectionListElement));
130             return selectionListElement;
131         } finally {
132             if (source != null)
133                 resolver.release(source);
134             if (resolver != null)
135                 serviceManager.release(resolver);
136         }
137     }
138 }
139
Popular Tags