You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Mukul Gandhi <ga...@gmail.com> on 2009/05/14 18:31:55 UTC

constructing XSModelImpl from javax.xml.validation.Schema

Hi all,
   Let's say we create a JAXP schema object,
javax.xml.validation.Schema (using
javax.xml.validation.SchemaFactory.newSchema calls).

Would it be useful, if we provide facility to users, so they can
construct a Xerces schema object, XSModelImpl from a
javax.xml.validation.Schema object?

For this need, we could provide an additional constructor to the user,
XSModelImpl(Schema schema).

Is the implementation of this requirement, feasible within Xerces? And
will this facility, provide additional value to the users?


-- 
Regards,
Mukul Gandhi

---------------------------------------------------------------------
To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-dev-help@xerces.apache.org


Re: constructing XSModelImpl from javax.xml.validation.Schema

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Mukul,

Having read through your e-mail I don't see why you would need a JAXP
Schema object in this context.

Consider that when we are checking assertions we are in the process of
validating the document. The schema validator is already producing:

1) PSVI for each element and attribute.
2) A grammar bucket containing the "effective" schema which one can convert
into an XSModel.

So you should be able to put that together programmatically to create a
PSVI DOM, and perhaps have the code fetch the XSModel [1] from the root
element of that DOM. You shouldn't need to construct any new
SchemaGrammars. The ones already in the grammar bucket should be
sufficient.

Thanks.

[1]
http://xerces.apache.org/xerces2-j/javadocs/xs/org/apache/xerces/xs/ElementPSVI.html#getSchemaInformation()

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

Mukul Gandhi <ga...@gmail.com> wrote on 05/18/2009 04:33:44 AM:

> Hi Michael,
>   Thanks for your reply. I'll try your code shortly.
>
> I am working to construct a Schema (javax.xml.validation.Schema)
> object for Psychopath interface. As of now, it seems I would need both
> Schema and XSModel (but it seems I may not need,
> javax.xml.validation.Schema finally.. please see what I feel, below).
>
> I have observed that following works fine with Psychopath to enable
> schema awareness:
>
> public static final String DOCUMENT_IMPLEMENTATION_PROPERTY =
> "http://apache.org/xml/properties/dom/document-class-name";
> public static final String DOCUMENT_PSVI_IMPLEMENTATION =
> "org.apache.xerces.dom.PSVIDocumentImpl";
>
> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
> factory.setAttribute(DOCUMENT_IMPLEMENTATION_PROPERTY,
> DOCUMENT_PSVI_IMPLEMENTATION);
> factory.setSchema(_schema); // _schema is the Schema object
>
> DocumentBuilder builder = factory.newDocumentBuilder();
> Document document = builder.parse(in); // 'in' is InputStream for
> the source XML
>
> When Psychopath works with the DOM tree 'document' it is able to
> perform schema aware XPath evaluations.
>
> Now further, we get XSModel as well.
>
> Psychopath needs XSModel to construct the dynmaic context as below.
>
> DefaultDynamicContext(XSModel xsmodel, Document doc);
>
> I observed that only having XSModel doesn't work well with Psychopath.
>
> What I would try now is:
> Construct the PSVI DOM from scratch (using say, createElement calls.
> this would be a substitute of builder.parse(in)) and then supply that
> along with XSModel to Psychopath. I have to see if this would work. If
> this works, then we don't need JAXP Schema object.
>
> The thing I would like to see working with much eagerness is
> constructing XSModel from a sequence of endElement calls (where I
> pass, ElementPSVI). I think constructing XSModel may not be as
> tendious as I think, because I just need to construct XSModel from
> only *one* endElement event (which is for the root node of the DOM
> tree).
> I am thinking to use the constructor:
>
> XSModelImpl(SchemaGrammar[] grammars)
>
> and construct the object, SchemaGrammar using number of addxxx calls
> (like, addGlobalElementDecl).
>
> Let's see how it goes.
>
> I'll share what I might achieve here.
>
> On Mon, May 18, 2009 at 12:16 PM, Michael Glavassevich
> <mr...@ca.ibm.com> wrote:
> > Hi Mukul,
> >
> > What you're attempting to do is already possible:
> >
> > SchemaFactory sf =
> > SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
> > XMLSchemaFactory xsf = (XMLSchemaFactory) sf;
> >
> > XSModel xsmodel = ...;
> > XSNamespaceItemList nsItemList = model.getNamespaceItems();
> > Grammar[] grammars = (Grammar[])
> > nsItemList.toArray(new Grammar[nsItemList.getLength()]);
> > XMLGrammarPool pool = new XMLGrammarPoolImpl();
> > pool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA, grammars);
> > Schema s = xsf.newSchema(pool);
> >
> > though I thought that Psychopath took XSModel as input. Or is
thisnecessary
> > for something else? Saxon perhaps?
> >
> > Thanks.
> >
> > Michael Glavassevich
> > XML Parser Development
> > IBM Toronto Lab
> > E-mail: mrglavas@ca.ibm.com
> > E-mail: mrglavas@apache.org
>
>
> --
> Regards,
> Mukul Gandhi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-dev-help@xerces.apache.org

