1 23 24 package org.objectweb.jorm.mi2xml.lib; 25 26 import org.apache.xerces.dom.DocumentImpl; 27 import org.objectweb.jorm.metainfo.api.*; 28 import org.objectweb.jorm.metainfo.api.Package; 29 import org.objectweb.jorm.metainfo.api.Class; 30 import org.objectweb.jorm.mi2xml.api.MappingDomtreeBuilder; 31 import org.objectweb.jorm.mi2xml.api.DomtreeBuilder; 32 import org.objectweb.jorm.util.api.Loggable; 33 import org.objectweb.jorm.api.PException; 34 import org.objectweb.jorm.compiler.api.PExceptionCompiler; 35 import org.objectweb.util.monolog.api.Logger; 36 import org.objectweb.util.monolog.api.LoggerFactory; 37 import org.objectweb.util.monolog.api.BasicLevel; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Element ; 40 41 import java.util.*; 42 43 46 public class BasicDomtreeBuilder implements DomtreeBuilder, Loggable { 47 public final static String ID = "ID"; 49 public final static String REF = "REF"; 50 public final static String IDVALUE_SEP = "."; 51 public final static String JORM = "jorm"; 52 public final static String PACKAGE = "package"; 53 public final static String CLASS = "class"; 54 public final static String MAPPING = "mapping"; 55 public final static String COMPOSITE_NAME = "composite-name"; 56 public final static String FIELD = "field"; 57 public final static String SCALAR_FIELD = "scalar-field"; 58 public final static String EXTENSION = "extension"; 59 public final static String NAME_DEF = "name-def"; 60 public final static String PRIMITIVE_TYPE = "primitive-type"; 61 public final static String CLASS_REF = "class-ref"; 62 public final static String GEN_CLASS_REF = "gen-class-ref"; 63 public final static String FIELD_REF = "field-ref"; 64 public final static String SCALAR_TYPE = "scalar-type"; 65 public final static String NULL_VALUE = "null-value"; 66 public final static String GEN_CLASS = "gen-class"; 67 public final static String INDEX = "index"; 68 public final static String SYSTEM = "system"; 69 public final static String COMPOSITE_NAME_REF = "composite-name-ref"; 70 public final static String COMPOSITE_NAME_FIELD_PROJECTION = 71 "composite-name-field-projection"; 72 73 76 private Map mappingDomtreeBuilders; 77 78 81 private Logger logger; 82 83 86 private LoggerFactory loggerFactory; 87 88 93 private Map metaobject2idvalue; 94 95 98 public BasicDomtreeBuilder() { 99 metaobject2idvalue = new Hashtable(); 100 mappingDomtreeBuilders = new HashMap(); 101 } 102 103 108 public void addMappingDomtreeBuilder(String mapperName, MappingDomtreeBuilder mdb) { 109 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 110 getLogger().log(BasicLevel.DEBUG, 111 "Add a MappingDomtreeBuilder (" + mapperName + 112 ") for the current builder"); 113 } 114 if (!mappingDomtreeBuilders.containsKey(mapperName)) { 115 mappingDomtreeBuilders.put(mapperName, mdb); 116 } 117 } 118 119 124 private MappingDomtreeBuilder getMappingDomtreeBuilder(String mapperName) throws PException { 125 MappingDomtreeBuilder mdb = (MappingDomtreeBuilder) mappingDomtreeBuilders.get(mapperName); 126 if (mdb == null) { 127 throw new PExceptionCompiler("<" + mapperName + 128 ">DomtreeBuilder has not been created."); 129 } 130 return mdb; 131 } 132 133 141 public Document build(MetaObject metaObject) throws PException { 142 Document document = new DocumentImpl(); 143 Element rootElement = 144 document.createElement(JORM); 145 String packageName = ((Package ) metaObject.getParent()).getName(); 147 Element packageElement = document.createElement(PACKAGE); 148 packageElement.appendChild(document.createTextNode(packageName)); 149 rootElement.appendChild(packageElement); 151 152 if (metaObject instanceof Class ) { 154 Class clazz = (Class ) metaObject; 155 String className = clazz.getName(); 156 Element classElement = document.createElement(CLASS); 157 classElement.setAttribute("name", className); 159 classElement.setAttribute("abstract", 160 ("" + clazz.isAbstract()).toUpperCase()); 161 rootElement.appendChild(classElement); 163 processClass(document, classElement, clazz); 165 if (!clazz.getClassProjects().isEmpty()) { 166 processMappings(document, rootElement, clazz); 167 } 168 } else { 169 CompositeName compositeName = (CompositeName) metaObject; 170 Element compositeNameElement = 171 document.createElement(COMPOSITE_NAME); 172 compositeNameElement.setAttribute("name", compositeName.getName()); 174 rootElement.appendChild(compositeNameElement); 176 processCompositeName(document, compositeNameElement, compositeName); 178 } 179 document.appendChild(rootElement); 180 return document; 181 } 182 183 184 191 private void processClass(Document document, Element classElement, Class clazz) { 192 Iterator fieldIterator = clazz.getFields().iterator(); 193 while (fieldIterator.hasNext()) { 195 TypedElement typedElement = (TypedElement) fieldIterator.next(); 196 String fieldName = typedElement.getName(); 197 String idValue = fieldName; 198 Element fieldElement = document.createElement(FIELD); 199 fieldElement.setAttribute("name", fieldName); 200 classElement.appendChild(fieldElement); 201 if (typedElement instanceof PrimitiveElement) { 203 Element ptElement = document.createElement(PRIMITIVE_TYPE); 204 ptElement.setAttribute("type", typedElement.getType().getJormName()); 206 fieldElement.appendChild(ptElement); 207 } 209 else if (typedElement instanceof ClassRef) { 212 Element crElement = document.createElement(CLASS_REF); 213 crElement.setAttribute("class-name", ((ClassRef) typedElement).getMOClass().getFQName()); 214 fieldElement.appendChild(crElement); 215 processClassRef(document, crElement, (ClassRef) typedElement, idValue); 216 } 217 else if (typedElement instanceof GenClassRef) { 219 Element gcrElement = document.createElement(GEN_CLASS_REF); 220 fieldElement.appendChild(gcrElement); 222 processGenClassRef(document, gcrElement, (GenClassRef) typedElement, idValue); 223 } 224 } 225 Iterator sfIterator = clazz.getHiddenFields().iterator(); 227 while (sfIterator.hasNext()) { 228 ScalarField scalarField = (ScalarField) sfIterator.next(); 229 String name = scalarField.getName(); 230 String idValue = name; 231 Element sfElement = document.createElement(SCALAR_FIELD); 232 sfElement.setAttribute("name", name); 233 classElement.appendChild(sfElement); 234 processScalarField(document, sfElement, scalarField, idValue); 235 } 236 Iterator superIterator = clazz.getSuperClasses().iterator(); 238 while (superIterator.hasNext()) { 239 Class superClazz = (Class ) superIterator.next(); 240 Element extElement = document.createElement(EXTENSION); 241 extElement.setAttribute("name", superClazz.getFQName()); 242 classElement.appendChild(extElement); 243 } 244 Iterator ndIterator = clazz.getNameDefs().iterator(); 246 while (ndIterator.hasNext()) { 247 NameDef nameDef = (NameDef) ndIterator.next(); 248 253 Element ndElement = document.createElement(NAME_DEF); 254 ndElement.setAttribute("name", nameDef.getName()); 255 classElement.appendChild(ndElement); 256 processNameDef(document, ndElement, nameDef); 258 } 259 } 260 261 268 private void processClassRef(Document document, Element crElement, 269 ClassRef classRef, String parentIdValue) { 270 Iterator ndIterator = classRef.getRefNameDef().iterator(); 272 while (ndIterator.hasNext()) { 273 NameDef nameDef = (NameDef) ndIterator.next(); 274 279 Element ndElement = document.createElement(NAME_DEF); 280 ndElement.setAttribute("name", nameDef.getName()); 281 crElement.appendChild(ndElement); 282 processNameDef(document, ndElement, nameDef); 284 } 285 } 286 287 295 private void processGenClassRef(Document document, Element gcrElement, 296 GenClassRef genClassRef, String parentIdValue) { 297 301 Element gcElement = document.createElement(GEN_CLASS); 302 gcElement.setAttribute("gen-class-name", genClassRef.getGenClassName()); 304 gcrElement.appendChild(gcElement); 305 metaobject2idvalue.put(genClassRef, genClassRef.getGenClassId()); 306 processGenClass(document, gcElement, genClassRef, ""); 307 Iterator ndIterator = genClassRef.getRefNameDef().iterator(); 309 while (ndIterator.hasNext()) { 310 NameDef nameDef = (NameDef) ndIterator.next(); 311 316 Element ndElement = document.createElement(NAME_DEF); 317 ndElement.setAttribute("name", nameDef.getName()); 318 gcrElement.appendChild(ndElement); 319 processNameDef(document, ndElement, nameDef); 321 } 322 } 323 324 332 private void processScalarField(Document document, Element sfElement, 333 ScalarField scalarField, String idValue) { 334 Element stElement = document.createElement(SCALAR_TYPE); 336 stElement.setAttribute("type", scalarField.getType().getJormName()); 338 sfElement.appendChild(stElement); 339 if (scalarField.getNullValue() != null) { 342 Element nvElement = document.createElement(NULL_VALUE); 343 nvElement.setAttribute("value", scalarField.getNullValue()); 344 stElement.appendChild(nvElement); 345 } 346 } 347 348 355 private void processNameDef(Document document, Element ndElement, NameDef nameDef) { 356 if (nameDef.isSystem()) { 358 Element systemElement = document.createElement(SYSTEM); 359 ndElement.appendChild(systemElement); 360 } else if (nameDef.getFieldName() != null) { 361 Element frElement = document.createElement(FIELD_REF); 362 frElement.setAttribute("field-name", nameDef.getFieldName()); 363 ndElement.appendChild(frElement); 364 } else if (nameDef.isNameRef()) { 365 NameRef nameRef = nameDef.getNameRef(); 366 Element cnrElement = document.createElement(COMPOSITE_NAME_REF); 367 cnrElement.setAttribute("composite-name-name", 368 nameRef.getCNPackageName() + "." + nameRef.getName()); 369 ndElement.appendChild(cnrElement); 370 processCompositeNameRef(document, cnrElement, nameRef); 371 } 372 } 373 374 380 private void processCompositeNameRef(Document document, Element cnrElement, NameRef nameRef) { 381 Iterator projIterator = nameRef.getProjection().keySet().iterator(); 383 while (projIterator.hasNext()) { 384 String fieldName = (String ) projIterator.next(); 385 String classFieldName = nameRef.getClassFieldName(fieldName); 386 Element cnfpElement = document.createElement(COMPOSITE_NAME_FIELD_PROJECTION); 387 cnfpElement.setAttribute("composite-name-field-name", fieldName); 388 cnfpElement.setAttribute("class-field-name", classFieldName); 389 cnrElement.appendChild(cnfpElement); 390 } 391 } 392 393 401 private void processGenClass(Document document, Element gcElement, 402 GenClassRef genClassRef, String parentIdValue) { 403 Iterator sfIterator = genClassRef.getHiddenFields().iterator(); 405 while (sfIterator.hasNext()) { 406 ScalarField scalarField = (ScalarField) sfIterator.next(); 407 Element sfElement = document.createElement(SCALAR_FIELD); 410 sfElement.setAttribute("name", scalarField.getName()); 411 gcElement.appendChild(sfElement); 412 processScalarField(document, sfElement, scalarField, ""); 414 } 415 Iterator ndIterator = genClassRef.getIdNameDef().iterator(); 417 while (ndIterator.hasNext()) { 418 NameDef nameDef = (NameDef) ndIterator.next(); 419 425 Element ndElement = document.createElement(NAME_DEF); 426 ndElement.setAttribute("name", nameDef.getName()); 427 gcElement.appendChild(ndElement); 428 processNameDef(document, ndElement, nameDef); 430 } 431 Iterator frIterator = genClassRef.getIndexFields().iterator(); 433 if (frIterator.hasNext()) { 434 Element indexElement = document.createElement(SCALAR_FIELD); 435 gcElement.appendChild(indexElement); 436 while (frIterator.hasNext()) { 437 ScalarField scalarField = (ScalarField) frIterator.next(); 438 String name = scalarField.getName(); 439 Element frElement = document.createElement(FIELD_REF); 440 frElement.setAttribute("name", name); 441 indexElement.appendChild(frElement); 442 } 443 } 444 if (genClassRef.isPrimitive()) { 446 PrimitiveElement primitiveElement = genClassRef.getPrimitiveElement(); 447 String fieldName = primitiveElement.getName(); 448 453 Element ptElement = document.createElement(PRIMITIVE_TYPE); 454 ptElement.setAttribute("type", primitiveElement.getType().getJormName()); 455 gcElement.appendChild(ptElement); 457 458 } 459 else if (genClassRef.isClassRef()) { 461 ClassRef classRef = genClassRef.getClassRef(); 462 Element crElement = document.createElement(CLASS_REF); 463 crElement.setAttribute("name", classRef.getClassName()); 464 gcElement.appendChild(crElement); 465 processClassRef(document, crElement, classRef, parentIdValue); 466 } 467 else if (genClassRef.isGenClassRef()) { 469 GenClassRef subGenClassRef = genClassRef.getGenClassRef(); 470 Element gcrElement = document.createElement(GEN_CLASS_REF); 471 gcElement.appendChild(gcrElement); 472 processGenClassRef(document, gcrElement, subGenClassRef, parentIdValue); 473 } 474 } 475 476 483 private void processCompositeName(Document document, Element cnElement, 484 CompositeName compositeName) { 485 Iterator sfIterator = compositeName.getFields().iterator(); 487 while (sfIterator.hasNext()) { 488 ScalarField scalarField = (ScalarField) sfIterator.next(); 489 String name = scalarField.getName(); 490 Element sfElement = document.createElement(SCALAR_FIELD); 491 sfElement.setAttribute("name", name); 492 cnElement.appendChild(sfElement); 493 processScalarField(document, sfElement, scalarField, name); 494 } 495 Iterator icnIterator = compositeName.getSuperCompositeNames().iterator(); 497 while (icnIterator.hasNext()) { 498 CompositeName aCompositeName = (CompositeName) icnIterator.next(); 499 Element extensionElement = document.createElement(EXTENSION); 500 extensionElement.appendChild(document.createTextNode(aCompositeName.getName())); 501 cnElement.appendChild(extensionElement); 502 } 503 } 504 505 511 private void processMappings(Document document, Element rootElement, Class clazz) 512 throws PException { 513 MappingDomtreeBuilder mdb = null; 514 Iterator cpIterator = clazz.getClassProjects().iterator(); 516 while (cpIterator.hasNext()) { 517 ClassProject classProject = (ClassProject) cpIterator.next(); 518 String projectName = classProject.getProjectName(); 519 Element mappingElement = document.createElement(MAPPING); 520 mappingElement.setAttribute("project-name", projectName); 521 rootElement.appendChild(mappingElement); 522 Iterator mappingIterator = classProject.getMappings().iterator(); 524 while (mappingIterator.hasNext()) { 525 Mapping mapping = (Mapping) mappingIterator.next(); 526 String mapperName = mapping.getMapperName(); 527 mdb = getMappingDomtreeBuilder(mapperName); 530 ((Loggable) mdb).setLogger(logger); 531 mdb.setCurrentClass(clazz); 532 mdb.setmetaobject2idvalue(metaobject2idvalue); 533 ClassMapping classMapping = mapping.getClassMapping(); 534 if (logger.isLoggable(BasicLevel.DEBUG)) { 535 logger.log(BasicLevel.DEBUG, 536 "current class =<" + clazz.getName() + ">"); 537 logger.log(BasicLevel.DEBUG, 538 "current project =<" + projectName + ">"); 539 logger.log(BasicLevel.DEBUG, 540 "current mapping =<" + mapperName + ">"); 541 } 542 mdb.processClassMapping(document, mappingElement, classMapping); 543 Iterator gcmIterator = mapping.getGenClassMappings().iterator(); 544 while (gcmIterator.hasNext()) { 545 GenClassMapping genClassMapping = (GenClassMapping) gcmIterator.next(); 546 mdb.processGenClassMapping(document, mappingElement, genClassMapping); 547 } 548 } 549 } 550 } 551 555 560 public void setLogger(Logger logger) { 561 this.logger = logger; 562 } 563 564 569 public void setLoggerFactory(LoggerFactory loggerfactory) { 570 this.loggerFactory = loggerfactory; 571 if (logger == null && loggerFactory != null) { 572 logger = loggerFactory.getLogger("org.objectweb.jorm.mi2xml.rdb"); 573 } 574 } 575 576 public Logger getLogger() { 577 return logger; 578 } 579 580 public LoggerFactory getLoggerFactory() { 581 return loggerFactory; 582 } 583 } 584 | Popular Tags |