Wednesday, August 19, 2009

Exploring xsd.core

I mentioned in the previous blog that the getProperty() method under the class XSDSchemaAdapter in org.eclipse.wst.xsd.core, XSDImpl.java calculates the name space of an attribute. However, as i looked more into the code, I found that the getProperty() method does not do more than retrieving the value from a created attribute. So, to find out how the name space of an attribute is calculated, I would need to look into the code where an attribute is created.

First, I go back to org.eclipse.wst.xml.core, DOMContentBuilderImpl.java and find out the method creating an element, which is visitCMElementDeclaration(). In visitCMElementDeclaration(), the following line of code is trying to creat the attributes of an element.

CMNamedNodeMap nodeMap = ed.getAttributes();

When ed is passed to visitCMElementDeclaration(), it is claimed to be an instance of CMElementDeclaration, but it is actually of data type XSDElementDeclarationAdapter derived from ElementDeclarationBaseImpl. XSDElementDeclarationAdapter and ElementDeclarationBaseImpl are both defined in org.eclipse.wst.xsd.core, XSDImpl.java.getAttributes() is one of the methods defined in ElementDeclarationBaseImpl. The following lines of code in getAttributes() are creating the attributes of an element.

XSDComplexTypeDefinition ctd = (XSDComplexTypeDefinition) xsdTypeDefinition;
for (Iterator i = ctd.getAttributeUses().iterator(); i.hasNext();) {    XSDAttributeUse xsdAttributeUse = (XSDAttributeUse) i.next();
    XSDAttributeUseAdapter adapter =          (XSDAttributeUseAdapter)getAdapter(xsdAttributeUse);
    if (adapter != null && adapter.getNodeName() != null) {
       map.getHashtable().put(adapter.getNodeName(), adapter);
    }
}

The most important variable in the these lines is ctd, which is corresponding to the definition of an element. XSDComplexTypeDefinition is defined in org.eclipse.xsd_2.5.0.v200905041408.jar in the plug-in dependencies folder. At this point, the "lang" attribute still gets the correct name space url, which is http://www.w3c.org/XML/1998/namespace. However, once the CMNode, representing the "lang" attribute, is converted to CMDocument later in DOMNamespaceHelper.java, the namespace url, for some unknown reason, has been changed to http://java.sun.com/xml/ns/javaee, which I think is the cause of Bug 245698