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

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

More path fixes.

File size: 7.9 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: StdInParse.cpp 833057 2009-11-05 15:25:10Z borisk $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26#include <xercesc/framework/StdInInputSource.hpp>
27#include <icxercesc/parsers/SAXParser.hpp>
28#include "StdInParse.hpp"
29#include <xercesc/util/OutOfMemoryException.hpp>
30
31// ---------------------------------------------------------------------------
32//  Local data
33//
34//  doNamespaces
35//      Indicates whether namespace processing should be enabled or not.
36//      The default is no, but -n overrides that.
37//
38//  doSchema
39//      Indicates whether schema processing should be enabled or not.
40//      The default is no, but -s overrides that.
41//
42//  schemaFullChecking
43//      Indicates whether full schema constraint checking should be enabled or not.
44//      The default is no, but -s overrides that.
45//
46//  valScheme
47//      Indicates what validation scheme to use. It defaults to 'auto', but
48//      can be set via the -v= command.
49// ---------------------------------------------------------------------------
50static bool     doNamespaces       = false;
51static bool     doSchema           = false;
52static bool     schemaFullChecking = false;
53static SAXParser::ValSchemes    valScheme       = SAXParser::Val_Auto;
54
55
56
57// ---------------------------------------------------------------------------
58//  Local helper methods
59// ---------------------------------------------------------------------------
60void usage()
61{
62    XERCES_STD_QUALIFIER cout << "\nUsage:\n"
63            "    StdInParse [options] < <XML file>\n\n"
64            "This program demonstrates streaming XML data from standard\n"
65            "input.  It then uses the SAX Parser, and prints the\n"
66            "number of elements, attributes, spaces and characters found\n"
67            "in the input, using SAX API.\n\n"
68            "Options:\n"
69            "    -v=xxx      Validation scheme [always | never | auto*].\n"
70            "    -n          Enable namespace processing. Defaults to off.\n"
71            "    -s          Enable schema processing. Defaults to off.\n"
72            "    -f          Enable full schema constraint checking. Defaults to off.\n"
73                      "    -?          Show this help.\n\n"
74            "  * = Default if not provided explicitly.\n"
75         << XERCES_STD_QUALIFIER endl;
76}
77
78
79// ---------------------------------------------------------------------------
80//  Program entry point
81// ---------------------------------------------------------------------------
82int main(int argC, char* argV[])
83{
84    // Initialize the XML4C system
85    try
86    {
87         XMLPlatformUtils::Initialize();
88    }
89
90    catch (const XMLException& toCatch)
91    {
92         XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
93              << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
94         return 1;
95    }
96
97    int parmInd;
98    for (parmInd = 1; parmInd < argC; parmInd++)
99    {
100        // Break out on first parm not starting with a dash
101        if (argV[parmInd][0] != '-')
102            break;
103
104        // Watch for special case help request
105        if (!strcmp(argV[parmInd], "-?"))
106        {
107            usage();
108            XMLPlatformUtils::Terminate();
109            return 2;
110        }
111         else if (!strncmp(argV[parmInd], "-v=", 3)
112              ||  !strncmp(argV[parmInd], "-V=", 3))
113        {
114            const char* const parm = &argV[parmInd][3];
115
116            if (!strcmp(parm, "never"))
117                valScheme = SAXParser::Val_Never;
118            else if (!strcmp(parm, "auto"))
119                valScheme = SAXParser::Val_Auto;
120            else if (!strcmp(parm, "always"))
121                valScheme = SAXParser::Val_Always;
122            else
123            {
124                XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl;
125                XMLPlatformUtils::Terminate();
126                return 2;
127            }
128        }
129         else if (!strcmp(argV[parmInd], "-n")
130              ||  !strcmp(argV[parmInd], "-N"))
131        {
132            doNamespaces = true;
133        }
134         else if (!strcmp(argV[parmInd], "-s")
135              ||  !strcmp(argV[parmInd], "-S"))
136        {
137            doSchema = true;
138        }
139         else if (!strcmp(argV[parmInd], "-f")
140              ||  !strcmp(argV[parmInd], "-F"))
141        {
142            schemaFullChecking = true;
143        }
144         else
145        {
146            XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[parmInd]
147                 << "', ignoring it\n" << XERCES_STD_QUALIFIER endl;
148        }
149    }
150
151    //
152    //  Create a SAX parser object. Then, according to what we were told on
153    //  the command line, set the options.
154    //
155    SAXParser* parser = new SAXParser;
156    parser->setValidationScheme(valScheme);
157    parser->setDoNamespaces(doNamespaces);
158    parser->setDoSchema(doSchema);
159    parser->setHandleMultipleImports (true);
160    parser->setValidationSchemaFullChecking(schemaFullChecking);
161
162
163    //
164    //  Create our SAX handler object and install it on the parser, as the
165    //  document and error handler. We are responsible for cleaning them
166    //  up, but since its just stack based here, there's nothing special
167    //  to do.
168    //
169    StdInParseHandlers handler;
170    parser->setDocumentHandler(&handler);
171    parser->setErrorHandler(&handler);
172
173    unsigned long duration;
174    int errorCount = 0;
175    // create a faux scope so that 'src' destructor is called before
176    // XMLPlatformUtils::Terminate
177    {
178        //
179        //  Kick off the parse and catch any exceptions. Create a standard
180        //  input input source and tell the parser to parse from that.
181        //
182        StdInInputSource src;
183        try
184        {
185            const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
186            parser->parse(src);
187            const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
188            duration = endMillis - startMillis;
189            errorCount = parser->getErrorCount();
190        }
191        catch (const OutOfMemoryException&)
192        {
193            XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
194            errorCount = 2;
195            return 4;
196        }
197        catch (const XMLException& e)
198        {
199            XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
200                 << StrX(e.getMessage())
201                 << "\n" << XERCES_STD_QUALIFIER endl;
202            errorCount = 1;
203            return 4;
204        }
205
206        // Print out the stats that we collected and time taken
207        if (!errorCount) {
208            XERCES_STD_QUALIFIER cout << StrX(src.getSystemId()) << ": " << duration << " ms ("
209                 << handler.getElementCount() << " elems, "
210                 << handler.getAttrCount() << " attrs, "
211                 << handler.getSpaceCount() << " spaces, "
212                 << handler.getCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
213        }
214    }
215
216    //
217    //  Delete the parser itself.  Must be done prior to calling Terminate, below.
218    //
219    delete parser;
220
221    XMLPlatformUtils::Terminate();
222
223    if (errorCount > 0)
224        return 4;
225    else
226        return 0;
227}
Note: See TracBrowser for help on using the repository browser.