001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.tags.core;
017:
018: import java.lang.reflect.InvocationTargetException;
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.apache.commons.beanutils.ConstructorUtils;
023: import org.apache.commons.jelly.JellyTagException;
024: import org.apache.commons.jelly.MissingAttributeException;
025: import org.apache.commons.jelly.XMLOutput;
026:
027: /** A tag which creates a new object of the given type
028: *
029: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
030: * @version $Revision: 155420 $
031: */
032: public class NewTag extends BaseClassLoaderTag implements ArgTagParent {
033:
034: /** the variable exported */
035: private String var;
036:
037: /** the class name of the object to instantiate */
038: private String className;
039:
040: private List paramTypes = new ArrayList();
041: private List paramValues = new ArrayList();
042:
043: public NewTag() {
044: }
045:
046: /** Sets the name of the variable exported by this tag */
047: public void setVar(String var) {
048: this .var = var;
049: }
050:
051: /** Sets the class name of the object to instantiate */
052: public void setClassName(String className) {
053: this .className = className;
054: }
055:
056: public void addArgument(Class type, Object value) {
057: paramTypes.add(type);
058: paramValues.add(value);
059: }
060:
061: // Tag interface
062: //-------------------------------------------------------------------------
063: public void doTag(XMLOutput output)
064: throws MissingAttributeException, JellyTagException {
065: ArgTag parentArg = null;
066: if (var == null) {
067: parentArg = (ArgTag) (findAncestorWithClass(ArgTag.class));
068: if (null == parentArg) {
069: throw new MissingAttributeException("var");
070: }
071: }
072: if (className == null) {
073: throw new MissingAttributeException("className");
074: }
075: invokeBody(output);
076:
077: try {
078: Class theClass = getClassLoader().loadClass(className);
079: Object object = null;
080: if (paramTypes.size() == 0) {
081: object = theClass.newInstance();
082: } else {
083: Object[] values = paramValues.toArray();
084: Class[] types = (Class[]) (paramTypes
085: .toArray(new Class[paramTypes.size()]));
086: object = ConstructorUtils.invokeConstructor(theClass,
087: values, types);
088: paramTypes.clear();
089: paramValues.clear();
090: }
091: if (null != var) {
092: context.setVariable(var, object);
093: } else {
094: parentArg.setValue(object);
095: }
096: } catch (ClassNotFoundException e) {
097: throw new JellyTagException(e);
098: } catch (InstantiationException e) {
099: throw new JellyTagException(e);
100: } catch (NoSuchMethodException e) {
101: throw new JellyTagException(e);
102: } catch (IllegalAccessException e) {
103: throw new JellyTagException(e);
104: } catch (InvocationTargetException e) {
105: throw new JellyTagException(e);
106: }
107: }
108: }
|