1 22 23 24 package com.mchange.v2.codegen.bean; 25 26 import org.w3c.dom.*; 27 import java.lang.reflect.Modifier ; 28 import com.mchange.v1.xml.DomParseUtils; 29 30 public class ParsedPropertyBeanDocument 31 { 32 final static String [] EMPTY_SA = new String [0]; 33 34 String packageName; 35 int class_modifiers; 36 String className; 37 String superclassName; 38 String [] interfaceNames = EMPTY_SA; 39 String [] generalImports = EMPTY_SA; 40 String [] specificImports = EMPTY_SA; 41 Property[] properties; 42 43 public ParsedPropertyBeanDocument(Document doc) 44 { 45 Element rootElem = doc.getDocumentElement(); 46 this.packageName = DomParseUtils.allTextFromUniqueChild( rootElem, "package" ); 47 Element modifiersElem = DomParseUtils.uniqueImmediateChild( rootElem, "modifiers" ); 48 if (modifiersElem != null) 49 class_modifiers = parseModifiers( modifiersElem ); 50 else 51 class_modifiers = Modifier.PUBLIC; 52 53 Element importsElem = DomParseUtils.uniqueChild( rootElem, "imports" ); 54 if (importsElem != null) 55 { 56 this.generalImports = DomParseUtils.allTextFromImmediateChildElements( importsElem, "general" ); 57 this.specificImports = DomParseUtils.allTextFromImmediateChildElements( importsElem, "specific" ); 58 } 59 this.className = DomParseUtils.allTextFromUniqueChild( rootElem, "output-class" ); 60 this.superclassName = DomParseUtils.allTextFromUniqueChild( rootElem, "extends" ); 61 62 Element implementsElem = DomParseUtils.uniqueChild( rootElem, "implements" ); 63 if (implementsElem != null) 64 this.interfaceNames = DomParseUtils.allTextFromImmediateChildElements( implementsElem, "interface" ); 65 Element propertiesElem = DomParseUtils.uniqueChild( rootElem, "properties" ); 66 this.properties = findProperties( propertiesElem ); 67 } 68 69 70 public ClassInfo getClassInfo() 71 { 72 return new ClassInfo() 73 { 74 public String getPackageName() 75 { return packageName; } 76 77 public int getModifiers() 78 { return class_modifiers; } 79 80 public String getClassName() 81 { return className; } 82 83 public String getSuperclassName() 84 { return superclassName; } 85 86 public String [] getInterfaceNames() 87 { return interfaceNames; } 88 89 public String [] getGeneralImports() 90 { return generalImports; } 91 92 public String [] getSpecificImports() 93 { return specificImports; } 94 }; 95 } 96 97 public Property[] getProperties() 98 { return (Property[]) properties.clone(); } 99 100 private Property[] findProperties( Element propertiesElem ) 101 { 102 NodeList nl = DomParseUtils.immediateChildElementsByTagName( propertiesElem, "property" ); 103 int len = nl.getLength(); 104 Property[] out = new Property[ len ]; 105 for( int i = 0; i < len; ++i) 106 { 107 Element propertyElem = (Element) nl.item( i ); 108 109 int variable_modifiers; 110 String name; 111 String simpleTypeName; 112 String defensiveCopyExpression; 113 String defaultValueExpression; 114 int getter_modifiers; 115 int setter_modifiers; 116 boolean is_read_only; 117 boolean is_bound; 118 boolean is_constrained; 119 120 variable_modifiers = modifiersThroughParentElem( propertyElem, "variable", Modifier.PRIVATE ); 121 name = DomParseUtils.allTextFromUniqueChild( propertyElem, "name", true ); 122 simpleTypeName = DomParseUtils.allTextFromUniqueChild( propertyElem, "type", true ); 123 defensiveCopyExpression = DomParseUtils.allTextFromUniqueChild( propertyElem, "defensive-copy", true ); 124 defaultValueExpression = DomParseUtils.allTextFromUniqueChild( propertyElem, "default-value", true ); 125 getter_modifiers = modifiersThroughParentElem( propertyElem, "getter", Modifier.PUBLIC ); 126 setter_modifiers = modifiersThroughParentElem( propertyElem, "setter", Modifier.PUBLIC ); 127 Element readOnlyElem = DomParseUtils.uniqueChild( propertyElem, "read-only" ); 128 is_read_only = (readOnlyElem != null); 129 Element isBoundElem = DomParseUtils.uniqueChild( propertyElem, "bound" ); 130 is_bound = (isBoundElem != null); 131 Element isConstrainedElem = DomParseUtils.uniqueChild( propertyElem, "constrained" ); 132 is_constrained = (isConstrainedElem != null); 133 out[i] = new SimpleProperty( variable_modifiers, name, simpleTypeName, defensiveCopyExpression, 134 defaultValueExpression, getter_modifiers, setter_modifiers, 135 is_read_only, is_bound, is_constrained ); 136 } 137 return out; 138 } 139 140 private static int modifiersThroughParentElem( Element grandparentElem, String parentElemName, int default_mods ) 141 { 142 Element parentElem = DomParseUtils.uniqueChild( grandparentElem, parentElemName ); 143 if (parentElem != null ) 144 { 145 Element modifiersElem = DomParseUtils.uniqueChild( parentElem, "modifiers" ); 146 if (modifiersElem != null) 147 return parseModifiers( modifiersElem ); 148 else 149 return default_mods; 150 } 151 else 152 return default_mods; 153 } 154 155 private static int parseModifiers( Element modifiersElem ) 156 { 157 int out = 0; 158 String [] all_modifiers = DomParseUtils.allTextFromImmediateChildElements( modifiersElem, "modifier", true ); 159 for ( int i = 0, len = all_modifiers.length; i < len; ++i) 160 { 161 String modifier = all_modifiers[i]; 162 if ("public".equals( modifier )) out |= Modifier.PUBLIC; 163 else if ("protected".equals( modifier )) out |= Modifier.PROTECTED; 164 else if ("private".equals( modifier )) out |= Modifier.PRIVATE; 165 else if ("final".equals( modifier )) out |= Modifier.FINAL; 166 else if ("abstract".equals( modifier )) out |= Modifier.ABSTRACT; 167 else if ("static".equals( modifier )) out |= Modifier.STATIC; 168 else if ("synchronized".equals( modifier )) out |= Modifier.SYNCHRONIZED; 169 else if ("volatile".equals( modifier )) out |= Modifier.VOLATILE; 170 else if ("transient".equals( modifier )) out |= Modifier.TRANSIENT; 171 else if ("strictfp".equals( modifier )) out |= Modifier.STRICT; 172 else if ("native".equals( modifier )) out |= Modifier.NATIVE; 173 else if ("interface".equals( modifier )) out |= Modifier.INTERFACE; else throw new IllegalArgumentException ("Bad modifier: " + modifier); 175 } 176 return out; 177 } 178 179 180 } 181 | Popular Tags |