1 23 24 package org.enhydra.xml.xmlc.codegen; 25 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.Set ; 29 import java.util.TreeMap ; 30 import java.util.TreeSet ; 31 32 import org.enhydra.xml.xmlc.misc.ReadIterator; 33 34 36 41 public final class JavaClass { 42 43 protected String fPackageName; 44 protected String fName; 45 protected int fModifiers; 46 protected String [] fDoc; 47 48 51 private TreeSet fClassImports = new TreeSet (); 52 53 56 private TreeSet fImports = new TreeSet (); 57 58 61 private String fExtends; 62 63 67 private String fInterface; 68 69 72 private TreeSet fImplements = new TreeSet (); 73 74 77 private TreeMap fFields = new TreeMap (); 78 79 82 private JavaCode fClassInitializer = new JavaCode();; 83 84 87 private ArrayList fConstructors = new ArrayList (); 88 89 92 private TreeMap fMethods = new TreeMap (); 93 94 103 public JavaClass(String packageName, 104 String name, 105 int modifiers, 106 String [] doc) { 107 fPackageName = packageName; 108 if (fPackageName.length() == 0) { 109 fPackageName = null; } 111 fName = name; 112 fModifiers = modifiers; 113 fDoc = (String [])doc.clone(); 114 } 115 116 119 public void setInterface(String interfaceName) { 120 fInterface = interfaceName; 121 } 122 123 129 public void addClassImport(String importSpec) { 130 fClassImports.add(importSpec); 131 } 132 133 139 public void addClassImports(String [] importSpecs) { 140 for (int idx = 0; idx < importSpecs.length; idx++) { 141 addClassImport(importSpecs[idx]); 142 } 143 } 144 145 150 public void addImport(String importSpec) { 151 fImports.add(importSpec); 152 } 153 154 159 public void addImports(String [] importSpecs) { 160 for (int idx = 0; idx < importSpecs.length; idx++) { 161 addImport(importSpecs[idx]); 162 } 163 } 164 165 168 public void setExtends(String extendsName) { 169 fExtends = extendsName; 170 } 171 172 175 public void addImplements(String implementsName) { 176 fImplements.add(implementsName); 177 } 178 179 182 public void addImplements(String [] implementsName) { 183 for (int idx = 0; idx < implementsName.length; idx++) { 184 fImplements.add(implementsName[idx]); 185 } 186 } 187 188 191 public void addField(JavaField field) { 192 fFields.put(field.getName(), field); 193 } 194 195 198 public void addConstructor(JavaMethod method) { 199 fConstructors.add(method); 200 } 201 202 205 public void addMethod(JavaMethod method) { 206 fMethods.put(method.getName(), method); 207 } 208 209 212 public String getName() { 213 return fName; 214 } 215 216 219 public int getModifiers() { 220 return fModifiers; 221 } 222 223 226 public String [] getDoc() { 227 return (fDoc == null) ? null : (String [])fDoc.clone(); 228 } 229 230 233 public Iterator getFields() { 234 return new ReadIterator(fFields.values().iterator()); 235 } 236 237 240 public JavaCode getClassInitializer() { 241 return fClassInitializer; 242 } 243 244 247 public Iterator getConstructors() { 248 return new ReadIterator(fConstructors.listIterator()); 249 } 250 251 254 public Iterator getMethods() { 255 return new ReadIterator(fMethods.values().iterator()); 256 } 257 258 261 private void printImports(IndentWriter out, 262 Set importSet) { 263 Iterator imports = importSet.iterator(); 264 while (imports.hasNext()) { 265 out.print("import "); 266 out.print((String )imports.next()); 267 out.println(';'); 268 } 269 } 270 271 274 private void printStart(IndentWriter out, 275 boolean forInterface) { 276 if (fPackageName != null) { 278 out.print("package "); 279 out.print(fPackageName); 280 out.println(';'); 281 out.println(); 282 } 283 284 printImports(out, fImports); 285 if (!forInterface) { 286 printImports(out, fClassImports); 287 } 288 out.println(); 289 290 out.println("/**"); 292 for (int idx = 0; idx < fDoc.length; idx++) { 293 out.print(" * "); 294 out.println(fDoc[idx]); 295 } 296 out.println(" */"); 297 } 298 299 303 private void printImplExtends(IndentWriter out, 304 String keyword, 305 String single, 306 Set set) { 307 boolean printedKeyword = false; 308 if (single != null) { 309 out.print(' '); 310 out.print(keyword); 311 out.print(' '); 312 printedKeyword = true; 313 out.print(single); 314 } 315 Iterator items = set.iterator(); 316 while (items.hasNext()) { 317 if (!printedKeyword) { 318 out.print(' '); 319 out.print(keyword); 320 out.print(' '); 321 printedKeyword = true; 322 } else { 323 out.print(", "); 324 } 325 out.print((String )items.next()); 326 } 327 } 328 329 332 private void printClassDecl(IndentWriter out) { 333 out.print(JavaModifiers.toDecl(fModifiers)); 334 out.print("class "); 335 out.print(fName); 336 if (fExtends != null) { 337 out.print(" extends "); 338 out.print(fExtends); 339 } 340 341 printImplExtends(out, "implements", fInterface, fImplements); 342 out.println(" {"); 343 } 344 345 348 private void printInterfaceDecl(IndentWriter out) { 349 out.print(JavaModifiers.toDecl(fModifiers)); 350 out.print("interface "); 351 out.print(fInterface); 352 353 printImplExtends(out, "extends", null, fImplements); 354 out.println(" {"); 355 } 356 357 360 private void printFields(IndentWriter out, 361 int omitFlags) { 362 Iterator fields = getFields(); 363 while (fields.hasNext()) { 364 JavaField field = (JavaField)fields.next(); 365 if ((field.getModifiers() & omitFlags) == 0) { 366 field.print(out); 367 } 368 } 369 } 370 371 374 private void printClassInitializer(IndentWriter out, 375 boolean printMethodBodies) { 376 if (printMethodBodies && !fClassInitializer.isEmpty()) { 377 out.println("/*"); 378 out.println(" * Class initializer."); 379 out.println(" */"); 380 out.println("static {"); 381 out.enter(); 382 fClassInitializer.print(out); 383 out.leave(); 384 out.println("}"); 385 out.println(); 386 } 387 } 388 389 392 private void printConstructors(IndentWriter out, 393 boolean printMethodBodies, 394 int omitFlags) { 395 Iterator constrs = getConstructors(); 396 while (constrs.hasNext()) { 397 JavaMethod method = (JavaMethod)constrs.next(); 398 if ((method.getModifiers() & omitFlags) == 0) { 399 method.print(out, printMethodBodies); 400 } 401 } 402 } 403 404 407 private void printMethods(IndentWriter out, 408 boolean printMethodBodies, 409 int omitFlags) { 410 Iterator methods = getMethods(); 411 while (methods.hasNext()) { 412 JavaMethod method = (JavaMethod)methods.next(); 413 if ((method.getModifiers() & omitFlags) == 0) { 414 method.print(out, printMethodBodies); 415 } 416 } 417 } 418 419 426 private void printBody(IndentWriter out, 427 boolean printMethodBodies, 428 int omitFlags) { 429 out.enter(); 430 printFields(out, omitFlags); 431 printClassInitializer(out, printMethodBodies); 432 printConstructors(out, printMethodBodies, omitFlags); 433 printMethods(out, printMethodBodies, omitFlags); 434 out.leave(); 435 out.println('}'); 436 } 437 438 442 public void printClass(IndentWriter out) { 443 printStart(out, false); 444 printClassDecl(out); 445 printBody(out, true, 0); 446 } 447 448 451 public void printInterface(IndentWriter out) { 452 printStart(out, true); 453 printInterfaceDecl(out); 454 printBody(out, false, JavaModifiers.OMIT_INTERFACE); 455 } 456 457 460 public void printImplementation(IndentWriter out) { 461 printStart(out, false); 462 printClassDecl(out); 463 printBody(out, true, JavaModifiers.OMIT_IMPLEMENTATION); 464 } 465 466 } 467 | Popular Tags |