Hi Carolina,
I am sorry I couldn't mail my solution earlier, but I had no Internet access yesterday :-(
Here is the code that works for me (on Windows); note that the proper solution would be using the SAX2 interface EntityResolver2 that has a dedicated callback to provide an external DTD for documents that have no DTD, but Xerces doesn't support it yet. So this is a kind of hack that work with the current codebase... no support offered ;-)
This code creates an XMLValidator that, when the parser tells him to store a new (empty) DTD grammar, it really populates it with the information it stored before.


class VxmlDTDValidator : public DTDValidator
{
public:
    VxmlDTDValidator()
    {
        setGrammar(DTDParser.loadGrammar("dtd.dtd", Grammar::DTDGrammarType));
    }

virtual void setGrammar(Grammar* aGrammar)
{
if(getGrammar()==NULL)
DTDValidator::setGrammar(aGrammar);
else
{
aGrammar->reset();
DTDGrammar* pDTDGrammar=(DTDGrammar*)getGrammar();
NameIdPoolEnumerator<DTDElementDecl> enumElem=pDTDGrammar->getElemEnumerator();
while(enumElem.hasMoreElements())
{
DTDElementDecl& elemDecl=enumElem.nextElement();
DTDElementDecl* copy = new DTDElementDecl
(
elemDecl.getFullName()
, elemDecl.getURI()
, DTDElementDecl::Any
);
copy->setCreateReason(elemDecl.getCreateReason());
aGrammar->putElemDecl(copy);
}
}
}


protected:
    XercesDOMParser DTDParser;
};

....
    VxmlDTDValidator* dtdValidator=new VxmlDTDValidator();

XercesDOMParser *VxmlGrammarFileParser = new XercesDOMParser(dtdValidator);
dtdValidator->setErrorReporter(VxmlGrammarFileParser);
VxmlGrammarFileParser->setValidationScheme(XercesDOMParser::Val_Always);
VxmlGrammarFileParser->setDoNamespaces(false);
VxmlGrammarFileParser->setErrorHandler(this);
VxmlGrammarFileParser->resetDocumentPool();
VxmlGrammarFileParser->parse("xml.xml");


    delete VxmlGrammarFileParser;

Hope this helps,
Alberto


At 09.47 17/09/2004 -0400, Carolina Garcia-Paris wrote:
Folks,

I reported this issue to Alberto yesterday (DOM parser fails after using
DTDValidator). Has anyone else experienced this problem with the parser
failing after being set with a DTDValidator? I also found an old bug
reported on what seems to be the same issue, but for the SAX2 parser as
opposed the DOM parser (See below). What's the status of this bug?

We need to be able to parse Voice XML files against a cached DTD. Our XML
files don't contain a DOCTYPE line. Alberto's solution, using a
DTDValidator, seemed to suit our needs but if fails (See stack trace at end
of e-mail message).

Any information will be greatly appreciated. Thanks!

--Carolina


---------------------------------------------------------------------------- ---- From: bugzilla Subject: [Bug 1820] Changed - Error while working of DTDValidator(Using SAX2 parser) Date: Sat, 19 May 2001 07:38:13 -0700 ---------------------------------------------------------------------------- ---- http://nagoya.apache.org/bugzilla/show_bug.cgi?id=1820

*** shadow/1820 Sat May 19 07:00:07 2001
--- shadow/1820.tmp.3437        Sat May 19 07:55:05 2001
***************
*** 4,10 ****
  |        Bug #: 1820                        Product: Xerces-C
|
  |       Status: NEW                         Version: 1.4
|
  |   Resolution:                            Platform: PC
|
! |     Severity: Normal                   OS/Version:
|
  |     Priority: High                      Component: Validating Parser
(DTD) |

+---------------------------------------------------------------------------
-+
  |  Assigned To: [EMAIL PROTECTED]                                  |
--- 4,10 ----
  |        Bug #: 1820                        Product: Xerces-C
|
  |       Status: NEW                         Version: 1.4
|
  |   Resolution:                            Platform: PC
|
! |     Severity: Normal                   OS/Version: All
|
  |     Priority: High                      Component: Validating Parser
(DTD) |

+---------------------------------------------------------------------------
-+
  |  Assigned To: [EMAIL PROTECTED]                                  |
***************
  ------- Additional Comments From [EMAIL PROTECTED]  2001-05-19
07:00 -------
  Created an attachment (id=169)
  The dtd parsing fails
+
+
+ ------- Additional Comments From [EMAIL PROTECTED]  2001-05-19
07:55 -------
+ Tryed the last nightly 05-17-2001  -  the problem is the same(the
BoundsChecker
+ reports the dynamic memory overrun)
+ The problem occures while creating createIntEntReader for the
+ timecontainer.attrib


---------------------------------------------------------------------




