1 package com.tirsen.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 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 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 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 }
| Popular Tags
|