[358] | 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: PParse.cpp,v $
|
---|
| 19 | * Revision 1.18 2004/09/08 13:55:32 peiyongz
|
---|
| 20 | * Apache License Version 2.0
|
---|
| 21 | *
|
---|
| 22 | * Revision 1.17 2004/09/02 14:59:29 cargilld
|
---|
| 23 | * Add OutOfMemoryException block to samples.
|
---|
| 24 | *
|
---|
| 25 | * Revision 1.16 2003/08/07 21:21:38 neilg
|
---|
| 26 | * 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.
|
---|
| 27 | *
|
---|
| 28 | * Revision 1.15 2003/05/30 09:36:35 gareth
|
---|
| 29 | * Use new macros for iostream.h and std:: issues.
|
---|
| 30 | *
|
---|
| 31 | * Revision 1.14 2002/04/17 20:18:08 tng
|
---|
| 32 | * [Bug 7493] The word "occured" is misspelled and it is a global error.
|
---|
| 33 | *
|
---|
| 34 | * Revision 1.13 2002/02/01 22:37:38 peiyongz
|
---|
| 35 | * sane_include
|
---|
| 36 | *
|
---|
| 37 | * Revision 1.12 2001/10/25 15:18:33 tng
|
---|
| 38 | * delete the parser before XMLPlatformUtils::Terminate.
|
---|
| 39 | *
|
---|
| 40 | * Revision 1.11 2001/10/19 18:52:04 tng
|
---|
| 41 | * Since PParse can take any XML file as input file, it shouldn't hardcode to expect 16 elements.
|
---|
| 42 | * Change it to work similar to SAXCount which just prints the number of elements, characters, attributes ... etc.
|
---|
| 43 | * And other modification for consistent help display and return code across samples.
|
---|
| 44 | *
|
---|
| 45 | * Revision 1.10 2001/08/01 19:11:01 tng
|
---|
| 46 | * Add full schema constraint checking flag to the samples and the parser.
|
---|
| 47 | *
|
---|
| 48 | * Revision 1.9 2001/05/11 13:24:55 tng
|
---|
| 49 | * Copyright update.
|
---|
| 50 | *
|
---|
| 51 | * Revision 1.8 2001/05/03 15:59:48 tng
|
---|
| 52 | * Schema: samples update with schema
|
---|
| 53 | *
|
---|
| 54 | * Revision 1.7 2000/06/20 02:23:08 rahulj
|
---|
| 55 | * Help message added by Joe Polastre.
|
---|
| 56 | *
|
---|
| 57 | * Revision 1.6 2000/03/03 01:29:31 roddey
|
---|
| 58 | * Added a scanReset()/parseReset() method to the scanner and
|
---|
| 59 | * parsers, to allow for reset after early exit from a progressive parse.
|
---|
| 60 | * Added calls to new Terminate() call to all of the samples. Improved
|
---|
| 61 | * documentation in SAX and DOM parsers.
|
---|
| 62 | *
|
---|
| 63 | * Revision 1.5 2000/03/02 19:53:44 roddey
|
---|
| 64 | * This checkin includes many changes done while waiting for the
|
---|
| 65 | * 1.1.0 code to be finished. I can't list them all here, but a list is
|
---|
| 66 | * available elsewhere.
|
---|
| 67 | *
|
---|
| 68 | * Revision 1.4 2000/02/11 02:37:48 abagchi
|
---|
| 69 | * Removed StrX::transcode
|
---|
| 70 | *
|
---|
| 71 | * Revision 1.3 2000/02/06 07:47:20 rahulj
|
---|
| 72 | * Year 2K copyright swat.
|
---|
| 73 | *
|
---|
| 74 | * Revision 1.2 2000/01/12 00:27:00 roddey
|
---|
| 75 | * Updates to work with the new URL and input source scheme.
|
---|
| 76 | *
|
---|
| 77 | * Revision 1.1.1.1 1999/11/09 01:09:45 twl
|
---|
| 78 | * Initial checkin
|
---|
| 79 | *
|
---|
| 80 | * Revision 1.5 1999/11/08 20:43:38 rahul
|
---|
| 81 | * Swat for adding in Product name and CVS comment log variable.
|
---|
| 82 | *
|
---|
| 83 | */
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | // ---------------------------------------------------------------------------
|
---|
| 87 | // This sample program demonstrates the progressive parse capabilities of
|
---|
| 88 | // the parser system. It allows you to do a scanFirst() call followed by
|
---|
| 89 | // a loop which calls scanNext(). You can drop out when you've found what
|
---|
| 90 | // ever it is you want. In our little test, our event handler looks for
|
---|
| 91 | // 16 new elements then sets a flag to indicate its found what it wants.
|
---|
| 92 | // At that point, our progressive parse loop below exits.
|
---|
| 93 | //
|
---|
| 94 | // The parameters are:
|
---|
| 95 | //
|
---|
| 96 | // [-?] - Show usage and exit
|
---|
| 97 | // [-v=xxx] - Validation scheme [always | never | auto*]
|
---|
| 98 | // [-n] - Enable namespace processing
|
---|
| 99 | // [-s] - Enable schema processing
|
---|
| 100 | // [-f] - Enable full schema constraint checking
|
---|
| 101 | // filename - The path to the XML file to parse
|
---|
| 102 | //
|
---|
| 103 | // * = Default if not provided explicitly
|
---|
| 104 | // These are non-case sensitive
|
---|
| 105 | // ---------------------------------------------------------------------------
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | // ---------------------------------------------------------------------------
|
---|
| 109 | // Includes
|
---|
| 110 | // ---------------------------------------------------------------------------
|
---|
| 111 | #include <xercesc/util/PlatformUtils.hpp>
|
---|
| 112 | #include <xercesc/framework/XMLPScanToken.hpp>
|
---|
| 113 | #include <xercesc/parsers/SAXParser.hpp>
|
---|
| 114 | #include "PParse.hpp"
|
---|
| 115 | #include <xercesc/util/OutOfMemoryException.hpp>
|
---|
| 116 |
|
---|
| 117 | // ---------------------------------------------------------------------------
|
---|
| 118 | // Local data
|
---|
| 119 | //
|
---|
| 120 | // xmlFile
|
---|
| 121 | // The path to the file to parser. Set via command line.
|
---|
| 122 | //
|
---|
| 123 | // doNamespaces
|
---|
| 124 | // Indicates whether namespace processing should be done.
|
---|
| 125 | //
|
---|
| 126 | // doSchema
|
---|
| 127 | // Indicates whether schema processing should be done.
|
---|
| 128 | //
|
---|
| 129 | // schemaFullChecking
|
---|
| 130 | // Indicates whether full schema constraint checking should be done.
|
---|
| 131 | //
|
---|
| 132 | // valScheme
|
---|
| 133 | // Indicates what validation scheme to use. It defaults to 'auto', but
|
---|
| 134 | // can be set via the -v= command.
|
---|
| 135 | // ---------------------------------------------------------------------------
|
---|
| 136 | static char* xmlFile = 0;
|
---|
| 137 | static bool doNamespaces = false;
|
---|
| 138 | static bool doSchema = false;
|
---|
| 139 | static bool schemaFullChecking = false;
|
---|
| 140 | static SAXParser::ValSchemes valScheme = SAXParser::Val_Auto;
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 | // ---------------------------------------------------------------------------
|
---|
| 145 | // Local helper methods
|
---|
| 146 | // ---------------------------------------------------------------------------
|
---|
| 147 | static void usage()
|
---|
| 148 | {
|
---|
| 149 | XERCES_STD_QUALIFIER cout << "\nUsage:\n"
|
---|
| 150 | " PParse [options] <XML file>\n\n"
|
---|
| 151 | "This program demonstrates the progressive parse capabilities of\n"
|
---|
| 152 | "the parser system. It allows you to do a scanFirst() call followed by\n"
|
---|
| 153 | "a loop which calls scanNext(). You can drop out when you've found what\n"
|
---|
| 154 | "ever it is you want. In our little test, our event handler looks for\n"
|
---|
| 155 | "16 new elements then sets a flag to indicate its found what it wants.\n"
|
---|
| 156 | "At that point, our progressive parse loop exits.\n\n"
|
---|
| 157 | "Options:\n"
|
---|
| 158 | " -v=xxx - Validation scheme [always | never | auto*].\n"
|
---|
| 159 | " -n - Enable namespace processing [default is off].\n"
|
---|
| 160 | " -s - Enable schema processing [default is off].\n"
|
---|
| 161 | " -f - Enable full schema constraint checking [default is off].\n"
|
---|
| 162 | " -? - Show this help.\n\n"
|
---|
| 163 | " * = Default if not provided explicitly.\n"
|
---|
| 164 | << XERCES_STD_QUALIFIER endl;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 | // ---------------------------------------------------------------------------
|
---|
| 170 | // Program entry point
|
---|
| 171 | // ---------------------------------------------------------------------------
|
---|
| 172 | int main(int argC, char* argV[])
|
---|
| 173 | {
|
---|
| 174 | // Initialize the XML4C system
|
---|
| 175 | try
|
---|
| 176 | {
|
---|
| 177 | XMLPlatformUtils::Initialize();
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | catch (const XMLException& toCatch)
|
---|
| 181 | {
|
---|
| 182 | XERCES_STD_QUALIFIER cerr << "Error during initialization! :\n"
|
---|
| 183 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
| 184 | return 1;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | // Check command line and extract arguments.
|
---|
| 188 | if (argC < 2)
|
---|
| 189 | {
|
---|
| 190 | usage();
|
---|
| 191 | XMLPlatformUtils::Terminate();
|
---|
| 192 | return 1;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | // See if non validating dom parser configuration is requested.
|
---|
| 196 | int parmInd;
|
---|
| 197 | for (parmInd = 1; parmInd < argC; parmInd++)
|
---|
| 198 | {
|
---|
| 199 | // Break out on first parm not starting with a dash
|
---|
| 200 | if (argV[parmInd][0] != '-')
|
---|
| 201 | break;
|
---|
| 202 |
|
---|
| 203 | // Watch for special case help request
|
---|
| 204 | if (!strcmp(argV[parmInd], "-?"))
|
---|
| 205 | {
|
---|
| 206 | usage();
|
---|
| 207 | XMLPlatformUtils::Terminate();
|
---|
| 208 | return 2;
|
---|
| 209 | }
|
---|
| 210 | else if (!strncmp(argV[parmInd], "-v=", 3)
|
---|
| 211 | || !strncmp(argV[parmInd], "-V=", 3))
|
---|
| 212 | {
|
---|
| 213 | const char* const parm = &argV[parmInd][3];
|
---|
| 214 |
|
---|
| 215 | if (!strcmp(parm, "never"))
|
---|
| 216 | valScheme = SAXParser::Val_Never;
|
---|
| 217 | else if (!strcmp(parm, "auto"))
|
---|
| 218 | valScheme = SAXParser::Val_Auto;
|
---|
| 219 | else if (!strcmp(parm, "always"))
|
---|
| 220 | valScheme = SAXParser::Val_Always;
|
---|
| 221 | else
|
---|
| 222 | {
|
---|
| 223 | XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl;
|
---|
| 224 | XMLPlatformUtils::Terminate();
|
---|
| 225 | return 2;
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 | else if (!strcmp(argV[parmInd], "-n")
|
---|
| 229 | || !strcmp(argV[parmInd], "-N"))
|
---|
| 230 | {
|
---|
| 231 | doNamespaces = true;
|
---|
| 232 | }
|
---|
| 233 | else if (!strcmp(argV[parmInd], "-s")
|
---|
| 234 | || !strcmp(argV[parmInd], "-S"))
|
---|
| 235 | {
|
---|
| 236 | doSchema = true;
|
---|
| 237 | }
|
---|
| 238 | else if (!strcmp(argV[parmInd], "-f")
|
---|
| 239 | || !strcmp(argV[parmInd], "-F"))
|
---|
| 240 | {
|
---|
| 241 | schemaFullChecking = true;
|
---|
| 242 | }
|
---|
| 243 | else
|
---|
| 244 | {
|
---|
| 245 | XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[parmInd]
|
---|
| 246 | << "', ignoring it\n" << XERCES_STD_QUALIFIER endl;
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | //
|
---|
| 251 | // And now we have to have only one parameter left and it must be
|
---|
| 252 | // the file name.
|
---|
| 253 | //
|
---|
| 254 | if (parmInd + 1 != argC)
|
---|
| 255 | {
|
---|
| 256 | usage();
|
---|
| 257 | XMLPlatformUtils::Terminate();
|
---|
| 258 | return 1;
|
---|
| 259 | }
|
---|
| 260 | xmlFile = argV[parmInd];
|
---|
| 261 | int errorCount = 0;
|
---|
| 262 |
|
---|
| 263 | //
|
---|
| 264 | // Create a SAX parser object to use and create our SAX event handlers
|
---|
| 265 | // and plug them in.
|
---|
| 266 | //
|
---|
| 267 | SAXParser* parser = new SAXParser;
|
---|
| 268 | PParseHandlers handler;
|
---|
| 269 | parser->setDocumentHandler(&handler);
|
---|
| 270 | parser->setErrorHandler(&handler);
|
---|
| 271 | parser->setValidationScheme(valScheme);
|
---|
| 272 | parser->setDoNamespaces(doNamespaces);
|
---|
| 273 | parser->setDoSchema(doSchema);
|
---|
| 274 | parser->setValidationSchemaFullChecking(schemaFullChecking);
|
---|
| 275 |
|
---|
| 276 | //
|
---|
| 277 | // Ok, lets do the progressive parse loop. On each time around the
|
---|
| 278 | // loop, we look and see if the handler has found what its looking
|
---|
| 279 | // for. When it does, we fall out then.
|
---|
| 280 | //
|
---|
| 281 | unsigned long duration;
|
---|
| 282 | int errorCode = 0;
|
---|
| 283 | try
|
---|
| 284 | {
|
---|
| 285 | // Create a progressive scan token
|
---|
| 286 | XMLPScanToken token;
|
---|
| 287 |
|
---|
| 288 | const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
| 289 | if (!parser->parseFirst(xmlFile, token))
|
---|
| 290 | {
|
---|
| 291 | XERCES_STD_QUALIFIER cerr << "scanFirst() failed\n" << XERCES_STD_QUALIFIER endl;
|
---|
| 292 | XMLPlatformUtils::Terminate();
|
---|
| 293 | return 1;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | //
|
---|
| 297 | // We started ok, so lets call scanNext() until we find what we want
|
---|
| 298 | // or hit the end.
|
---|
| 299 | //
|
---|
| 300 | bool gotMore = true;
|
---|
| 301 | while (gotMore && !parser->getErrorCount())
|
---|
| 302 | gotMore = parser->parseNext(token);
|
---|
| 303 |
|
---|
| 304 | const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
| 305 | duration = endMillis - startMillis;
|
---|
| 306 |
|
---|
| 307 | errorCount = parser->getErrorCount();
|
---|
| 308 | //
|
---|
| 309 | // Reset the parser-> In this simple progrma, since we just exit
|
---|
| 310 | // now, its not technically required. But, in programs which
|
---|
| 311 | // would remain open, you should reset after a progressive parse
|
---|
| 312 | // in case you broke out before the end of the file. This insures
|
---|
| 313 | // that all opened files, sockets, etc... are closed.
|
---|
| 314 | //
|
---|
| 315 | parser->parseReset(token);
|
---|
| 316 | }
|
---|
| 317 | catch (const OutOfMemoryException&)
|
---|
| 318 | {
|
---|
| 319 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
| 320 | errorCode = 5;
|
---|
| 321 | }
|
---|
| 322 | catch (const XMLException& toCatch)
|
---|
| 323 | {
|
---|
| 324 | XERCES_STD_QUALIFIER cerr << "\nAn error occurred: '" << xmlFile << "'\n"
|
---|
| 325 | << "Exception message is: \n"
|
---|
| 326 | << StrX(toCatch.getMessage())
|
---|
| 327 | << "\n" << XERCES_STD_QUALIFIER endl;
|
---|
| 328 | errorCode = 4;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | if(errorCode) {
|
---|
| 332 | XMLPlatformUtils::Terminate();
|
---|
| 333 | return errorCode;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | if (!errorCount) {
|
---|
| 337 | XERCES_STD_QUALIFIER cout << xmlFile << ": " << duration << " ms ("
|
---|
| 338 | << handler.getElementCount() << " elems, "
|
---|
| 339 | << handler.getAttrCount() << " attrs, "
|
---|
| 340 | << handler.getSpaceCount() << " spaces, "
|
---|
| 341 | << handler.getCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | //
|
---|
| 345 | // Delete the parser itself. Must be done prior to calling Terminate, below.
|
---|
| 346 | //
|
---|
| 347 | delete parser;
|
---|
| 348 |
|
---|
| 349 | // And call the termination method
|
---|
| 350 | XMLPlatformUtils::Terminate();
|
---|
| 351 |
|
---|
| 352 | if (errorCount > 0)
|
---|
| 353 | return 4;
|
---|
| 354 | else
|
---|
| 355 | return 0;
|
---|
| 356 | }
|
---|
| 357 |
|
---|