1 23 24 package org.enhydra.xml.xmlc.metadata; 25 26 import java.io.File ; 27 28 import org.enhydra.xml.xmlc.XMLCError; 29 import org.enhydra.xml.xmlc.XMLCException; 30 import org.enhydra.xml.xmlc.codegen.JavaLang; 31 import org.w3c.dom.Document ; 32 33 36 public class DocumentClass extends MetaDataElement { 37 40 public static final String TAG_NAME = "documentClass"; 41 42 45 private static final String NAME_ATTR = "name"; 46 private static final String GENERATE_ATTR = "generate"; 47 private static final String DELEGATE_SUPPORT_ATTR = "delegateSupport"; 48 private static final String CREATE_META_DATA_ATTR = "createMetaData"; 49 private static final String RECOMPILATION_ATTR = "recompilation"; 50 private static final String DEFERRED_PARSING_ATTR = "deferParsing"; 51 private static final String EXTENDS_ATTR = "extends"; 52 private static final String DOM_FACTORY_ATTR = "domFactory"; 53 private static final String DOM_ATTR = "dom"; 54 private static final String CREATE_GET_TAG_METHODS_ATTR = "createGetTagMethods"; 55 private static final String GET_TAG_RETURN_TYPE_ATTR = "getTagReturnType"; 56 57 62 private static final String RECOMPILE_SOURCE_FILE_ATTR = "recompileSourceFile"; 63 64 67 public static final String IMPLEMENTATION_SUFFIX = "Impl"; 68 69 72 public static final DOMType DEFAULT_DOM_TYPE = DOMType.LAZYDOM; 73 74 78 public static final String ACCESSOR_TYPE_ELEMENT = "Element"; 79 80 84 public static final String ACCESSOR_TYPE_HTML_ELEMENT = "HTMLElement"; 85 86 90 public static final String ACCESSOR_TYPE_CLASS = "class"; 91 92 97 public static final String ACCESSOR_TYPE_INTERFACE = "interface"; 98 99 107 private transient boolean determinedBaseClassName; 108 private transient String baseClassName; 109 110 113 public DocumentClass(Document ownerDoc) { 114 super(ownerDoc, TAG_NAME); 115 } 116 117 121 private String getBaseClassName() { 122 if (!determinedBaseClassName) { 123 if (getMetaData().getCompileOptions().getCreateSource()) { 124 baseClassName = classNameFromSourceName(); 125 } 126 determinedBaseClassName = true; 127 } 128 return baseClassName; 129 } 130 131 135 public String getName() { 136 String name = getAttributeNull(NAME_ATTR); 137 if (name == null) { 138 return getBaseClassName(); 139 } else { 140 return name; 141 } 142 } 143 144 147 public void setName(String value) { 148 setRemoveAttribute(NAME_ATTR, value); 149 } 150 151 154 public boolean isNameSpecified() { 155 return isAttributeSpecified(NAME_ATTR); 156 } 157 158 161 public GenerateType getGenerate() { 162 String value = getAttributeNull(GENERATE_ATTR); 163 if (value == null) { 164 return GenerateType.CLASS; 165 } else { 166 return GenerateType.getType(value); 167 } 168 } 169 170 173 public void setGenerate(GenerateType value) { 174 if (value == null) { 175 removeAttribute(GENERATE_ATTR); 176 } else { 177 setAttribute(GENERATE_ATTR, value.toString()); 178 } 179 } 180 181 184 public boolean isGenerateSpecified() { 185 return isAttributeSpecified(GENERATE_ATTR); 186 } 187 188 191 public boolean getDelegateSupport() { 192 return getBooleanAttribute(DELEGATE_SUPPORT_ATTR); 193 } 194 195 198 public void setDelegateSupport(boolean value) { 199 setBooleanAttribute(DELEGATE_SUPPORT_ATTR, value); 200 } 201 202 205 public boolean getCreateMetaData() { 206 return getBooleanAttribute(CREATE_META_DATA_ATTR); 207 } 208 209 212 public void setCreateMetaData(boolean value) { 213 setBooleanAttribute(CREATE_META_DATA_ATTR, value); 214 } 215 216 219 public boolean getRecompilation() { 220 return getBooleanAttribute(RECOMPILATION_ATTR); 221 } 222 223 234 public void setRecompilation(boolean value) { 235 setBooleanAttribute(RECOMPILATION_ATTR, value); 236 if (value) { 237 setGenerate(GenerateType.BOTH); 238 setDelegateSupport(true); 239 setDeferredParsing(false); 240 } 241 } 242 243 246 public boolean getDeferredParsing() { 247 return getBooleanAttribute(DEFERRED_PARSING_ATTR); 248 } 249 250 261 public void setDeferredParsing(boolean value) { 262 setBooleanAttribute(DEFERRED_PARSING_ATTR, value); 263 if (value) { 264 setGenerate(GenerateType.CLASS); 265 setDelegateSupport(false); 266 setRecompilation(false); 267 } 268 } 269 270 273 public String getExtends() { 274 return getAttributeNull(EXTENDS_ATTR); 275 } 276 277 280 public void setExtends(String value) { 281 setRemoveAttribute(EXTENDS_ATTR, value); 282 } 283 284 287 public String getDomFactory() { 288 return getAttributeNull(DOM_FACTORY_ATTR); 289 } 290 291 295 public void setDomFactory(String value) { 296 setRemoveAttribute(DOM_FACTORY_ATTR, value); 297 if (value != null) { 298 removeAttribute(DOM_ATTR); 299 } 300 } 301 302 305 public DOMType getDom() { 306 return DOMType.getType(getAttributeNull(DOM_ATTR)); 307 } 308 309 313 public void setDom(DOMType value) { 314 if (value == null) { 315 removeAttribute(DOM_ATTR); 316 } else { 317 setAttribute(DOM_ATTR, value.getName()); 318 removeAttribute(DOM_FACTORY_ATTR); 319 } 320 } 321 322 326 public String getDomFactoryClass(boolean html) { 327 String domFactory = getAttributeNull(DOM_FACTORY_ATTR); 328 if (domFactory == null) { 329 DOMType domType = getDom(); 330 Class dfClass; 331 if (domType == null) { 332 domType = DEFAULT_DOM_TYPE; 333 } 334 if (html) { 335 dfClass = domType.getHTMLDomFactoryClass(); 336 } else { 337 dfClass = domType.getXMLDomFactoryClass(); 338 } 339 if (dfClass == null) { 340 throw new XMLCError("The '" + domType.getName() + 341 "' DOM type only supports " + 342 (html?"XML":"HTML") + " documents."); 343 } 344 domFactory = dfClass.getName(); 345 } 346 return domFactory; 347 } 348 349 352 public String [] getImplements() { 353 Implements[] impls = (Implements[])getChildren(Implements.class); 354 String [] implNames = new String [impls.length]; 355 for (int idx = 0; idx < impls.length; idx++) { 356 implNames[idx] = impls[idx].getName(); 357 } 358 return implNames; 359 } 360 361 364 public void addImplements(String interfaceName) { 365 Implements impl 366 = (Implements)getOwnerDocument().createElement(Implements.TAG_NAME); 367 impl.setName(interfaceName); 368 appendChild(impl); 369 } 370 371 374 public String getPackageName() { 375 String base = getName(); 376 if (base == null) { 377 return null; 378 } else { 379 return JavaLang.getPackageName(base); 380 } 381 } 382 383 388 public String getUnqualClassName() { 389 String simpleName = JavaLang.simpleClassName(getName()); 390 if (getGenerate() == GenerateType.CLASS) { 391 return simpleName; 392 } else { 393 return simpleName + IMPLEMENTATION_SUFFIX; 394 } 395 } 396 397 401 public String getUnqualInterfaceName() { 402 if (getGenerate() == GenerateType.CLASS) { 403 return null; 404 } else { 405 return JavaLang.simpleClassName(getName()); 406 } 407 } 408 409 412 private File getSourceOutputDir() { 413 String sourceOutputRoot = getMetaData().getCompileOptions().getSourceOutputRoot(); 414 415 File sourceDirectory = null; 416 String packageName = getPackageName(); 417 if ((sourceOutputRoot != null) && (packageName != null)) { 418 sourceDirectory = new File (sourceOutputRoot); 419 if (packageName != null) { 420 String [] packageParts = JavaLang.parseClassName(packageName); 421 for (int i = 0; i < packageParts.length; i++) { 422 sourceDirectory = new File (sourceDirectory, packageParts[i]); 423 } 424 } 425 } 426 return sourceDirectory; 427 } 428 429 432 private File getSourceFile(String unqualName) { 433 File sourceDirectory = getSourceOutputDir(); 434 String fileName = unqualName + ".java"; 435 if (sourceDirectory == null) { 436 return new File (fileName); 437 } else { 438 return new File (sourceDirectory, fileName); 439 } 440 } 441 442 445 public File getJavaClassSource() { 446 return getSourceFile(getUnqualClassName()); 447 } 448 449 452 public File getJavaInterfaceSource() { 453 return getSourceFile(getUnqualInterfaceName()); 454 } 455 456 459 public boolean getCreateGetTagMethods() { 460 return getBooleanAttribute(CREATE_GET_TAG_METHODS_ATTR); 461 } 462 463 466 public void setCreateGetTagMethods(boolean value) { 467 setBooleanAttribute(CREATE_GET_TAG_METHODS_ATTR, value); 468 } 469 470 474 public String getGetTagReturnType() { 475 String type = getAttributeNull(GET_TAG_RETURN_TYPE_ATTR); 476 if (type == null) { 477 return ACCESSOR_TYPE_ELEMENT; 478 } else { 479 return type; 480 } 481 } 482 483 487 public void setGetTagReturnType(String value) { 488 if (ACCESSOR_TYPE_ELEMENT.equals(value)) { 489 value = null; 490 } 491 setRemoveAttribute(GET_TAG_RETURN_TYPE_ATTR, value); 492 } 493 494 497 private String classNameFromSourceName() { 498 String srcName = getMetaData().getInputDocument().getUrl(); 499 500 if (srcName == null) { 501 throw new XMLCError("No input document name from which to determine the class name"); 503 } 504 505 String className = new File (srcName).getName(); 506 int dotIdx = className.lastIndexOf('.'); 507 if (dotIdx >= 0) { 508 className = className.substring(0, dotIdx); 509 } 510 if (!JavaLang.legalJavaIdentifier(className)) { 511 throw new XMLCError("Can't use source file name \"" 513 + className 514 + "\" as class name: " 515 + "not a legal Java identifier"); 516 } 517 return className; 518 } 519 520 523 private void moveToInputDocument() { 524 InputDocument inputDoc = getMetaData().getInputDocument(); 525 String value = getAttributeNull(RECOMPILE_SOURCE_FILE_ATTR); 526 if (value != null) { 527 inputDoc.setRecompileSource(value); 528 removeAttribute(RECOMPILE_SOURCE_FILE_ATTR); 529 } 530 } 531 532 536 protected void completeModifications() throws XMLCException { 537 if (isAttributeSpecified(DOM_FACTORY_ATTR) 538 && isAttributeSpecified(DOM_ATTR)) { 539 throw new XMLCException("Can't specify both the " 540 + DOM_FACTORY_ATTR 541 + " and " + DOM_ATTR 542 + " attributes in <" 543 + TAG_NAME + ">"); 544 } 545 if (isAttributeSpecified(RECOMPILE_SOURCE_FILE_ATTR)) { 546 moveToInputDocument(); 547 } 548 } 549 } 550 | Popular Tags |