KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > tree > builder > JavaTreeModelDefinitionBuilder


1 /*
2  * Copyright 1999-2005 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.formmodel.tree.builder;
17
18 import org.apache.avalon.framework.CascadingException;
19 import org.apache.avalon.framework.context.Context;
20 import org.apache.avalon.framework.context.ContextException;
21 import org.apache.avalon.framework.context.Contextualizable;
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.avalon.framework.service.ServiceException;
24 import org.apache.avalon.framework.service.ServiceManager;
25 import org.apache.avalon.framework.service.Serviceable;
26 import org.apache.cocoon.components.LifecycleHelper;
27 import org.apache.cocoon.forms.formmodel.tree.JavaTreeModelDefinition;
28 import org.apache.cocoon.forms.formmodel.tree.TreeModel;
29 import org.apache.cocoon.forms.formmodel.tree.TreeModelDefinition;
30 import org.apache.cocoon.forms.util.DomHelper;
31 import org.w3c.dom.Element JavaDoc;
32
33 /**
34  * Builds a {@link TreeModelDefinition} based on an arbitrary Java class.
35  * Avalon lifecycle will be run on the target class when instanciated.
36  *
37  * @version $Id: JavaTreeModelDefinitionBuilder.java 190962 2005-06-16 17:17:00Z sylvain $
38  */

39 public class JavaTreeModelDefinitionBuilder extends AbstractLogEnabled
40     implements TreeModelDefinitionBuilder, Contextualizable, Serviceable {
41
42     Context ctx;
43     ServiceManager manager;
44     
45     public void contextualize(Context context) throws ContextException {
46         this.ctx = context;
47     }
48
49     public void service(ServiceManager manager) throws ServiceException {
50         this.manager = manager;
51     }
52
53     public TreeModelDefinition build(Element JavaDoc treeModelElement) throws Exception JavaDoc {
54         String JavaDoc className = DomHelper.getAttribute(treeModelElement, "class");
55         
56         Class JavaDoc modelClass;
57         try {
58             modelClass = Thread.currentThread().getContextClassLoader().loadClass(className);
59         } catch(Exception JavaDoc e) {
60             throw new CascadingException("Cannot load class '" + className + "', at " +
61                     DomHelper.getLocation(treeModelElement), e);
62         }
63         
64         if (!TreeModel.class.isAssignableFrom(modelClass)) {
65             throw new Exception JavaDoc("Class '" + className + "' doesn't implement TreeModel, at " +
66                     DomHelper.getLocation(treeModelElement));
67         }
68         
69         JavaTreeModelDefinition definition = new JavaTreeModelDefinition();
70         
71         LifecycleHelper.setupComponent(definition, getLogger(), ctx, manager, null);
72         
73         definition.setModelClass(modelClass);
74         
75         return definition;
76     }
77
78 }
79
Popular Tags