KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > tags > core > NewTag


1 /*
2  * Copyright 2002,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.commons.jelly.tags.core;
17
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.commons.beanutils.ConstructorUtils;
23 import org.apache.commons.jelly.JellyTagException;
24 import org.apache.commons.jelly.MissingAttributeException;
25 import org.apache.commons.jelly.XMLOutput;
26
27 /** A tag which creates a new object of the given type
28   *
29   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
30   * @version $Revision: 155420 $
31   */

32 public class NewTag extends BaseClassLoaderTag implements ArgTagParent {
33
34     /** the variable exported */
35     private String JavaDoc var;
36
37     /** the class name of the object to instantiate */
38     private String JavaDoc className;
39
40     private List JavaDoc paramTypes = new ArrayList JavaDoc();
41     private List JavaDoc paramValues = new ArrayList JavaDoc();
42
43     public NewTag() {
44     }
45
46     /** Sets the name of the variable exported by this tag */
47     public void setVar(String JavaDoc var) {
48         this.var = var;
49     }
50
51     /** Sets the class name of the object to instantiate */
52     public void setClassName(String JavaDoc className) {
53         this.className = className;
54     }
55
56     public void addArgument(Class JavaDoc type, Object JavaDoc value) {
57         paramTypes.add(type);
58         paramValues.add(value);
59     }
60
61     // Tag interface
62
//-------------------------------------------------------------------------
63
public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
64         ArgTag parentArg = null;
65         if ( var == null ) {
66             parentArg = (ArgTag)(findAncestorWithClass(ArgTag.class));
67             if(null == parentArg) {
68                 throw new MissingAttributeException( "var" );
69             }
70         }
71         if ( className == null ) {
72             throw new MissingAttributeException( "className" );
73         }
74         invokeBody(output);
75
76         try {
77             Class JavaDoc theClass = getClassLoader().loadClass( className );
78             Object JavaDoc object = null;
79             if(paramTypes.size() == 0) {
80                 object = theClass.newInstance();
81             } else {
82                 Object JavaDoc[] values = paramValues.toArray();
83                 Class JavaDoc[] types = (Class JavaDoc[])(paramTypes.toArray(new Class JavaDoc[paramTypes.size()]));
84                 object = ConstructorUtils.invokeConstructor(theClass,values,types);
85                 paramTypes.clear();
86                 paramValues.clear();
87             }
88             if(null != var) {
89                 context.setVariable(var, object);
90             } else {
91                 parentArg.setValue(object);
92             }
93         }
94         catch (ClassNotFoundException JavaDoc e) {
95             throw new JellyTagException(e);
96         }
97         catch (InstantiationException JavaDoc e) {
98             throw new JellyTagException(e);
99         }
100         catch (NoSuchMethodException JavaDoc e) {
101             throw new JellyTagException(e);
102         }
103         catch (IllegalAccessException JavaDoc e) {
104             throw new JellyTagException(e);
105         }
106         catch (InvocationTargetException JavaDoc e) {
107             throw new JellyTagException(e);
108         }
109     }
110 }
111
Popular Tags