FileDocCategorySizeDatePackage
XMLHelper.javaAPI DocGlassfish v2 API20437Tue May 22 16:54:30 BST 2007oracle.toplink.essentials.internal.ejb.cmp3.xml

XMLHelper

public class XMLHelper extends Object
Utility class used for handling element inspection.
author
Guy Pelletier
since
TopLink EJB 3.0 Reference Implementation

Fields Summary
private Document
m_document
private ClassLoader
m_loader
private String
m_documentName
private String
m_defaultPackage
private XPathEngine
m_xPathEngine
Constructors Summary
protected XMLHelper(Document document, ClassLoader loader)
INTERNAL:

		m_xPathEngine = XPathEngine.getInstance();
		m_loader = loader;
		m_document = document;
		
		Node node = getNode(document, new String[] {XMLConstants.ENTITY_MAPPINGS, XMLConstants.PACKAGE, XMLConstants.TEXT});
        
        if (node != null && node.getNodeValue() != null) {
        	m_defaultPackage = node.getNodeValue();
        } else {
        	m_defaultPackage = "";
        }
	
public XMLHelper(Document document, String fileName, ClassLoader loader)
INTERNAL:

        this(document, loader);
        m_documentName = fileName;
    
Methods Summary
public java.util.ListgetCascadeTypes(org.w3c.dom.Node node)
INTERNAL:

        ArrayList<String> cTypes = new ArrayList<String>();
        
        NodeList cascadeTypes = getNodes(node, XMLConstants.CASCADE, XMLConstants.ALL_CHILDREN);
		for (int i = 0; i < cascadeTypes.getLength(); i++) {
            cTypes.add(cascadeTypes.item(i).getLocalName());
        }
        
        return cTypes;
    
public java.lang.ClassgetClassForName(java.lang.String className)
INTERNAL:

		return MetadataHelper.getClassForName(getFullyQualifiedClassName(className), m_loader);
	
public java.lang.ClassgetClassForNode(org.w3c.dom.Node node)
INTERNAL: Return the Class for a given node. This method assumes that the node requires a class attribute, i.e. entity or mapped-superclass.

		return MetadataHelper.getClassForName(getClassNameForNode(node), m_loader);
	
public java.lang.ClassLoadergetClassLoader()
INTERNAL: Get the loader.

        return m_loader;
    
public java.lang.StringgetClassNameForNode(org.w3c.dom.Node node)
INTERNAL: Return the fully qualified class name for a given node. This method assumes that the node requires a class attribute, i.e. entity or mapped-superclass.

        return getFullyQualifiedClassName(getNodeValue(node, XMLConstants.ATT_CLASS));
	
public org.w3c.dom.DocumentgetDocument()
INTERNAL: Return the instance document associated with this helper.

		return m_document;
	
public java.lang.StringgetDocumentName()
INTERNAL: Return the instance document name associated with this helper.

		return m_documentName;
	
public java.lang.StringgetFetchTypeDefaultEAGER(org.w3c.dom.Node node)
INTERNAL:

        return getNodeValue(node, XMLConstants.ATT_FETCH, MetadataConstants.EAGER);
    
public java.lang.StringgetFetchTypeDefaultLAZY(org.w3c.dom.Node node)
INTERNAL:

        return getNodeValue(node, XMLConstants.ATT_FETCH, MetadataConstants.LAZY);
    
public java.lang.StringgetFullyQualifiedClassName(java.lang.String className)
INTERNAL: This convenience method will attempt to fully qualify a class name if required. This assumes that the className value is non-null, and a "qualified" class name contains at least one '.'

        return getFullyQualifiedClassName(className, m_defaultPackage);
    
public static java.lang.StringgetFullyQualifiedClassName(java.lang.String className, java.lang.String packageName)
INTERNAL: This convenience method will attempt to fully qualify a class name if required. This assumes that the className value is non-null, and a "qualified" class name contains at least one '.'

        // if there is no global package defined or the class name is qualified, return className
        if (packageName.equals("") || className.indexOf(".") != -1) {
            return className;
        }
        
        // prepend the package to the class name
        // format of global package is "foo.bar."
        if (packageName.endsWith(".")) {
            return (packageName + className);
        }
        
        // format of global package is "foo.bar"
        return (packageName + "." + className);
    
