| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.apache.commons.jelly.tags.core; |
| 17 |
|
|
| 18 |
|
import java.lang.reflect.InvocationTargetException; |
| 19 |
|
import java.util.ArrayList; |
| 20 |
|
import java.util.List; |
| 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 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
public class NewTag extends BaseClassLoaderTag implements ArgTagParent { |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
private String var; |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
private String className; |
| 39 |
|
|
| 40 |
351 |
private List paramTypes = new ArrayList(); |
| 41 |
351 |
private List paramValues = new ArrayList(); |
| 42 |
|
|
| 43 |
351 |
public NewTag() { |
| 44 |
351 |
} |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
public void setVar(String var) { |
| 48 |
286 |
this.var = class="keyword">var; |
| 49 |
286 |
} |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
public void setClassName(String className) { |
| 53 |
351 |
this.className = className; |
| 54 |
351 |
} |
| 55 |
|
|
| 56 |
|
public void addArgument(Class type, Object value) { |
| 57 |
247 |
paramTypes.add(type); |
| 58 |
247 |
paramValues.add(value); |
| 59 |
247 |
} |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { |
| 64 |
351 |
ArgTag parentArg = null; |
| 65 |
351 |
if ( var == null ) { |
| 66 |
91 |
parentArg = (ArgTag)(findAncestorWithClass(ArgTag.class)); |
| 67 |
65 |
if(null == parentArg) { |
| 68 |
0 |
throw new MissingAttributeException( "var" ); |
| 69 |
|
} |
| 70 |
|
} |
| 71 |
351 |
if ( className == null ) { |
| 72 |
0 |
throw new MissingAttributeException( "className" ); |
| 73 |
|
} |
| 74 |
351 |
invokeBody(output); |
| 75 |
|
|
| 76 |
|
try { |
| 77 |
351 |
Class theClass = getClassLoader().loadClass( className ); |
| 78 |
351 |
Object object = null; |
| 79 |
351 |
if(paramTypes.size() == 0) { |
| 80 |
195 |
object = theClass.newInstance(); |
| 81 |
195 |
} else { |
| 82 |
156 |
Object[] values = paramValues.toArray(); |
| 83 |
156 |
Class[] types = (Class[])(paramTypes.toArray(new Class[paramTypes.size()])); |
| 84 |
156 |
object = ConstructorUtils.invokeConstructor(theClass,values,types); |
| 85 |
156 |
paramTypes.clear(); |
| 86 |
156 |
paramValues.clear(); |
| 87 |
|
} |
| 88 |
351 |
if(null != var) { |
| 89 |
286 |
context.setVariable(var, object); |
| 90 |
286 |
} else { |
| 91 |
65 |
parentArg.setValue(object); |
| 92 |
|
} |
| 93 |
|
} |
| 94 |
0 |
catch (ClassNotFoundException e) { |
| 95 |
0 |
throw new JellyTagException(e); |
| 96 |
|
} |
| 97 |
0 |
catch (InstantiationException e) { |
| 98 |
0 |
throw new JellyTagException(e); |
| 99 |
|
} |
| 100 |
0 |
catch (NoSuchMethodException e) { |
| 101 |
0 |
throw new JellyTagException(e); |
| 102 |
|
} |
| 103 |
0 |
catch (IllegalAccessException e) { |
| 104 |
0 |
throw new JellyTagException(e); |
| 105 |
|
} |
| 106 |
0 |
catch (InvocationTargetException e) { |
| 107 |
0 |
throw new JellyTagException(e); |
| 108 |
351 |
} |
| 109 |
351 |
} |
| 110 |
|
} |