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

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

More path fixes.

File size: 9.5 KB
RevLine 
[2726]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/*
20 * $Id: MemParse.cpp 833057 2009-11-05 15:25:10Z borisk $
21 */
22
23
24/**
25 * This sample program illustrates how one can use a memory buffer as the
26 * input to parser. The memory buffer contains raw bytes representing XML
27 * statements.
28 *
29 * Look at the API documentation for 'MemBufInputSource' for more information
30 * on parameters to the constructor.
31 *
32 */
33
34
35// ---------------------------------------------------------------------------
36//  Includes
37// ---------------------------------------------------------------------------
[2733]38#include <icxercesc/parsers/SAXParser.hpp>
[2726]39#include <xercesc/framework/MemBufInputSource.hpp>
40#include "MemParse.hpp"
41#include <xercesc/util/OutOfMemoryException.hpp>
42
43// ---------------------------------------------------------------------------
44//  Local const data
45//
46//  gXMLInMemBuf
47//      Defines the memory buffer contents here which parsed by the XML
48//      parser. This is the cheap way to do it, instead of reading it from
49//      somewhere. For this demo, its fine.
50//
51//      NOTE: If your encoding is not ascii you will need to change
52//            the MEMPARSE_ENCODING #define
53//
54//  gMemBufId
55//      A simple name to give as the system id for the memory buffer. This
56//      just for indentification purposes in case of errors. Its not a real
57//      system id (and the parser knows that.)
58// ---------------------------------------------------------------------------
59
60#ifndef MEMPARSE_ENCODING
61   #if defined(OS390)
62      #define MEMPARSE_ENCODING "ibm-1047-s390"
63   #elif defined(OS400)
64      #define MEMPARSE_ENCODING "ibm037"
65   #else
66      #define MEMPARSE_ENCODING "ascii"
67   #endif
68#endif /* ifndef MEMPARSE_ENCODING */
69
70static const char*  gXMLInMemBuf =
71"\
72<?xml version='1.0' encoding='" MEMPARSE_ENCODING "'?>\n\
73<!DOCTYPE company [\n\
74<!ELEMENT company     (product,category,developedAt)>\n\
75<!ELEMENT product     (#PCDATA)>\n\
76<!ELEMENT category    (#PCDATA)>\n\
77<!ATTLIST category idea CDATA #IMPLIED>\n\
78<!ELEMENT developedAt (#PCDATA)>\n\
79]>\n\n\
80<company>\n\
81    <product>XML4C</product>\n\
82    <category idea='great'>XML Parsing Tools</category>\n\
83    <developedAt>\n\
84      IBM Center for Java Technology, Silicon Valley, Cupertino, CA\n\
85    </developedAt>\n\
86</company>\
87";
88
89static const char*  gMemBufId = "prodInfo";
90
91
92
93// ---------------------------------------------------------------------------
94//  Local helper methods
95// ---------------------------------------------------------------------------
96void usage()
97{
98    XERCES_STD_QUALIFIER cout << "\nUsage:\n"
99            "    MemParse [options]\n\n"
100            "This program uses the SAX Parser to parse a memory buffer\n"
101            "containing XML statements, and reports the number of\n"
102            "elements and attributes found.\n\n"
103            "Options:\n"
104            "    -v=xxx      Validation scheme [always | never | auto*].\n"
105            "    -n          Enable namespace processing. Defaults to off.\n"
106            "    -s          Enable schema processing. Defaults to off.\n"
107            "    -f          Enable full schema constraint checking. Defaults to off.\n"
108                      "    -?          Show this help.\n\n"
109            "  * = Default if not provided explicitly.\n"
110         << XERCES_STD_QUALIFIER endl;
111}
112
113
114// ---------------------------------------------------------------------------
115//  Program entry point
116// ---------------------------------------------------------------------------
117int main(int argC, char* argV[])
118{
119    // Initialize the XML4C2 system
120    try
121    {
122         XMLPlatformUtils::Initialize();
123    }
124    catch (const XMLException& toCatch)
125    {
126         XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
127              << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
128         return 1;
129    }
130
131    SAXParser::ValSchemes    valScheme = SAXParser::Val_Auto;
132    bool doNamespaces       = false;
133    bool doSchema           = false;
134    bool schemaFullChecking = false;
135
136    int argInd;
137    for (argInd = 1; argInd < argC; argInd++)
138    {
139        // Break out on first parm not starting with a dash
140        if (argV[argInd][0] != '-')
141        {
142            usage();
143            XMLPlatformUtils::Terminate();
144            return 1;
145        }
146
147        // Watch for special case help request
148        if (!strcmp(argV[argInd], "-?"))
149        {
150            usage();
151            XMLPlatformUtils::Terminate();
152            return 1;
153        }
154         else if (!strncmp(argV[argInd], "-v=", 3)
155              ||  !strncmp(argV[argInd], "-V=", 3))
156        {
157            const char* const parm = &argV[argInd][3];
158
159            if (!strcmp(parm, "never"))
160                valScheme = SAXParser::Val_Never;
161            else if (!strcmp(parm, "auto"))
162                valScheme = SAXParser::Val_Auto;
163            else if (!strcmp(parm, "always"))
164                valScheme = SAXParser::Val_Always;
165            else
166            {
167                XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl;
168                return 2;
169            }
170        }
171         else if (!strcmp(argV[argInd], "-n")
172              ||  !strcmp(argV[argInd], "-N"))
173        {
174            doNamespaces = true;
175        }
176         else if (!strcmp(argV[argInd], "-s")
177              ||  !strcmp(argV[argInd], "-S"))
178        {
179            doSchema = true;
180        }
181         else if (!strcmp(argV[argInd], "-f")
182              ||  !strcmp(argV[argInd], "-F"))
183        {
184            schemaFullChecking = true;
185        }
186         else
187        {
188            XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[argInd]
189                 << "', ignoring it\n" << XERCES_STD_QUALIFIER endl;
190        }
191    }
192
193    //
194    //  Create a SAX parser object. Then, according to what we were told on
195    //  the command line, set it to validate or not.
196    //
197    SAXParser* parser = new SAXParser;
198    parser->setValidationScheme(valScheme);
199    parser->setDoNamespaces(doNamespaces);
200    parser->setDoSchema(doSchema);
201    parser->setHandleMultipleImports (true);
202    parser->setValidationSchemaFullChecking(schemaFullChecking);
203
204    //
205    //  Create our SAX handler object and install it on the parser, as the
206    //  document and error handlers.
207    //
208    MemParseHandlers handler;
209    parser->setDocumentHandler(&handler);
210    parser->setErrorHandler(&handler);
211
212    //
213    //  Create MemBufferInputSource from the buffer containing the XML
214    //  statements.
215    //
216    //  NOTE: We are using strlen() here, since we know that the chars in
217    //  our hard coded buffer are single byte chars!!! The parameter wants
218    //  the number of BYTES, not chars, so when you create a memory buffer
219    //  give it the byte size (which just happens to be the same here.)
220    //
221    MemBufInputSource* memBufIS = new MemBufInputSource
222    (
223        (const XMLByte*)gXMLInMemBuf
224        , strlen(gXMLInMemBuf)
225        , gMemBufId
226        , false
227    );
228
229    //
230    //  Get the starting time and kick off the parse of the indicated
231    //  file. Catch any exceptions that might propogate out of it.
232    //
233    unsigned long duration;
234    int errorCount = 0;
235    int errorCode = 0;
236    try
237    {
238        const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
239        parser->parse(*memBufIS);
240        const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
241        duration = endMillis - startMillis;
242        errorCount = parser->getErrorCount();
243    }
244    catch (const OutOfMemoryException&)
245    {
246        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
247        errorCode = 5;
248    }
249    catch (const XMLException& e)
250    {
251        XERCES_STD_QUALIFIER cerr << "\nError during parsing memory stream:\n"
252             << "Exception message is:  \n"
253             << StrX(e.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl;
254        errorCode = 4;
255    }
256    if(errorCode) {
257        XMLPlatformUtils::Terminate();
258        return errorCode;
259    }
260
261    // Print out the stats that we collected and time taken.
262    if (!errorCount) {
263        XERCES_STD_QUALIFIER cout << "\nFinished parsing the memory buffer containing the following "
264             << "XML statements:\n\n"
265             << gXMLInMemBuf
266             << "\n\n\n"
267             << "Parsing took " << duration << " ms ("
268             << handler.getElementCount() << " elements, "
269             << handler.getAttrCount() << " attributes, "
270             << handler.getSpaceCount() << " spaces, "
271             << handler.getCharacterCount() << " characters).\n" << XERCES_STD_QUALIFIER endl;
272    }
273
274    //
275    //  Delete the parser itself.  Must be done prior to calling Terminate, below.
276    //
277    delete parser;
278
279    delete memBufIS;
280
281    // And call the termination method
282    XMLPlatformUtils::Terminate();
283
284    if (errorCount > 0)
285        return 4;
286    else
287        return 0;
288}
Note: See TracBrowser for help on using the repository browser.