1 27 28 package org.objectweb.speedo.generation.parser; 29 30 import java.io.File ; 31 import java.util.Iterator ; 32 import java.util.Properties ; 33 import java.util.StringTokenizer ; 34 35 import org.apache.tools.ant.types.DTDLocation; 36 import org.objectweb.jorm.xml2mi.lib.SAXParserHelper; 37 import org.objectweb.speedo.api.SpeedoException; 38 import org.objectweb.speedo.api.SpeedoProperties; 39 import org.objectweb.speedo.generation.api.SpeedoXMLError; 40 import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent; 41 import org.objectweb.speedo.metadata.SpeedoArray; 42 import org.objectweb.speedo.metadata.SpeedoClass; 43 import org.objectweb.speedo.metadata.SpeedoCollection; 44 import org.objectweb.speedo.metadata.SpeedoDatastoreId; 45 import org.objectweb.speedo.metadata.SpeedoElement; 46 import org.objectweb.speedo.metadata.SpeedoExtension; 47 import org.objectweb.speedo.metadata.SpeedoFetchGroup; 48 import org.objectweb.speedo.metadata.SpeedoField; 49 import org.objectweb.speedo.metadata.SpeedoIdentity; 50 import org.objectweb.speedo.metadata.SpeedoMap; 51 import org.objectweb.speedo.metadata.SpeedoModifier; 52 import org.objectweb.speedo.metadata.SpeedoNullValue; 53 import org.objectweb.speedo.metadata.SpeedoPackage; 54 import org.objectweb.speedo.metadata.SpeedoPredefinedQuery; 55 import org.objectweb.speedo.metadata.SpeedoTuple; 56 import org.objectweb.speedo.metadata.SpeedoVersion; 57 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor; 58 import org.objectweb.speedo.sequence.lib.SpeedoSequence; 59 import org.objectweb.util.monolog.api.BasicLevel; 60 import org.w3c.dom.Document ; 61 import org.w3c.dom.Node ; 62 import org.w3c.dom.NodeList ; 63 import org.xml.sax.SAXException ; 64 65 68 public class JdoParser 69 extends AbstractGeneratorComponent { 70 71 public final static String LOGGER_NAME 72 = SpeedoProperties.LOGGER_NAME + ".generation.parser"; 73 74 SAXParserHelper parser; 75 76 79 80 83 public boolean init() throws SpeedoException { 84 logger = scp.loggerFactory.getLogger(LOGGER_NAME); 85 if (scp.jdo.isEmpty()) 86 return false; 87 Properties p = new Properties (); 88 for (Iterator iter = scp.dtdLocations.iterator(); iter.hasNext();) { 89 DTDLocation element = (DTDLocation) iter.next(); 90 p.setProperty(element.getPublicId(), element.getLocation()); 91 } 92 try { 93 parser = new SAXParserHelper(p, logger, getClass().getClassLoader(), false); 94 } catch (SAXException e) { 95 throw new SpeedoException(e); 96 } 97 return true; 98 } 99 100 public void process() throws SpeedoException { 101 if (scp.jdo.isEmpty()) 102 return; 103 logger.log(BasicLevel.DEBUG, scp.jdo.size() + " files to parse"); 104 for (Iterator it = scp.jdo.iterator(); it.hasNext();) { 105 String xmlFileName = (String ) it.next(); 106 logger.log(BasicLevel.DEBUG, "Parse " + xmlFileName); 107 scp.getXmldescriptor().put(xmlFileName, createObjectModel(xmlFileName)); 108 } 109 110 for (Iterator itDesc = scp.getXmldescriptor().values().iterator(); itDesc.hasNext();) { 111 SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next(); 112 for (Iterator itPack = desc.jdoPackage.values().iterator(); itPack.hasNext();) { 113 SpeedoPackage sp = (SpeedoPackage) itPack.next(); 114 for (Iterator itclass = sp.jdoClass.values().iterator(); itclass.hasNext();) { 115 SpeedoClass jdoclass = (SpeedoClass) itclass.next(); 116 jdoclass.computeFieldNumbers(); 117 } 118 } 119 } 120 } 121 122 123 126 134 private SpeedoXMLDescriptor createObjectModel(String xmlFile) 135 throws SpeedoException { 136 try { 137 Document document = parser.parse(scp.jdoDir + File.separator + xmlFile); 139 140 SpeedoXMLDescriptor desc = new SpeedoXMLDescriptor(scp.smi); 142 desc.xmlFile = xmlFile; 143 return (SpeedoXMLDescriptor) treatDocument(document, desc); 145 } catch (SpeedoException e) { 146 throw e; 147 } catch (Exception e) { 148 throw new SpeedoException("Error with the construction of file " 149 + scp.jdoDir + File.separator + xmlFile, e); 150 } 151 } 152 153 163 private Object treatNode(Node node, Object o) throws SpeedoException { 164 int type = node.getNodeType(); 166 if (type != Node.ELEMENT_NODE) 167 return o; 168 String tag = node.getNodeName(); 170 Node n = null; 171 logger.log(BasicLevel.DEBUG, "Parse tag: " + tag); 172 173 if (tag.equals("jdo")) { 175 return o; 177 178 } else if (tag.equals("package")) { SpeedoPackage p = new SpeedoPackage(); 180 181 n = node.getAttributes().getNamedItem("name"); 183 if (n == null) 184 throw new SpeedoXMLError("Attribute name for tag package requested."); 185 p.name = n.getNodeValue(); 186 187 logger.log(BasicLevel.DEBUG, "New package: name= " + p.name); 188 return p; 189 190 } else if (tag.equals("class")) { SpeedoClass c = new SpeedoClass(); 192 193 n = node.getAttributes().getNamedItem("name"); 195 if (n == null) 196 throw new SpeedoXMLError("Attribute name for tag class requested."); 197 c.name = n.getNodeValue(); 198 199 n = node.getAttributes().getNamedItem("identity-type"); 201 if (n != null) 202 c.identityType = SpeedoIdentity.toByte(n.getNodeValue()); 203 204 n = node.getAttributes().getNamedItem("objectid-class"); 206 if (n != null) { 207 c.objectidClass = n.getNodeValue(); 208 c.identityType = SpeedoIdentity.USER_ID; 209 } 210 211 n = node.getAttributes().getNamedItem("requires-extent"); 213 if (n != null) 214 c.requiresExtent = Boolean.valueOf(n.getNodeValue()).booleanValue(); 215 216 n = node.getAttributes().getNamedItem("detachable"); 218 if (n != null) 219 c.isDetachable = Boolean.valueOf(n.getNodeValue()).booleanValue(); 220 221 n = node.getAttributes().getNamedItem("persistence-capable-superclass"); 223 if (n != null) 224 c.superClassName = n.getNodeValue(); 225 226 logger.log(BasicLevel.DEBUG, "New class: " 227 + "name=" + c.name 228 + " / id = " + c.identityType 229 + " / idClass = " + c.objectidClass 230 + " / requiresExtent = " + c.requiresExtent 231 + " / detachable = " + c.isDetachable 232 ); 233 return c; 234 } else if (tag.equals("sequence")) { SpeedoSequence s = new SpeedoSequence(); 236 237 n = node.getAttributes().getNamedItem("name"); 239 if (n == null) 240 throw new SpeedoXMLError("Attribute name for tag sequence requested."); 241 s.name = n.getNodeValue(); 242 243 n = node.getAttributes().getNamedItem("datastore-sequence"); 245 if (n != null) 246 s.datastoreName = n.getNodeValue(); 247 248 n = node.getAttributes().getNamedItem("factory-class"); 250 if (n != null) { 251 s.factoryClass = n.getNodeValue(); 252 } 253 254 n = node.getAttributes().getNamedItem("strategy"); 256 if (n == null) 257 throw new SpeedoXMLError("Attribute strategy for tag sequence requested."); 258 s.strategy = SpeedoSequence.strategyToByte(n.getNodeValue()); 259 260 logger.log(BasicLevel.DEBUG, "New sequence: " 261 + "name=" + s.name 262 + " / datastoreName = " + s.datastoreName 263 + " / factoryClass = " + s.factoryClass 264 + " / strategy = " + s.strategy 265 ); 266 267 return s; 268 } else if (tag.equals("field")) { SpeedoField f = new SpeedoField(); 270 271 n = node.getAttributes().getNamedItem("name"); 273 if (n == null) 274 throw new SpeedoXMLError("Attribute name for tag field requested."); 275 String name = n.getNodeValue(); 276 if(name.indexOf(".") != -1){ 277 278 } 279 f.name = n.getNodeValue(); 280 281 n = node.getAttributes().getNamedItem("persistence-modifier"); 283 if (n != null) 284 f.persistenceModifier = SpeedoModifier.toByte(n.getNodeValue()); 285 286 n = node.getAttributes().getNamedItem("primary-key"); 288 if (n != null) 289 f.primaryKey = Boolean.valueOf(n.getNodeValue()).booleanValue(); 290 291 n = node.getAttributes().getNamedItem("null-value"); 293 if (n != null) { 294 f.nullValue = SpeedoNullValue.toByte(n.getNodeValue()); 295 } 296 297 n = node.getAttributes().getNamedItem("default-fetch-group"); 299 if (n != null) 300 f.defaultFetchGroup = Boolean.valueOf(n.getNodeValue()).booleanValue(); 301 302 n = node.getAttributes().getNamedItem("fetch-group"); 304 if (n != null) 305 f.fetchGroup = n.getNodeValue(); 306 307 n = node.getAttributes().getNamedItem("depth"); 309 if (n != null) 310 f.depth = Integer.valueOf(n.getNodeValue()).intValue(); 311 312 n = node.getAttributes().getNamedItem("embedded"); 314 if (n != null) 315 f.embedded = Boolean.valueOf(n.getNodeValue()).booleanValue(); 316 317 n = node.getAttributes().getNamedItem("value-strategy"); 319 if (n != null) 320 f.valueStrategy = n.getNodeValue(); 321 322 n = node.getAttributes().getNamedItem("sequence"); 324 if (n != null) 325 f.sequence = n.getNodeValue(); 326 327 logger.log(BasicLevel.DEBUG, "New field: " 328 + "name=" + f.name 329 + " / persistenceModifier = " + f.persistenceModifier 330 + " / primaryKey = " + f.primaryKey 331 + " / nullValue = " + f.nullValue 332 + " / defaultFetchGroup = " + f.defaultFetchGroup 333 + " / embedded = " + f.embedded 334 + " / depth = " + f.depth 335 + " / valueStrategy = " + f.valueStrategy 336 + " / sequence = " + f.sequence 337 ); 338 return f; 339 340 } else if (tag.equals("datastore-identity")) { SpeedoDatastoreId d = new SpeedoDatastoreId(); 342 343 n = node.getAttributes().getNamedItem("strategy"); 345 if (n == null) 346 throw new SpeedoXMLError("Attribute strategy for tag datastore-identity requested."); 347 d.strategy = SpeedoDatastoreId.toByte(n.getNodeValue()); 348 349 n = node.getAttributes().getNamedItem("sequence"); 351 if (n != null) 352 d.sequence = n.getNodeValue(); 353 354 logger.log(BasicLevel.DEBUG, "New datastore-identity: " 355 + "strategy =" + d.strategy 356 + " / sequence = " + d.sequence 357 ); 358 return d; 359 } else if (tag.equals("collection")) { SpeedoCollection co = new SpeedoCollection(); 361 362 n = node.getAttributes().getNamedItem("element-type"); 364 if (n != null) 365 co.elementType = n.getNodeValue(); 366 367 n = node.getAttributes().getNamedItem("embedded-element"); 369 if (n != null) 370 co.embeddedElement = Boolean.valueOf(n.getNodeValue()).booleanValue(); 371 372 logger.log(BasicLevel.DEBUG, "New collection: " 373 + "elementType=" + co.elementType 374 + " / embeddedElement = " + co.embeddedElement 375 ); 376 return co; 377 378 } else if (tag.equals("map")) { SpeedoMap m = new SpeedoMap(); 380 381 n = node.getAttributes().getNamedItem("key-type"); 383 if (n != null) 384 m.keyType = n.getNodeValue(); 385 386 n = node.getAttributes().getNamedItem("embedded-key"); 388 if (n != null) 389 m.embeddedKey = Boolean.valueOf(n.getNodeValue()).booleanValue(); 390 391 n = node.getAttributes().getNamedItem("value-type"); 393 if (n != null) 394 m.valueType = n.getNodeValue(); 395 396 n = node.getAttributes().getNamedItem("embedded-value"); 398 if (n != null) 399 m.embeddedValue = Boolean.valueOf(n.getNodeValue()).booleanValue(); 400 401 logger.log(BasicLevel.DEBUG, "New map: " 402 + "keyType=" + m.keyType 403 + " / embeddedKey = " + m.embeddedKey 404 + " / valueType = " + m.valueType 405 + " / embeddedValue = " + m.embeddedValue 406 ); 407 return m; 408 409 } else if (tag.equals("array")) { SpeedoArray a = new SpeedoArray(); 411 412 n = node.getAttributes().getNamedItem("embedded-element"); 414 if (n != null) 415 a.embeddedElement = Boolean.valueOf(n.getNodeValue()).booleanValue(); 416 417 logger.log(BasicLevel.DEBUG, "New array: " 418 + "embeddedElement=" + a.embeddedElement 419 ); 420 return a; 421 } else if (tag.equals("version")) { SpeedoVersion v = new SpeedoVersion(); 423 424 n = node.getAttributes().getNamedItem("strategy"); 426 v.strategy = SpeedoVersion.toByte(n.getNodeValue()); 427 428 logger.log(BasicLevel.DEBUG, "New version: " 429 + "strategy=" + v.strategy); 430 return v; 431 } else if (tag.equals("extension")) { SpeedoExtension e = new SpeedoExtension(); 433 434 n = node.getAttributes().getNamedItem("vendor-name"); 436 e.vendorName = n.getNodeValue(); 437 438 n = node.getAttributes().getNamedItem("key"); 440 if (n != null) 441 e.key = n.getNodeValue(); 442 443 n = node.getAttributes().getNamedItem("value"); 445 if (n != null) 446 e.value = n.getNodeValue(); 447 448 return e; 449 } else if (tag.equals("fetch-group")) { SpeedoFetchGroup fg = new SpeedoFetchGroup(); 451 452 n = node.getAttributes().getNamedItem("name"); 454 fg.name = n.getNodeValue(); 455 456 n = node.getAttributes().getNamedItem("post-load"); 458 if (n != null){ 459 fg.postLoad = Boolean.valueOf(n.getNodeValue()).booleanValue(); 460 } 461 else{ 462 if(fg.name.equals("default")) 463 fg.postLoad = true; 464 else 465 fg.postLoad = false; 466 } 467 return fg; 468 } else if (tag.equals("query")) { 469 SpeedoPredefinedQuery spq = new SpeedoPredefinedQuery(); 470 471 spq.name = getStringAttributeValue(node, "name", null); 472 spq.language = getStringAttributeValue(node, "language", null); 473 spq.filter = getStringAttributeValue(node, "filter", null); 474 spq.ordering = getStringAttributeValue(node, "ordering", null); 475 spq.sql = getStringAttributeValue(node, "sql", null); 476 spq.ignoreCache = getBooleanAttributeValue(node, "ignore-cache", false); 477 spq.includeSubclasses = getBooleanAttributeValue(node, "include-subclasses", true); 478 479 String range = getStringAttributeValue(node, "range", null); 480 if (range != null) { 481 StringTokenizer st = new StringTokenizer (range, ",.;:/#", false); 482 if (st.hasMoreTokens()) { 483 spq.rangeFirst = Integer.valueOf(st.nextToken()).intValue(); 484 if (st.hasMoreTokens()) { 485 spq.rangeLast = Integer.valueOf(st.nextToken()).intValue(); 486 } 487 if (st.hasMoreTokens()) { 488 logger.log(BasicLevel.WARN, 489 "Query range contains too values, first=" 490 + spq.rangeFirst + ", last=" + spq.rangeLast 491 + ", query=" + spq.name + ", range: " + range); 492 } 493 } else { 494 logger.log(BasicLevel.WARN, 495 "Query range value malformed (ignored), query:" 496 + spq.name + ", range: " + range); 497 } 498 } 499 return spq; 500 } else if (tag.equals("declare")) { SpeedoPredefinedQuery spq = (SpeedoPredefinedQuery) o; 502 spq.declareImports = getStringAttributeValue(node, "imports", null); 503 spq.declareParameters = getStringAttributeValue(node, "parameters", null); 504 spq.declareVariables = getStringAttributeValue(node, "variables", null); 505 return spq; 506 } else if (tag.equals("result")) { SpeedoPredefinedQuery spq = (SpeedoPredefinedQuery) o; 508 spq.resultGrouping = getStringAttributeValue(node, "grouping", null); 509 spq.resultClass = getStringAttributeValue(node, "class", null); 510 spq.resultUnique = getBooleanAttributeValue(node, "unique", false); 511 return spq; 512 } else 513 return o; 514 } 515 516 private String getStringAttributeValue(Node node, String attribName, String defaultValue) { 517 Node n = node.getAttributes().getNamedItem(attribName); 518 if (n != null) { 519 return n.getNodeValue(); 520 } else { 521 return defaultValue; 522 } 523 } 524 525 526 private boolean getBooleanAttributeValue(Node node, String attribName, boolean defaultValue) { 527 Node n = node.getAttributes().getNamedItem(attribName); 528 if (n != null) { 529 return Boolean.valueOf(n.getNodeValue()).booleanValue(); 530 } else { 531 return defaultValue; 532 } 533 } 534 535 542 private Object treatDocument(Node node, Object o) throws SpeedoException { 543 o = treatNode(node, o); 545 NodeList children = node.getChildNodes(); 547 548 if (children != null) { 550 for (int i = 0; i < children.getLength(); i++) { 552 Object fils = treatDocument(children.item(i), o); 554 add(o, fils, children.item(i).getNodeName()); 556 } 557 } 558 return o; 559 } 560 561 569 private void add(Object graph, Object newElem, String tag) 570 throws SpeedoException { 571 572 if (tag.equals("package")) { logger.log(BasicLevel.DEBUG, "add package '" + ((SpeedoPackage) newElem).name + "'"); 574 ((SpeedoXMLDescriptor) graph).add(newElem, true, logger); 575 576 } else if (tag.equals("class")) { logger.log(BasicLevel.DEBUG, "add class '" + ((SpeedoClass) newElem).name + "'"); 578 ((SpeedoPackage) graph).addClass(newElem, true, logger); 579 580 } else if (tag.equals("sequence")) { logger.log(BasicLevel.DEBUG, "add sequence '" + ((SpeedoSequence) newElem).name + "'"); 582 ((SpeedoPackage) graph).addSequence(newElem); 583 584 } else if (tag.equals("datastore-identity")) { logger.log(BasicLevel.DEBUG, "add datastore-identity '" + ((SpeedoDatastoreId) newElem).strategy + "'"); 586 ((SpeedoClass) graph).addDatastoreId(newElem); 587 588 } else if (tag.equals("field")) { if(graph instanceof SpeedoClass){ 590 if (logger.isLoggable(BasicLevel.DEBUG)) { 591 logger.log(BasicLevel.DEBUG, 592 "add field '" + ((SpeedoField) newElem).name 593 + "' into the class '" + ((SpeedoClass) graph).name +"'"); 594 } 595 ((SpeedoClass) graph).add(newElem, true, logger); 596 if (((SpeedoField) newElem).valueStrategy != null && 597 ((SpeedoField) newElem).valueStrategy.equals("sequence") && 598 ((SpeedoField) newElem).sequence != null) { 599 ((SpeedoClass) graph).datastoreSequence = ((SpeedoField) newElem).sequence; 601 } 602 } 603 else{ 604 if (logger.isLoggable(BasicLevel.DEBUG)) { 605 logger.log(BasicLevel.DEBUG, 606 "add field '" + ((SpeedoField) newElem).name 607 + "' into the fetchgroup '" + ((SpeedoFetchGroup) graph).name +"'"); 608 } 609 ((SpeedoFetchGroup) graph).addField(newElem); 610 } 611 } else if (tag.equals("version")) { if (logger.isLoggable(BasicLevel.DEBUG)) { 613 logger.log(BasicLevel.DEBUG, 614 "add version into the class '" + ((SpeedoClass) graph).name +"'"); 615 } 616 ((SpeedoClass) graph).addVersion(newElem); 617 } else if (tag.equals("collection")) { logger.log(BasicLevel.DEBUG, 619 "add collection: '" + ((SpeedoField) graph).name + "'"); 620 ((SpeedoField) graph).jdoTuple = (SpeedoTuple) newElem; 621 ((SpeedoTuple) newElem).jdoField = (SpeedoField) graph; 622 623 } else if (tag.equals("map")) { logger.log(BasicLevel.DEBUG, 625 "add map: '" + ((SpeedoField) graph).name + "'"); 626 ((SpeedoField) graph).jdoTuple = (SpeedoTuple) newElem; 627 ((SpeedoTuple) newElem).jdoField = (SpeedoField) graph; 628 629 } else if (tag.equals("array")) { logger.log(BasicLevel.DEBUG, 631 "add array: '" + ((SpeedoField) graph).name + "'"); 632 ((SpeedoField) graph).jdoTuple = (SpeedoTuple) newElem; 633 ((SpeedoTuple) newElem).jdoField = (SpeedoField) graph; 634 635 } else if (tag.equals("extension")) { logger.log(BasicLevel.DEBUG, "add extension"); 637 ((SpeedoElement) graph).addExtension((SpeedoExtension) newElem); 638 ((SpeedoExtension) newElem).jdoElement = (SpeedoElement) graph; 639 640 } else if (tag.equals("fetch-group")) { if (graph instanceof SpeedoClass){ 642 if (logger.isLoggable(BasicLevel.DEBUG)) { 643 logger.log(BasicLevel.DEBUG, 644 "add fetchgroup '" + ((SpeedoFetchGroup) newElem).name + 645 "' into the class '" + ((SpeedoClass) graph).name +"'"); 646 } 647 ((SpeedoClass) graph).addFetchGroup(newElem); 648 } 649 else{ 650 if (logger.isLoggable(BasicLevel.DEBUG)) { 651 logger.log(BasicLevel.DEBUG, 652 "add fetchgroup '" + ((SpeedoFetchGroup) newElem).name + 653 "' into the class '" + ((SpeedoClass) graph).name +"'"); 654 } 655 ((SpeedoFetchGroup) graph).addFetchGroup(newElem); 656 } 657 } else if (tag.equals("query")) { 658 ((SpeedoClass) graph).name2query.put( 659 ((SpeedoPredefinedQuery) newElem).name, newElem); 660 } 661 } 662 } 663 | Popular Tags |