-----Original Message----- From: Carolina Garcia-Paris [mailto:[EMAIL PROTECTED] Sent: Thursday, September 16, 2004 10:26 AM To: [EMAIL PROTECTED] Subject: RE: Validating against DTD in memory (Xerces C++ 2.5.0)


Hi Alberto,

I tried your code suggestion, but I'm getting a segmentation fault when
running it on Linux:

---------------------------
Program received signal SIGSEGV, Segmentation fault.
0x000232b7 in ?? ()
(gdb) bt
#0  0x000232b7 in ?? ()
#1  0x401decfe in
xercesc_2_5::IGXMLScanner::scanReset(xercesc_2_5::InputSource const&) ()
from /d4m/tools/i686ling/xerces-c-src_2_5_0/lib/libxerces-c.so.25
#2  0x401e485f in
xercesc_2_5::IGXMLScanner::scanDocument(xercesc_2_5::InputSource const&) ()
from /d4m/tools/i686ling/xerces-c-src_2_5_0/lib/libxerces-c.so.25
#3  0x4029a776 in xercesc_2_5::XMLScanner::scanDocument(unsigned short
const*)
    () from /d4m/tools/i686ling/xerces-c-src_2_5_0/lib/libxerces-c.so.25
#4  0x4029a7fd in xercesc_2_5::XMLScanner::scanDocument(char const*) ()
   from /d4m/tools/i686ling/xerces-c-src_2_5_0/lib/libxerces-c.so.25
#5  0x40142695 in xercesc_2_5::AbstractDOMParser::parse(char const*) ()
   from /d4m/tools/i686ling/xerces-c-src_2_5_0/lib/libxerces-c.so.25
#6  0x080584fa in vxmlparser::parse(char const*) (this=0xbfffeae0,
    filename=0x80e8fdc "../simple.grxml") at ../vxmlparser.cpp:169
#7  0x0804d7c9 in main (argc=3, argv=0xbfffebd4) at
../compile_vxmlgram.cpp:130
(gdb) frame 6
#6  0x080584fa in vxmlparser::parse(char const*) (this=0xbfffeae0,
    filename=0x80e8fdc "../simple.grxml") at ../vxmlparser.cpp:169
(gdb) p filename
$5 = 0x80e8fdc "../simple.grxml"
--------------------


The input XML file does not contain a DOCTYPE line (I am assuming is not needed). Here is a snipet of my code, reflecting your suggestion. Notice that I haven't set the error handler for the DTDParser object (do I need to?)

-----------
    // inputdtd is a std::string containing the dtd in memory...
    . . .
    const XMLByte* dtdsource =
        reinterpret_cast<const XMLByte*>(inputdtd.c_str());
    unsigned int dtdsize     = gramdtd.length();
    const char* fakeSysId    = "VoiceXML";

    MemBufInputSource dtdsrc(dtdsource, dtdsize, fakeSysId);

    DTDParser = new XercesDOMParser();
    Grammar* pDTD = DTDParser->loadGrammar(dtdsrc, Grammar::DTDGrammarType,
true);
    DTDValidator dtdValidator;
    dtdValidator.setGrammar(pDTD);

    VxmlGrammarFileParser = new XercesDOMParser(&dtdValidator);
    VxmlGrammarFileParser->setValidationScheme(XercesDOMParser::Val_Always);
    VxmlGrammarFileParser->setDoNamespaces(false);
    VxmlGrammarFileParser->setErrorHandler(this);
    VxmlGrammarFileParser->resetDocumentPool();
    VxmlGrammarFileParser->parse(filename);  <----- THIS CALLS GIVES
SEGMENTATION FAULT
   . . .


Let me know if I need to set additional stuff in the code. Thanks once again!!

--Carolina


-----Original Message----- From: Alberto Massari [mailto:[EMAIL PROTECTED] Sent: Wednesday, September 15, 2004 5:58 PM To: [EMAIL PROTECTED] Subject: RE: Validating against DTD in memory (Xerces C++ 2.5.0)


Hi Carolina,

At 17.05 15/09/2004 -0400, Carolina Garcia-Paris wrote:
>I posted this message yesterday but haven't gotten a response yet.
>
>The bottomline is... has anyone gotten XercesDOMParser::loadGrammar() to
>work for loading a DTD grammar in memory and then use it to parse XML files
>against it? See my code below. Any hints/recommendations will be greatly
>appreciated.

The problem is that DTDs are cached using their system id, but your XML
files don't specify one, so they cannot be retrieved back. The only
solution is to use a custom XMLValidator.

Try starting with this code

#include <xercesc/validators/common/Grammar.hpp>
#include <xercesc/validators/DTD/DTDValidator.hpp>

...
DOMParser dtdParser;
MemBufInputSource dtdsrc(dtdsource, inputdtd.length(), "VoiceXML");
Grammar* pDTD=dtdParser.loadGrammar(dtdsrc, Grammar::DTDGrammarType, true);
DTDValidator dtdValidator;
dtdValidator.setGrammar(pDTD);

DOMParser VxmlParser(&dtdValidator);
VxmlParser.setValidationScheme(DOMParser::Val_Always);
VxmlParser.parse(...)

Hope this helps,
Alberto



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to