1 23 24 package tests.org.enhydra.xml.xhtml.dominfo; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.lang.reflect.Method ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import java.util.TreeMap ; 34 35 import org.enhydra.error.FatalExceptionError; 36 37 41 class DOMInfo { 42 45 public class ElementProperty { 46 47 private String fName; 48 49 50 private String fNameLower; 51 52 53 private Method fAccessor; 54 55 56 private Method fMutator; 57 58 59 ElementProperty(String name) { 60 fName = name; 61 fNameLower = name.toLowerCase(); 62 } 63 64 67 void addAccessor(Method method) { 68 if (method.getName().startsWith("get")) { 69 fAccessor = method; 70 } else { 71 fMutator = method; 72 } 73 } 74 75 76 public String getName() { 77 return fName; 78 } 79 80 81 public String getNameLower() { 82 return fNameLower; 83 } 84 85 86 public String getAttrName() { 87 return fNameLower; 88 } 89 90 91 public Method getAccessor() { 92 return fAccessor; 93 } 94 95 96 public Method getMutator() { 97 return fMutator; 98 } 99 100 101 public void dump(PrintWriter out, 102 int level) { 103 PrintUtils.printIndent(out, level); 104 out.println(fName + ": " + 105 ((fAccessor == null) ? "null" : fAccessor.getName()) 106 + "/" + 107 ((fMutator == null) ? "null" : fMutator.getName())); 108 } 109 } 110 111 114 public class ElementClass { 115 116 private Class fClassObj; 117 118 122 private Map fImplements = new TreeMap (); 123 124 129 private Map fProperties = new TreeMap (); 130 131 132 private void processPropertyMethod(Method method) { 133 String name = method.getName().substring(3); 134 String nameLower = name.toLowerCase(); 135 ElementProperty prop = (ElementProperty)fProperties.get(nameLower); 136 if (prop == null) { 137 prop = new ElementProperty(name); 138 fProperties.put(prop.getNameLower(), prop); 139 } 140 prop.addAccessor(method); 141 } 142 143 144 private void processMethod(Method method) { 145 if (isPropertyMethod(method)) { 146 processPropertyMethod(method); 147 } 148 } 149 150 151 private void findProperties() { 152 Method [] methods = fClassObj.getMethods(); 153 for (int idx = 0; idx < methods.length; idx++) { 154 processMethod(methods[idx]); 155 } 156 } 157 158 163 private void processImplements(Class classObj) { 164 if (classObj.isInterface() && !fImplements.containsKey(classObj.getName())) { 165 fImplements.put(classObj.getName(), classObj); 166 findImplements(classObj); 167 } 168 } 169 170 174 private void findImplements(Class classObj) { 175 Class [] ifaces = classObj.getInterfaces(); 176 for (int idx = 0; idx < ifaces.length; idx++) { 177 if (!fImplements.containsKey(ifaces[idx].getName())) { 178 fImplements.put(ifaces[idx].getName(), ifaces[idx]); 179 findImplements(ifaces[idx]); 180 } 181 } 182 Class superObj = classObj.getSuperclass(); 183 if (superObj != null) { 184 processImplements(superObj); 185 } 186 } 187 188 189 public ElementClass(Class classObj) { 190 fClassObj = classObj; 191 findProperties(); 192 findImplements(fClassObj); 193 } 194 195 196 public String getName() { 197 return fClassObj.getName(); 198 } 199 200 201 public Set getPropertyNames() { 202 return fProperties.keySet(); 203 } 204 205 206 public ElementProperty getProperty(String name) { 207 ElementProperty prop = (ElementProperty)fProperties.get(name.toLowerCase()); 208 if (prop == null) { 209 throw new Error ("Can't find property \"" + name + "\" of element \"" + getName() + "\""); 210 } 211 return prop; 212 } 213 214 215 public Set getImplementsNames() { 216 return fImplements.keySet(); 217 } 218 219 220 public void dump(PrintWriter out, 221 int level) { 222 PrintUtils.printIndent(out, level); 223 out.println(getName()); 224 225 PrintUtils.printIndent(out, level+1); 226 out.println("Implements:"); 227 Iterator impls = getImplementsNames().iterator(); 228 while (impls.hasNext()) { 229 PrintUtils.printIndent(out, level+2); 230 out.println((String )impls.next()); 231 } 232 233 PrintUtils.printIndent(out, level+1); 234 out.println("Properties:"); 235 Iterator propNames = getPropertyNames().iterator(); 236 while (propNames.hasNext()) { 237 getProperty((String )propNames.next()).dump(out, level+2); 238 } 239 } 240 241 242 public String toString() { 243 return fClassObj.getName(); 244 } 245 } 246 247 248 private Set fDropElementNames = new HashSet (); 249 250 251 private Set fDropPropertyMethodNames = new HashSet (); 252 253 254 private Map fElements = new TreeMap (); 255 256 257 private boolean dropElementClass(String className) { 258 return fDropElementNames.contains(className.toLowerCase()); 259 } 260 261 262 private boolean isPropertyMethod(Method method) { 263 String name = method.getName(); 264 return ((name.startsWith("get") || name.startsWith("set")) && !fDropPropertyMethodNames.contains(name)); 265 } 266 267 268 private void addDropPropertyMethod(String name) { 269 fDropPropertyMethodNames.add(name); 270 } 271 272 273 private void addDropPropertyMethods(Class classObj) { 274 Method [] methods = classObj.getMethods(); 275 for (int idx = 0; idx < methods.length; idx++) { 276 if (isPropertyMethod(methods[idx])) { 277 fDropPropertyMethodNames.add(methods[idx].getName()); 278 } 279 } 280 } 281 282 283 public DOMInfo() { 284 addDropPropertyMethods(org.w3c.dom.Element .class); 285 addDropPropertyMethods(org.enhydra.apache.xerces.dom.ElementImpl.class); 286 addDropPropertyMethods(org.enhydra.apache.xerces.dom.ElementNSImpl.class); 287 } 288 289 290 291 public DOMInfo(String pkgName) { 292 this(); 293 addElementsFromClassPath(pkgName); 294 } 295 296 297 public Set getElementNames() { 298 return fElements.keySet(); 299 } 300 301 302 public ElementClass getElement(String name) { 303 ElementClass element = (ElementClass)fElements.get(name); 304 if (element == null) { 305 throw new Error ("Can't find element \"" + name + "\""); 306 } 307 return element; 308 } 309 310 311 public Set getElementClassSet() { 312 HashSet set = new HashSet (); 313 314 Iterator elementNames = getElementNames().iterator(); 315 while (elementNames.hasNext()) { 316 set.add(getElement((String )elementNames.next())); 317 } 318 return set; 319 } 320 321 322 private void addElement(ElementClass element) { 323 if (fElements.containsKey(element.getName())) { 324 throw new Error ("Element class info already exists: " + element.getName()); 325 } 326 fElements.put(element.getName(), element); 327 } 328 329 330 public void addElement(Class element) { 331 addElement(new ElementClass(element)); 332 } 333 334 335 private String dropSuffix(String suffix, 336 String name) { 337 if (name.regionMatches(name.length()-suffix.length(), suffix, 0, suffix.length())) { 338 name = name.substring(0, name.length()-suffix.length()); 339 } 340 return name; 341 } 342 343 344 private String cleanUnqualClassName(String name) { 345 name = dropSuffix(".class", name); 347 name = dropSuffix(".java", name); 348 349 int idx = name.lastIndexOf('.'); 351 if (idx >= 0) { 352 name = name.substring(idx+1); 353 } 354 return name; 355 } 356 357 358 359 public void addElement(String className) { 360 try { 361 addElement(Class.forName(className)); 362 } catch (ClassNotFoundException except) { 363 throw new FatalExceptionError(except); 364 } 365 } 366 367 368 public void addElement(String pkgName, 369 String unqualClassName) { 370 String className = pkgName + "." + cleanUnqualClassName(unqualClassName); 371 try { 372 addElement(Class.forName(className)); 373 } catch (ClassNotFoundException except) { 374 throw new FatalExceptionError(except); 375 } 376 } 377 378 379 public void addElements(String pkgName, 380 String [] unqualClassNames) { 381 for (int idx = 0; idx < unqualClassNames.length; idx++) { 382 addElement(pkgName, unqualClassNames[idx]); 383 } 384 } 385 386 390 public void addElementsFromClassPath(String pkgName) { 391 String [] classes = ElementClassMappings.getPackageElementClasses(pkgName); 392 for (int idx = 0; idx < classes.length; idx++) { 393 addElement(classes[idx]); 394 } 395 } 396 397 400 public void dump(PrintWriter out) { 401 Iterator elementNames = getElementNames().iterator(); 402 while (elementNames.hasNext()) { 403 getElement((String )elementNames.next()).dump(out, 0); 404 } 405 } 406 407 410 public static void main(String [] args) throws IOException { 411 if (args.length < 1) { 412 System.err.println("Wrong # args: DOMInfo package [classFile1 classFile2 ...]]"); 413 System.exit(1); 414 } 415 DOMInfo domInfo = new DOMInfo(); 416 417 if (args.length == 1) { 418 domInfo.addElementsFromClassPath(args[0]); 419 } else { 420 for (int idx = 1; idx < args.length; idx++) { 421 domInfo.addElement(args[0], args[idx]); 422 } 423 } 424 domInfo.dump(new PrintWriter (System.out, true)); 425 } 426 } 427 | Popular Tags |