source: icXML/icXML-devel/samples/src/PParse/PParse.cpp @ 2733

Last change on this file since 2733 was 2733, checked in by cameron, 6 years ago

More path fixes.

File size: 10.1 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * $Id: PParse.cpp 833057 2009-11-05 15:25:10Z borisk $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  This sample program demonstrates the progressive parse capabilities of
25//  the parser system. It allows you to do a scanFirst() call followed by
26//  a loop which calls scanNext(). You can drop out when you've found what
27//  ever it is you want. In our little test, our event handler looks for
28//  16 new elements then sets a flag to indicate its found what it wants.
29//  At that point, our progressive parse loop below exits.
30//
31//  The parameters are:
32//
33//      [-?]            - Show usage and exit
34//      [-v=xxx]        - Validation scheme [always | never | auto*]
35//      [-n]            - Enable namespace processing
36//      [-s]            - Enable schema processing
37//      [-f]            - Enable full schema constraint checking
38//      filename        - The path to the XML file to parse
39//
40//  * = Default if not provided explicitly
41//  These are non-case sensitive
42// ---------------------------------------------------------------------------
43
44
45// ---------------------------------------------------------------------------
46//  Includes
47// ---------------------------------------------------------------------------
48#include <icxercesc/util/PlatformUtils.hpp>
49#include <xercesc/framework/XMLPScanToken.hpp>
50#include <icxercesc/parsers/SAXParser.hpp>
51#include "PParse.hpp"
52#include <xercesc/util/OutOfMemoryException.hpp>
53
54// ---------------------------------------------------------------------------
55//  Local data
56//
57//  xmlFile
58//      The path to the file to parser. Set via command line.
59//
60//  doNamespaces
61//      Indicates whether namespace processing should be done.
62//
63//  doSchema
64//      Indicates whether schema processing should be done.
65//
66//  schemaFullChecking
67//      Indicates whether full schema constraint checking should be done.
68//
69//  valScheme
70//      Indicates what validation scheme to use. It defaults to 'auto', but
71//      can be set via the -v= command.
72// ---------------------------------------------------------------------------
73static char*     xmlFile         = 0;
74static bool     doNamespaces       = false;
75static bool     doSchema           = false;
76static bool     schemaFullChecking = false;
77static SAXParser::ValSchemes    valScheme       = SAXParser::Val_Auto;
78
79
80
81// ---------------------------------------------------------------------------
82//  Local helper methods
83// ---------------------------------------------------------------------------
84static void usage()
85{
86    XERCES_STD_QUALIFIER cout << "\nUsage:\n"
87            "    PParse [options] <XML file>\n\n"
88            "This program demonstrates the progressive parse capabilities of\n"
89                 "the parser system. It allows you to do a scanFirst() call followed by\n"
90            "a loop which calls scanNext(). You can drop out when you've found what\n"
91            "ever it is you want. In our little test, our event handler looks for\n"
92            "16 new elements then sets a flag to indicate its found what it wants.\n"
93            "At that point, our progressive parse loop exits.\n\n"
94            "Options:\n"
95            "      -v=xxx        - Validation scheme [always | never | auto*].\n"
96            "      -n            - Enable namespace processing [default is off].\n"
97            "      -s            - Enable schema processing [default is off].\n"
98            "      -f            - Enable full schema constraint checking [default is off].\n"
99            "      -?            - Show this help.\n\n"
100            "  * = Default if not provided explicitly.\n"
101         <<  XERCES_STD_QUALIFIER endl;
102}
103
104
105
106// ---------------------------------------------------------------------------
107//  Program entry point
108// ---------------------------------------------------------------------------
109int main(int argC, char* argV[])
110{
111    // Initialize the XML4C system
112    try
113    {
114         XMLPlatformUtils::Initialize();
115    }
116
117    catch (const XMLException& toCatch)
118    {
119         XERCES_STD_QUALIFIER cerr << "Error during initialization! :\n"
120              << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
121         return 1;
122    }
123
124    // Check command line and extract arguments.
125    if (argC < 2)
126    {
127        usage();
128        XMLPlatformUtils::Terminate();
129        return 1;
130    }
131
132    // See if non validating dom parser configuration is requested.
133    int parmInd;
134    for (parmInd = 1; parmInd < argC; parmInd++)
135    {
136        // Break out on first parm not starting with a dash
137        if (argV[parmInd][0] != '-')
138            break;
139
140        // Watch for special case help request
141        if (!strcmp(argV[parmInd], "-?"))
142        {
143            usage();
144            XMLPlatformUtils::Terminate();
145            return 2;
146        }
147         else if (!strncmp(argV[parmInd], "-v=", 3)
148              ||  !strncmp(argV[parmInd], "-V=", 3))
149        {
150            const char* const parm = &argV[parmInd][3];
151
152            if (!strcmp(parm, "never"))
153                valScheme = SAXParser::Val_Never;
154            else if (!strcmp(parm, "auto"))
155                valScheme = SAXParser::Val_Auto;
156            else if (!strcmp(parm, "always"))
157                valScheme = SAXParser::Val_Always;
158            else
159            {
160                XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl;
161                XMLPlatformUtils::Terminate();
162                return 2;
163            }
164        }
165         else if (!strcmp(argV[parmInd], "-n")
166              ||  !strcmp(argV[parmInd], "-N"))
167        {
168            doNamespaces = true;
169        }
170         else if (!strcmp(argV[parmInd], "-s")
171              ||  !strcmp(argV[parmInd], "-S"))
172        {
173            doSchema = true;
174        }
175         else if (!strcmp(argV[parmInd], "-f")
176              ||  !strcmp(argV[parmInd], "-F"))
177        {
178            schemaFullChecking = true;
179        }
180        else
181        {
182            XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[parmInd]
183                << "', ignoring it\n" << XERCES_STD_QUALIFIER endl;
184        }
185    }
186
187    //
188    //  And now we have to have only one parameter left and it must be
189    //  the file name.
190    //
191    if (parmInd + 1 != argC)
192    {
193        usage();
194        XMLPlatformUtils::Terminate();
195        return 1;
196    }
197    xmlFile = argV[parmInd];
198    int errorCount = 0;
199
200    //
201    //  Create a SAX parser object to use and create our SAX event handlers
202    //  and plug them in.
203    //
204    SAXParser* parser = new SAXParser;
205    PParseHandlers handler;
206    parser->setDocumentHandler(&handler);
207    parser->setErrorHandler(&handler);
208    parser->setValidationScheme(valScheme);
209    parser->setDoNamespaces(doNamespaces);
210    parser->setDoSchema(doSchema);
211    parser->setHandleMultipleImports (true);
212    parser->setValidationSchemaFullChecking(schemaFullChecking);
213
214    //
215    //  Ok, lets do the progressive parse loop. On each time around the
216    //  loop, we look and see if the handler has found what its looking
217    //  for. When it does, we fall out then.
218    //
219    unsigned long duration;
220    int errorCode = 0;
221    try
222    {
223        // Create a progressive scan token
224        XMLPScanToken token;
225
226        const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
227        if (!parser->parseFirst(xmlFile, token))
228        {
229            XERCES_STD_QUALIFIER cerr << "scanFirst() failed\n" << XERCES_STD_QUALIFIER endl;
230            XMLPlatformUtils::Terminate();
231            return 1;
232        }
233
234        //
235        //  We started ok, so lets call scanNext() until we find what we want
236        //  or hit the end.
237        //
238        bool gotMore = true;
239        while (gotMore && !parser->getErrorCount())
240            gotMore = parser->parseNext(token);
241
242        const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
243        duration = endMillis - startMillis;
244
245        errorCount = parser->getErrorCount();
246        //
247        //  Reset the parser-> In this simple progrma, since we just exit
248        //  now, its not technically required. But, in programs which
249        //  would remain open, you should reset after a progressive parse
250        //  in case you broke out before the end of the file. This insures
251        //  that all opened files, sockets, etc... are closed.
252        //
253        parser->parseReset(token);
254    }
255    catch (const OutOfMemoryException&)
256    {
257        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
258        errorCode = 5;
259    }
260    catch (const XMLException& toCatch)
261    {
262        XERCES_STD_QUALIFIER cerr << "\nAn error occurred: '" << xmlFile << "'\n"
263             << "Exception message is: \n"
264             << StrX(toCatch.getMessage())
265             << "\n" << XERCES_STD_QUALIFIER endl;
266        errorCode = 4;
267    }
268
269    if(errorCode) {
270        XMLPlatformUtils::Terminate();
271        return errorCode;
272    }
273
274    if (!errorCount) {
275        XERCES_STD_QUALIFIER cout << xmlFile << ": " << duration << " ms ("
276            << handler.getElementCount() << " elems, "
277            << handler.getAttrCount() << " attrs, "
278            << handler.getSpaceCount() << " spaces, "
279            << handler.getCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
280    }
281
282    //
283    //  Delete the parser itself.  Must be done prior to calling Terminate, below.
284    //
285    delete parser;
286
287    // And call the termination method
288    XMLPlatformUtils::Terminate();
289
290    if (errorCount > 0)
291        return 4;
292    else
293        return 0;
294}
Note: See TracBrowser for help on using the repository browser.