public java.lang.StringgetLoggingContextForDefaultMappingReferenceClass(org.w3c.dom.Node mappingNode)
INTERNAL: This convenience method determines the type of relationship mapping the node represents, and returns the appropriate logging context.

        if (mappingNode.getLocalName().equals(XMLConstants.ONE_TO_ONE)) {
            return MetadataLogger.ONE_TO_ONE_MAPPING_REFERENCE_CLASS;
        }
        if (mappingNode.getLocalName().equals(XMLConstants.ONE_TO_MANY)) {
            return MetadataLogger.ONE_TO_MANY_MAPPING_REFERENCE_CLASS;
        }
        if (mappingNode.getLocalName().equals(XMLConstants.MANY_TO_ONE)) {
            return MetadataLogger.MANY_TO_ONE_MAPPING_REFERENCE_CLASS;
        }
        // assume many-to-many
        return MetadataLogger.MANY_TO_MANY_MAPPING_REFERENCE_CLASS;
    
public java.lang.StringgetMappedBy(org.w3c.dom.Node node)
INTERNAL:

        return getNodeValue(node, XMLConstants.ATT_MAPPED_BY, "");
    
public org.w3c.dom.NodegetNode(org.w3c.dom.Node node, java.lang.String xPath)
INTERNAL: Get a node off the given node.

        return getNode(node, new String[] {xPath});
    
public org.w3c.dom.NodegetNode(org.w3c.dom.Node node, java.lang.String[] xPath)
INTERNAL: Get a node off the given node.

        return m_xPathEngine.selectSingleNode(node, xPath);
    
public org.w3c.dom.NodegetNode(java.lang.String[] xPath)
INTERNAL: Get a node off the document node.

        return getNode(m_document, xPath);
    
public java.lang.StringgetNodeTextValue(org.w3c.dom.Node node, java.lang.String xPath)
INTERNAL:

        return getNodeValue(node, new String[] {xPath, XMLConstants.TEXT});
    
public java.lang.StringgetNodeTextValue(java.lang.String xPath1, java.lang.String xPath2)
INTERNAL:

        return getNodeValue(m_document, new String[] {xPath1, xPath2, XMLConstants.TEXT});
    
public java.lang.StringgetNodeTextValue(java.lang.String xPath1, java.lang.String xPath2, java.lang.String defaultValue)
INTERNAL:

        return getNodeValue(m_document, new String[] {xPath1, xPath2, XMLConstants.TEXT}, defaultValue);
    
public java.lang.StringgetNodeValue(org.w3c.dom.Node node, java.lang.String xPath)
INTERNAL:

        return getNodeValue(node, new String[] {xPath});
    
public booleangetNodeValue(org.w3c.dom.Node node, java.lang.String xPath, boolean defaultValue)
INTERNAL:

        return getNodeValue(node, new String[] {xPath}, defaultValue);
    
public java.lang.ClassgetNodeValue(org.w3c.dom.Node node, java.lang.String xPath, java.lang.Class defaultValue)
INTERNAL:

        return getNodeValue(node, new String[] {xPath}, defaultValue);
    
public intgetNodeValue(org.w3c.dom.Node node, java.lang.String xPath, int defaultValue)
INTERNAL:

        return getNodeValue(node, new String[] {xPath}, defaultValue);
    
public java.lang.StringgetNodeValue(org.w3c.dom.Node node, java.lang.String xPath, java.lang.String defaultValue)
INTERNAL:

        return getNodeValue(node, new String[] {xPath}, defaultValue);
    
public booleangetNodeValue(org.w3c.dom.Node node, java.lang.String[] xPath, boolean defaultValue)
INTERNAL:

        return getValue(getNode(node, xPath), defaultValue);
    
public java.lang.ClassgetNodeValue(org.w3c.dom.Node node, java.lang.String[] xPath, java.lang.Class defaultValue)
INTERNAL:

        return getValue(getNode(node, xPath), defaultValue);
    
public intgetNodeValue(org.w3c.dom.Node node, java.lang.String[] xPath, int defaultValue)
INTERNAL:

        return getValue(getNode(node, xPath), defaultValue);
    
public java.lang.StringgetNodeValue(org.w3c.dom.Node node, java.lang.String[] xPath, java.lang.String defaultValue)
INTERNAL:

        return getValue(getNode(node, xPath), defaultValue);
    
public java.lang.StringgetNodeValue(org.w3c.dom.Node node, java.lang.String[] xPath)
INTERNAL:

        return getNodeValue(node, xPath, "");
    
