You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by kn...@apache.org on 2002/06/18 18:19:41 UTC

cvs commit: xml-xerces/c/samples/DOMCount DOMCount.cpp DOMCount.hpp

knoaman     2002/06/18 09:19:41

  Modified:    c/samples/DOMCount DOMCount.cpp DOMCount.hpp
  Log:
  Replace XercesDOMParser with DOMBuilder for parsing XML documents.
  
  Revision  Changes    Path
  1.25      +61 -44    xml-xerces/c/samples/DOMCount/DOMCount.cpp
  
  Index: DOMCount.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/samples/DOMCount/DOMCount.cpp,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- DOMCount.cpp	21 May 2002 19:15:51 -0000	1.24
  +++ DOMCount.cpp	18 Jun 2002 16:19:40 -0000	1.25
  @@ -62,12 +62,16 @@
   //  Includes
   // ---------------------------------------------------------------------------
   #include <xercesc/util/PlatformUtils.hpp>
  -#include <xercesc/sax/SAXException.hpp>
  -#include <xercesc/sax/SAXParseException.hpp>
  -#include <xercesc/parsers/XercesDOMParser.hpp>
  +#include <xercesc/parsers/AbstractDOMParser.hpp>
  +#include <xercesc/dom/DOMImplementation.hpp>
  +#include <xercesc/dom/DOMImplementationLS.hpp>
  +#include <xercesc/dom/DOMImplementationRegistry.hpp>
  +#include <xercesc/dom/DOMBuilder.hpp>
   #include <xercesc/dom/DOMException.hpp>
   #include <xercesc/dom/DOMDocument.hpp>
   #include <xercesc/dom/DOMNodeList.hpp>
  +#include <xercesc/dom/DOMError.hpp>
  +#include <xercesc/dom/DOMLocator.hpp>
   #include "DOMCount.hpp"
   #include <string.h>
   #include <stdlib.h>
  @@ -85,7 +89,7 @@
   {
       cout << "\nUsage:\n"
               "    DOMCount [options] <XML file | List file>\n\n"
  -            "This program invokes the DOM parser, builds the DOM tree,\n"
  +            "This program invokes the DOMBuilder, builds the DOM tree,\n"
               "and then prints the number of elements found in each XML file.\n\n"
               "Options:\n"
               "    -l          Indicate the input file is a List File that has a list of xml files.\n"
  @@ -148,13 +152,13 @@
           return 1;
       }
   
  -    const char*               xmlFile = 0;
  -    XercesDOMParser::ValSchemes    valScheme = XercesDOMParser::Val_Auto;
  -    bool                      doNamespaces       = false;
  -    bool                      doSchema           = false;
  -    bool                      schemaFullChecking = false;
  -    bool                      doList = false;
  -    bool                      errorOccurred = false;
  +    const char*                xmlFile = 0;
  +    AbstractDOMParser::ValSchemes valScheme = AbstractDOMParser::Val_Auto;
  +    bool                       doNamespaces       = false;
  +    bool                       doSchema           = false;
  +    bool                       schemaFullChecking = false;
  +    bool                       doList = false;
  +    bool                       errorOccurred = false;
   
       int argInd;
       for (argInd = 1; argInd < argC; argInd++)
  @@ -176,11 +180,11 @@
               const char* const parm = &argV[argInd][3];
   
               if (!strcmp(parm, "never"))
  -                valScheme = XercesDOMParser::Val_Never;
  +                valScheme = AbstractDOMParser::Val_Never;
               else if (!strcmp(parm, "auto"))
  -                valScheme = XercesDOMParser::Val_Auto;
  +                valScheme = AbstractDOMParser::Val_Auto;
               else if (!strcmp(parm, "always"))
  -                valScheme = XercesDOMParser::Val_Always;
  +                valScheme = AbstractDOMParser::Val_Always;
               else
               {
                   cerr << "Unknown -v= value: " << parm << endl;
  @@ -227,11 +231,29 @@
       }
   
       // Instantiate the DOM parser.
  -    XercesDOMParser* parser = new XercesDOMParser;
  -    parser->setValidationScheme(valScheme);
  -    parser->setDoNamespaces(doNamespaces);
  -    parser->setDoSchema(doSchema);
  -    parser->setValidationSchemaFullChecking(schemaFullChecking);
  +    static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
  +    DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(gLS);
  +    DOMBuilder        *parser = ((DOMImplementationLS*)impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
  +
  +    parser->setFeature(XMLUni::fgDOMNamespaces, doNamespaces);
  +    parser->setFeature(XMLUni::fgXercesSchema, doSchema);
  +    parser->setFeature(XMLUni::fgXercesSchemaFullChecking, schemaFullChecking);
  +
  +    if (valScheme == AbstractDOMParser::Val_Auto)
  +    {
  +        parser->setFeature(XMLUni::fgDOMValidateIfSchema, true);
  +    }
  +    else if (valScheme == AbstractDOMParser::Val_Never)
  +    {
  +        parser->setFeature(XMLUni::fgDOMValidation, false);
  +    }
  +    else if (valScheme == AbstractDOMParser::Val_Always)
  +    {
  +        parser->setFeature(XMLUni::fgDOMValidation, true);
  +    }
  +
  +    // enable datatype normalization - default is off
  +    parser->setFeature(XMLUni::fgDOMDatatypeNormalization, true);
   
       // And create our error handler and install it
       DOMCountErrorHandler errorHandler;
  @@ -277,11 +299,15 @@
           //reset error count first
           errorHandler.resetErrors();
   
  +        DOMDocument *doc = 0;
  +
           try
           {
  +            // reset document pool
  +            parser->setFeature(XMLUni::fgXercesResetDocumentPool, true);
  +
               const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
  -            parser->resetDocumentPool();
  -            parser->parse(xmlFile);
  +            doc = parser->parseURI(xmlFile);
               const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
               duration = endMillis - startMillis;
           }
  @@ -320,7 +346,6 @@
           }
            else
           {
  -            DOMDocument *doc = parser->getDocument();
               unsigned int elementCount = 0;
               if (doc) {
                   elementCount = countChildElements((DOMNode*)doc->getDocumentElement());
  @@ -371,32 +396,24 @@
   
   
   // ---------------------------------------------------------------------------
  -//  DOMCountHandlers: Overrides of the SAX ErrorHandler interface
  +//  DOMCountHandlers: Overrides of the DOM ErrorHandler interface
   // ---------------------------------------------------------------------------
  -void DOMCountErrorHandler::error(const SAXParseException& e)
  +bool DOMCountErrorHandler::handleError(const DOMError& domError)
   {
       fSawErrors = true;
  -    cerr << "\nError at file " << StrX(e.getSystemId())
  -         << ", line " << e.getLineNumber()
  -         << ", char " << e.getColumnNumber()
  -         << "\n  Message: " << StrX(e.getMessage()) << endl;
  -}
  +    if (domError.getSeverity() == DOMError::DOM_SEVERITY_WARNING)
  +        cerr << "\nWarning at file ";
  +    else if (domError.getSeverity() == DOMError::DOM_SEVERITY_ERROR)
  +        cerr << "\nError at file ";
  +    else
  +        cerr << "\nFatal Error at file ";
   
  -void DOMCountErrorHandler::fatalError(const SAXParseException& e)
  -{
  -    fSawErrors = true;
  -    cerr << "\nFatal Error at file " << StrX(e.getSystemId())
  -         << ", line " << e.getLineNumber()
  -         << ", char " << e.getColumnNumber()
  -         << "\n  Message: " << StrX(e.getMessage()) << endl;
  -}
  +    cerr << StrX(domError.getLocation()->getURI())
  +         << ", line " << domError.getLocation()->getLineNumber()
  +         << ", char " << domError.getLocation()->getColumnNumber()
  +         << "\n  Message: " << StrX(domError.getMessage()) << endl;
   
  -void DOMCountErrorHandler::warning(const SAXParseException& e)
  -{
  -    cerr << "\nWarning at file " << StrX(e.getSystemId())
  -         << ", line " << e.getLineNumber()
  -         << ", char " << e.getColumnNumber()
  -         << "\n  Message: " << StrX(e.getMessage()) << endl;
  +    return false;
   }
   
   void DOMCountErrorHandler::resetErrors()
  
  
  
  1.7       +8 -8      xml-xerces/c/samples/DOMCount/DOMCount.hpp
  
  Index: DOMCount.hpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/samples/DOMCount/DOMCount.hpp,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- DOMCount.hpp	1 Feb 2002 22:35:01 -0000	1.6
  +++ DOMCount.hpp	18 Jun 2002 16:19:40 -0000	1.7
  @@ -56,6 +56,9 @@
   
   /*
    * $Log$
  + * Revision 1.7  2002/06/18 16:19:40  knoaman
  + * Replace XercesDOMParser with DOMBuilder for parsing XML documents.
  + *
    * Revision 1.6  2002/02/01 22:35:01  peiyongz
    * sane_include
    *
  @@ -84,16 +87,15 @@
   // ---------------------------------------------------------------------------
   //  Includes
   // ---------------------------------------------------------------------------
  -#include <xercesc/sax/ErrorHandler.hpp>
  +#include <xercesc/dom/DOMErrorHandler.hpp>
  +#include <xercesc/util/XMLString.hpp>
   #include <iostream.h>
   
  -class SAXParseException;
  -
   
   // ---------------------------------------------------------------------------
   //  Simple error handler deriviative to install on parser
   // ---------------------------------------------------------------------------
  -class DOMCountErrorHandler : public ErrorHandler
  +class DOMCountErrorHandler : public DOMErrorHandler
   {
   public:
       // -----------------------------------------------------------------------
  @@ -110,11 +112,9 @@
   
   
       // -----------------------------------------------------------------------
  -    //  Implementation of the SAX ErrorHandler interface
  +    //  Implementation of the DOM ErrorHandler interface
       // -----------------------------------------------------------------------
  -    void warning(const SAXParseException& e);
  -    void error(const SAXParseException& e);
  -    void fatalError(const SAXParseException& e);
  +    bool handleError(const DOMError& domError);
       void resetErrors();
   
   
  
  
  

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