PageRenderTime 33ms CodeModel.GetById 10ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/DetectorDescription/RegressionTest/test/simple_dom_parser2.cpp

https://github.com/aivanov-cern/cmssw
C++ | 325 lines | 236 code | 40 blank | 49 comment | 52 complexity | 5739e7d804120c314323f385b6c89144 MD5 | raw file
  1#include "DetectorDescription/RegressionTest/src/SaxToDom2.h"
  2#include "DetectorDescription/RegressionTest/src/TinyDomTest2.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 ADummy2
 15{
 16
 17};
 18
 19// ---------------------------------------------------------------------------
 20//  Local helper methods
 21// ---------------------------------------------------------------------------
 22void usage2()
 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        usage2();
 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            usage2();
 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        usage2();
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    SaxToDom2 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 TOTALduration = 0;
200    unsigned long duration;
201
202    bool more = true;
203    ifstream fin;
204
205    // the input is a list file
206    if (doList)
207        fin.open(argV[argInd]);
208
209    cout << "argInd = " << argInd << endl;
210
211    while (more)
212    {
213        char fURI[1000];
214        //initialize the array to zeros
215        memset(fURI,0,sizeof(fURI));
216
217        if (doList) {
218            if (! fin.eof() ) {
219                fin.getline (fURI, sizeof(fURI));
220                if (!*fURI)
221                    continue;
222                else {
223                    xmlFile = fURI;
224                    cerr << "==Parsing== " << xmlFile << endl;
225                }
226            }
227            else
228                break;
229        }
230        else {
231            xmlFile = argV[argInd];
232            more = false;
233        }
234
235        //reset error count first
236        handler.resetErrors();
237
238        try
239        {
240            const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
241	     cout << "start parsing:" << xmlFile << endl;
242            parser->parse(xmlFile);
243	     cout << "parsing ended" << endl;
244            const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
245            duration = endMillis - startMillis;
246	    TOTALduration += duration;
247	    cout << "duration = " << duration << endl;
248        }
249
250        catch (const XMLException& e)
251        {
252            cerr << "\nError during parsing: '" << xmlFile << "'\n"
253                << "Exception message is:  \n"
254                << StrX(e.getMessage()) << "\n" << endl;
255            errorOccurred = true;
256            continue;
257        }
258
259        catch (...)
260        {
261            cerr << "\nUnexpected exception during parsing: '" << xmlFile << "'\n";
262            errorOccurred = true;
263            continue;
264        }
265
266
267        // Print out the stats that we collected and time taken
268        if (true && getenv("DOTEST"))
269        {
270	
271	   TinyDomTest2 test(handler.dom());
272	   vector<const AttList2*> allAtts;
273	   AttList2 atl2;
274	   Node2 n2(TagName("Box"), atl2);
275	   test.allNodes(n2, allAtts);
276	   unsigned int i = 0;
277	   for (; i < allAtts.size(); ++i) {
278	      const AttList2 & a = *(allAtts[i]);
279	      AttList2::const_iterator it = a.begin();
280	      for (; it != a.end(); ++it) {
281	         cout << it->first.str() << '=' << it->second.str() << ' ';
282	      }
283	      cout << endl;
284	   }
285	   cout << "dom-size=" << handler.dom().size() << endl;
286	   /*
287	   TinyDomWalker walker(handler.dom());
288	   bool go = true;
289	   TagName name("Box");
290	   while (go) {
291	      if (name.sameName(walker.current().first)) {
292	        cout << walker.current().first.str() << endl;
293	      }
294	      go = walker.next();
295	   }
296	   */
297	   
298	   
299	   
300        }
301        else
302            errorOccurred = true;
303    }
304
305    if (doList)
306        fin.close();
307
308    cout << "Total Duration: ~ " << TOTALduration << endl;
309
310    //
311    //  Delete the parser itself.  Must be done prior to calling Terminate, below.
312    //
313    delete parser;
314
315    // And call the termination method
316    cms::concurrency::xercesTerminate();
317
318    if (errorOccurred)
319        return 4;
320    else
321        return 0;
322
323}
324
325