public java.lang.StringgetNodeValue(java.lang.String[] xPath)
INTERNAL:

        return getNodeValue(xPath, "");
    
public intgetNodeValue(java.lang.String[] xPath, int defaultValue)
INTERNAL:

        return getValue(getNode(xPath), defaultValue);
    
public java.lang.StringgetNodeValue(java.lang.String[] xPath, java.lang.String defaultValue)
INTERNAL:

        return getValue(getNode(xPath), defaultValue);
    
public org.w3c.dom.NodeListgetNodes(java.lang.String xPath1, java.lang.String xPath2)
INTERNAL: Get the nodes off the given node.

        return getNodes(m_document, new String[] {xPath1, xPath2});
    
public org.w3c.dom.NodeListgetNodes(java.lang.String[] xPath)
INTERNAL: Get the nodes off the given node.

        return getNodes(m_document, xPath);
    
public org.w3c.dom.NodeListgetNodes(org.w3c.dom.Node node, java.lang.String xPath)
INTERNAL:

        return getNodes(node, new String[] {xPath});
    
public org.w3c.dom.NodeListgetNodes(org.w3c.dom.Node node, java.lang.String xPath1, java.lang.String xPath2)
INTERNAL:

        return getNodes(node, new String[] {xPath1, xPath2});
    
public org.w3c.dom.NodeListgetNodes(org.w3c.dom.Node node, java.lang.String[] xPath)
INTERNAL:

        return m_xPathEngine.selectNodes(node, xPath);
    
public java.lang.StringgetPackage()
INTERNAL:

		return m_defaultPackage;
	
public java.lang.ClassgetTargetEntity(org.w3c.dom.Node node)
INTERNAL:

        return getNodeValue(node, XMLConstants.ATT_TARGET_ENTITY, void.class);
    
public org.w3c.dom.NodeListgetTextColumnNodes(org.w3c.dom.Node node)
INTERNAL:

        return getNodes(node, new String[] {XMLConstants.COLUMN_NAME, XMLConstants.TEXT});
    
private booleangetValue(org.w3c.dom.Node node, boolean defaultValue)
INTERNAL:

        if (node == null) {
            return defaultValue;
        } else {
            return Boolean.parseBoolean(node.getNodeValue());
        }
    
private java.lang.ClassgetValue(org.w3c.dom.Node node, java.lang.Class defaultValue)
INTERNAL:

        if (node == null) {
            return defaultValue;
        } else {
            return getClassForName(node.getNodeValue());
        }
    
private intgetValue(org.w3c.dom.Node node, int defaultValue)
INTERNAL:

        if (node == null) {
            return defaultValue;
        } else {
            return Integer.parseInt(node.getNodeValue());
        }
    
private java.lang.StringgetValue(org.w3c.dom.Node node, java.lang.String defaultValue)
INTERNAL:

        if (node == null) {
            return defaultValue;
        } else {
            String value = node.getNodeValue();
            if (value == null) {
                return defaultValue;
            } else {
                return value;
            }
        }
    
public booleanhasNode(org.w3c.dom.Node node, java.lang.String xPath)
INTERNAL:

        return getNode(node, xPath) != null;
    
public booleanisOptional(org.w3c.dom.Node node)
INTERNAL:

        return getNodeValue(node, XMLConstants.ATT_OPTIONAL, true);
    
public org.w3c.dom.NodelocateEmbeddableNode(java.lang.Class cls)
INTERNAL: Locate a node in the DOM tree for a given class.

        return locateNode(cls, XMLConstants.EMBEDDABLE);
    
public org.w3c.dom.NodelocateEntityNode(java.lang.Class cls)
INTERNAL: Locate a node in the DOM tree for a given class.

        return locateNode(cls, XMLConstants.ENTITY);
    
public org.w3c.dom.NodelocateMappedSuperclassNode(java.lang.Class cls)
INTERNAL: Locate a node in the DOM tree for a given class.

        return locateNode(cls, XMLConstants.MAPPED_SUPERCLASS);
    
public org.w3c.dom.NodelocateNode(java.lang.Class cls)
INTERNAL: Locate a node in the DOM tree for the given class. Will look for an entity, embeddable, or mapped-superclass node with @class matching the class name.

    	Node result = null;
        result = locateEntityNode(cls);
    	
        if (result == null) {
        	result = locateMappedSuperclassNode(cls);
    	}
        
    	if (result == null) {
    		result = locateEmbeddableNode(cls);
    	}
        
    	return result;
    
