KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.apache.avalon.framework.service.ServiceException;
20 import org.apache.avalon.framework.service.ServiceManager;
21 import org.apache.avalon.framework.service.Serviceable;
22 import org.apache.avalon.framework.context.Contextualizable;
23 import org.apache.avalon.framework.context.Context;
24 import org.apache.avalon.framework.context.ContextException;
25 import org.apache.avalon.framework.thread.ThreadSafe;
26 import org.apache.avalon.framework.component.Component;
27
28 import org.apache.cocoon.components.LifecycleHelper;
29 import org.apache.cocoon.forms.util.DomHelper;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.NamedNodeMap JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33
34 /**
35  * Builds {@link SelectionList}s from a JavaSelectionList class
36  *
37  * @version $Id$
38  */

39 public class JavaSelectionListBuilder extends AbstractLogEnabled
40                                       implements SelectionListBuilder,
41                                                  Contextualizable, Serviceable, ThreadSafe, Component {
42
43     /**
44      * The Avalon Context
45      */

46     private Context context;
47
48     /**
49      * The Service Manager
50      */

51     private ServiceManager manager;
52
53     public void contextualize(Context context) throws ContextException {
54         this.context = context;
55     }
56
57     public void service(ServiceManager manager) throws ServiceException {
58         this.manager = manager;
59     }
60
61     public SelectionList build(Element JavaDoc selectionListElement, Datatype datatype)
62     throws Exception JavaDoc {
63         String JavaDoc className = DomHelper.getAttribute(selectionListElement, "class");
64         boolean nullable = DomHelper.getAttributeAsBoolean(selectionListElement, "nullable", true);
65
66         try {
67             Class JavaDoc clasz = Class.forName(className);
68             if (JavaSelectionList.class.isAssignableFrom(clasz)) {
69                 JavaSelectionList list = (JavaSelectionList) clasz.newInstance();
70                 LifecycleHelper.setupComponent(list, getLogger(), this.context, this.manager, null, true);
71                 list.setDatatype(datatype);
72                 list.setNullable(nullable);
73
74                 // pass the attributes to the SelectionList
75
NamedNodeMap JavaDoc attrs = selectionListElement.getAttributes();
76                 final int size = attrs.getLength();
77                 for (int i = 0; i < size; i++) {
78                     final Node JavaDoc attr = attrs.item(i);
79                     final String JavaDoc name = attr.getNodeName();
80                     list.setAttribute(name, attr.getNodeValue());
81                 }
82
83                 return list;
84             } else {
85                 getLogger().warn("Class " + className + " does not implement JavaSelectionList, returning empty selection list.");
86                 return new StaticSelectionList(datatype);
87             }
88         } catch (Exception JavaDoc e) {
89             if (getLogger().isDebugEnabled()) {
90                 getLogger().debug("Got exception in build, re-throwing", e);
91             }
92             throw e;
93         }
94     }
95 }
96
Popular Tags