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

Revision 358, 12.4 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: SAX2Print.cpp,v $
19 * Revision 1.17  2004/09/08 13:55:33  peiyongz
20 * Apache License Version 2.0
21 *
22 * Revision 1.16  2004/09/02 14:59:29  cargilld
23 * Add OutOfMemoryException block to samples.
24 *
25 * Revision 1.15  2004/02/06 15:04:16  cargilld
26 * Misc 390 changes.
27 *
28 * Revision 1.14  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.13  2003/05/30 09:36:36  gareth
32 * Use new macros for iostream.h and std:: issues.
33 *
34 * Revision 1.12  2002/06/17 15:33:00  tng
35 * Name Xerces features as XMLUni::fgXercesXXXX instead of XMLUni::fgSAX2XercesXXXX so that they can be shared with DOM parser.
36 *
37 * Revision 1.11  2002/05/28 20:20:26  tng
38 * Add option '-n' to SAX2Print.
39 *
40 * Revision 1.10  2002/04/17 20:18:08  tng
41 * [Bug 7493] The word "occured" is misspelled and it is a global error.
42 *
43 * Revision 1.9  2002/02/13 16:11:06  knoaman
44 * Update samples to use SAX2 features/properties constants from XMLUni.
45 *
46 * Revision 1.8  2002/02/06 16:36:51  knoaman
47 * Added a new flag '-p' to SAX2 samples to set the 'namespace-prefixes' feature.
48 *
49 * Revision 1.7  2002/02/01 22:40:44  peiyongz
50 * sane_include
51 *
52 * Revision 1.6  2001/10/25 15:18:33  tng
53 * delete the parser before XMLPlatformUtils::Terminate.
54 *
55 * Revision 1.5  2001/10/19 19:02:43  tng
56 * [Bug 3909] return non-zero an exit code when error was encounted.
57 * And other modification for consistent help display and return code across samples.
58 *
59 * Revision 1.4  2001/08/02 17:10:29  tng
60 * Allow DOMCount/SAXCount/IDOMCount/SAX2Count to take a file that has a list of xml file as input.
61 *
62 * Revision 1.3  2001/08/01 19:11:01  tng
63 * Add full schema constraint checking flag to the samples and the parser.
64 *
65 * Revision 1.2  2000/08/09 22:20:38  jpolast
66 * updates for changes to sax2 core functionality.
67 *
68 * Revision 1.1  2000/08/02 19:16:14  jpolast
69 * initial checkin of SAX2Print
70 *
71 *
72 */
73
74
75// ---------------------------------------------------------------------------
76//  Includes
77// ---------------------------------------------------------------------------
78#include <xercesc/util/PlatformUtils.hpp>
79#include <xercesc/util/TransService.hpp>
80#include <xercesc/sax2/SAX2XMLReader.hpp>
81#include <xercesc/sax2/XMLReaderFactory.hpp>
82#include "SAX2Print.hpp"
83#include <xercesc/util/OutOfMemoryException.hpp>
84
85// ---------------------------------------------------------------------------
86//  Local data
87//
88//  encodingName
89//      The encoding we are to output in. If not set on the command line,
90//      then it is defaulted to LATIN1.
91//
92//  xmlFile
93//      The path to the file to parser. Set via command line.
94//
95//  valScheme
96//      Indicates what validation scheme to use. It defaults to 'auto', but
97//      can be set via the -v= command.
98//
99//      expandNamespaces
100//              Indicates if the output should expand the namespaces Alias with
101//              their URI's, defaults to false, can be set via the command line -e
102// ---------------------------------------------------------------------------
103static const char*              encodingName    = "LATIN1";
104static XMLFormatter::UnRepFlags unRepFlags      = XMLFormatter::UnRep_CharRef;
105static char*                    xmlFile         = 0;
106static SAX2XMLReader::ValSchemes valScheme      = SAX2XMLReader::Val_Auto;
107static bool                                             expandNamespaces= false ;
108static bool                     doNamespaces    = true;
109static bool                     doSchema        = true;
110static bool                     schemaFullChecking = false;
111static bool                     namespacePrefixes = false;
112
113
114// ---------------------------------------------------------------------------
115//  Local helper methods
116// ---------------------------------------------------------------------------
117static void usage()
118{
119    XERCES_STD_QUALIFIER cout << "\nUsage:\n"
120            "    SAX2Print [options] <XML file>\n\n"
121            "This program invokes the SAX2XMLReader, and then prints the\n"
122            "data returned by the various SAX2 handlers for the specified\n"
123            "XML file.\n\n"
124            "Options:\n"
125             "    -u=xxx      Handle unrepresentable chars [fail | rep | ref*].\n"
126             "    -v=xxx      Validation scheme [always | never | auto*].\n"
127             "    -e          Expand Namespace Alias with URI's. Defaults to off.\n"
128             "    -x=XXX      Use a particular encoding for output (LATIN1*).\n"
129             "    -f          Enable full schema constraint checking processing. Defaults to off.\n"
130             "    -p          Enable namespace-prefixes feature. Defaults to off.\n"
131             "    -n          Disable namespace processing. Defaults to on.\n"
132             "                NOTE: THIS IS OPPOSITE FROM OTHER SAMPLES.\n"
133             "    -s          Disable schema processing. Defaults to on.\n"
134             "                NOTE: THIS IS OPPOSITE FROM OTHER SAMPLES.\n"
135             "    -?          Show this help.\n\n"
136             "  * = Default if not provided explicitly.\n\n"
137             "The parser has intrinsic support for the following encodings:\n"
138             "    UTF-8, USASCII, ISO8859-1, UTF-16[BL]E, UCS-4[BL]E,\n"
139             "    WINDOWS-1252, IBM1140, IBM037, IBM1047.\n"
140         <<  XERCES_STD_QUALIFIER endl;
141}
142
143
144
145// ---------------------------------------------------------------------------
146//  Program entry point
147// ---------------------------------------------------------------------------
148int main(int argC, char* argV[])
149{
150    // Initialize the XML4C2 system
151    try
152    {
153         XMLPlatformUtils::Initialize();
154    }
155
156    catch (const XMLException& toCatch)
157    {
158         XERCES_STD_QUALIFIER cerr << "Error during initialization! :\n"
159              << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
160         return 1;
161    }
162
163    // Check command line and extract arguments.
164    if (argC < 2)
165    {
166        usage();
167        XMLPlatformUtils::Terminate();
168        return 1;
169    }
170
171    int parmInd;
172    for (parmInd = 1; parmInd < argC; parmInd++)
173    {
174        // Break out on first parm not starting with a dash
175        if (argV[parmInd][0] != '-')
176            break;
177
178        // Watch for special case help request
179        if (!strcmp(argV[parmInd], "-?"))
180        {
181            usage();
182            XMLPlatformUtils::Terminate();
183            return 2;
184        }
185         else if (!strncmp(argV[parmInd], "-v=", 3)
186              ||  !strncmp(argV[parmInd], "-V=", 3))
187        {
188            const char* const parm = &argV[parmInd][3];
189
190            if (!strcmp(parm, "never"))
191                valScheme = SAX2XMLReader::Val_Never;
192            else if (!strcmp(parm, "auto"))
193                valScheme = SAX2XMLReader::Val_Auto;
194            else if (!strcmp(parm, "always"))
195                valScheme = SAX2XMLReader::Val_Always;
196            else
197            {
198                XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl;
199                XMLPlatformUtils::Terminate();
200                return 2;
201            }
202        }
203         else if (!strcmp(argV[parmInd], "-e")
204              ||  !strcmp(argV[parmInd], "-E"))
205        {
206            expandNamespaces = true;
207        }
208         else if (!strncmp(argV[parmInd], "-x=", 3)
209              ||  !strncmp(argV[parmInd], "-X=", 3))
210        {
211            // Get out the encoding name
212            encodingName = &argV[parmInd][3];
213        }
214         else if (!strncmp(argV[parmInd], "-u=", 3)
215              ||  !strncmp(argV[parmInd], "-U=", 3))
216        {
217            const char* const parm = &argV[parmInd][3];
218
219            if (!strcmp(parm, "fail"))
220                unRepFlags = XMLFormatter::UnRep_Fail;
221            else if (!strcmp(parm, "rep"))
222                unRepFlags = XMLFormatter::UnRep_Replace;
223            else if (!strcmp(parm, "ref"))
224                unRepFlags = XMLFormatter::UnRep_CharRef;
225            else
226            {
227                XERCES_STD_QUALIFIER cerr << "Unknown -u= value: " << parm << XERCES_STD_QUALIFIER endl;
228                XMLPlatformUtils::Terminate();
229                return 2;
230            }
231        }
232         else if (!strcmp(argV[parmInd], "-n")
233              ||  !strcmp(argV[parmInd], "-N"))
234        {
235            doNamespaces = false;
236        }
237         else if (!strcmp(argV[parmInd], "-s")
238              ||  !strcmp(argV[parmInd], "-S"))
239        {
240            doSchema = false;
241        }
242         else if (!strcmp(argV[parmInd], "-f")
243              ||  !strcmp(argV[parmInd], "-F"))
244        {
245            schemaFullChecking = true;
246        }
247         else if (!strcmp(argV[parmInd], "-p")
248              ||  !strcmp(argV[parmInd], "-P"))
249        {
250            namespacePrefixes = true;
251        }
252         else
253        {
254            XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[parmInd]
255                 << "', ignoring it\n" << XERCES_STD_QUALIFIER endl;
256        }
257    }
258
259    //
260    //  And now we have to have only one parameter left and it must be
261    //  the file name.
262    //
263    if (parmInd + 1 != argC)
264    {
265        usage();
266        XMLPlatformUtils::Terminate();
267        return 1;
268    }
269    xmlFile = argV[parmInd];
270
271    //
272    //  Create a SAX parser object. Then, according to what we were told on
273    //  the command line, set it to validate or not.
274    //
275    SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
276
277    //
278    //  Then, according to what we were told on
279    //  the command line, set it to validate or not.
280    //
281    if (valScheme == SAX2XMLReader::Val_Auto)
282    {
283        parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
284        parser->setFeature(XMLUni::fgXercesDynamic, true);
285    }
286
287    if (valScheme == SAX2XMLReader::Val_Never)
288    {
289        parser->setFeature(XMLUni::fgSAX2CoreValidation, false);
290    }
291
292    if (valScheme == SAX2XMLReader::Val_Always)
293    {
294        parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
295        parser->setFeature(XMLUni::fgXercesDynamic, false);
296    }
297
298    parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, doNamespaces);
299    parser->setFeature(XMLUni::fgXercesSchema, doSchema);
300    parser->setFeature(XMLUni::fgXercesSchemaFullChecking, schemaFullChecking);
301    parser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes, namespacePrefixes);
302
303    //
304    //  Create the handler object and install it as the document and error
305    //  handler for the parser. Then parse the file and catch any exceptions
306    //  that propogate out
307    //
308
309    int errorCount = 0;
310    int errorCode = 0;
311    try
312    {
313        SAX2PrintHandlers handler(encodingName, unRepFlags, expandNamespaces);
314        parser->setContentHandler(&handler);
315        parser->setErrorHandler(&handler);
316        parser->parse(xmlFile);
317        errorCount = parser->getErrorCount();
318    }
319    catch (const OutOfMemoryException&)
320    {
321        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
322        errorCode = 5;         
323    }
324    catch (const XMLException& toCatch)
325    {
326        XERCES_STD_QUALIFIER cerr << "\nAn error occurred\n  Error: "
327             << StrX(toCatch.getMessage())
328             << "\n" << XERCES_STD_QUALIFIER endl;
329        errorCode = 4;
330    }
331
332    if(errorCode) {
333        XMLPlatformUtils::Terminate();
334        return errorCode;
335    }
336
337    //
338    //  Delete the parser itself.  Must be done prior to calling Terminate, below.
339    //
340    delete parser;
341
342    // And call the termination method
343    XMLPlatformUtils::Terminate();
344
345    if (errorCount > 0)
346        return 4;
347    else
348        return 0;
349}
350
Note: See TracBrowser for help on using the repository browser.