KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > attribute > AttributesBuilder


1 package com.tirsen.nanning.attribute;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Properties JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Stack JavaDoc;
7 import java.util.ArrayList JavaDoc;
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.com">Joe Walnes</a>
24  * @version $Revision: 1.11 $
25  */

26 public class AttributesBuilder implements Builder {
27
28     private final Properties JavaDoc currentAttributes = new Properties JavaDoc();
29     private Stack JavaDoc classPropertiesHelperStack = new Stack JavaDoc();
30     private List JavaDoc classPropertiesHelpers = new ArrayList JavaDoc();
31     private String JavaDoc packageName;
32
33
34     public void addPackage(String JavaDoc packageName) {
35         this.packageName = packageName;
36     }
37
38     public void addImport(String JavaDoc importName) {
39     }
40
41     public void addJavaDoc(String JavaDoc text) {
42     }
43
44     public void endClass() {
45         classPropertiesHelperStack.pop();
46     }
47
48     public void addJavaDocTag(String JavaDoc tag, String JavaDoc text) {
49         currentAttributes.setProperty(tag, text);
50     }
51
52     public void beginClass(ClassDef def) {
53         String JavaDoc 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 JavaDoc method = new StringBuffer JavaDoc();
78         method.append(def.name);
79         method.append('(');
80         for (Iterator JavaDoc 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 JavaDoc getTypeWithoutPackage(FieldDef param) {
95         String JavaDoc 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 JavaDoc fieldName, String JavaDoc methodSignature) {
107         if (currentAttributes.size() > 0) {
108             final Iterator JavaDoc keys = currentAttributes.keySet().iterator();
109             while (keys.hasNext()) {
110                 final String JavaDoc attributeName = (String JavaDoc) keys.next();
111                 final String JavaDoc 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 JavaDoc getClassPropertiesHelpers() {
129         assert classPropertiesHelperStack.isEmpty();
130         return classPropertiesHelpers;
131     }
132 }
Popular Tags