Hi Mukul,

Mukul Gandhi <[email protected]> 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
> <[email protected]> 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: [email protected]
> For additional commands, e-mail: [email protected]

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: [email protected]
E-mail: [email protected]

Reply via email to