PageRenderTime 34ms CodeModel.GetById 13ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/DetectorDescription/RegressionTest/test/simple_dom_parser.cpp

https://github.com/aivanov-cern/cmssw
C++ | 316 lines | 229 code | 38 blank | 49 comment | 52 complexity | 89faf934b87b88851150384b5c9c09ef MD5 | raw file
  1#include "DetectorDescription/RegressionTest/src/SaxToDom.h"
  2#include "DetectorDescription/RegressionTest/src/TinyDomTest.h"
  3#include "DetectorDescription/RegressionTest/src/StrX.h"
  4
  5#include "FWCore/Concurrency/interface/Xerces.h"
  6#include <xercesc/sax2/SAX2XMLReader.hpp>
  7#include <xercesc/sax2/XMLReaderFactory.hpp>
  8#include <fstream>
  9#include <map>
 10#include <stdlib.h>
 11
 12using namespace std;
 13
 14class ADummy
 15{
 16
 17};
 18
 19// ---------------------------------------------------------------------------
 20//  Local helper methods
 21// ---------------------------------------------------------------------------
 22void usage()
 23{
 24    cout << "\nUsage:\n"
 25            "    SAX2Count [options] <XML file | List file>\n\n"
 26            "This program invokes the SAX2XMLReader, and then prints the\n"
 27            "number of elements, attributes, spaces and characters found\n"
 28            "in each XML file, using SAX2 API.\n\n"
 29            "Options:\n"
 30            "    -l          Indicate the input file is a List File that has a list of xml files.\n"
 31            "                Default to off (Input file is an XML file).\n"
 32            "    -v=xxx      Validation scheme [always | never | auto*].\n"
 33            "    -f          Enable full schema constraint checking processing. Defaults to off.\n"
 34            "    -p          Enable namespace-prefixes feature. Defaults to off.\n"
 35            "    -n          Disable namespace processing. Defaults to on.\n"
 36            "                NOTE: THIS IS OPPOSITE FROM OTHER SAMPLES.\n"
 37            "    -s          Disable schema processing. Defaults to on.\n"
 38            "                NOTE: THIS IS OPPOSITE FROM OTHER SAMPLES.\n"
 39		      "    -?          Show this help.\n\n"
 40            "  * = Default if not provided explicitly.\n"
 41         << endl;
 42}
 43
 44// ---------------------------------------------------------------------------
 45//  Program entry point
 46// ---------------------------------------------------------------------------
 47int main(int argC, char* argV[])
 48{
 49    // Initialize the XML4C2 system
 50    try
 51    {
 52        cms::concurrency::xercesInitialize();
 53    }
 54
 55    catch (const XMLException& toCatch)
 56    {
 57        cerr << "Error during initialization! Message:\n"
 58            << StrX(toCatch.getMessage()) << endl;
 59        return 1;
 60    }
 61
 62    // Check command line and extract arguments.
 63    if (argC < 2)
 64    {
 65        usage();
 66        cms::concurrency::xercesTerminate();
 67        return 1;
 68    }
 69
 70    const char*                  xmlFile      = 0;
 71    SAX2XMLReader::ValSchemes    valScheme    = SAX2XMLReader::Val_Auto;
 72    bool                         doNamespaces = true;
 73    bool                         doSchema = true;
 74    bool                         schemaFullChecking = false;
 75    bool                         doList = false;
 76    bool                         errorOccurred = false;
 77    bool                         namespacePrefixes = false;
 78
 79    int argInd;
 80    for (argInd = 1; argInd < argC; ++argInd)
 81    {
 82        // Break out on first parm not starting with a dash
 83        if (argV[argInd][0] != '-')
 84            break;
 85
 86        // Watch for special case help request
 87        if (!strcmp(argV[argInd], "-?"))
 88        {
 89            usage();
 90            cms::concurrency::xercesTerminate();
 91            return 2;
 92        }
 93         else if (!strncmp(argV[argInd], "-v=", 3)
 94              ||  !strncmp(argV[argInd], "-V=", 3))
 95        {
 96            const char* const parm = &argV[argInd][3];
 97
 98            if (!strcmp(parm, "never"))
 99                valScheme = SAX2XMLReader::Val_Never;
100            else if (!strcmp(parm, "auto"))
101                valScheme = SAX2XMLReader::Val_Auto;
102            else if (!strcmp(parm, "always"))
103                valScheme = SAX2XMLReader::Val_Always;
104            else
105            {
106                cerr << "Unknown -v= value: " << parm << endl;
107                cms::concurrency::xercesTerminate();
108                return 2;
109            }
110        }
111         else if (!strcmp(argV[argInd], "-n")
112              ||  !strcmp(argV[argInd], "-N"))
113        {
114            doNamespaces = false;
115        }
116         else if (!strcmp(argV[argInd], "-s")
117              ||  !strcmp(argV[argInd], "-S"))
118        {
119            doSchema = false;
120        }
121         else if (!strcmp(argV[argInd], "-f")
122              ||  !strcmp(argV[argInd], "-F"))
123        {
124            schemaFullChecking = true;
125        }
126         else if (!strcmp(argV[argInd], "-l")
127              ||  !strcmp(argV[argInd], "-L"))
128        {
129            doList = true;
130        }
131         else if (!strcmp(argV[argInd], "-p")
132              ||  !strcmp(argV[argInd], "-P"))
133        {
134            namespacePrefixes = true;
135        }
136         else if (!strcmp(argV[argInd], "-special:nel"))
137        {
138            // turning this on will lead to non-standard compliance behaviour
139            // it will recognize the unicode character 0x85 as new line character
140            // instead of regular character as specified in XML 1.0
141            // do not turn this on unless really necessary
142            XMLPlatformUtils::recognizeNEL(true);
143        }
144        else
145        {
146            cerr << "Unknown option '" << argV[argInd]
147                << "', ignoring it\n" << endl;
148        }
149    }
150
151    //
152    //  There should be only one and only one parameter left, and that
153    //  should be the file name.
154    //
155    if (argInd != argC - 1)
156    {
157        usage();
158        cms::concurrency::xercesTerminate();
159        return 1;
160    }
161
162    //
163    //  Create a SAX parser object. Then, according to what we were told on
164    //  the command line, set it to validate or not.
165    //
166    SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
167    parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, doNamespaces);
168    parser->setFeature(XMLUni::fgXercesSchema, doSchema);
169    parser->setFeature(XMLUni::fgXercesSchemaFullChecking, schemaFullChecking);
170    parser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes, namespacePrefixes);
171
172    if (valScheme == SAX2XMLReader::Val_Auto)
173    {
174        parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
175        parser->setFeature(XMLUni::fgXercesDynamic, true);
176    }
177    if (valScheme == SAX2XMLReader::Val_Never)
178    {
179        parser->setFeature(XMLUni::fgSAX2CoreValidation, false);
180    }
181    if (valScheme == SAX2XMLReader::Val_Always)
182    {
183        parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
184        parser->setFeature(XMLUni::fgXercesDynamic, false);
185    }
186
187    //
188    //  Create our SAX handler object and install it on the parser, as the
189    //  document and error handler.
190    //
191    SaxToDom handler;
192    parser->setContentHandler(&handler);
193    parser->setErrorHandler(&handler);
194
195    //
196    //  Get the starting time and kick off the parse of the indicated
197    //  file. Catch any exceptions that might propogate out of it.
198    //
199    unsigned long duration;
200
201    bool more = true;
202    ifstream fin;
203
204    // the input is a list file
205    if (doList)
206        fin.open(argV[argInd]);
207
208    while (more)
209    {
210        char fURI[1000];
211        //initialize the array to zeros
212        memset(fURI,0,sizeof(fURI));
213
214        if (doList) {
215            if (! fin.eof() ) {
216                fin.getline (fURI, sizeof(fURI));
217                if (!*fURI)
218                    continue;
219                else {
220                    xmlFile = fURI;
221                    cerr << "==Parsing== " << xmlFile << endl;
222                }
223            }
224            else
225                break;
226        }
227        else {
228            xmlFile = argV[argInd];
229            more = false;
230        }
231
232        //reset error count first
233        handler.resetErrors();
234
235        try
236        {
237            const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
238	     cout << "start parsing:" << xmlFile << endl;
239            parser->parse(xmlFile);
240	     cout << "parsing ended" << endl;
241            const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
242            duration = endMillis - startMillis;
243        }
244
245        catch (const XMLException& e)
246        {
247            cerr << "\nError during parsing: '" << xmlFile << "'\n"
248                << "Exception message is:  \n"
249                << StrX(e.getMessage()) << "\n" << endl;
250            errorOccurred = true;
251            continue;
252        }
253
254        catch (...)
255        {
256            cerr << "\nUnexpected exception during parsing: '" << xmlFile << "'\n";
257            errorOccurred = true;
258            continue;
259        }
260
261
262        // Print out the stats that we collected and time taken
263        if (true && getenv("DOTEST"))
264        {
265	
266	   TinyDomTest test(handler.dom());
267	   vector<const AttList *> atts;
268	   test.allNodes(NodeName("Box"),atts);
269	   unsigned int i = 0;
270	   for (; i < atts.size(); ++i) {
271	      const AttList & a = *(atts[i]);
272	      AttList::const_iterator it = a.begin();
273	      for (; it != a.end(); ++it) {
274	         cout << it->first.str() << '=' << it->second.str() << ' ';
275	      }
276	      cout << endl;
277	   }
278	   cout << "dom-size=" << handler.dom().size() << endl;
279	   /*
280	   TinyDomWalker walker(handler.dom());
281	   bool go = true;
282	   TagName name("Box");
283	   while (go) {
284	      if (name.sameName(walker.current().first)) {
285	        cout << walker.current().first.str() << endl;
286	      }
287	      go = walker.next();
288	   }
289	   */
290	   
291	   
292	   
293        }
294        else
295            errorOccurred = true;
296    }
297
298    if (doList)
299        fin.close();
300
301    //
302    //  Delete the parser itself.  Must be done prior to calling Terminate, below.
303    //
304    delete parser;
305
306    // And call the termination method
307    cms::concurrency::xercesTerminate();
308
309    if (errorOccurred)
310        return 4;
311    else
312        return 0;
313
314}
315
316