You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Erik Sjölund <er...@gmail.com> on 2012/09/19 21:31:15 UTC

deserializeGrammars from multiple BinInputStream, is it possible?

Hi,

Short story:
Is it possible serialize out one binary file per schema file and then
later just load in a subset of the binary files into an
XMLGrammarPool?

Longer story:
Assume we have some software that makes use of dynamically loaded
plugins where each plugin has a schema that defines an xml element
that belongs to a certain substitutionGroup. It would be nice to be
able to create binary representation of the schema grammar and store
it inside the plugin. I have seen these two Xerces functions

XMLGrammarPool::serializeGrammars(BinOutputStream *const)
XMLGrammarPool::deserializeGrammars(BinInputStream *const)

It looks like the normal way of using these two functions is to first
load all schema files and then call serializeGrammars once and later
call deserializeGrammars once. By the way, I found a nice example of
how to do exactly that in the "embedded" example in CodeSynthesis XSD.

http://scm.codesynthesis.com/?p=xsd/xsd.git;a=tree;f=examples/cxx/tree/embedded;hb=HEAD

But is it possible to save each binary grammar representation
seperately and at a later time load them seperately?

cheers,
Erik Sjölund

Re: deserializeGrammars from multiple BinInputStream, is it possible?

Posted by Erik Sjölund <er...@gmail.com>.
On Wed, Sep 19, 2012 at 9:31 PM, Erik Sjölund <er...@gmail.com> wrote:
> Is it possible serialize out one binary file per schema file and then
> later just load in a subset of the binary files into an
> XMLGrammarPool?

I did some experimenting:

I modified the "embedded" example from CodeSynthesis XSD and added a
second schema file containing a polymorphic element (belonging to a
substitutionGroup). The two schema files were "compiled" separately
into two binary files with the xsdbin executable (
 http://scm.codesynthesis.com/?p=xsd/xsd.git;a=blob_plain;f=examples/cxx/tree/embedded/xsdbin.cxx;hb=HEAD
).

In my modified "driver.cxx" I had

      grammar_input_stream is (library_schema, sizeof (library_schema));
      gp->deserializeGrammars(&is);
      grammar_input_stream is2 (polymorphic_schema, sizeof
(polymorphic_schema));
      gp->deserializeGrammars(&is2);

When calling deserializeGrammars the second time I got this error

"unable to load schema: string pool is not empty"

So it is not working. I also found that in the Xerces source code it
is stated that multiple deserializations are not supported:

erik@linux:~/tmp$ grep -A2 "Multiple deserializations"
xerces-c-3.1.1/src/xercesc/framework/XMLGrammarPoolImpl.hpp
      * Multiple deserializations
      *
      *    Not supported
erik@linux:~/tmp$

The second idea I had was to use two XMLGrammarPool and let each one
of them run deserializeGrammars once. After that I would move a
Grammar from one XMLGrammarPool to the other with the help of the
functions

Grammar* XMLGrammarPool::orphanGrammar(const XMLCh *const nameSpaceKey)
bool XMLGrammarPool::cacheGrammar(Grammar *const gramToCache)

( see http://xerces.apache.org/xerces-c/apiDocs-3/classXMLGrammarPool.html )

There seems to be a bug related to this

https://issues.apache.org/jira/browse/XERCESC-1798

A workaround for the bug is found in the test case file (test.cpp):

erik@linux:/tmp$ wget -q
https://issues.apache.org/jira/secure/attachment/12381097/test.cpp
erik@linux:/tmp$ grep -A7 -B1 "Copy string" test.cpp
  /*
  // Copy string pool contents.
  //
  const XMLStringPool* src = grpool->getURIStringPool ();
  XMLStringPool* dst = newgrpool->getURIStringPool ();

  for (unsigned int i = 1; i < src->getStringCount () + 1; ++i)
    dst->addOrFind (src->getValueForId (i));
  */


I tried to move the Grammar in this way but it didn't work. When I
later parsed an XML file I got this error:

"error: no declaration found for element 'polymorphic:polymorphic'"

which is the same error I would have got if I wouldn't have used the
second schema at all.

But I don't understand how the workaround should work. If the target
XMLGrammarPool has a non-empty XMLStringPool, the strings will get new
Id numbers, wouldn't they? The Grammar that was moved would then
contain wrong string Id numbers. But I don't know the details, so I
might be missing something here.

cheers,
Erik Sjölund