1 23 24 package org.objectweb.jorm.xml2mi.lib; 25 26 import org.apache.tools.ant.types.DTDLocation; 27 import org.objectweb.jorm.api.PException; 28 import org.objectweb.jorm.metainfo.api.Class; 29 import org.objectweb.jorm.metainfo.api.Package; 30 import org.objectweb.jorm.metainfo.api.Manager; 31 import org.objectweb.jorm.metainfo.api.MetaObject; 32 import org.objectweb.jorm.metainfo.api.CompositeName; 33 import org.objectweb.jorm.metainfo.api.ClassProject; 34 import org.objectweb.jorm.metainfo.api.NameDef; 35 import org.objectweb.jorm.metainfo.api.ScalarField; 36 import org.objectweb.jorm.metainfo.api.GenClassRef; 37 import org.objectweb.jorm.metainfo.api.ClassRef; 38 import org.objectweb.jorm.metainfo.api.Reference; 39 import org.objectweb.jorm.metainfo.api.NameRef; 40 import org.objectweb.jorm.metainfo.api.Mapping; 41 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 42 import org.objectweb.jorm.metainfo.api.TypedElement; 43 import org.objectweb.jorm.metainfo.api.ClassMapping; 44 import org.objectweb.jorm.metainfo.api.ParentClassMapping; 45 import org.objectweb.jorm.metainfo.api.IdentifierMapping; 46 import org.objectweb.jorm.type.api.PType; 47 import org.objectweb.jorm.type.api.PTypeSpace; 48 import org.objectweb.jorm.util.api.Loggable; 49 import org.objectweb.jorm.util.io.api.PathExplorer; 50 import org.objectweb.jorm.util.io.lib.DirJavaExplorer; 51 import org.objectweb.jorm.xml2mi.api.MappingParser; 52 import org.objectweb.jorm.xml2mi.api.Parser; 53 import org.objectweb.util.monolog.api.BasicLevel; 54 import org.objectweb.util.monolog.api.Logger; 55 import org.w3c.dom.Document ; 56 import org.w3c.dom.Element ; 57 import org.w3c.dom.Node ; 58 import org.w3c.dom.NodeList ; 59 import org.xml.sax.SAXException ; 60 61 import java.io.InputStream ; 62 import java.util.Map ; 63 import java.util.HashMap ; 64 import java.util.Hashtable ; 65 import java.util.List ; 66 import java.util.ArrayList ; 67 import java.util.Collection ; 68 import java.util.Iterator ; 69 import java.util.Properties ; 70 import java.util.Set ; 71 import java.util.HashSet ; 72 73 79 public class BasicDomParser 80 extends ParserHelper 81 implements Parser, Loggable { 82 86 private Manager metaInfoManager; 87 88 91 private Map mappingParsers = new HashMap (); 92 93 96 private SAXParserHelper parser; 97 100 private PathExplorer pathExplorer; 101 102 103 106 private Exception parserException = null; 107 108 109 112 private boolean genDep = false; 113 114 private DTDResolver resolver = null; 115 116 private List parsedMO = null; 117 private List undefinedMO = null; 118 119 124 private Map idvalue2genclassref; 125 126 128 134 public void init(boolean dtdVerify, ArrayList dtds) { 135 Properties p = new Properties (); 136 for (Iterator iter = dtds.iterator(); iter.hasNext();) { 137 DTDLocation element = (DTDLocation) iter.next(); 138 p.setProperty(element.getPublicId(), element.getLocation()); 139 } 140 try { 141 parser = new SAXParserHelper(p, logger, null, dtdVerify); 142 } catch (SAXException e) { 143 logger.log(BasicLevel.ERROR, "Error during the parser initialization", e); 144 } 145 parsedMO = new ArrayList (); 146 undefinedMO = new ArrayList (); 147 motable = new Hashtable (); 148 mappingParsers = new HashMap (); 149 idvalue2genclassref = new HashMap (); 150 } 151 152 157 public void addMappingParser(String mapperName, 158 MappingParser mappingParser) throws PException { 159 if (mappingParser == null) { 160 throw new PException("<" + mapperName + 161 "> MappingParser has not been created."); 162 } 163 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 164 getLogger().log(BasicLevel.DEBUG, 165 "Add a MappingParser (" + mapperName + 166 ") for the current Parser"); 167 } 168 if (!mappingParsers.containsKey(mapperName)) { 169 mappingParsers.put(mapperName, mappingParser); 170 } 171 } 172 173 178 public MappingParser getMappingParser(String mapperName) { 179 MappingParser mappingParser = (MappingParser) mappingParsers.get(mapperName); 180 return mappingParser; 181 } 182 183 192 public Collection parse(Iterator files) throws PException { 193 Collection res = new ArrayList (); 194 while (files.hasNext()) { 195 String filename = (String ) files.next(); 196 MetaObject mo = (MetaObject) motable.get(filename); 197 if (mo == null) { 198 mo = parse(filename); 199 if (mo != null) { 200 motable.put(filename, mo); 201 } 202 } 203 res.add(mo); 204 } 205 while (!undefinedMO.isEmpty()) { 206 MetaObject mo = (MetaObject) undefinedMO.remove(0); 207 String fqname = null; 208 if (mo instanceof Class ) { 209 fqname = ((Class ) mo).getFQName(); 210 } else if (mo instanceof CompositeName) { 211 fqname = ((CompositeName) mo).getFQName(); 212 } else { 213 throw new PException("Unknown meta object: " + mo); 214 } 215 fqname = fqname.replace('.', fileSeparator.charAt(0)) + ".pd"; 216 parse(fqname); 217 motable.put(fqname, mo); 218 } 219 220 Set handledImplicitMappings = new HashSet (); 222 for (Iterator itMO = res.iterator(); itMO.hasNext();) { 223 MetaObject metaObject = (MetaObject) itMO.next(); 224 if (metaObject instanceof Class ) { 225 Class clazz = (Class ) metaObject; 226 addImplicitMappings(clazz, handledImplicitMappings); 227 } 228 } 229 for (Iterator itMO = res.iterator(); itMO.hasNext();) { 231 MetaObject metaObject = (MetaObject) itMO.next(); 232 if (metaObject instanceof Class ) { 233 Class clazz = (Class ) metaObject; 234 addImplicitDependencies(clazz); 235 } 236 } 237 238 239 Set filenames = motable.keySet(); 240 if (! filenames.isEmpty()) { 241 for (Iterator iter = filenames.iterator(); iter.hasNext();) { 242 String filename = (String ) iter.next(); 243 logger.log(BasicLevel.DEBUG, 244 "BasicDomParser filename in motable " + filename); 245 } 247 } 248 249 logger.log(BasicLevel.DEBUG, 250 "BasicDomParser size of motable " + motable.size()); 251 252 logger.log(BasicLevel.DEBUG, 253 "BasicDomParser size of res " + res.size()); 254 255 return res; 256 } 257 258 268 public MetaObject parse(String file) throws PException { 269 logger.log(BasicLevel.INFO, "Parsing the file " + file + "..."); 270 boolean debug = logger.isLoggable(BasicLevel.DEBUG); 271 if (debug) 272 logger.log(BasicLevel.DEBUG, "try to get input stream from <" 273 + file + ">"); 274 InputStream is = pathExplorer.getInputStream(file); 275 if (is == null) { 276 throw new PException("The file '" + file + 277 "' has not been found in the following classpath \"" 278 + pathExplorer.getClassPath() + "\""); 279 } else if (debug) 280 logger.log(BasicLevel.DEBUG, "File " + file + " found"); 281 Document doc; 282 try { 283 doc = parser.parse(is); 284 } catch (Exception e) { 285 parserException = e; 286 throw new PException(e, "there is a problem (exception) during the parsing of <" 287 + file + "> from the SAX module (SAXNotRecognized)"); 288 } 289 if (debug) 290 logger.log(BasicLevel.DEBUG, "Parsing done."); 291 return (doc.getDocumentElement() != null 293 ? build(doc, file) 294 : null); 295 } 296 297 303 public void setMetaInfoManager(Manager mim) { 304 this.metaInfoManager = mim; 305 } 306 307 312 public void setPathExplorer(PathExplorer pathexpl) { 313 pathExplorer = pathexpl; 314 if (pathExplorer == null) { 315 pathExplorer = new DirJavaExplorer(); 316 } 317 logger.log(BasicLevel.DEBUG, "jorm classpath=" + pathExplorer.getClassPath()); 318 } 319 320 325 public void setGenDep(boolean gendep) { 326 genDep = gendep; 327 } 328 329 334 public boolean isGenDep() { 335 return genDep; 336 } 337 338 343 public Exception getParserException() { 344 return parserException; 345 } 346 347 351 358 private MetaObject build(Document doc, String name) throws PException { 359 if (logger.isLoggable(BasicLevel.DEBUG)) 360 logger.log(BasicLevel.DEBUG, 361 "build the meta information for <" + name + "> description file"); 362 Package schema = null; 363 Class clazz = null; 364 CompositeName aname = null; 365 NodeList nodes = doc.getDocumentElement().getChildNodes(); 366 for (int i = 0; i < nodes.getLength(); i++) { 367 Node node = nodes.item(i); 368 String nodeName = node.getNodeName(); 369 if (nodeName.equals("package")) { 370 if (logger.isLoggable(BasicLevel.DEBUG)) { 371 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 372 logger.log(BasicLevel.DEBUG, "package name =<" + 373 node.getFirstChild().getNodeValue() + ">"); 374 } 375 schema = metaInfoManager.createPackage(node.getFirstChild().getNodeValue()); 376 if (logger.isLoggable(BasicLevel.DEBUG)) 377 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 378 } else if (nodeName.equals("class")) { 379 String className = ((Element) node).getAttribute("name"); 380 int index = name.lastIndexOf(fileSeparator); 382 int endIdx = name.lastIndexOf("."); 383 if (endIdx <= index) 384 endIdx = name.length(); 385 String sub = name.substring(index + 1, endIdx); 386 if (!sub.equals(className)) 387 logger.log(BasicLevel.WARN, "Be careful, the name of the class" + 388 " is different from the name of the file : " + 389 sub + " and " + className + 390 "(" + className + " is used)"); 391 392 if (logger.isLoggable(BasicLevel.DEBUG)) 393 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 394 395 String isAbstractString = ((Element) node).getAttribute("abstract").toLowerCase(); 396 if (schema == null) { 397 schema = metaInfoManager.createPackage(""); 398 } 399 clazz = schema.createClass(className); 400 if (!parsedMO.contains(clazz)) { 401 parsedMO.add(clazz); 403 undefinedMO.remove(clazz); 404 boolean abstractClass = (isAbstractString != null && isAbstractString.equals("true")); 406 clazz.setAbstract(abstractClass); 407 if (logger.isLoggable(BasicLevel.DEBUG)) { 408 logger.log(BasicLevel.DEBUG, "isAbstractString:<" + isAbstractString +">"); 409 logger.log(BasicLevel.DEBUG, "Boolean.getBoolean(" + isAbstractString + "):" + Boolean.getBoolean(isAbstractString)); 411 logger.log(BasicLevel.DEBUG, "abstractClass:<" + abstractClass + ">"); 412 logger.log(BasicLevel.DEBUG, "class FQName:<" + clazz.getFQName() + "> isabstract:<" + clazz.isAbstract() +">"); 413 } 414 processClass(clazz, (Element) node); 415 } 416 if (logger.isLoggable(BasicLevel.DEBUG)) 417 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 418 } else if (nodeName.equals("mapping")) { 419 if (logger.isLoggable(BasicLevel.DEBUG)) 420 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 421 String projectName = ((Element) node).getAttribute("project-name"); 422 if (clazz == null) { 423 logger.log(BasicLevel.DEBUG, "the class definition is missing!"); 424 break; 425 } else { 426 if (logger.isLoggable(BasicLevel.DEBUG)) 427 logger.log(BasicLevel.DEBUG, "project-name =<" + projectName + ">"); 428 ClassProject classProject = clazz.createClassProject(projectName); 429 parseMapping((Element) node, classProject); 430 } 431 if (logger.isLoggable(BasicLevel.DEBUG)) 432 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 433 } else if (nodeName.equals("composite-name")) { 434 String CN_name = ((Element) node).getAttribute("name"); 435 int index = name.lastIndexOf(fileSeparator); 437 String sub = name.substring(index + 1, name.length() - 3); 438 439 if (sub.compareTo(CN_name) != 0) 440 logger.log(BasicLevel.WARN, "Be careful, the name of the composite name" + 441 " is different from the name of the file : " + 442 name + " and " + CN_name + 443 "(" + CN_name + " is used)"); 444 445 if (logger.isLoggable(BasicLevel.DEBUG)) 446 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 447 448 if (schema == null) { 449 schema = metaInfoManager.createPackage(""); 450 } 451 aname = schema.createCompositeName(CN_name); 452 if (!parsedMO.contains(aname)) { 453 parsedMO.add(aname); 454 undefinedMO.remove(aname); 455 processCompositeName(aname, (Element) node); 457 } 458 if (logger.isLoggable(BasicLevel.DEBUG)) 459 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 460 return aname; 461 } 462 } 463 return clazz; 464 } 465 466 private void processCompositeName(CompositeName compositeName, 467 Element compositeNameElem) { 468 469 NodeList children = compositeNameElem.getChildNodes(); 470 for (int i = 0; i < children.getLength(); i++) { 471 Node child = children.item(i); 472 String childName = child.getNodeName(); 473 474 if (childName.equals("scalar-field")) { 475 if (logger.isLoggable(BasicLevel.DEBUG)) 476 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 477 478 processScalarField(compositeName, (Element) child); 479 480 if (logger.isLoggable(BasicLevel.DEBUG)) 481 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">"); 482 } else if (childName.equals("extension")) { 483 if (logger.isLoggable(BasicLevel.DEBUG)) 486 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 487 488 String CN_name = ((Element) child).getAttribute("Name"); 489 CompositeName parentCN = metaInfoManager.getCompositeName(CN_name); 490 if (parentCN == null) { 491 parentCN = metaInfoManager.createCompositeName(CN_name); 492 undefinedMO.add(parentCN); 493 } 494 compositeName.addInheritedCompositeName(parentCN); 495 if (logger.isLoggable(BasicLevel.DEBUG)) 496 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">"); 497 } 498 } 499 } 500 501 502 512 private void processClass(Class clazz, Element classElem) throws PException { 513 514 NodeList children = classElem.getChildNodes(); 515 516 for (int i = 0; i < children.getLength(); i++) { 517 Node child = children.item(i); 518 String nodeName = child.getNodeName(); 519 if (nodeName.equals("field")) { 520 if (logger.isLoggable(BasicLevel.DEBUG)) 521 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 522 processField(clazz, (Element) child); 523 if (logger.isLoggable(BasicLevel.DEBUG)) 524 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 525 } else if (nodeName.equals("scalar-field")) { 526 if (logger.isLoggable(BasicLevel.DEBUG)) 527 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 528 processScalarField(clazz, (Element) child); 529 if (logger.isLoggable(BasicLevel.DEBUG)) 530 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 531 } else if (nodeName.equals("constant-value")) { 532 if (logger.isLoggable(BasicLevel.DEBUG)) 533 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 534 processConstantValue(clazz, (Element) child); 535 if (logger.isLoggable(BasicLevel.DEBUG)) 536 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 537 } else if (nodeName.equals("extension")) { 538 String fqcn = ((Element) child).getAttribute("name"); 539 if (logger.isLoggable(BasicLevel.DEBUG)) { 540 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 541 logger.log(BasicLevel.DEBUG, "name of the extension=<" + fqcn + ">"); 544 } 545 Class inheritedClass = metaInfoManager.getClass(fqcn); 547 if (inheritedClass == null) { 549 552 fqcn = fqcn.replace('.', fileSeparator.charAt(0)) + ".pd"; 553 inheritedClass = (Class ) parse(fqcn); 554 motable.put(fqcn, inheritedClass); 555 } 556 clazz.addSuperClass(inheritedClass); 557 if (logger.isLoggable(BasicLevel.DEBUG)) 558 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 559 } else if (nodeName.equals("name-def")) { 560 564 if (logger.isLoggable(BasicLevel.DEBUG)) 565 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 566 NameDef nameDef = clazz.createNameDef(); 567 processNameDef(nameDef, ((Element) child)); 568 if (logger.isLoggable(BasicLevel.DEBUG)) 569 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 570 571 } else if (nodeName.equals("name-def-filter")) { 572 if (logger.isLoggable(BasicLevel.DEBUG)) 573 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 574 String filter = ((Element) child).getAttribute("filter"); 575 NameDef nd = getIdNameDef(clazz, ((Element) child).getAttribute("link-end")); 576 clazz.setInheritanceFilter(nd, filter); 577 if (logger.isLoggable(BasicLevel.DEBUG)) 578 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 579 580 } else if (nodeName.equals("name-def-key")) { 581 if (logger.isLoggable(BasicLevel.DEBUG)) 582 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 583 String key = ((Element) child).getAttribute("key"); 584 NameDef nd = getIdNameDef(clazz, ((Element) child).getAttribute("link-end")); 585 clazz.setInheritanceNamingKey(nd, key); 586 if (logger.isLoggable(BasicLevel.DEBUG)) 587 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 588 } 589 } 590 } 591 592 602 private void processScalarField(MetaObject mo, Element scalarFieldElem) { 603 606 String scalarFieldName = scalarFieldElem.getAttribute("name"); 607 NodeList children = scalarFieldElem.getChildNodes(); 608 ScalarField scalarField = null; 609 for (int j = 0; j < children.getLength(); j++) { 610 Node child = children.item(j); 611 String childName = child.getNodeName(); 612 if (logger.isLoggable(BasicLevel.DEBUG)) 613 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 614 if (childName.equals("scalar-type")) { 615 String type = ((Element) child).getAttribute("type"); 616 if (mo instanceof Class ) { 617 scalarField = ((Class ) mo).createHiddenField( 618 scalarFieldName, getPType(type), 619 Integer.parseInt(((Element) child).getAttribute("size")), 620 Integer.parseInt(((Element) child).getAttribute("scale"))); 621 } else if (mo instanceof GenClassRef) { 622 scalarField = ((GenClassRef) mo).createHiddenField( 623 scalarFieldName, getPType(type), 624 Integer.parseInt(((Element) child).getAttribute("size")), 625 Integer.parseInt(((Element) child).getAttribute("scale"))); 626 } else if (mo instanceof CompositeName) { 627 scalarField = ((CompositeName) mo).createCompositeNameField( 628 scalarFieldName, getPType(type), 629 Integer.parseInt(((Element) child).getAttribute("size")), 630 Integer.parseInt(((Element) child).getAttribute("scale"))); 631 } 632 } else if (childName.equals("null-value")) { 633 String value = ((Element) child).getAttribute("value"); 634 scalarField.setNullValue(value); 635 } 636 if (logger.isLoggable(BasicLevel.DEBUG)) 637 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">"); 638 } 639 scalarField.setIsAutoCalculated(Boolean.valueOf( 640 scalarFieldElem.getAttribute("auto-calculated")).booleanValue()); 641 String status = scalarFieldElem.getAttribute("status"); 642 if (status == null || status.equals("variable-persistent")) { 643 scalarField.setStatus(PrimitiveElement.VARIABLE_PERSISTENT); 644 } else if (status.equals("constant-persistent")) { 645 scalarField.setStatus(PrimitiveElement.CONSTANT_PERSISTENT); 646 } else if (status.equals("constant-non-persistent")) { 647 scalarField.setStatus(PrimitiveElement.CONSTANT_NON_PERSISTENT); 648 } 649 } 650 651 660 private void processConstantValue(MetaObject mo, Element constantElem) 661 throws PException { 662 665 String fieldName = constantElem.getAttribute("field-name"); 666 String fieldValue = constantElem.getAttribute("field-value"); 667 TypedElement te = ((Class )mo).getTypedElement(fieldName); 668 if (te != null) { 669 if (!(te instanceof PrimitiveElement)) { 670 throw new PException("Trying to assign a constant value to a non-primitive field " + fieldName); 671 } 672 PrimitiveElement pe = (PrimitiveElement) te; 673 if (! pe.isConstant()) { 674 throw new PException("Trying to assign a constant value to a non-constant field " + fieldName); 675 } 676 } 677 if (logger.isLoggable(BasicLevel.DEBUG)) { 678 logger.log(BasicLevel.DEBUG, "Assign constant value: name=" 679 + fieldName + " / value=" + fieldValue); 680 } 681 ((Class )mo).setConstantValue(fieldName, fieldValue); 682 } 683 684 695 private void processField(Class clazz, Element fieldElem) { 696 699 String fieldName = fieldElem.getAttribute("name"); 700 NodeList children = fieldElem.getChildNodes(); 701 for (int i = 0; i < children.getLength(); i++) { 702 Node child = children.item(i); 703 String nodeName = child.getNodeName(); 704 if (nodeName.equals("primitive-type")) { 705 710 if (logger.isLoggable(BasicLevel.DEBUG)) 711 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 712 PrimitiveElement pe = clazz.createPrimitiveElement(fieldName, 713 getPType(((Element) child).getAttribute("type")), 714 Integer.parseInt(((Element) child).getAttribute("size")), 715 Integer.parseInt(((Element) child).getAttribute("scale")) 716 ); 717 pe.setIsAutoCalculated(Boolean.valueOf( 718 ((Element) child).getAttribute("auto-calculated")).booleanValue()); 719 String status = fieldElem.getAttribute("status"); 720 if (status == null || status.equals("variable-persistent")) { 721 pe.setStatus(PrimitiveElement.VARIABLE_PERSISTENT); 722 } else if (status.equals("constant-persistent")) { 723 pe.setStatus(PrimitiveElement.CONSTANT_PERSISTENT); 724 } else if (status.equals("constant-non-persistent")) { 725 pe.setStatus(PrimitiveElement.CONSTANT_NON_PERSISTENT); 726 } 727 if (logger.isLoggable(BasicLevel.DEBUG)) 728 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 729 } else if (nodeName.equals("class-ref")) { 730 if (logger.isLoggable(BasicLevel.DEBUG)) 731 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 732 processClassRef(clazz, (Element) child); 733 if (logger.isLoggable(BasicLevel.DEBUG)) 734 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 735 } else if (nodeName.equals("gen-class-ref")) { 736 if (logger.isLoggable(BasicLevel.DEBUG)) 737 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 738 processGenClassRef(clazz, (Element) child); 739 if (logger.isLoggable(BasicLevel.DEBUG)) 740 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 741 } 742 } 743 } 744 745 751 private Class getReferencedClass(MetaObject mo, Element classRefElem) { 752 String fqClassName = classRefElem.getAttribute("class-name"); 753 Class aClass = metaInfoManager.getClass(fqClassName); 754 if (aClass == null) { 755 aClass = metaInfoManager.createClass(fqClassName); 756 undefinedMO.add(aClass); 757 } 758 return aClass; 759 } 760 761 772 private void processClassRef(MetaObject mo, Element classRefElem) { 773 Class referencedClass = getReferencedClass(mo, classRefElem); 774 ClassRef classRef = null; 776 if (mo instanceof Class ) { 777 Node fieldNode = classRefElem.getParentNode(); 778 String fieldName = ((Element) fieldNode).getAttribute("name"); 779 classRef = ((Class ) mo).createClassRef(fieldName, referencedClass); 780 } else if (mo instanceof GenClassRef) { 781 classRef = ((GenClassRef) mo).createClassRef(referencedClass); 782 } 783 NodeList children = classRefElem.getChildNodes(); 784 for (int i = 0; i < children.getLength(); i++) { 785 Node child = children.item(i); 786 String childName = child.getNodeName(); 787 if (childName.equals("name-def")) { 788 792 if (logger.isLoggable(BasicLevel.DEBUG)) 793 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 794 NameDef nameDef = classRef.createRefNameDef(); 795 processNameDef(nameDef, ((Element) child)); 796 if (logger.isLoggable(BasicLevel.DEBUG)) 797 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 798 } 799 } 800 } 801 802 809 private void processFieldRef(Reference mo, Element refTypeElem) { 810 Node fieldRefNode = refTypeElem.getNextSibling(); 811 if (fieldRefNode != null) { 812 815 String fieldRefNodeName = fieldRefNode.getNodeName(); 816 if (logger.isLoggable(BasicLevel.DEBUG)) 817 logger.log(BasicLevel.DEBUG, 818 "begin =<" + fieldRefNodeName + ">"); 819 820 if (logger.isLoggable(BasicLevel.DEBUG)) 822 logger.log(BasicLevel.DEBUG, 823 "end =<" + fieldRefNodeName + ">"); 824 } 825 } 826 827 833 private PType getPType(String name) { 834 if (name.equals(PTypeSpace.BOOLEAN.getJormName())) 835 return PTypeSpace.BOOLEAN; 836 if (name.equals(PTypeSpace.OBJBOOLEAN.getJormName())) 837 return PTypeSpace.OBJBOOLEAN; 838 else if (name.equals(PTypeSpace.OBJCHAR.getJormName()) 839 || name.equals("Character")) 840 return PTypeSpace.OBJCHAR; 841 else if (name.equals(PTypeSpace.CHAR.getJormName())) 842 return PTypeSpace.CHAR; 843 else if (name.equals(PTypeSpace.OBJBYTE.getJormName())) 844 return PTypeSpace.OBJBYTE; 845 else if (name.equals(PTypeSpace.BYTE.getJormName())) 846 return PTypeSpace.BYTE; 847 else if (name.equals(PTypeSpace.OBJSHORT.getJormName())) 848 return PTypeSpace.OBJSHORT; 849 else if (name.equals(PTypeSpace.SHORT.getJormName())) 850 return PTypeSpace.SHORT; 851 else if (name.equals(PTypeSpace.OBJINT.getJormName()) 852 || name.equals("Integer")) 853 return PTypeSpace.OBJINT; 854 else if (name.equals(PTypeSpace.INT.getJormName())) 855 return PTypeSpace.INT; 856 else if (name.equals(PTypeSpace.OBJLONG.getJormName())) 857 return PTypeSpace.OBJLONG; 858 else if (name.equals(PTypeSpace.LONG.getJormName())) 859 return PTypeSpace.LONG; 860 else if (name.equals(PTypeSpace.OBJFLOAT.getJormName())) 861 return PTypeSpace.OBJFLOAT; 862 else if (name.equals(PTypeSpace.FLOAT.getJormName())) 863 return PTypeSpace.FLOAT; 864 else if (name.equals(PTypeSpace.OBJDOUBLE.getJormName())) 865 return PTypeSpace.OBJDOUBLE; 866 else if (name.equals(PTypeSpace.DOUBLE.getJormName())) 867 return PTypeSpace.DOUBLE; 868 else if (name.equals(PTypeSpace.STRING.getJormName()) 869 || name.equals("String")) 870 return PTypeSpace.STRING; 871 else if (name.equals(PTypeSpace.DATE.getJormName()) 872 || name.equals("Date")) 873 return PTypeSpace.DATE; 874 else if (name.equals(PTypeSpace.SERIALIZED.getJormName()) 875 || name.equals("Serializable")) 876 return PTypeSpace.SERIALIZED; 877 else if (name.equals(PTypeSpace.BYTEARRAY.getJormName())) 878 return PTypeSpace.BYTEARRAY; 879 else if (name.equals(PTypeSpace.CHARARRAY.getJormName())) 880 return PTypeSpace.CHARARRAY; 881 else if (name.equals(PTypeSpace.BIGINTEGER.getJormName())) 882 return PTypeSpace.BIGINTEGER; 883 else if (name.equals(PTypeSpace.BIGDECIMAL.getJormName())) 884 return PTypeSpace.BIGDECIMAL; 885 else { 886 logger.log(BasicLevel.ERROR, "Invalid type name: " + name); 887 return null; 888 } 889 } 890 891 892 903 private void processNameDef(NameDef nameDef, Element nameDefElem) { 904 String name = nameDefElem.getAttribute("name"); 905 nameDef.setName(name); 906 NodeList children = nameDefElem.getChildNodes(); 907 for (int i = 0; i < children.getLength(); i++) { 908 Node child = children.item(i); 909 String childName = child.getNodeName(); 910 if (childName.equals("system")) { 911 if (logger.isLoggable(BasicLevel.DEBUG)) 912 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 913 nameDef.setSystem(true); 914 if (logger.isLoggable(BasicLevel.DEBUG)) 915 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">"); 916 } else if (childName.equals("field-ref")) { 917 920 if (logger.isLoggable(BasicLevel.DEBUG)) 921 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 922 String fieldName = ((Element) child).getAttribute("field-name"); 923 nameDef.setFieldName(fieldName); 924 if (logger.isLoggable(BasicLevel.DEBUG)) 925 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">"); 926 } else if (childName.equals("composite-name-ref")) { 927 930 if (logger.isLoggable(BasicLevel.DEBUG)) 931 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 932 String fqCN_Name = 933 ((Element) child).getAttribute("composite-name-name"); 934 if (logger.isLoggable(BasicLevel.DEBUG)) { 935 logger.log(BasicLevel.DEBUG, 936 "composite-name-ref composite-name-name =<" + fqCN_Name + ">"); 937 } 938 int lastindexofpoint = fqCN_Name.lastIndexOf('.'); 939 String CN_Name = null; 940 Package CN_Schema = null; 941 if (lastindexofpoint != -1) { 943 String schemaName = 945 fqCN_Name.substring(0, fqCN_Name.lastIndexOf('.')); 946 CN_Schema = metaInfoManager.createPackage(schemaName); 947 CN_Name = 948 fqCN_Name.substring(fqCN_Name.lastIndexOf('.') + 1, 949 fqCN_Name.length()); 950 } else { 951 Object tmp = nameDef.getParent(); 953 while (!(tmp instanceof Package )) { 955 tmp = ((MetaObject) tmp).getParent(); 956 } 957 CN_Schema = (Package ) tmp; 958 logger.log(BasicLevel.WARN, "Be careful, " + 959 "the name of package (Package name) is not specified, " + 960 "continue with " + CN_Schema.getName() + " schema."); 961 CN_Name = fqCN_Name; 962 } 963 if (logger.isLoggable(BasicLevel.DEBUG)) { 964 logger.log(BasicLevel.DEBUG, 965 "schema name =<" + CN_Schema.getName() + ">"); 966 } 967 CompositeName aCompositeName = CN_Schema.getCompositeName(CN_Name); 969 if (aCompositeName == null) { 970 aCompositeName = CN_Schema.createCompositeName(CN_Name); 971 undefinedMO.add(aCompositeName); 972 } 973 NameRef nameRef = nameDef.createNameRef(aCompositeName); 974 processNameRef(nameRef, ((Element) child)); 975 if (logger.isLoggable(BasicLevel.DEBUG)) 976 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">"); 977 } 978 } 979 } 980 981 990 private void processNameRef(NameRef nameRef, Element compositeNameRefElem) { 991 NodeList children = compositeNameRefElem.getChildNodes(); 992 for (int i = 0; i < children.getLength(); i++) { 993 Node child = children.item(i); 994 String nodeName = child.getNodeName(); 995 if (nodeName.equals("composite-name-field-projection")) { 996 1000 if (logger.isLoggable(BasicLevel.DEBUG)) 1001 logger.log(BasicLevel.DEBUG, "begin =<" + nodeName + ">"); 1002 String CN_fieldName = 1003 ((Element) child).getAttribute("composite-name-field-name"); 1004 String classfieldName = 1005 ((Element) child).getAttribute("class-field-name"); 1006 nameRef.addProjection(CN_fieldName, classfieldName); 1007 if (logger.isLoggable(BasicLevel.DEBUG)) 1008 logger.log(BasicLevel.DEBUG, "end =<" + nodeName + ">"); 1009 } 1010 } 1011 } 1012 1013 1022 1042 1043 1052 private void processGenClassRef(MetaObject mo, Element genClassRefElem) { 1053 NodeList children = genClassRefElem.getChildNodes(); 1054 GenClassRef genClassRef = null; 1055 for (int i = 0; i < children.getLength(); i++) { 1056 Node child = children.item(i); 1057 String childName = child.getNodeName(); 1058 if (childName.equals("gen-class")) { 1059 1063 if (logger.isLoggable(BasicLevel.DEBUG)) 1064 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 1065 String genClassName = 1066 ((Element) child).getAttribute("gen-class-name"); 1067 if (mo instanceof Class ) { 1068 Node fieldNode = genClassRefElem.getParentNode(); 1069 String fieldName = ((Element) fieldNode).getAttribute("name"); 1070 genClassRef = 1071 ((Class ) mo).createGenClassRef(fieldName, genClassName); 1072 1077 } else if (mo instanceof GenClassRef) { 1078 genClassRef = 1079 ((GenClassRef) mo).createGenClassRef(genClassName); 1080 } 1081 idvalue2genclassref.put(genClassRef.getGenClassId(), genClassRef); 1082 if (logger.isLoggable(BasicLevel.DEBUG)) { 1083 logger.log(BasicLevel.DEBUG, 1084 "genclass id =<" + genClassRef.getGenClassId() + ">"); 1085 } 1086 processGenClass(genClassRef, (Element) child); 1087 if (logger.isLoggable(BasicLevel.DEBUG)) 1088 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">"); 1089 } else if (childName.equals("name-def")) { 1090 1094 if (logger.isLoggable(BasicLevel.DEBUG)) 1095 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 1096 NameDef nameDef = genClassRef.createRefNameDef(); 1097 processNameDef(nameDef, ((Element) child)); 1098 if (logger.isLoggable(BasicLevel.DEBUG)) 1099 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">"); 1100 } 1101 } 1102 } 1103 1104 1118 private void processGenClass(GenClassRef genClassRef, 1119 Element genClassElem) { 1120 NodeList children = genClassElem.getChildNodes(); 1121 for (int i = 0; i < children.getLength(); i++) { 1122 Node child = children.item(i); 1123 String childName = child.getNodeName(); 1124 if (logger.isLoggable(BasicLevel.DEBUG)) 1125 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 1126 if (childName.equals("scalar-field")) { 1127 1130 processScalarField(genClassRef, (Element) child); 1131 } else if (childName.equals("name-def")) { 1132 1136 NameDef nameDef = genClassRef.createIdNameDef(); 1137 processNameDef(nameDef, ((Element) child)); 1138 } else if (childName.equals("index")) { 1139 processIndex(genClassRef, ((Element) child)); 1140 } else if (childName.equals("primitive-type")) { 1141 1146 genClassRef.createPrimitiveElement( 1147 getPType(((Element) child).getAttribute("type")), 1148 Integer.parseInt(((Element) child).getAttribute("size")), 1149 Integer.parseInt(((Element) child).getAttribute("scale"))); 1150 } else if (childName.equals("class-ref")) { 1151 processClassRef(genClassRef, (Element) child); 1152 } else if (childName.equals("gen-class-ref")) { 1153 processClassRef(genClassRef, (Element) child); 1154 } 1155 if (logger.isLoggable(BasicLevel.DEBUG)) 1156 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">"); 1157 } 1158 } 1159 1160 private void processIndex(GenClassRef genClassRef, 1161 Element indexElem) { 1162 NodeList children = indexElem.getChildNodes(); 1163 for (int i = 0; i < children.getLength(); i++) { 1164 Node child = children.item(i); 1165 String childName = child.getNodeName(); 1166 if (logger.isLoggable(BasicLevel.DEBUG)) 1167 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 1168 if (childName.equals("field-ref")) { 1169 genClassRef.addIndexField( 1170 ((Element) child).getAttribute("field-name")); 1171 } 1172 if (logger.isLoggable(BasicLevel.DEBUG)) 1173 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">"); 1174 } 1175 } 1176 1177 1185 public void parseMapping(Element mappingElem, ClassProject classProject) 1186 throws PException { 1187 NodeList nodes = mappingElem.getChildNodes(); 1188 MappingParser mp = null; 1189 Mapping mapping = null; 1190 String mapperName = null; 1191 for (int i = 0; i < nodes.getLength(); i++) { 1192 Node child = nodes.item(i); 1193 String childName = child.getNodeName(); 1194 if (logger.isLoggable(BasicLevel.DEBUG)) { 1195 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">"); 1196 } 1197 int pos = childName.indexOf("-"); 1198 if (pos == -1) { 1199 continue; 1200 } 1201 mapperName = childName.substring(0, pos); 1202 mp = getMappingParser(mapperName); 1203 if (mp == null) { 1204 if (logger.isLoggable(BasicLevel.DEBUG)) { 1205 logger.log(BasicLevel.DEBUG, 1206 "cannot get the mapping parser!"); 1207 } 1208 break; 1209 } 1210 mp.setPathExplorer(pathExplorer); 1211 ((Loggable) mp).setLogger(logger); 1212 mp.setMetaInfoManager(metaInfoManager); 1213 mp.setidvalue2genclassref(idvalue2genclassref); 1214 mp.setCurrentClass((Class ) classProject.getParent()); 1215 mp.setmotable(motable); 1216 mapping = classProject.createMapping(mapperName); 1217 if (logger.isLoggable(BasicLevel.DEBUG)) { 1218 logger.log(BasicLevel.DEBUG, 1219 "current class =<" + ((Class ) classProject.getParent()).getName() + ">"); 1220 logger.log(BasicLevel.DEBUG, 1221 "current project =<" + classProject.getProjectName() + ">"); 1222 logger.log(BasicLevel.DEBUG, 1223 "current mapping =<" + mapping.getMapperName() + ">"); 1224 } 1225 if (mapping instanceof Loggable) { 1226 if (loggerFactory != null) { 1227 ((Loggable) mapping).setLoggerFactory(loggerFactory); 1228 } else if (logger != null) { 1229 ((Loggable) mapping).setLogger(logger); 1230 } 1231 } 1232 mp.parseMapping((Element) child, mapping); 1233 if (logger.isLoggable(BasicLevel.DEBUG)) { 1234 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">"); 1235 } 1236 } 1237 } 1238 1239 1244 public void setLogger(Logger logger) { 1245 this.logger = logger; 1246 if (resolver != null) 1247 resolver.setLogger(logger); 1248 } 1249 1250 1260 private void addImplicitMappings(Class clazz, Set handledClasses) { 1261 for (Iterator itSuperClass = clazz.getSuperClasses().iterator(); 1263 itSuperClass.hasNext();) { 1264 Class superClass = (Class ) itSuperClass.next(); 1265 if (!handledClasses.contains(superClass)) { 1267 addImplicitMappings(superClass, handledClasses); 1268 } 1269 for (Iterator itProject = superClass.getClassProjects().iterator(); 1271 itProject.hasNext();) { 1272 ClassProject superClassProject = (ClassProject) itProject.next(); 1273 ClassProject classProject = clazz. 1276 getClassProject(superClassProject.getProjectName()); 1277 if (classProject == null) { 1278 classProject = clazz.createClassProject(superClassProject. 1279 getProjectName()); 1280 logger.log(BasicLevel.DEBUG, "Create implicit class project " 1281 + clazz.getName() + "<" + classProject.getProjectName() 1282 + ">"); 1283 } 1284 1285 for (Iterator itMapping = superClassProject.getMappings().iterator(); 1287 itMapping.hasNext();) { 1288 Mapping superMapping = (Mapping) itMapping.next(); 1289 Mapping mapping = classProject.getMapping(superMapping.getMapperName()); 1292 ClassMapping classMapping; 1293 if (mapping == null) { 1294 mapping = classProject.createMapping(superMapping.getMapperName()); 1295 classMapping = mapping.createClassMapping(""); 1296 logger.log(BasicLevel.DEBUG, "Create implicit mapping & class mapping " 1297 + clazz.getName() + "<" + classMapping.getProjectName() + 1298 "," + classMapping.getMapperName() + ">"); 1299 } else { 1300 classMapping = mapping.getClassMapping(); 1301 } 1302 IdentifierMapping id = classMapping.getIdentifierMapping(); 1303 if (id == null) { 1304 logger.log(BasicLevel.DEBUG, "Create implicit id mapping " 1305 + clazz.getName() + "<" + classMapping.getProjectName() + 1306 "," + classMapping.getMapperName() + "> from IM of " 1307 + superClass.getName()); 1308 classMapping.createIdentifierMapping((NameDef) 1309 superMapping.getClassMapping().getIdentifierMapping().getLinkedMO()); 1310 } 1311 ParentClassMapping pm = classMapping.getParentClassMapping(superClass.getFQName()); 1312 if (pm == null) { 1313 pm = classMapping.createImplicitParentClassMapping(superClass); 1314 logger.log(BasicLevel.DEBUG, "Create implicit parent class mapping " 1315 + clazz.getName() + "<" + classMapping.getProjectName() + 1316 "," + classMapping.getMapperName() + "> to " 1317 + superClass.getName()); 1318 } 1319 } 1320 } 1321 } 1322 } 1323 1329 private void addImplicitDependencies(Class clazz) { 1330 for (Iterator itProject = clazz.getClassProjects().iterator(); itProject.hasNext();) { 1331 ClassProject classProject = (ClassProject) itProject.next(); 1332 for (Iterator itMapping = classProject.getMappings().iterator(); itMapping.hasNext();) { 1333 Mapping mapping = (Mapping) itMapping.next(); 1334 ClassMapping classMapping = mapping.getClassMapping(); 1335 classMapping.addImplicitDependencies(); 1336 } 1337 } 1338 } 1339 1340} 1341 1342 | Popular Tags |