1 21 package oracle.toplink.essentials.internal.ejb.cmp3.xml; 23 24 import java.net.URL ; 25 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.NodeList ; 36 37 import org.xml.sax.SAXException ; 38 39 import oracle.toplink.essentials.exceptions.XMLParseException; 40 import oracle.toplink.essentials.exceptions.ValidationException; 41 42 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataHelper; 43 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataLogger; 44 45 import oracle.toplink.essentials.internal.ejb.cmp3.xml.parser.XPathEngine; 46 import oracle.toplink.essentials.internal.ejb.cmp3.xml.parser.XMLException; 47 import oracle.toplink.essentials.internal.ejb.cmp3.xml.parser.XMLExceptionHandler; 48 49 54 public class XMLHelper { 55 private Document m_document; 56 private ClassLoader m_loader; 57 private String m_documentName; 58 private String m_defaultPackage; 59 private XPathEngine m_xPathEngine; 60 61 64 protected XMLHelper(Document document, ClassLoader loader) { 65 m_xPathEngine = XPathEngine.getInstance(); 66 m_loader = loader; 67 m_document = document; 68 69 Node node = getNode(document, new String [] {XMLConstants.ENTITY_MAPPINGS, XMLConstants.PACKAGE, XMLConstants.TEXT}); 70 71 if (node != null && node.getNodeValue() != null) { 72 m_defaultPackage = node.getNodeValue(); 73 } else { 74 m_defaultPackage = ""; 75 } 76 } 77 78 81 public XMLHelper(InputStream xmlDocStream, String fileName, ClassLoader loader) { 82 this(parseDocument(xmlDocStream, fileName, loader), loader); 83 m_documentName = fileName; 84 } 85 86 89 public Class getClassForName(String className) { 90 return MetadataHelper.getClassForName(getFullyQualifiedClassName(className), m_loader); 91 } 92 93 98 public Class getClassForNode(Node node) { 99 return MetadataHelper.getClassForName(getClassNameForNode(node), m_loader); 100 } 101 102 108 public String getClassNameForNode(Node node) { 109 return getFullyQualifiedClassName(getNodeValue(node, XMLConstants.ATT_CLASS)); 110 } 111 112 116 public Document getDocument() { 117 return m_document; 118 } 119 120 124 public String getDocumentName() { 125 return m_documentName; 126 } 127 128 134 public String getFullyQualifiedClassName(String className) { 135 return getFullyQualifiedClassName(className, m_defaultPackage); 136 } 137 138 144 public static String getFullyQualifiedClassName(String className, String packageName) { 145 if (packageName.equals("") || className.indexOf(".") != -1) { 147 return className; 148 } 149 150 if (packageName.endsWith(".")) { 153 return (packageName + className); 154 } 155 156 return (packageName + "." + className); 158 } 159 160 165 public String getLoggingContextForDefaultMappingReferenceClass(Node mappingNode) { 166 if (mappingNode.getLocalName().equals(XMLConstants.ONE_TO_ONE)) { 167 return MetadataLogger.ONE_TO_ONE_MAPPING_REFERENCE_CLASS; 168 } 169 if (mappingNode.getLocalName().equals(XMLConstants.ONE_TO_MANY)) { 170 return MetadataLogger.ONE_TO_MANY_MAPPING_REFERENCE_CLASS; 171 } 172 if (mappingNode.getLocalName().equals(XMLConstants.MANY_TO_ONE)) { 173 return MetadataLogger.MANY_TO_ONE_MAPPING_REFERENCE_CLASS; 174 } 175 return MetadataLogger.MANY_TO_MANY_MAPPING_REFERENCE_CLASS; 177 } 178 179 183 public Node getNode(Node node, String xPath) { 184 return getNode(node, new String [] {xPath}); 185 } 186 187 191 public Node getNode(Node node, String [] xPath) { 192 return m_xPathEngine.selectSingleNode(node, xPath); 193 } 194 195 199 public Node getNode(String [] xPath) { 200 return getNode(m_document, xPath); 201 } 202 203 207 public NodeList getNodes(String xPath1, String xPath2) { 208 return getNodes(m_document, new String [] {xPath1, xPath2}); 209 } 210 211 215 public NodeList getNodes(String [] xPath) { 216 return getNodes(m_document, xPath); 217 } 218 219 222 public String getNodeTextValue(Node node, String xPath) { 223 return getNodeValue(node, new String [] {xPath, XMLConstants.TEXT}); 224 } 225 226 229 public String getNodeTextValue(String xPath1, String xPath2) { 230 return getNodeValue(m_document, new String [] {xPath1, xPath2, XMLConstants.TEXT}); 231 } 232 233 236 public String getNodeTextValue(String xPath1, String xPath2, String defaultValue) { 237 return getNodeValue(m_document, new String [] {xPath1, xPath2, XMLConstants.TEXT}, defaultValue); 238 } 239 240 243 public String getNodeValue(Node node, String xPath) { 244 return getNodeValue(node, new String [] {xPath}); 245 } 246 247 250 public boolean getNodeValue(Node node, String xPath, boolean defaultValue) { 251 return getNodeValue(node, new String [] {xPath}, defaultValue); 252 } 253 254 257 public Class getNodeValue(Node node, String xPath, Class defaultValue) { 258 return getNodeValue(node, new String [] {xPath}, defaultValue); 259 } 260 261 264 public int getNodeValue(Node node, String xPath, int defaultValue) { 265 return getNodeValue(node, new String [] {xPath}, defaultValue); 266 } 267 268 271 public String getNodeValue(Node node, String xPath, String defaultValue) { 272 return getNodeValue(node, new String [] {xPath}, defaultValue); 273 } 274 275 278 public NodeList getNodes(Node node, String xPath) { 279 return getNodes(node, new String [] {xPath}); 280 } 281 282 285 public NodeList getNodes(Node node, String xPath1, String xPath2) { 286 return getNodes(node, new String [] {xPath1, xPath2}); 287 } 288 289 292 public NodeList getNodes(Node node, String [] xPath) { 293 return m_xPathEngine.selectNodes(node, xPath); 294 } 295 296 299 public boolean getNodeValue(Node node, String [] xPath, boolean defaultValue) { 300 return getValue(getNode(node, xPath), defaultValue); 301 } 302 303 306 public Class getNodeValue(Node node, String [] xPath, Class defaultValue) { 307 return getValue(getNode(node, xPath), defaultValue); 308 } 309 310 313 public int getNodeValue(Node node, String [] xPath, int defaultValue) { 314 return getValue(getNode(node, xPath), defaultValue); 315 } 316 317 320 public String getNodeValue(Node node, String [] xPath, String defaultValue) { 321 return getValue(getNode(node, xPath), defaultValue); 322 } 323 324 327 public String getNodeValue(Node node, String [] xPath) { 328 return getNodeValue(node, xPath, ""); 329 } 330 331 334 public String getNodeValue(String [] xPath) { 335 return getNodeValue(xPath, ""); 336 } 337 338 341 public int getNodeValue(String [] xPath, int defaultValue) { 342 return getValue(getNode(xPath), defaultValue); 343 } 344 345 348 public String getNodeValue(String [] xPath, String defaultValue) { 349 return getValue(getNode(xPath), defaultValue); 350 } 351 352 355 public String getPackage() { 356 return m_defaultPackage; 357 } 358 359 362 public NodeList getTextColumnNodes(Node node) { 363 return getNodes(node, new String [] {XMLConstants.COLUMN_NAME, XMLConstants.TEXT}); 364 } 365 366 369 private boolean getValue(Node node, boolean defaultValue) { 370 if (node == null) { 371 return defaultValue; 372 } else { 373 return Boolean.parseBoolean(node.getNodeValue()); 374 } 375 } 376 377 380 private Class getValue(Node node, Class defaultValue) { 381 if (node == null) { 382 return defaultValue; 383 } else { 384 return getClassForName(node.getNodeValue()); 385 } 386 } 387 388 391 private int getValue(Node node, int defaultValue) { 392 if (node == null) { 393 return defaultValue; 394 } else { 395 return Integer.parseInt(node.getNodeValue()); 396 } 397 } 398 399 402 private String getValue(Node node, String defaultValue) { 403 if (node == null) { 404 return defaultValue; 405 } else { 406 String value = node.getNodeValue(); 407 if (value == null) { 408 return defaultValue; 409 } else { 410 return value; 411 } 412 } 413 } 414 415 418 public boolean hasNode(Node node, String xPath) { 419 return getNode(node, xPath) != null; 420 } 421 422 426 public Node locateEmbeddableNode(Class cls) { 427 return locateNode(cls, XMLConstants.EMBEDDABLE); 428 } 429 430 434 public Node locateEntityNode(Class cls) { 435 return locateNode(cls, XMLConstants.ENTITY); 436 } 437 438 442 public Node locateMappedSuperclassNode(Class cls) { 443 return locateNode(cls, XMLConstants.MAPPED_SUPERCLASS); 444 } 445 446 452 public Node locateNode(Class cls) { 453 Node result = null; 454 result = locateEntityNode(cls); 455 456 if (result == null) { 457 result = locateMappedSuperclassNode(cls); 458 } 459 460 if (result == null) { 461 result = locateEmbeddableNode(cls); 462 } 463 464 return result; 465 } 466 467 477 private Node locateNode(Class cls, String searchString) { 478 NodeList nodes = getNodes(m_document, XMLConstants.ENTITY_MAPPINGS, searchString); 479 480 if (nodes != null) { 481 for (int i = 0; i < nodes.getLength(); i++) { 482 Node node = nodes.item(i); 483 if (getClassNameForNode(node).equals(cls.getName())) { 485 return node; 486 } 487 } 488 } 489 490 return null; 491 } 492 493 497 public Node locateNodeForAttribute(Node node, String attributeName) { 499 NodeList attributeNodes = getNodes(node, XMLConstants.ATTRIBUTES, XMLConstants.ALL_CHILDREN); 500 501 if (attributeNodes != null) { 502 Node attributeNode; 503 for (int i = 0; i < attributeNodes.getLength(); i++) { 504 attributeNode = attributeNodes.item(i); 505 if (getNodeValue(attributeNode, XMLConstants.ATT_NAME).equals(attributeName)) { 507 return attributeNode; 508 } 509 } 510 } 511 512 return null; 513 } 514 515 519 public Class locateRootEntity(Class entityClass) { 520 Class superclass = entityClass.getSuperclass(); 521 if (superclass != null) { 522 Node entityNode = locateEntityNode(superclass); 523 524 if (entityNode != null) { 525 return locateRootEntity(superclass); 526 } 527 } 528 529 return entityClass; 530 } 531 532 536 public boolean nodeHasPrimaryKeyJoinColumns(Node node) { 537 if (node == null) { 538 return false; 539 } 540 541 NodeList nodes = getNodes(node, XMLConstants.PK_JOIN_COLUMN); 542 return (nodes != null && nodes.getLength() > 0); 543 } 544 545 549 public boolean nodeHasJoinColumns(Node node) { 550 if (node == null) { 551 return false; 552 } 553 554 NodeList nodes = getNodes(node, XMLConstants.JOIN_COLUMN); 555 return (nodes != null && nodes.getLength() > 0); 556 } 557 558 562 public static Document parseDocument(InputStream xmlDocumentInputStream, String documentName, ClassLoader loader) { 563 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 564 dbf.setNamespaceAware(true); 565 dbf.setAttribute(XMLConstants.SCHEMA_LANGUAGE, XMLConstants.XML_SCHEMA); 566 dbf.setValidating(true); 567 568 URL schemaURL = loader.getResource(XMLConstants.ORM_SCHEMA_NAME); 570 if (schemaURL != null) { 571 dbf.setAttribute(XMLConstants.JAXP_SCHEMA_SOURCE, schemaURL.toString()); 572 } 573 574 DocumentBuilder db; 576 try { 577 db = dbf.newDocumentBuilder(); 578 } catch (ParserConfigurationException pex) { 579 throw XMLParseException.exceptionCreatingDocumentBuilder(documentName, pex); 580 } 581 582 XMLExceptionHandler xmlExceptionHandler = new XMLExceptionHandler(); 584 db.setErrorHandler(xmlExceptionHandler); 585 586 Document doc = null; 588 try { 589 doc = db.parse(xmlDocumentInputStream); 590 } catch (IOException ioex) { 591 throw XMLParseException.exceptionReadingXMLDocument(documentName, ioex); 592 } catch (SAXException saxex) { 593 } 595 596 XMLException xmlEx = xmlExceptionHandler.getXMLException(); 597 if (xmlEx != null) { 598 throw ValidationException.invalidEntityMappingsDocument(documentName, xmlEx); 599 } 600 601 return doc; 602 } 603 604 608 public void setLoader(ClassLoader loader) { 609 m_loader = loader; 610 } 611 } 612 | Popular Tags |