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: StdInParse.cpp,v $
|
---|
19 | * Revision 1.17 2004/09/08 13:55:34 peiyongz
|
---|
20 | * Apache License Version 2.0
|
---|
21 | *
|
---|
22 | * Revision 1.16 2004/09/02 14:59:30 cargilld
|
---|
23 | * Add OutOfMemoryException block to samples.
|
---|
24 | *
|
---|
25 | * Revision 1.15 2003/05/30 09:36:36 gareth
|
---|
26 | * Use new macros for iostream.h and std:: issues.
|
---|
27 | *
|
---|
28 | * Revision 1.14 2003/05/16 15:32:12 knoaman
|
---|
29 | * Change scope of 'src' so that it's deallocated properly.
|
---|
30 | *
|
---|
31 | * Revision 1.13 2002/02/01 22:41:37 peiyongz
|
---|
32 | * sane_include
|
---|
33 | *
|
---|
34 | * Revision 1.12 2001/10/25 15:18:33 tng
|
---|
35 | * delete the parser before XMLPlatformUtils::Terminate.
|
---|
36 | *
|
---|
37 | * Revision 1.11 2001/10/19 19:02:43 tng
|
---|
38 | * [Bug 3909] return non-zero an exit code when error was encounted.
|
---|
39 | * And other modification for consistent help display and return code across samples.
|
---|
40 | *
|
---|
41 | * Revision 1.10 2001/08/01 19:11:01 tng
|
---|
42 | * Add full schema constraint checking flag to the samples and the parser.
|
---|
43 | *
|
---|
44 | * Revision 1.9 2001/05/11 13:24:59 tng
|
---|
45 | * Copyright update.
|
---|
46 | *
|
---|
47 | * Revision 1.8 2001/05/03 16:00:32 tng
|
---|
48 | * Schema: samples update with schema
|
---|
49 | *
|
---|
50 | * Revision 1.7 2001/02/22 20:59:57 tng
|
---|
51 | * [Bug 678] StdInParse doesn't output filename or duration
|
---|
52 | *
|
---|
53 | * Revision 1.6 2000/06/20 02:23:10 rahulj
|
---|
54 | * Help message added by Joe Polastre.
|
---|
55 | *
|
---|
56 | * Revision 1.5 2000/03/02 19:53:50 roddey
|
---|
57 | * This checkin includes many changes done while waiting for the
|
---|
58 | * 1.1.0 code to be finished. I can't list them all here, but a list is
|
---|
59 | * available elsewhere.
|
---|
60 | *
|
---|
61 | * Revision 1.4 2000/02/11 02:40:58 abagchi
|
---|
62 | * Removed StrX::transcode
|
---|
63 | *
|
---|
64 | * Revision 1.3 2000/02/06 07:47:25 rahulj
|
---|
65 | * Year 2K copyright swat.
|
---|
66 | *
|
---|
67 | * Revision 1.2 2000/01/12 00:27:01 roddey
|
---|
68 | * Updates to work with the new URL and input source scheme.
|
---|
69 | *
|
---|
70 | * Revision 1.1.1.1 1999/11/09 01:09:27 twl
|
---|
71 | * Initial checkin
|
---|
72 | *
|
---|
73 | * Revision 1.5 1999/11/08 20:43:43 rahul
|
---|
74 | * Swat for adding in Product name and CVS comment log variable.
|
---|
75 | *
|
---|
76 | */
|
---|
77 |
|
---|
78 |
|
---|
79 | // ---------------------------------------------------------------------------
|
---|
80 | // Includes
|
---|
81 | // ---------------------------------------------------------------------------
|
---|
82 | #include <xercesc/framework/StdInInputSource.hpp>
|
---|
83 | #include <xercesc/parsers/SAXParser.hpp>
|
---|
84 | #include "StdInParse.hpp"
|
---|
85 | #include <xercesc/util/OutOfMemoryException.hpp>
|
---|
86 |
|
---|
87 | // ---------------------------------------------------------------------------
|
---|
88 | // Local data
|
---|
89 | //
|
---|
90 | // doNamespaces
|
---|
91 | // Indicates whether namespace processing should be enabled or not.
|
---|
92 | // The default is no, but -n overrides that.
|
---|
93 | //
|
---|
94 | // doSchema
|
---|
95 | // Indicates whether schema processing should be enabled or not.
|
---|
96 | // The default is no, but -s overrides that.
|
---|
97 | //
|
---|
98 | // schemaFullChecking
|
---|
99 | // Indicates whether full schema constraint checking should be enabled or not.
|
---|
100 | // The default is no, but -s overrides that.
|
---|
101 | //
|
---|
102 | // valScheme
|
---|
103 | // Indicates what validation scheme to use. It defaults to 'auto', but
|
---|
104 | // can be set via the -v= command.
|
---|
105 | // ---------------------------------------------------------------------------
|
---|
106 | static bool doNamespaces = false;
|
---|
107 | static bool doSchema = false;
|
---|
108 | static bool schemaFullChecking = false;
|
---|
109 | static SAXParser::ValSchemes valScheme = SAXParser::Val_Auto;
|
---|
110 |
|
---|
111 |
|
---|
112 |
|
---|
113 | // ---------------------------------------------------------------------------
|
---|
114 | // Local helper methods
|
---|
115 | // ---------------------------------------------------------------------------
|
---|
116 | void usage()
|
---|
117 | {
|
---|
118 | XERCES_STD_QUALIFIER cout << "\nUsage:\n"
|
---|
119 | " StdInParse [options] < <XML file>\n\n"
|
---|
120 | "This program demonstrates streaming XML data from standard\n"
|
---|
121 | "input. It then uses the SAX Parser, and prints the\n"
|
---|
122 | "number of elements, attributes, spaces and characters found\n"
|
---|
123 | "in the input, using SAX API.\n\n"
|
---|
124 | "Options:\n"
|
---|
125 | " -v=xxx Validation scheme [always | never | auto*].\n"
|
---|
126 | " -n Enable namespace processing. Defaults to off.\n"
|
---|
127 | " -s Enable schema processing. Defaults to off.\n"
|
---|
128 | " -f Enable full schema constraint checking. Defaults to off.\n"
|
---|
129 | " -? Show this help.\n\n"
|
---|
130 | " * = Default if not provided explicitly.\n"
|
---|
131 | << XERCES_STD_QUALIFIER endl;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | // ---------------------------------------------------------------------------
|
---|
136 | // Program entry point
|
---|
137 | // ---------------------------------------------------------------------------
|
---|
138 | int main(int argC, char* argV[])
|
---|
139 | {
|
---|
140 | // Initialize the XML4C system
|
---|
141 | try
|
---|
142 | {
|
---|
143 | XMLPlatformUtils::Initialize();
|
---|
144 | }
|
---|
145 |
|
---|
146 | catch (const XMLException& toCatch)
|
---|
147 | {
|
---|
148 | XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
|
---|
149 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
150 | return 1;
|
---|
151 | }
|
---|
152 |
|
---|
153 | int parmInd;
|
---|
154 | for (parmInd = 1; parmInd < argC; parmInd++)
|
---|
155 | {
|
---|
156 | // Break out on first parm not starting with a dash
|
---|
157 | if (argV[parmInd][0] != '-')
|
---|
158 | break;
|
---|
159 |
|
---|
160 | // Watch for special case help request
|
---|
161 | if (!strcmp(argV[parmInd], "-?"))
|
---|
162 | {
|
---|
163 | usage();
|
---|
164 | XMLPlatformUtils::Terminate();
|
---|
165 | return 2;
|
---|
166 | }
|
---|
167 | else if (!strncmp(argV[parmInd], "-v=", 3)
|
---|
168 | || !strncmp(argV[parmInd], "-V=", 3))
|
---|
169 | {
|
---|
170 | const char* const parm = &argV[parmInd][3];
|
---|
171 |
|
---|
172 | if (!strcmp(parm, "never"))
|
---|
173 | valScheme = SAXParser::Val_Never;
|
---|
174 | else if (!strcmp(parm, "auto"))
|
---|
175 | valScheme = SAXParser::Val_Auto;
|
---|
176 | else if (!strcmp(parm, "always"))
|
---|
177 | valScheme = SAXParser::Val_Always;
|
---|
178 | else
|
---|
179 | {
|
---|
180 | XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl;
|
---|
181 | XMLPlatformUtils::Terminate();
|
---|
182 | return 2;
|
---|
183 | }
|
---|
184 | }
|
---|
185 | else if (!strcmp(argV[parmInd], "-n")
|
---|
186 | || !strcmp(argV[parmInd], "-N"))
|
---|
187 | {
|
---|
188 | doNamespaces = true;
|
---|
189 | }
|
---|
190 | else if (!strcmp(argV[parmInd], "-s")
|
---|
191 | || !strcmp(argV[parmInd], "-S"))
|
---|
192 | {
|
---|
193 | doSchema = true;
|
---|
194 | }
|
---|
195 | else if (!strcmp(argV[parmInd], "-f")
|
---|
196 | || !strcmp(argV[parmInd], "-F"))
|
---|
197 | {
|
---|
198 | schemaFullChecking = true;
|
---|
199 | }
|
---|
200 | else
|
---|
201 | {
|
---|
202 | XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[parmInd]
|
---|
203 | << "', ignoring it\n" << XERCES_STD_QUALIFIER endl;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | //
|
---|
208 | // Create a SAX parser object. Then, according to what we were told on
|
---|
209 | // the command line, set the options.
|
---|
210 | //
|
---|
211 | SAXParser* parser = new SAXParser;
|
---|
212 | parser->setValidationScheme(valScheme);
|
---|
213 | parser->setDoNamespaces(doNamespaces);
|
---|
214 | parser->setDoSchema(doSchema);
|
---|
215 | parser->setValidationSchemaFullChecking(schemaFullChecking);
|
---|
216 |
|
---|
217 |
|
---|
218 | //
|
---|
219 | // Create our SAX handler object and install it on the parser, as the
|
---|
220 | // document and error handler. We are responsible for cleaning them
|
---|
221 | // up, but since its just stack based here, there's nothing special
|
---|
222 | // to do.
|
---|
223 | //
|
---|
224 | StdInParseHandlers handler;
|
---|
225 | parser->setDocumentHandler(&handler);
|
---|
226 | parser->setErrorHandler(&handler);
|
---|
227 |
|
---|
228 | unsigned long duration;
|
---|
229 | int errorCount = 0;
|
---|
230 | // create a faux scope so that 'src' destructor is called before
|
---|
231 | // XMLPlatformUtils::Terminate
|
---|
232 | {
|
---|
233 | //
|
---|
234 | // Kick off the parse and catch any exceptions. Create a standard
|
---|
235 | // input input source and tell the parser to parse from that.
|
---|
236 | //
|
---|
237 | StdInInputSource src;
|
---|
238 | try
|
---|
239 | {
|
---|
240 | const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
241 | parser->parse(src);
|
---|
242 | const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
243 | duration = endMillis - startMillis;
|
---|
244 | errorCount = parser->getErrorCount();
|
---|
245 | }
|
---|
246 | catch (const OutOfMemoryException&)
|
---|
247 | {
|
---|
248 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
249 | errorCount = 2;
|
---|
250 | return 4;
|
---|
251 | }
|
---|
252 | catch (const XMLException& e)
|
---|
253 | {
|
---|
254 | XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
|
---|
255 | << StrX(e.getMessage())
|
---|
256 | << "\n" << XERCES_STD_QUALIFIER endl;
|
---|
257 | errorCount = 1;
|
---|
258 | return 4;
|
---|
259 | }
|
---|
260 |
|
---|
261 | // Print out the stats that we collected and time taken
|
---|
262 | if (!errorCount) {
|
---|
263 | XERCES_STD_QUALIFIER cout << StrX(src.getSystemId()) << ": " << duration << " ms ("
|
---|
264 | << handler.getElementCount() << " elems, "
|
---|
265 | << handler.getAttrCount() << " attrs, "
|
---|
266 | << handler.getSpaceCount() << " spaces, "
|
---|
267 | << handler.getCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | //
|
---|
272 | // Delete the parser itself. Must be done prior to calling Terminate, below.
|
---|
273 | //
|
---|
274 | delete parser;
|
---|
275 |
|
---|
276 | XMLPlatformUtils::Terminate();
|
---|
277 |
|
---|
278 | if (errorCount > 0)
|
---|
279 | return 4;
|
---|
280 | else
|
---|
281 | return 0;
|
---|
282 | }
|
---|
283 |
|
---|