Re: constructing XSModelImpl from javax.xml.validation.Schema

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Michael,
  Thanks for your reply. I'll try your code shortly.

I am working to construct a Schema (javax.xml.validation.Schema)
object for Psychopath interface. As of now, it seems I would need both
Schema and XSModel (but it seems I may not need,
javax.xml.validation.Schema finally.. please see what I feel, below).

I have observed that following works fine with Psychopath to enable
schema awareness:

public static final String DOCUMENT_IMPLEMENTATION_PROPERTY =
"http://apache.org/xml/properties/dom/document-class-name";
public static final String DOCUMENT_PSVI_IMPLEMENTATION =
"org.apache.xerces.dom.PSVIDocumentImpl";

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setAttribute(DOCUMENT_IMPLEMENTATION_PROPERTY,
DOCUMENT_PSVI_IMPLEMENTATION);
factory.setSchema(_schema); // _schema is the Schema object

DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(in); // 'in' is InputStream for the source XML

When Psychopath works with the DOM tree 'document' it is able to
perform schema aware XPath evaluations.

Now further, we get XSModel as well.

Psychopath needs XSModel to construct the dynmaic context as below.

DefaultDynamicContext(XSModel xsmodel, Document doc);

I observed that only having XSModel doesn't work well with Psychopath.

What I would try now is:
Construct the PSVI DOM from scratch (using say, createElement calls.
this would be a substitute of builder.parse(in)) and then supply that
along with XSModel to Psychopath. I have to see if this would work. If
this works, then we don't need JAXP Schema object.

The thing I would like to see working with much eagerness is
constructing XSModel from a sequence of endElement calls (where I
pass, ElementPSVI). I think constructing XSModel may not be as
tendious as I think, because I just need to construct XSModel from
only *one* endElement event (which is for the root node of the DOM
tree).
I am thinking to use the constructor:

XSModelImpl(SchemaGrammar[] grammars)

and construct the object, SchemaGrammar using number of addxxx calls
(like, addGlobalElementDecl).

Let's see how it goes.

I'll share what I might achieve here.

On Mon, May 18, 2009 at 12:16 PM, Michael Glavassevich
<mr...@ca.ibm.com> wrote:
> Hi Mukul,
>
> What you're attempting to do is already possible:
>
> SchemaFactory sf =
> SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
> XMLSchemaFactory xsf = (XMLSchemaFactory) sf;
>
> XSModel xsmodel = ...;
> XSNamespaceItemList nsItemList = model.getNamespaceItems();
> Grammar[] grammars = (Grammar[])
> nsItemList.toArray(new Grammar[nsItemList.getLength()]);
> XMLGrammarPool pool = new XMLGrammarPoolImpl();
> pool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA, grammars);
> Schema s = xsf.newSchema(pool);
>
> though I thought that Psychopath took XSModel as input. Or is this necessary
> for something else? Saxon perhaps?
>
> Thanks.
>
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: mrglavas@ca.ibm.com
> E-mail: mrglavas@apache.org



-- 
Regards,
Mukul Gandhi

---------------------------------------------------------------------
To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-dev-help@xerces.apache.org


Re: constructing XSModelImpl from javax.xml.validation.Schema

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Mukul,

What you're attempting to do is already possible:

SchemaFactory sf =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
XMLSchemaFactory xsf = (XMLSchemaFactory) sf;

XSModel xsmodel = ...;
XSNamespaceItemList nsItemList = model.getNamespaceItems();
Grammar[] grammars = (Grammar[]) nsItemList.toArray(new
Grammar[nsItemList.getLength()]);
XMLGrammarPool pool = new XMLGrammarPoolImpl();
pool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA, grammars);
Schema s = xsf.newSchema(pool);

though I thought that Psychopath took XSModel as input. Or is this
necessary for something else? Saxon perhaps?

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

Mukul Gandhi <ga...@gmail.com> wrote on 05/17/2009 01:59:27 AM:

