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

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

More path fixes.

File size: 8.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: SAXPrint.cpp 833057 2009-11-05 15:25:10Z borisk $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26#include <icxercesc/util/PlatformUtils.hpp>
27#include <icxercesc/util/TransService.hpp>
28#include <icxercesc/parsers/SAXParser.hpp>
29#include "SAXPrint.hpp"
30#include <xercesc/util/OutOfMemoryException.hpp>
31
32// ---------------------------------------------------------------------------
33//  Local data
34//
35//  doNamespaces
36//      Indicates whether namespace processing should be enabled or not.
37//      Defaults to disabled.
38//
39//  doSchema
40//      Indicates whether schema processing should be enabled or not.
41//      Defaults to disabled.
42//
43//  schemaFullChecking
44//      Indicates whether full schema constraint checking should be enabled or not.
45//      Defaults to disabled.
46//
47//  encodingName
48//      The encoding we are to output in. If not set on the command line,
49//      then it is defaulted to LATIN1.
50//
51//  xmlFile
52//      The path to the file to parser. Set via command line.
53//
54//  valScheme
55//      Indicates what validation scheme to use. It defaults to 'auto', but
56//      can be set via the -v= command.
57// ---------------------------------------------------------------------------
58static bool                     doNamespaces        = false;
59static bool                     doSchema            = false;
60static bool                     schemaFullChecking  = false;
61static const char*              encodingName    = "LATIN1";
62static XMLFormatter::UnRepFlags unRepFlags      = XMLFormatter::UnRep_CharRef;
63static char*                    xmlFile         = 0;
64static SAXParser::ValSchemes    valScheme       = SAXParser::Val_Auto;
65
66
67
68// ---------------------------------------------------------------------------
69//  Local helper methods
70// ---------------------------------------------------------------------------
71static void usage()
72{
73    XERCES_STD_QUALIFIER cout << "\nUsage:\n"
74            "    SAXPrint [options] <XML file>\n\n"
75            "This program invokes the SAX Parser, and then prints the\n"
76            "data returned by the various SAX handlers for the specified\n"
77            "XML file.\n\n"
78            "Options:\n"
79             "    -u=xxx      Handle unrepresentable chars [fail | rep | ref*].\n"
80             "    -v=xxx      Validation scheme [always | never | auto*].\n"
81             "    -n          Enable namespace processing.\n"
82             "    -s          Enable schema processing.\n"
83             "    -f          Enable full schema constraint checking.\n"
84             "    -x=XXX      Use a particular encoding for output (LATIN1*).\n"
85             "    -?          Show this help.\n\n"
86             "  * = Default if not provided explicitly.\n\n"
87             "The parser has intrinsic support for the following encodings:\n"
88             "    UTF-8, US-ASCII, ISO8859-1, UTF-16[BL]E, UCS-4[BL]E,\n"
89             "    WINDOWS-1252, IBM1140, IBM037, IBM1047.\n"
90         <<  XERCES_STD_QUALIFIER endl;
91}
92
93
94
95// ---------------------------------------------------------------------------
96//  Program entry point
97// ---------------------------------------------------------------------------
98int main(int argC, char* argV[])
99{
100    // Initialize the XML4C2 system
101    try
102    {
103         XMLPlatformUtils::Initialize();
104    }
105
106    catch (const XMLException& toCatch)
107    {
108         XERCES_STD_QUALIFIER cerr << "Error during initialization! :\n"
109              << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
110         return 1;
111    }
112
113    // Check command line and extract arguments.
114    if (argC < 2)
115    {
116        usage();
117        XMLPlatformUtils::Terminate();
118        return 1;
119    }
120
121    int parmInd;
122    for (parmInd = 1; parmInd < argC; parmInd++)
123    {
124        // Break out on first parm not starting with a dash
125        if (argV[parmInd][0] != '-')
126            break;
127
128        // Watch for special case help request
129        if (!strcmp(argV[parmInd], "-?"))
130        {
131            usage();
132            XMLPlatformUtils::Terminate();
133            return 2;
134        }
135         else if (!strncmp(argV[parmInd], "-v=", 3)
136              ||  !strncmp(argV[parmInd], "-V=", 3))
137        {
138            const char* const parm = &argV[parmInd][3];
139
140            if (!strcmp(parm, "never"))
141                valScheme = SAXParser::Val_Never;
142            else if (!strcmp(parm, "auto"))
143                valScheme = SAXParser::Val_Auto;
144            else if (!strcmp(parm, "always"))
145                valScheme = SAXParser::Val_Always;
146            else
147            {
148                XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl;
149                XMLPlatformUtils::Terminate();
150                return 2;
151            }
152        }
153         else if (!strcmp(argV[parmInd], "-n")
154              ||  !strcmp(argV[parmInd], "-N"))
155        {
156            doNamespaces = true;
157        }
158         else if (!strcmp(argV[parmInd], "-s")
159              ||  !strcmp(argV[parmInd], "-S"))
160        {
161            doSchema = true;
162        }
163         else if (!strcmp(argV[parmInd], "-f")
164              ||  !strcmp(argV[parmInd], "-F"))
165        {
166            schemaFullChecking = true;
167        }
168         else if (!strncmp(argV[parmInd], "-x=", 3)
169              ||  !strncmp(argV[parmInd], "-X=", 3))
170        {
171            // Get out the encoding name
172            encodingName = &argV[parmInd][3];
173        }
174         else if (!strncmp(argV[parmInd], "-u=", 3)
175              ||  !strncmp(argV[parmInd], "-U=", 3))
176        {
177            const char* const parm = &argV[parmInd][3];
178
179            if (!strcmp(parm, "fail"))
180                unRepFlags = XMLFormatter::UnRep_Fail;
181            else if (!strcmp(parm, "rep"))
182                unRepFlags = XMLFormatter::UnRep_Replace;
183            else if (!strcmp(parm, "ref"))
184                unRepFlags = XMLFormatter::UnRep_CharRef;
185            else
186            {
187                XERCES_STD_QUALIFIER cerr << "Unknown -u= value: " << parm << XERCES_STD_QUALIFIER endl;
188                XMLPlatformUtils::Terminate();
189                return 2;
190            }
191        }
192         else
193        {
194            XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[parmInd]
195                 << "', ignoring it\n" << XERCES_STD_QUALIFIER endl;
196        }
197    }
198
199    //
200    //  And now we have to have only one parameter left and it must be
201    //  the file name.
202    //
203    if (parmInd + 1 != argC)
204    {
205        usage();
206        XMLPlatformUtils::Terminate();
207        return 1;
208    }
209    xmlFile = argV[parmInd];
210    int errorCount = 0;
211
212    //
213    //  Create a SAX parser object. Then, according to what we were told on
214    //  the command line, set it to validate or not.
215    //
216    SAXParser* parser = new SAXParser;
217    parser->setValidationScheme(valScheme);
218    parser->setDoNamespaces(doNamespaces);
219    parser->setDoSchema(doSchema);
220    parser->setHandleMultipleImports (true);
221    parser->setValidationSchemaFullChecking(schemaFullChecking);
222
223    //
224    //  Create the handler object and install it as the document and error
225    //  handler for the parser-> Then parse the file and catch any exceptions
226    //  that propogate out
227    //
228    int errorCode = 0;
229    try
230    {
231        SAXPrintHandlers handler(encodingName, unRepFlags);
232        parser->setDocumentHandler(&handler);
233        parser->setErrorHandler(&handler);
234        parser->parse(xmlFile);
235        errorCount = parser->getErrorCount();
236    }
237    catch (const OutOfMemoryException&)
238    {
239        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
240        errorCode = 5;
241    }
242    catch (const XMLException& toCatch)
243    {
244        XERCES_STD_QUALIFIER cerr << "\nAn error occurred\n  Error: "
245             << StrX(toCatch.getMessage())
246             << "\n" << XERCES_STD_QUALIFIER endl;
247        errorCode = 4;
248    }
249    if(errorCode) {
250        XMLPlatformUtils::Terminate();
251        return errorCode;
252    }
253
254    //
255    //  Delete the parser itself.  Must be done prior to calling Terminate, below.
256    //
257    delete parser;
258
259    // And call the termination method
260    XMLPlatformUtils::Terminate();
261
262    if (errorCount > 0)
263        return 4;
264    else
265        return 0;
266}
Note: See TracBrowser for help on using the repository browser.