source: NonGTP/Xerces/xerces/samples/SAXPrint/SAXPrint.cpp @ 358

Revision 358, 12.1 KB checked in by bittner, 19 years ago (diff)

xerces added

Line 
1/*
2 * Copyright 1999-2001,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * $Log: SAXPrint.cpp,v $
19 * Revision 1.25  2004/09/08 13:55:34  peiyongz
20 * Apache License Version 2.0
21 *
22 * Revision 1.24  2004/09/02 14:59:29  cargilld
23 * Add OutOfMemoryException block to samples.
24 *
25 * Revision 1.23  2004/02/06 15:04:16  cargilld
26 * Misc 390 changes.
27 *
28 * Revision 1.22  2003/08/07 21:21:38  neilg
29 * fix segmentation faults that may arise when the parser throws exceptions during document parsing.  In general, XMLPlatformUtils::Terminate() should not be called from within a catch statement.
30 *
31 * Revision 1.21  2003/05/30 09:36:36  gareth
32 * Use new macros for iostream.h and std:: issues.
33 *
34 * Revision 1.20  2002/04/17 20:18:08  tng
35 * [Bug 7493] The word "occured" is misspelled and it is a global error.
36 *
37 * Revision 1.19  2002/02/01 22:41:17  peiyongz
38 * sane_include
39 *
40 * Revision 1.18  2001/10/29 17:02:57  tng
41 * Fix typo in samples.
42 *
43 * Revision 1.17  2001/10/25 15:18:33  tng
44 * delete the parser before XMLPlatformUtils::Terminate.
45 *
46 * Revision 1.16  2001/10/19 19:02:43  tng
47 * [Bug 3909] return non-zero an exit code when error was encounted.
48 * And other modification for consistent help display and return code across samples.
49 *
50 * Revision 1.15  2001/08/01 19:11:01  tng
51 * Add full schema constraint checking flag to the samples and the parser.
52 *
53 * Revision 1.14  2001/05/11 13:24:58  tng
54 * Copyright update.
55 *
56 * Revision 1.13  2001/05/03 16:00:21  tng
57 * Schema: samples update with schema
58 *
59 * Revision 1.12  2000/06/16 20:25:43  rahulj
60 * Add the -v=always option to force validation checking. Need this
61 * option for running the conformance tests.
62 *
63 * Revision 1.11  2000/05/31 18:36:26  rahulj
64 * Matched the command line options supported by DOMPrint.
65 *
66 * Revision 1.10  2000/04/12 22:58:27  roddey
67 * Added support for 'auto validate' mode.
68 *
69 * Revision 1.9  2000/04/06 19:09:51  roddey
70 * Some more improvements to output formatting. Now it will correctly
71 * handle doing the 'replacement char' style of dealing with chars
72 * that are unrepresentable.
73 *
74 * Revision 1.8  2000/04/05 00:20:32  roddey
75 * More updates for the low level formatted output support
76 *
77 * Revision 1.7  2000/03/28 19:43:11  roddey
78 * Fixes for signed/unsigned warnings. New work for two way transcoding
79 * stuff.
80 *
81 * Revision 1.6  2000/03/03 01:29:31  roddey
82 * Added a scanReset()/parseReset() method to the scanner and
83 * parsers, to allow for reset after early exit from a progressive parse.
84 * Added calls to new Terminate() call to all of the samples. Improved
85 * documentation in SAX and DOM parsers.
86 *
87 * Revision 1.5  2000/03/02 19:53:49  roddey
88 * This checkin includes many changes done while waiting for the
89 * 1.1.0 code to be finished. I can't list them all here, but a list is
90 * available elsewhere.
91 *
92 * Revision 1.4  2000/02/11 02:39:43  abagchi
93 * Removed StrX::transcode
94 *
95 * Revision 1.3  2000/02/06 07:47:24  rahulj
96 * Year 2K copyright swat.
97 *
98 * Revision 1.2  2000/01/12 00:27:01  roddey
99 * Updates to work with the new URL and input source scheme.
100 *
101 * Revision 1.1.1.1  1999/11/09 01:09:28  twl
102 * Initial checkin
103 *
104 * Revision 1.7  1999/11/08 20:43:41  rahul
105 * Swat for adding in Product name and CVS comment log variable.
106 *
107 */
108
109
110// ---------------------------------------------------------------------------
111//  Includes
112// ---------------------------------------------------------------------------
113#include <xercesc/util/PlatformUtils.hpp>
114#include <xercesc/util/TransService.hpp>
115#include <xercesc/parsers/SAXParser.hpp>
116#include "SAXPrint.hpp"
117#include <xercesc/util/OutOfMemoryException.hpp>
118
119// ---------------------------------------------------------------------------
120//  Local data
121//
122//  doNamespaces
123//      Indicates whether namespace processing should be enabled or not.
124//      Defaults to disabled.
125//
126//  doSchema
127//      Indicates whether schema processing should be enabled or not.
128//      Defaults to disabled.
129//
130//  schemaFullChecking
131//      Indicates whether full schema constraint checking should be enabled or not.
132//      Defaults to disabled.
133//
134//  encodingName
135//      The encoding we are to output in. If not set on the command line,
136//      then it is defaulted to LATIN1.
137//
138//  xmlFile
139//      The path to the file to parser. Set via command line.
140//
141//  valScheme
142//      Indicates what validation scheme to use. It defaults to 'auto', but
143//      can be set via the -v= command.
144// ---------------------------------------------------------------------------
145static bool                     doNamespaces        = false;
146static bool                     doSchema            = false;
147static bool                     schemaFullChecking  = false;
148static const char*              encodingName    = "LATIN1";
149static XMLFormatter::UnRepFlags unRepFlags      = XMLFormatter::UnRep_CharRef;
150static char*                    xmlFile         = 0;
151static SAXParser::ValSchemes    valScheme       = SAXParser::Val_Auto;
152
153
154
155// ---------------------------------------------------------------------------
156//  Local helper methods
157// ---------------------------------------------------------------------------
158static void usage()
159{
160    XERCES_STD_QUALIFIER cout << "\nUsage:\n"
161            "    SAXPrint [options] <XML file>\n\n"
162            "This program invokes the SAX Parser, and then prints the\n"
163            "data returned by the various SAX handlers for the specified\n"
164            "XML file.\n\n"
165            "Options:\n"
166             "    -u=xxx      Handle unrepresentable chars [fail | rep | ref*].\n"
167             "    -v=xxx      Validation scheme [always | never | auto*].\n"
168             "    -n          Enable namespace processing.\n"
169             "    -s          Enable schema processing.\n"
170             "    -f          Enable full schema constraint checking.\n"
171             "    -x=XXX      Use a particular encoding for output (LATIN1*).\n"
172             "    -?          Show this help.\n\n"
173             "  * = Default if not provided explicitly.\n\n"
174             "The parser has intrinsic support for the following encodings:\n"
175             "    UTF-8, USASCII, ISO8859-1, UTF-16[BL]E, UCS-4[BL]E,\n"
176             "    WINDOWS-1252, IBM1140, IBM037, IBM1047.\n"
177         <<  XERCES_STD_QUALIFIER endl;
178}
179
180
181
182// ---------------------------------------------------------------------------
183//  Program entry point
184// ---------------------------------------------------------------------------
185int main(int argC, char* argV[])
186{
187    // Initialize the XML4C2 system
188    try
189    {
190         XMLPlatformUtils::Initialize();
191    }
192
193    catch (const XMLException& toCatch)
194    {
195         XERCES_STD_QUALIFIER cerr << "Error during initialization! :\n"
196              << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
197         return 1;
198    }
199
200    // Check command line and extract arguments.
201    if (argC < 2)
202    {
203        usage();
204        XMLPlatformUtils::Terminate();
205        return 1;
206    }
207
208    int parmInd;
209    for (parmInd = 1; parmInd < argC; parmInd++)
210    {
211        // Break out on first parm not starting with a dash
212        if (argV[parmInd][0] != '-')
213            break;
214
215        // Watch for special case help request
216        if (!strcmp(argV[parmInd], "-?"))
217        {
218            usage();
219            XMLPlatformUtils::Terminate();
220            return 2;
221        }
222         else if (!strncmp(argV[parmInd], "-v=", 3)
223              ||  !strncmp(argV[parmInd], "-V=", 3))
224        {
225            const char* const parm = &argV[parmInd][3];
226
227            if (!strcmp(parm, "never"))
228                valScheme = SAXParser::Val_Never;
229            else if (!strcmp(parm, "auto"))
230                valScheme = SAXParser::Val_Auto;
231            else if (!strcmp(parm, "always"))
232                valScheme = SAXParser::Val_Always;
233            else
234            {
235                XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl;
236                XMLPlatformUtils::Terminate();
237                return 2;
238            }
239        }
240         else if (!strcmp(argV[parmInd], "-n")
241              ||  !strcmp(argV[parmInd], "-N"))
242        {
243            doNamespaces = true;
244        }
245         else if (!strcmp(argV[parmInd], "-s")
246              ||  !strcmp(argV[parmInd], "-S"))
247        {
248            doSchema = true;
249        }
250         else if (!strcmp(argV[parmInd], "-f")
251              ||  !strcmp(argV[parmInd], "-F"))
252        {
253            schemaFullChecking = true;
254        }
255         else if (!strncmp(argV[parmInd], "-x=", 3)
256              ||  !strncmp(argV[parmInd], "-X=", 3))
257        {
258            // Get out the encoding name
259            encodingName = &argV[parmInd][3];
260        }
261         else if (!strncmp(argV[parmInd], "-u=", 3)
262              ||  !strncmp(argV[parmInd], "-U=", 3))
263        {
264            const char* const parm = &argV[parmInd][3];
265
266            if (!strcmp(parm, "fail"))
267                unRepFlags = XMLFormatter::UnRep_Fail;
268            else if (!strcmp(parm, "rep"))
269                unRepFlags = XMLFormatter::UnRep_Replace;
270            else if (!strcmp(parm, "ref"))
271                unRepFlags = XMLFormatter::UnRep_CharRef;
272            else
273            {
274                XERCES_STD_QUALIFIER cerr << "Unknown -u= value: " << parm << XERCES_STD_QUALIFIER endl;
275                XMLPlatformUtils::Terminate();
276                return 2;
277            }
278        }
279         else
280        {
281            XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[parmInd]
282                 << "', ignoring it\n" << XERCES_STD_QUALIFIER endl;
283        }
284    }
285
286    //
287    //  And now we have to have only one parameter left and it must be
288    //  the file name.
289    //
290    if (parmInd + 1 != argC)
291    {
292        usage();
293        XMLPlatformUtils::Terminate();
294        return 1;
295    }
296    xmlFile = argV[parmInd];
297    int errorCount = 0;
298
299    //
300    //  Create a SAX parser object. Then, according to what we were told on
301    //  the command line, set it to validate or not.
302    //
303    SAXParser* parser = new SAXParser;
304    parser->setValidationScheme(valScheme);
305    parser->setDoNamespaces(doNamespaces);
306    parser->setDoSchema(doSchema);
307    parser->setValidationSchemaFullChecking(schemaFullChecking);
308
309    //
310    //  Create the handler object and install it as the document and error
311    //  handler for the parser-> Then parse the file and catch any exceptions
312    //  that propogate out
313    //
314    int errorCode = 0;
315    try
316    {
317        SAXPrintHandlers handler(encodingName, unRepFlags);
318        parser->setDocumentHandler(&handler);
319        parser->setErrorHandler(&handler);
320        parser->parse(xmlFile);
321        errorCount = parser->getErrorCount();
322    }
323    catch (const OutOfMemoryException&)
324    {
325        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
326        errorCode = 5;
327    }
328    catch (const XMLException& toCatch)
329    {
330        XERCES_STD_QUALIFIER cerr << "\nAn error occurred\n  Error: "
331             << StrX(toCatch.getMessage())
332             << "\n" << XERCES_STD_QUALIFIER endl;
333        errorCode = 4;
334    }
335    if(errorCode) {
336        XMLPlatformUtils::Terminate();
337        return errorCode;
338    }
339
340    //
341    //  Delete the parser itself.  Must be done prior to calling Terminate, below.
342    //
343    delete parser;
344
345    // And call the termination method
346    XMLPlatformUtils::Terminate();
347
348    if (errorCount > 0)
349        return 4;
350    else
351        return 0;
352}
353
Note: See TracBrowser for help on using the repository browser.