1 package net.sourceforge.jparam.parser;
2
3 import java.util.Iterator;
4 import java.util.List;
5
6 import net.sourceforge.jparam.JParamException;
7 import net.sourceforge.jparam.conversion.ConverterRegistry;
8 import net.sourceforge.jparam.conversion.creators.ObjectCreator;
9 import net.sourceforge.jparam.conversion.weights.ScalarConversionWeight;
10 import net.sourceforge.jparam.typename.TypeNameRegistry;
11
12 /***
13 * A description of class CompositeTreeNode
14 */
15 public class CompositeTreeNode implements TreeNode {
16 private Class actualType;
17
18 private String typeName;
19
20 private List parameters;
21
22 /***
23 * Create a new CompositeTreeNode object.
24 */
25 public CompositeTreeNode(String typeName, List parameters) {
26 this.typeName = typeName;
27 this.parameters = parameters;
28 }
29
30 /***
31 * Returns the typename.
32 */
33
34
35
36 /***
37 * Returns the parameters.
38 */
39 public Iterator getParameters() {
40 return parameters.iterator();
41 }
42
43 public ScalarConversionWeight getConversionWeight(Class expectedTypeClass)
44 throws JParamException {
45 if (actualType == null) {
46 this.actualType = TypeNameRegistry.getInstance().getJavaType(
47 typeName);
48 }
49
50 return ConverterRegistry.getInstance().getConversionWeight(actualType,
51 expectedTypeClass);
52 }
53
54 public Object getConstructedValue(Class expectedTypeClass)
55 throws JParamException {
56
57 if (actualType == null) {
58 this.actualType = TypeNameRegistry.getInstance().getJavaType(
59 typeName);
60 }
61
62 Object retVal = ObjectCreator.getInstance().create(
63 actualType,
64 (TreeNode[]) parameters
65 .toArray(new TreeNode[parameters.size()]));
66 return ConverterRegistry.getInstance().convert(expectedTypeClass,
67 retVal);
68 }
69
70 public String toString() {
71 StringBuffer res = new StringBuffer("CompositeTreeNode, typename="
72 + typeName + ", parameters=[");
73 boolean isFirst = true;
74 Iterator itr = parameters.iterator();
75 while (itr.hasNext()) {
76 if (isFirst) {
77 isFirst = false;
78 } else {
79 res.append(" ,");
80 }
81 res.append(itr.next().toString());
82 }
83 res.append(']');
84 return res.toString();
85 }
86
87 public static TreeNode createNode(String text, List args) {
88 TreeNode retVal = null;
89 if (text.startsWith("map") && args.size() == 1) {
90 Object arg0 = args.get(0);
91 if (arg0 instanceof MapLeaf) {
92 retVal = (TreeNode) arg0;
93 }
94 } else if (text.startsWith("vector") && args.size() == 1) {
95 Object arg0 = args.get(0);
96 if (arg0 instanceof VectorLeaf) {
97 retVal = (TreeNode) arg0;
98 }
99 }
100
101 if (retVal == null) {
102 retVal = new CompositeTreeNode(text, args);
103 }
104
105 return retVal;
106 }
107 }