KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > DefaultFormManager


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;
17
18 import org.apache.avalon.framework.CascadingException;
19 import org.apache.avalon.framework.activity.Disposable;
20 import org.apache.avalon.framework.activity.Initializable;
21 import org.apache.avalon.framework.component.Component;
22 import org.apache.avalon.framework.configuration.Configurable;
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.logger.AbstractLogEnabled;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.Serviceable;
29 import org.apache.avalon.framework.thread.ThreadSafe;
30 import org.apache.cocoon.woody.formmodel.Form;
31 import org.apache.cocoon.woody.formmodel.FormDefinition;
32 import org.apache.cocoon.woody.formmodel.FormDefinitionBuilder;
33 import org.apache.cocoon.woody.formmodel.WidgetDefinitionBuilder;
34 import org.apache.cocoon.woody.util.DomHelper;
35 import org.apache.cocoon.woody.util.SimpleServiceSelector;
36 import org.apache.excalibur.source.Source;
37 import org.w3c.dom.Document JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39 import org.xml.sax.InputSource JavaDoc;
40
41 /**
42  * Component implementing the {@link FormManager} role.
43  *
44  * @version $Id: DefaultFormManager.java 36537 2004-08-17 20:36:47Z vgritsenko $
45  */

46 public class DefaultFormManager
47   extends AbstractLogEnabled
48   implements FormManager, ThreadSafe, Serviceable, Disposable, Configurable, Component, Initializable {
49
50     protected static final String JavaDoc PREFIX = "WoodyForm:";
51     protected ServiceManager manager;
52     protected Configuration configuration;
53     protected SimpleServiceSelector widgetDefinitionBuilderSelector;
54     protected CacheManager cacheManager;
55
56     public void service(ServiceManager serviceManager) throws ServiceException {
57         this.manager = serviceManager;
58         this.cacheManager = (CacheManager)serviceManager.lookup(CacheManager.ROLE);
59     }
60
61     /**
62      * Configurable
63      */

64     public void configure(Configuration configuration) throws ConfigurationException {
65         this.configuration = configuration;
66     }
67
68     public void initialize() throws Exception JavaDoc {
69         widgetDefinitionBuilderSelector = new SimpleServiceSelector("widget", WidgetDefinitionBuilder.class);
70         widgetDefinitionBuilderSelector.service(new ServiceManager() {
71             final String JavaDoc WIDGET_DEFINITION_BUILDER_SELECTOR_ROLE = WidgetDefinitionBuilder.class.getName() + "Selector";
72
73             public Object JavaDoc lookup(String JavaDoc name) throws ServiceException {
74                 if (WIDGET_DEFINITION_BUILDER_SELECTOR_ROLE.equals(name))
75                     return widgetDefinitionBuilderSelector;
76                 else
77                     return manager.lookup(name);
78             }
79
80             public boolean hasService(String JavaDoc name) {
81                 if (WIDGET_DEFINITION_BUILDER_SELECTOR_ROLE.equals(name))
82                     return true;
83                 else
84                     return manager.hasService(name);
85             }
86
87             public void release(Object JavaDoc service) {
88                 if (service != widgetDefinitionBuilderSelector)
89                     manager.release(service);
90             }
91         });
92         widgetDefinitionBuilderSelector.configure(configuration.getChild("widgets"));
93     }
94
95     public Form createForm(Source source) throws Exception JavaDoc {
96         FormDefinition formDefinition = getFormDefinition(source);
97         return (Form)formDefinition.createInstance();
98     }
99
100     public FormDefinition getFormDefinition(Source source) throws Exception JavaDoc {
101         FormDefinition formDefinition = (FormDefinition)this.cacheManager.get(source, PREFIX);
102         if (formDefinition == null) {
103             Document JavaDoc formDocument;
104             try {
105                 InputSource JavaDoc inputSource = new InputSource JavaDoc(source.getInputStream());
106                 inputSource.setSystemId(source.getURI());
107                 formDocument = DomHelper.parse(inputSource);
108             } catch (Exception JavaDoc e) {
109                 throw new CascadingException("Could not parse form definition from " +
110                                              source.getURI(), e);
111             }
112
113             Element JavaDoc formElement = formDocument.getDocumentElement();
114
115             // check that the root element is a wd:form element
116
if (!(formElement.getLocalName().equals("form") || Constants.WD_NS.equals(formElement.getNamespaceURI())))
117                 throw new Exception JavaDoc("Expected a Woody form element at " + DomHelper.getLocation(formElement));
118
119             FormDefinitionBuilder formDefinitionBuilder = (FormDefinitionBuilder)widgetDefinitionBuilderSelector.select("form");
120             formDefinition = (FormDefinition)formDefinitionBuilder.buildWidgetDefinition(formElement);
121             this.cacheManager.set(formDefinition, source, PREFIX);
122         }
123         return formDefinition;
124     }
125
126     /**
127      * Disposable
128      */

129     public void dispose() {
130         if (this.widgetDefinitionBuilderSelector != null) {
131             this.widgetDefinitionBuilderSelector.dispose();
132             this.widgetDefinitionBuilderSelector = null;
133         }
134         this.manager.release(this.cacheManager);
135         this.cacheManager = null;
136         this.manager = null;
137     }
138 }
139
Popular Tags