> Hi Michael,
>   Actually, it would help me if I can construct the JAXP object,
> "Schema" from XSModel. I need this for XML Schema 1.1 assertions
> implementation.
>
> I looked into Xerces code, and it doesn't seem to be possible
> currently with given exposed public APIs.
>
> I further thought about this, and think, making following changes
> would make this possible:
>
> Expose, the object fGrammarList in XSModelImpl.java
>
> So, we could have following new method [1] in XSModelImpl.java (seems
> to me feasible to do):
>
> public SchemaGrammar[] getSchemaGrammars() {
>   return fGrammarList;
> }
>
> I think, introducing this new method in XSModelImpl.java would make it
> possible to construct JAXP, "Schema" object.
>
> Assuming the method [1] is now available, I think we need to write
> following code to achieve this:
>
> XSModel xsmodel = ...; // we have a valid XSModel with us
> SchemaGrammar[] schemaGrammars = xsmodel.getSchemaGrammars(); // the
> new method I propose
> XMLGrammarPoolImpl grammarPool = new XMLGrammarPoolImpl();
> grammarPool.putGrammar(schemaGrammars[0]);
>
> Schema schema = new XMLSchema(grammarPool,
> XMLConstants.W3C_XML_SCHEMA_NS_URI); // this is what I need :)
>
> Is this feasible and correct? The whole of idea above, relies on
> adding a new method, getSchemaGrammars() in XSModelImpl.java.
>
> On Fri, May 15, 2009 at 6:52 PM, Mukul Gandhi <ga...@gmail.com>
wrote:
> > Hi Michael,
> >   Thanks for reply. This works fine, and is really nice.
> >
> > I have another question.
> >
> > Can we convert XSModel into the JAXP object,
javax.xml.validation.Schema?
>
>
> --
> Regards,
> Mukul Gandhi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-dev-help@xerces.apache.org

Re: constructing XSModelImpl from javax.xml.validation.Schema

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Michael,
  Actually, it would help me if I can construct the JAXP object,
"Schema" from XSModel. I need this for XML Schema 1.1 assertions
implementation.

I looked into Xerces code, and it doesn't seem to be possible
currently with given exposed public APIs.

I further thought about this, and think, making following changes
would make this possible:

Expose, the object fGrammarList in XSModelImpl.java

So, we could have following new method [1] in XSModelImpl.java (seems
to me feasible to do):

public SchemaGrammar[] getSchemaGrammars() {
  return fGrammarList;
}

I think, introducing this new method in XSModelImpl.java would make it
possible to construct JAXP, "Schema" object.

Assuming the method [1] is now available, I think we need to write
following code to achieve this:

XSModel xsmodel = ...; // we have a valid XSModel with us
SchemaGrammar[] schemaGrammars = xsmodel.getSchemaGrammars(); // the
new method I propose
XMLGrammarPoolImpl grammarPool = new XMLGrammarPoolImpl();
grammarPool.putGrammar(schemaGrammars[0]);

Schema schema = new XMLSchema(grammarPool,
XMLConstants.W3C_XML_SCHEMA_NS_URI); // this is what I need :)

Is this feasible and correct? The whole of idea above, relies on
adding a new method, getSchemaGrammars() in XSModelImpl.java.

On Fri, May 15, 2009 at 6:52 PM, Mukul Gandhi <ga...@gmail.com> wrote:
> Hi Michael,
>   Thanks for reply. This works fine, and is really nice.
>
> I have another question.
>
> Can we convert XSModel into the JAXP object, javax.xml.validation.Schema?


-- 
Regards,
Mukul Gandhi

---------------------------------------------------------------------
To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-dev-help@xerces.apache.org


Re: constructing XSModelImpl from javax.xml.validation.Schema

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Michael,
   Thanks for reply. This works fine, and is really nice.

I have another question.

Can we convert XSModel into the JAXP object, javax.xml.validation.Schema?

On Fri, May 15, 2009 at 5:58 PM, Michael Glavassevich
<mr...@ca.ibm.com> wrote:
> I wasn't implying that you could cast this directly. :-)
>
> The array type is Grammar however each of the values inside will be an
> instance of XSGrammar. So you need to create a new XSGrammar array and copy
> each of the items:
>
> XSGrammar [] xsGrammars = new XSGrammar[grammars.length];
> System.arraycopy(grammars, 0, xsGrammars, 0, grammars.length);
>
> and then things should work fine from there.


-- 
Regards,
Mukul Gandhi

---------------------------------------------------------------------
To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-dev-help@xerces.apache.org


Re: constructing XSModelImpl from javax.xml.validation.Schema

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Mukul,

Mukul Gandhi <ga...@gmail.com> wrote on 05/15/2009 06:05:32 AM:

