Hi all,
   I am trying to enhance the Xerces XML Schema 1.1 assertions
implementation as per the latest suggestions from Khaled on the JIRA
issue, XERCESJ-1365.

While making some enhancements, I have bumped into a little problem,
for which I need help..

Below is the description of the problem, my analysis why Xerces gives
an exception (a NPE as stated below), and my questions..

I have got an element declaration as following:

<xs:element name="phonebill">
   <xs:complexType>
     <xs:sequence>
       <xs:element name="plan" type="xs:string" />
       <xs:element name="rent" type="xs:integer" />
     </xs:sequence>
     <xs:attribute name="custId" type="xs:string" />
     <xs:assert test="starts-with(@custId, 'XYZTelecom')" />
     <xs:assert test="(plan = 'A' and rent = 500) or
                      (plan = 'B' and rent = 650) or
                      (plan = 'C' and rent = 1000)" />
   </xs:complexType>
</xs:element>

You could see, that the complex type of the element, "phonebill" is anonymous.

After debugging the execution, I could see, that
Xerces generates internally the name, "#AnonType_phonebill" for this
anonymous complex type.

I am trying to create a local XSModel object for an element's content
model (not of the full schema, but am trying to build the XSModel for
this single element - phonebill in this case).

for this, I have written a code like below:

SchemaGrammar schemaGrammar = new SchemaGrammar(null, null, null,
Constants.SCHEMA_VERSION_1_1);

schemaGrammar.addGlobalElementDecl((XSElementDecl)elementPSVI.getElementDeclaration());
schemaGrammar.addGlobalTypeDecl(elementPSVI.getTypeDefinition()); //
[1] a java.lang.NullPointerException comes here
XSNotationDeclaration notationDecl = elementPSVI.getNotation();
if (notationDecl != null) {
  schemaGrammar.addGlobalNotationDecl((XSNotationDecl) notationDecl);
}

XSModel xsModel = new XSModelImpl(new SchemaGrammar[] { schemaGrammar
}); // this is the object, that I need to create

(elementPSVI is of type ElementPSVI and is populated correctly, which
is passed from the class XMLSchemaValidator.java)

When I run this code, I get a null pointer exception at location [1]
stated above.

When I further debugged the code, I could find following cause of the
null pointer exception:

null pointer exception is produced by the call,
schemaGrammar.addGlobalTypeDecl as stated above. When I look the
implementation of method, addGlobalTypeDecl in SchemaGrammar.java, I
can see that NPE is coming in the function call:

fGlobalTypeDecls.put(decl.getName(), decl);

decl.getName() in this case is null (because, we have an anonymous
type). The implementation of decl.getName() is as following:

public String getName() {
  return getAnonymous() ? null : fName;
}

(you could see, that null is returned if the type is an anonymous type)

decl.getName() being null, the call fGlobalTypeDecls.put fails

put is being implemented as follows:

public void put(Object key, Object value) {
  int bucket = (key.hashCode() & 0x7FFFFFFF) % fTableSize;

since the 'key' is null, we get a NPE (I think at the call, key.hashCode).

I need help to understand this behaviour, and want to create the
SchemaGrammar above (without NPE..)

I can think of following options:

1. I cannot do,
schemaGrammar.addGlobalTypeDecl(elementPSVI.getTypeDefinition());
if the type is an anonymous type.

It seem, *I need to do this* to have a fully populated schema.

2. To solve this issue, the implementation of getName() above could have been:

public String getName() {
  return fName;
}

So the getName call would have returned "#AnonType_phonebill" in this case.

Is the change 2. feasible to be implemented in Xerces? Does it affect
other places of Xerces code?

I have following questions:
1) Am I building the SchemaGrammar correctly? If not, what's the correct way?
2) If yes, then how do I avoid NPE in this case?


-- 
Regards,
Mukul Gandhi

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to