1 package org.codehaus.nanning.attribute;
2
3 import java.util.Iterator;
4 import java.util.Properties;
5 import java.util.List;
6 import java.util.Stack;
7 import java.util.ArrayList;
8
9 import com.thoughtworks.qdox.parser.Builder;
10 import com.thoughtworks.qdox.parser.structs.ClassDef;
11 import com.thoughtworks.qdox.parser.structs.FieldDef;
12 import com.thoughtworks.qdox.parser.structs.MethodDef;
13
14 /***
15 * QDox Builder implementation for creating Properties containing attributes.
16 *
17 * <p>This Builder should be fed to the QDox Parser where it shall receive callbacks as a source file is parsed.
18 * After the file has been parsed, getEdit() can be called to retrieved the compiled classPropertiesHelper of the class.</p>
19 *
20 * <p>An AttributesBuilder can only be used to parse <b>one</b> file at a time. If the AttributesBuilder is to be reused
21 * to parse another file, the reset() method must be called.</p>
22 *
23 * @author <a href="joe@truemesh.org">Joe Walnes</a>
24 * @version $Revision: 1.1 $
25 */
26 public class AttributesBuilder implements Builder {
27
28 private final Properties currentAttributes = new Properties();
29 private Stack classPropertiesHelperStack = new Stack();
30 private List classPropertiesHelpers = new ArrayList();
31 private String packageName;
32
33
34 public void addPackage(String packageName) {
35 this.packageName = packageName;
36 }
37
38 public void addImport(String importName) {
39 }
40
41 public void addJavaDoc(String text) {
42 }
43
44 public void endClass() {
45 classPropertiesHelperStack.pop();
46 }
47
48 public void addJavaDocTag(String tag, String text) {
49 currentAttributes.setProperty(tag, text);
50 }
51
52 public void beginClass(ClassDef def) {
53 String baseClassName = "";
54 if (!classPropertiesHelperStack.isEmpty()) {
55 baseClassName = classPropertiesHelper().getClassName() + "$";
56 }
57
58 classPropertiesHelperStack.push(new ClassPropertiesHelper());
59 classPropertiesHelpers.add(classPropertiesHelper());
60
61 classPropertiesHelper().setClassName(baseClassName + def.name);
62
63 classPropertiesHelper().setPackageName(packageName);
64 addCurrentAttributes(null, null);
65 }
66
67 private ClassPropertiesHelper classPropertiesHelper() {
68 return (ClassPropertiesHelper) classPropertiesHelperStack.peek();
69 }
70
71 public void addMethod(MethodDef def) {
72 // don't build attributes for constructors as it is not supported
73 if (def.constructor) {
74 return;
75 }
76
77 final StringBuffer method = new StringBuffer();
78 method.append(def.name);
79 method.append('(');
80 for (Iterator params = def.params.iterator(); params.hasNext();) {
81 FieldDef param = (FieldDef) params.next();
82 method.append(getTypeWithoutPackage(param));
83 method.append(',');
84 }
85 if (def.params.size() > 0) {
86 // trim last comma
87 method.setLength(method.length() - 1);
88 }
89 method.append(')');
90
91 addCurrentAttributes(null, method.toString());
92 }
93
94 private String getTypeWithoutPackage(FieldDef param) {
95 String type = param.type;
96 if (type.indexOf('.') != -1) {
97 type = type.substring(type.lastIndexOf('.') + 1);
98 }
99 return type;
100 }
101
102 public void addField(FieldDef def) {
103 addCurrentAttributes(def.name, null);
104 }
105
106 private void addCurrentAttributes(String fieldName, String methodSignature) {
107 if (currentAttributes.size() > 0) {
108 final Iterator keys = currentAttributes.keySet().iterator();
109 while (keys.hasNext()) {
110 final String attributeName = (String) keys.next();
111 final String attributeValue = currentAttributes.getProperty(attributeName);
112
113 if (fieldName != null) {
114 classPropertiesHelper().loadFieldAttribute(fieldName, attributeName, attributeValue);
115
116 } else if (methodSignature != null) {
117 classPropertiesHelper().loadMethodAttribute(methodSignature, attributeName, attributeValue);
118
119 } else {
120 classPropertiesHelper().loadClassAttribute(attributeName, attributeValue);
121
122 }
123 }
124 currentAttributes.clear();
125 }
126 }
127
128 public List getClassPropertiesHelpers() {
129 assert classPropertiesHelperStack.isEmpty();
130 return classPropertiesHelpers;
131 }
132 }
This page was automatically generated by Maven