You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by tn...@apache.org on 2001/10/19 20:52:04 UTC

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

tng         01/10/19 11:52:04

  Modified:    c/samples/PParse PParse.cpp PParseHandlers.cpp
                        PParseHandlers.hpp
  Log:
  Since PParse can take any XML file as input file, it shouldn't hardcode to expect 16 elements.
  Change it to work similar to SAXCount which just prints the number of elements, characters, attributes ... etc.
  And other modification for consistent help display and return code across samples.
  
  Revision  Changes    Path
  1.11      +56 -35    xml-xerces/c/samples/PParse/PParse.cpp
  
  Index: PParse.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/samples/PParse/PParse.cpp,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- PParse.cpp	2001/08/01 19:11:01	1.10
  +++ PParse.cpp	2001/10/19 18:52:04	1.11
  @@ -56,6 +56,11 @@
   
   /*
    * $Log: PParse.cpp,v $
  + * Revision 1.11  2001/10/19 18:52:04  tng
  + * Since PParse can take any XML file as input file, it shouldn't hardcode to expect 16 elements.
  + * Change it to work similar to SAXCount which just prints the number of elements, characters, attributes ... etc.
  + * And other modification for consistent help display and return code across samples.
  + *
    * Revision 1.10  2001/08/01 19:11:01  tng
    * Add full schema constraint checking flag to the samples and the parser.
    *
  @@ -160,20 +165,21 @@
   // ---------------------------------------------------------------------------
   static void usage()
   {
  -    cout <<  "\nUsage: PParse [options] <file>\n\n"
  -         <<  "This sample program demonstrates the progressive parse capabilities of\n"
  -	     <<  "the parser system. It allows you to do a scanFirst() call followed by\n"
  -         <<  "a loop which calls scanNext(). You can drop out when you've found what\n"
  -         <<  "ever it is you want. In our little test, our event handler looks for\n"
  -         <<  "16 new elements then sets a flag to indicate its found what it wants.\n"
  -         <<  "At that point, our progressive parse loop exits.\n\n"
  -         <<  "Options:\n"
  -         <<  "      -v=xxx        - Validation scheme [always | never | auto*]\n"
  -         <<  "      -n            - Enable namespace processing [default is off]\n"
  -         <<  "      -s            - Enable schema processing [default is off]\n"
  -         <<  "      -f            - Enable full schema constraint checking [default is off]\n"
  -         <<  "      -?            - Show this help (must be the only parameter)\n\n"
  -         <<  "  * = Default if not provided explicitly\n"
  +    cout << "\nUsage:\n"
  +            "    PParse [options] <XML file>\n\n"
  +            "This program demonstrates the progressive parse capabilities of\n"
  +	         "the parser system. It allows you to do a scanFirst() call followed by\n"
  +            "a loop which calls scanNext(). You can drop out when you've found what\n"
  +            "ever it is you want. In our little test, our event handler looks for\n"
  +            "16 new elements then sets a flag to indicate its found what it wants.\n"
  +            "At that point, our progressive parse loop exits.\n\n"
  +            "Options:\n"
  +            "      -v=xxx        - Validation scheme [always | never | auto*].\n"
  +            "      -n            - Enable namespace processing [default is off].\n"
  +            "      -s            - Enable schema processing [default is off].\n"
  +            "      -f            - Enable full schema constraint checking [default is off].\n"
  +            "      -?            - Show this help.\n\n"
  +            "  * = Default if not provided explicitly.\n"
            <<  endl;
   }
   
  @@ -197,21 +203,14 @@
            return 1;
       }
   
  -
       // Check command line and extract arguments.
       if (argC < 2)
       {
           usage();
  +        XMLPlatformUtils::Terminate();
           return 1;
       }
   
  -    // Watch for special case help request
  -    if (!strcmp(argV[1], "-?"))
  -    {
  -        usage();
  -        return 2;
  -    }
  -
       // See if non validating dom parser configuration is requested.
       int parmInd;
       for (parmInd = 1; parmInd < argC; parmInd++)
  @@ -220,8 +219,15 @@
           if (argV[parmInd][0] != '-')
               break;
   
  -        if (!strncmp(argV[parmInd], "-v=", 3)
  -        ||  !strncmp(argV[parmInd], "-V=", 3))
  +        // Watch for special case help request
  +        if (!strcmp(argV[parmInd], "-?"))
  +        {
  +            usage();
  +            XMLPlatformUtils::Terminate();
  +            return 2;
  +        }
  +         else if (!strncmp(argV[parmInd], "-v=", 3)
  +              ||  !strncmp(argV[parmInd], "-V=", 3))
           {
               const char* const parm = &argV[parmInd][3];
   
  @@ -234,6 +240,7 @@
               else
               {
                   cerr << "Unknown -v= value: " << parm << endl;
  +                XMLPlatformUtils::Terminate();
                   return 2;
               }
           }
  @@ -252,10 +259,10 @@
           {
               schemaFullChecking = true;
           }
  -         else
  +        else
           {
  -            usage();
  -            return 1;
  +            cerr << "Unknown option '" << argV[parmInd]
  +                << "', ignoring it\n" << endl;
           }
       }
   
  @@ -266,11 +273,12 @@
       if (parmInd + 1 != argC)
       {
           usage();
  +        XMLPlatformUtils::Terminate();
           return 1;
       }
       xmlFile = argV[parmInd];
  +    int errorCount = 0;
   
  -
       //
       //  Create a SAX parser object to use and create our SAX event handlers
       //  and plug them in.
  @@ -289,14 +297,17 @@
       //  loop, we look and see if the handler has found what its looking
       //  for. When it does, we fall out then.
       //
  +    unsigned long duration;
       try
       {
           // Create a progressive scan token
           XMLPScanToken token;
   
  +        const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
           if (!parser.parseFirst(xmlFile, token))
           {
               cerr << "scanFirst() failed\n" << endl;
  +            XMLPlatformUtils::Terminate();
               return 1;
           }
   
  @@ -305,9 +316,13 @@
           //  or hit the end.
           //
           bool gotMore = true;
  -        while (gotMore && !handler.getDone())
  +        while (gotMore && !parser.getErrorCount())
               gotMore = parser.parseNext(token);
   
  +        const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
  +        duration = endMillis - startMillis;
  +
  +        errorCount = parser.getErrorCount();
           //
           //  Reset the parser. In this simple progrma, since we just exit
           //  now, its not technically required. But, in programs which
  @@ -324,17 +339,25 @@
                << "Exception message is: \n"
                << StrX(toCatch.getMessage())
                << "\n" << endl;
  -        return -1;
  +        XMLPlatformUtils::Terminate();
  +        return 4;
       }
   
  -    if (handler.getDone())
  -        cout << "Got the required 16 elements\n" << endl;
  -    else
  -        cout << "Did not get the required 16 elements\n" << endl;
  +
  +    if (!errorCount) {
  +        cout << xmlFile << ": " << duration << " ms ("
  +            << handler.getElementCount() << " elems, "
  +            << handler.getAttrCount() << " attrs, "
  +            << handler.getSpaceCount() << " spaces, "
  +            << handler.getCharacterCount() << " chars)" << endl;
  +    }
   
       // And call the termination method
       XMLPlatformUtils::Terminate();
   
  -    return 0;
  +    if (errorCount > 0)
  +        return 4;
  +    else
  +        return 0;
   }
   
  
  
  
  1.5       +41 -20    xml-xerces/c/samples/PParse/PParseHandlers.cpp
  
  Index: PParseHandlers.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/samples/PParse/PParseHandlers.cpp,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- PParseHandlers.cpp	2000/07/25 22:41:16	1.4
  +++ PParseHandlers.cpp	2001/10/19 18:52:04	1.5
  @@ -1,37 +1,37 @@
   /*
    * The Apache Software License, Version 1.1
  - * 
  - * Copyright (c) 1999-2000 The Apache Software Foundation.  All rights
  + *
  + * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
    * reserved.
  - * 
  + *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions
    * are met:
  - * 
  + *
    * 1. Redistributions of source code must retain the above copyright
  - *    notice, this list of conditions and the following disclaimer. 
  - * 
  + *    notice, this list of conditions and the following disclaimer.
  + *
    * 2. Redistributions in binary form must reproduce the above copyright
    *    notice, this list of conditions and the following disclaimer in
    *    the documentation and/or other materials provided with the
    *    distribution.
  - * 
  + *
    * 3. The end-user documentation included with the redistribution,
  - *    if any, must include the following acknowledgment:  
  + *    if any, must include the following acknowledgment:
    *       "This product includes software developed by the
    *        Apache Software Foundation (http://www.apache.org/)."
    *    Alternately, this acknowledgment may appear in the software itself,
    *    if and wherever such third-party acknowledgments normally appear.
  - * 
  + *
    * 4. The names "Xerces" and "Apache Software Foundation" must
    *    not be used to endorse or promote products derived from this
  - *    software without prior written permission. For written 
  + *    software without prior written permission. For written
    *    permission, please contact apache\@apache.org.
  - * 
  + *
    * 5. Products derived from this software may not be called "Apache",
    *    nor may "Apache" appear in their name, without prior written
    *    permission of the Apache Software Foundation.
  - * 
  + *
    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  @@ -45,7 +45,7 @@
    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    * SUCH DAMAGE.
    * ====================================================================
  - * 
  + *
    * This software consists of voluntary contributions made by many
    * individuals on behalf of the Apache Software Foundation, and was
    * originally based on software copyright (c) 1999, International
  @@ -55,7 +55,7 @@
    */
   
   /*
  - * $Id: PParseHandlers.cpp,v 1.4 2000/07/25 22:41:16 aruna1 Exp $
  + * $Id: PParseHandlers.cpp,v 1.5 2001/10/19 18:52:04 tng Exp $
    */
   
   
  @@ -74,8 +74,10 @@
   // ---------------------------------------------------------------------------
   PParseHandlers::PParseHandlers() :
   
  -    fCount(0)
  -    , fDone(false)
  +    fElementCount(0)
  +    , fAttrCount(0)
  +    , fCharacterCount(0)
  +    , fSpaceCount(0)
   {
   }
   
  @@ -87,12 +89,31 @@
   // ---------------------------------------------------------------------------
   //  PParseHandlers: Overrides of the SAX DocumentHandler interface
   // ---------------------------------------------------------------------------
  -void PParseHandlers::startElement(  const   XMLCh* const    name
  +void PParseHandlers::startElement(const   XMLCh* const    name
                                       ,       AttributeList&  attributes)
   {
  -    // Bump up the count of elements, and set done if we hit 16
  -    fCount++;
  -    fDone = (fCount == 16);
  +    fElementCount++;
  +    fAttrCount += attributes.getLength();
  +}
  +
  +void PParseHandlers::characters(  const   XMLCh* const    chars
  +								    , const unsigned int    length)
  +{
  +    fCharacterCount += length;
  +}
  +
  +void PParseHandlers::ignorableWhitespace( const   XMLCh* const chars
  +										    , const unsigned int length)
  +{
  +    fSpaceCount += length;
  +}
  +
  +void PParseHandlers::resetDocument()
  +{
  +    fAttrCount = 0;
  +    fCharacterCount = 0;
  +    fElementCount = 0;
  +    fSpaceCount = 0;
   }
   
   
  
  
  
  1.4       +66 -35    xml-xerces/c/samples/PParse/PParseHandlers.hpp
  
  Index: PParseHandlers.hpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/samples/PParse/PParseHandlers.hpp,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PParseHandlers.hpp	2000/03/02 19:53:44	1.3
  +++ PParseHandlers.hpp	2001/10/19 18:52:04	1.4
  @@ -1,37 +1,37 @@
   /*
    * The Apache Software License, Version 1.1
  - * 
  - * Copyright (c) 1999-2000 The Apache Software Foundation.  All rights
  + *
  + * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
    * reserved.
  - * 
  + *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions
    * are met:
  - * 
  + *
    * 1. Redistributions of source code must retain the above copyright
  - *    notice, this list of conditions and the following disclaimer. 
  - * 
  + *    notice, this list of conditions and the following disclaimer.
  + *
    * 2. Redistributions in binary form must reproduce the above copyright
    *    notice, this list of conditions and the following disclaimer in
    *    the documentation and/or other materials provided with the
    *    distribution.
  - * 
  + *
    * 3. The end-user documentation included with the redistribution,
  - *    if any, must include the following acknowledgment:  
  + *    if any, must include the following acknowledgment:
    *       "This product includes software developed by the
    *        Apache Software Foundation (http://www.apache.org/)."
    *    Alternately, this acknowledgment may appear in the software itself,
    *    if and wherever such third-party acknowledgments normally appear.
  - * 
  + *
    * 4. The names "Xerces" and "Apache Software Foundation" must
    *    not be used to endorse or promote products derived from this
  - *    software without prior written permission. For written 
  + *    software without prior written permission. For written
    *    permission, please contact apache\@apache.org.
  - * 
  + *
    * 5. Products derived from this software may not be called "Apache",
    *    nor may "Apache" appear in their name, without prior written
    *    permission of the Apache Software Foundation.
  - * 
  + *
    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  @@ -45,7 +45,7 @@
    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    * SUCH DAMAGE.
    * ====================================================================
  - * 
  + *
    * This software consists of voluntary contributions made by many
    * individuals on behalf of the Apache Software Foundation, and was
    * originally based on software copyright (c) 1999, International
  @@ -56,6 +56,11 @@
   
   /*
    * $Log: PParseHandlers.hpp,v $
  + * Revision 1.4  2001/10/19 18:52:04  tng
  + * Since PParse can take any XML file as input file, it shouldn't hardcode to expect 16 elements.
  + * Change it to work similar to SAXCount which just prints the number of elements, characters, attributes ... etc.
  + * And other modification for consistent help display and return code across samples.
  + *
    * Revision 1.3  2000/03/02 19:53:44  roddey
    * This checkin includes many changes done while waiting for the
    * 1.1.0 code to be finished. I can't list them all here, but a list is
  @@ -84,12 +89,42 @@
       PParseHandlers();
       ~PParseHandlers();
   
  +    // -----------------------------------------------------------------------
  +    //  Getter methods
  +    // -----------------------------------------------------------------------
  +    unsigned int getElementCount() const
  +    {
  +        return fElementCount;
  +    }
  +
  +    unsigned int getAttrCount() const
  +    {
  +        return fAttrCount;
  +    }
  +
  +    unsigned int getCharacterCount() const
  +    {
  +        return fCharacterCount;
  +    }
  +
  +    bool getSawErrors() const
  +    {
  +        return fSawErrors;
  +    }
  +
  +    unsigned int getSpaceCount() const
  +    {
  +        return fSpaceCount;
  +    }
  +
   
       // -----------------------------------------------------------------------
  -    //  Implementations of the SAX DocumentHandler interface
  +    //  Handlers for the SAX DocumentHandler interface
       // -----------------------------------------------------------------------
       void startElement(const XMLCh* const name, AttributeList& attributes);
  -
  +    void characters(const XMLCh* const chars, const unsigned int length);
  +    void ignorableWhitespace(const XMLCh* const chars, const unsigned int length);
  +    void resetDocument();
   
   
       // -----------------------------------------------------------------------
  @@ -100,29 +135,25 @@
       void fatalError(const SAXParseException& exception);
   
   
  +    private:
       // -----------------------------------------------------------------------
  -    //  Getter methods
  -    // -----------------------------------------------------------------------
  -    bool getDone() const;
  -
  -
  -private :
  -    // -----------------------------------------------------------------------
       //  Private data members
       //
  -    //  fCount
  -    //      This is a counter that we use to count elements. When it hits
  -    //      16, we set our done flag.
  +    //  fAttrCount
  +    //  fCharacterCount
  +    //  fElementCount
  +    //  fSpaceCount
  +    //      These are just counters that are run upwards based on the input
  +    //      from the document handlers.
       //
  -    //  fDone
  -    //      This flag is set once we find what we are looking for.
  -    // -----------------------------------------------------------------------
  -    unsigned int    fCount;
  -    bool            fDone;
  +    //  fSawErrors
  +    //      This is set by the error handlers, and is queryable later to
  +    //      see if any errors occured.
  +    // -----------------------------------------------------------------------
  +    unsigned int    fAttrCount;
  +    unsigned int    fCharacterCount;
  +    unsigned int    fElementCount;
  +    unsigned int    fSpaceCount;
  +    bool            fSawErrors;
   };
   
  -
  -inline bool PParseHandlers::getDone() const
  -{
  -    return fDone;
  -}
  
  
  

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