From 0e4dd3e1b115facf4c6171a300154e6708b9d8bd Mon Sep 17 00:00:00 2001
From: Tinny Ng <tng@apache.org>
Date: Fri, 19 Oct 2001 18:52:04 +0000
Subject: [PATCH] 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.

git-svn-id: https://svn.apache.org/repos/asf/xerces/c/trunk@173137 13f79535-47bb-0310-9956-ffa450edef68
---
 samples/PParse/PParse.cpp         | 93 ++++++++++++++++++-----------
 samples/PParse/PParseHandlers.cpp | 59 ++++++++++++------
 samples/PParse/PParseHandlers.hpp | 99 ++++++++++++++++++++-----------
 3 files changed, 163 insertions(+), 88 deletions(-)

diff --git a/samples/PParse/PParse.cpp b/samples/PParse/PParse.cpp
index d0a6c5e1c..e9b1341b1 100644
--- a/samples/PParse/PParse.cpp
+++ b/samples/PParse/PParse.cpp
@@ -56,6 +56,11 @@
 
 /*
  * $Log$
+ * 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 SAXParser::ValSchemes    valScheme       = SAXParser::Val_Auto;
 // ---------------------------------------------------------------------------
 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 @@ int main(int argC, char* argV[])
          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 @@ int main(int argC, char* argV[])
         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 @@ int main(int argC, char* argV[])
             else
             {
                 cerr << "Unknown -v= value: " << parm << endl;
+                XMLPlatformUtils::Terminate();
                 return 2;
             }
         }
@@ -252,10 +259,10 @@ int main(int argC, char* argV[])
         {
             schemaFullChecking = true;
         }
-         else
+        else
         {
-            usage();
-            return 1;
+            cerr << "Unknown option '" << argV[parmInd]
+                << "', ignoring it\n" << endl;
         }
     }
 
@@ -266,10 +273,11 @@ int main(int argC, char* argV[])
     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
@@ -289,14 +297,17 @@ int main(int argC, char* argV[])
     //  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 @@ int main(int argC, char* argV[])
         //  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 @@ int main(int argC, char* argV[])
              << "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;
 }
 
diff --git a/samples/PParse/PParseHandlers.cpp b/samples/PParse/PParseHandlers.cpp
index a153218a1..7de1956a2 100644
--- a/samples/PParse/PParseHandlers.cpp
+++ b/samples/PParse/PParseHandlers.cpp
@@ -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
@@ -74,8 +74,10 @@
 // ---------------------------------------------------------------------------
 PParseHandlers::PParseHandlers() :
 
-    fCount(0)
-    , fDone(false)
+    fElementCount(0)
+    , fAttrCount(0)
+    , fCharacterCount(0)
+    , fSpaceCount(0)
 {
 }
 
@@ -87,12 +89,31 @@ PParseHandlers::~PParseHandlers()
 // ---------------------------------------------------------------------------
 //  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;
 }
 
 
diff --git a/samples/PParse/PParseHandlers.hpp b/samples/PParse/PParseHandlers.hpp
index 8c1b0dce8..6cdea1110 100644
--- a/samples/PParse/PParseHandlers.hpp
+++ b/samples/PParse/PParseHandlers.hpp
@@ -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$
+ * 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 @@ public :
     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 @@ public :
     void fatalError(const SAXParseException& exception);
 
 
-    // -----------------------------------------------------------------------
-    //  Getter methods
-    // -----------------------------------------------------------------------
-    bool getDone() const;
-
-
-private :
+    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.
+    //  fSawErrors
+    //      This is set by the error handlers, and is queryable later to
+    //      see if any errors occured.
     // -----------------------------------------------------------------------
-    unsigned int    fCount;
-    bool            fDone;
+    unsigned int    fAttrCount;
+    unsigned int    fCharacterCount;
+    unsigned int    fElementCount;
+    unsigned int    fSpaceCount;
+    bool            fSawErrors;
 };
 
-
-inline bool PParseHandlers::getDone() const
-{
-    return fDone;
-}
-- 
GitLab