private org.w3c.dom.NodelocateNode(java.lang.Class cls, java.lang.String searchString)
INTERNAL: Locate a node in the DOM tree for a given class. The search string should be used as follows: - For an entity: XMLConstants.ENTITY - For an embeddable: XMLConstants.EMBEDDABLE - For a mapped superclass: XMLConstants.MAPPED_SUPERCLASS Or call locateNode which will check them all. For efficiency, it looks for an entity first.

        NodeList nodes = getNodes(m_document, XMLConstants.ENTITY_MAPPINGS, searchString);

        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                // process @class (required)
                if (getClassNameForNode(node).equals(cls.getName())) {
                    return node;
                }
            }
        }
        
        return null;
    
public org.w3c.dom.NodelocateNodeForAttribute(org.w3c.dom.Node node, java.lang.String attributeName)
INTERNAL: Locate a node in the DOM tree for a given attribute name.

        NodeList attributeNodes = getNodes(node, XMLConstants.ATTRIBUTES, XMLConstants.ALL_CHILDREN);

        if (attributeNodes != null) {
            Node attributeNode;
            for (int i = 0; i < attributeNodes.getLength(); i++) {
            	attributeNode = attributeNodes.item(i);
                // process @name (required)
                if (getNodeValue(attributeNode, XMLConstants.ATT_NAME).equals(attributeName)) {
                    return attributeNode;
                }
            }
        }
        
        return null;
    
public java.lang.ClasslocateRootEntity(java.lang.Class entityClass)
INTERNAL: Return the root entity in an entity class hierarchy

    
    	Class superclass = entityClass.getSuperclass();
        if (superclass != null) {
            Node entityNode = locateEntityNode(superclass);
            
            if (entityNode != null) {
                return locateRootEntity(superclass);
            }
        }
        
        return entityClass;
    
public booleannodeHasJoinColumns(org.w3c.dom.Node node)
INTERNAL: Indicates if a given node has a primary-key-join-column sub-element.

    	if (node == null) {
    		return false;
    	}

    	NodeList nodes = getNodes(node, XMLConstants.JOIN_COLUMN);
    	return (nodes != null && nodes.getLength() > 0);
    
public booleannodeHasPrimaryKeyJoinColumns(org.w3c.dom.Node node)
INTERNAL: Indicates if a given node has a primary-key-join-column sub-element.

    	if (node == null) {
    		return false;
    	}

    	NodeList nodes = getNodes(node, XMLConstants.PK_JOIN_COLUMN);
    	return (nodes != null && nodes.getLength() > 0);
    
public static org.w3c.dom.DocumentparseDocument(java.io.InputStream xmlDocumentInputStream, java.lang.String documentName, java.lang.ClassLoader loader)
INTERNAL: Build a DOM from an instance document using the provided URL.

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setAttribute(XMLConstants.SCHEMA_LANGUAGE, XMLConstants.XML_SCHEMA);
        dbf.setValidating(true);
        
        // attempt to load the schema from the classpath
        URL schemaURL = loader.getResource(XMLConstants.ORM_SCHEMA_NAME);
        if (schemaURL != null) {
        	dbf.setAttribute(XMLConstants.JAXP_SCHEMA_SOURCE, schemaURL.toString());
        }
        
        // create a document builder
        DocumentBuilder db;
        try {
            db = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException pex) {
            throw XMLParseException.exceptionCreatingDocumentBuilder(documentName, pex);
        }
        
        // set the parse exception handler
        XMLExceptionHandler xmlExceptionHandler = new XMLExceptionHandler();
        db.setErrorHandler(xmlExceptionHandler);
        
        // parse the document
        Document doc = null;
        try {
            doc = db.parse(xmlDocumentInputStream);
        } catch (IOException ioex) {
            throw XMLParseException.exceptionReadingXMLDocument(documentName, ioex);
        } catch (SAXException saxex) {
        	// XMLExceptionHandler will handle parse exceptions
        }
        
        XMLException xmlEx = xmlExceptionHandler.getXMLException();
        if (xmlEx != null) {
        	throw ValidationException.invalidEntityMappingsDocument(documentName, xmlEx);
        }
        
        return doc;
    
public voidsetLoader(java.lang.ClassLoader loader)
INTERNAL: Update the loader after it changes.

        m_loader = loader;