> Hi Michael,
>    Thanks for your reply. Kindly see my comments below.
>
> On Fri, May 15, 2009 at 7:32 AM, Michael Glavassevich
> <mr...@ca.ibm.com> wrote:
> > SchemaFactory sf =
> > SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
> > Schema s = sf.newSchema(new StreamSource(...));
> > XSGrammarPoolContainer poolContainer = (XSGrammarPoolContainer) s;
> > XMLGrammarPool pool = poolContainer.getGrammarPool();
> > Grammar[] grammars =
> > pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
> > XSGrammar [] xsGrammars = ...;
> > ...
> > xsGrammars[0].toXSModel(xsGrammars);
>
> The above code works fine.
>
> But I get a ClassCastException at runtime (I don't get a compilation
> error), when I try to do:
>
> XSGrammar[] xsGrammars =  ( XSGrammar[] ) grammars;
>
> // star code
>
> Grammar[] grammars =
> pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
>
> XSGrammar[] xsGrammars =  ( XSGrammar[] ) grammars; // get a
> ClassCastException here
>
> XSModel xsmodel = xsGrammars[0].toXSModel(xsGrammars);
>
> // end code
>
> How do I I convert Grammar to XSGrammar?

I wasn't implying that you could cast this directly. :-)

The array type is Grammar however each of the values inside will be an
instance of XSGrammar. So you need to create a new XSGrammar array and copy
each of the items:

XSGrammar [] xsGrammars = new XSGrammar[grammars.length];
System.arraycopy(grammars, 0, xsGrammars, 0, grammars.length);

and then things should work fine from there.

> --
> Regards,
> Mukul Gandhi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-dev-help@xerces.apache.org

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

Re: constructing XSModelImpl from javax.xml.validation.Schema

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Michael,
   Thanks for your reply. Kindly see my comments below.

On Fri, May 15, 2009 at 7:32 AM, Michael Glavassevich
<mr...@ca.ibm.com> wrote:
> SchemaFactory sf =
> SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
> Schema s = sf.newSchema(new StreamSource(...));
> XSGrammarPoolContainer poolContainer = (XSGrammarPoolContainer) s;
> XMLGrammarPool pool = poolContainer.getGrammarPool();
> Grammar[] grammars =
> pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
> XSGrammar [] xsGrammars = ...;
> ...
> xsGrammars[0].toXSModel(xsGrammars);

The above code works fine.

But I get a ClassCastException at runtime (I don't get a compilation
error), when I try to do:

XSGrammar[] xsGrammars =  ( XSGrammar[] ) grammars;

// star code

Grammar[] grammars =
pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);

XSGrammar[] xsGrammars =  ( XSGrammar[] ) grammars; // get a
ClassCastException here

XSModel xsmodel = xsGrammars[0].toXSModel(xsGrammars);

// end code

How do I I convert Grammar to XSGrammar?


-- 
Regards,
Mukul Gandhi

---------------------------------------------------------------------
To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-dev-help@xerces.apache.org


Re: constructing XSModelImpl from javax.xml.validation.Schema

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Mukul,

Mukul Gandhi <ga...@gmail.com> wrote on 05/14/2009 12:31:55 PM:

> Hi all,
>    Let's say we create a JAXP schema object,
> javax.xml.validation.Schema (using
> javax.xml.validation.SchemaFactory.newSchema calls).
>
> Would it be useful, if we provide facility to users, so they can
> construct a Xerces schema object, XSModelImpl from a
> javax.xml.validation.Schema object?
>
> For this need, we could provide an additional constructor to the user,
> XSModelImpl(Schema schema).
>
> Is the implementation of this requirement, feasible within Xerces? And
> will this facility, provide additional value to the users?

It's already possible to do this if a user is willing to dive into
internals:

// Must be Xerces' implementation
SchemaFactory sf =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema s = sf.newSchema(new StreamSource(...));
XSGrammarPoolContainer poolContainer = (XSGrammarPoolContainer) s;
XMLGrammarPool pool = poolContainer.getGrammarPool();
Grammar[] grammars =
pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
XSGrammar [] xsGrammars = ...;
...
xsGrammars[0].toXSModel(xsGrammars);

In general you can't convert a Schema into an XSModel because it may not
come from Xerces and may not even be a Schema for a W3C XML Schema (i.e.
could be Relax NG, Schematron or something else). Even with Xerces you
can't construct XSModels directly from certain Schema objects, specifically
the one returned from SchemaFactory.newSchema() (i.e. the factory method
with no args) which caches based on the schema locations specified in the
documents that it validates and may throw out some of these grammar objects
over time in response to demand for memory.

The user can always get the effective XSModel by retrieving it from the
PSVI [1] of the documents they validate.

> --
> Regards,
> Mukul Gandhi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-dev-help@xerces.apache.org

Thanks.

[1]
http://xerces.apache.org/xerces2-j/javadocs/xs/org/apache/xerces/xs/ElementPSVI.html#getSchemaInformation()

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org