source: GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp @ 712

Revision 712, 22.1 KB checked in by mattausch, 18 years ago (diff)

added exporter for Vrml
added transformations to x3d loader

Line 
1// ---------------------------------------------------------------------------
2//  Includes for all the program files to see
3// ---------------------------------------------------------------------------
4#include <string.h>
5#include <stdlib.h>
6#include <iostream>
7using namespace std;
8#include <xercesc/util/PlatformUtils.hpp>
9
10// ---------------------------------------------------------------------------
11//  Includes
12// ---------------------------------------------------------------------------
13#include <xercesc/framework/StdInInputSource.hpp>
14#include <xercesc/parsers/SAXParser.hpp>
15#include <xercesc/util/OutOfMemoryException.hpp>
16
17// ---------------------------------------------------------------------------
18//  Includes
19// ---------------------------------------------------------------------------
20#include <xercesc/sax/AttributeList.hpp>
21#include <xercesc/sax/SAXParseException.hpp>
22#include <xercesc/sax/SAXException.hpp>
23
24#include "X3dParser.h"
25
26#include "X3dParserXerces.h"
27#include "Mesh.h"
28#include "SceneGraph.h"
29#include "Triangle3.h"
30#include "ViewCellsManager.h"
31
32// ---------------------------------------------------------------------------
33//  Local data
34//
35//  doNamespaces
36//      Indicates whether namespace processing should be enabled or not.
37//      The default is no, but -n overrides that.
38//
39//  doSchema
40//      Indicates whether schema processing should be enabled or not.
41//      The default is no, but -s overrides that.
42//
43//  schemaFullChecking
44//      Indicates whether full schema constraint checking should be enabled or not.
45//      The default is no, but -s overrides that.
46//
47//  valScheme
48//      Indicates what validation scheme to use. It defaults to 'auto', but
49//      can be set via the -v= command.
50// ---------------------------------------------------------------------------
51static bool     doNamespaces       = false;
52static bool     doSchema           = false;
53static bool     schemaFullChecking = false;
54static SAXParser::ValSchemes    valScheme       = SAXParser::Val_Auto;
55
56#define ROTATE_SCENE 0
57
58
59
60// ---------------------------------------------------------------------------
61//  StdInParseHandlers: Constructors and Destructor
62// ---------------------------------------------------------------------------
63X3dParseHandlers::X3dParseHandlers(SceneGraphNode *root, const bool loadPolygonsAsMeshes):
64  mElementCount(0)
65  , mAttrCount(0)
66  , mCharacterCount(0)
67  , mSpaceCount(0)
68  , mLoadPolygonsAsMeshes(loadPolygonsAsMeshes)
69{
70  mCurrentNode = root;
71  // this matrix should never be removed from stack
72  //mTransformations.push(IdentityMatrix());
73}
74
75X3dParseHandlers::~X3dParseHandlers()
76{
77        if (!mTransformations.empty())
78                cout << "error: transformation stack size: " << (int)mTransformations.size() << endl;
79}
80
81
82// ---------------------------------------------------------------------------
83//  StdInParseHandlers: Implementation of the SAX DocumentHandler interface
84// ---------------------------------------------------------------------------
85void X3dParseHandlers::endElement(const XMLCh* const name)
86{
87  StrX lname(name);
88  string element(lname.LocalForm());
89 
90  if (element == "Shape")
91    EndShape();
92  if (1)
93  if (element == "Transform")
94          EndTransform();
95}
96
97
98static void RotateMesh(Mesh *mesh)
99{
100        VertexContainer::iterator it, it_end = mesh->mVertices.end();
101
102        const float angle = 30.0f * PI / 180.0f;
103        const Matrix4x4 rot = RotationYMatrix(angle);
104
105        for (it = mesh->mVertices.begin(); it != it_end; ++ it)
106        {
107                (*it) = rot * (*it);       
108        }
109}
110
111
112void X3dParseHandlers::ApplyTransformation(Mesh *mesh,
113                                                                                   const Matrix4x4 &m) const
114{
115        VertexContainer::iterator it, it_end = mesh->mVertices.end();
116
117        for (it = mesh->mVertices.begin(); it != it_end; ++ it)
118        {
119                (*it) = m * (*it);       
120        }
121}
122
123
124void X3dParseHandlers::ApplyTransformations(TrafoStack trafos, Mesh *mesh) const
125{
126        while (!trafos.empty())
127        {
128                const Matrix4x4 m = trafos.top();
129                trafos.pop();
130
131                ApplyTransformation(mesh, m);
132        }
133}
134
135void X3dParseHandlers::StartTransform(AttributeList&  attributes)
136{
137        Matrix4x4 currentTransform = IdentityMatrix();
138
139        const int len = attributes.getLength();
140   
141        for (int i = 0; i < len; ++ i)
142        {
143                string attrName(StrX(attributes.getName(i)).LocalForm());
144                       
145                StrX attrValue(attributes.getValue(i));
146                const char *ptr = attrValue.LocalForm();
147                       
148                if (attrName == "rotation")
149                {
150                        Vector3 axis;
151                        float angle;
152                               
153                        if (sscanf(ptr, "%f %f %f %f", &axis.x, &axis.y, &axis.z, &angle) == 4)
154                        {
155                                Matrix4x4 rot = RotationAxisMatrix(axis, angle);
156                                currentTransform *= rot;
157                        }
158                }
159                else if (attrName == "translation")
160                {
161                        Vector3 transl;
162                                                       
163                        if (sscanf(ptr, "%f %f %f %f", &transl.x, &transl.y, &transl.z) == 3)
164                        {
165                                Matrix4x4 tm = TranslationMatrix(transl);
166
167                                currentTransform *= tm;
168                        }
169                }
170        }
171       
172        mTransformations.push(currentTransform);
173}
174
175
176void X3dParseHandlers::EndTransform()
177{
178        mTransformations.pop();
179}
180
181
182void
183X3dParseHandlers::EndShape()
184{
185        if (mLoadPolygonsAsMeshes)
186        {
187                vector<VertexIndexContainer>::const_iterator it, it_end = mCurrentVertexIndices.end();
188
189                for (it = mCurrentVertexIndices.begin(); it != it_end; ++ it)
190                {
191                        // only one face per mesh
192                        Mesh *mesh = new Mesh();
193
194                        VertexIndexContainer vc;
195
196                        // add vertices
197                        for (int i = 0; i < (int)(*it).size(); ++ i)
198                        {
199                                mesh->mVertices.push_back(mCurrentVertices[(*it)[i]]);
200                                vc.push_back(i);
201                        }
202                       
203                        mesh->mFaces.push_back(new Face(vc));
204                       
205                        // NOTE: should rather be written into trafo of mesh instance
206                        ApplyTransformations(mTransformations, mesh);
207
208                        mesh->Preprocess();
209                        // make an instance of this mesh
210                        MeshInstance *mi = new MeshInstance(mesh);
211                        mCurrentNode->mGeometry.push_back(mi);
212
213                }
214
215                delete mCurrentMesh;
216                mCurrentVertices.clear();
217                mCurrentVertexIndices.clear();
218        }
219        else
220        {
221                if (mCurrentMesh->mFaces.size())
222                {
223                        // should rather be written into the transformation
224                        // of a mesh instance
225                        ApplyTransformations(mTransformations, mCurrentMesh);
226
227                        mCurrentMesh->Preprocess();
228                        // make an instance of this mesh
229                        MeshInstance *mi = new MeshInstance(mCurrentMesh);
230                        mCurrentNode->mGeometry.push_back(mi);
231                        // set the object id to a unique value
232                        //mi->SetId(mCurrentObjectId ++);
233                }
234                else
235                {
236                        cout<<"X";
237                        delete mCurrentMesh;
238                }
239
240                mCurrentMesh = NULL;
241        }
242}
243
244
245void
246X3dParseHandlers::StartIndexedFaceSet(
247                                                                          AttributeList&  attributes)
248{
249  int len = attributes.getLength();
250  int i;
251  VertexIndexContainer vertices;
252 
253  for (i=0; i < len; i++) {
254    string attrName(StrX(attributes.getName(i)).LocalForm());
255    if (attrName == "coordIndex") {
256      StrX attrValue(attributes.getValue(i));
257      // handle coordIndex
258      vertices.clear();
259      const char *ptr = attrValue.LocalForm();
260      char *endptr;
261      while(1) {
262        int index = strtol(ptr, &endptr, 10);
263        if (ptr == endptr || index == -1) {
264          if (vertices.size() > 2) {
265                  Face *face = new Face(vertices);
266
267                   if (!mLoadPolygonsAsMeshes)
268                   {
269                           mCurrentMesh->mFaces.push_back(face);
270                   }
271                   else
272                   // every polygon is a mesh
273                   {
274                           mCurrentVertexIndices.push_back(vertices);
275                   }
276          }
277
278          vertices.clear();
279          if (ptr == endptr)
280            break;
281        } else {
282          vertices.push_back(index);
283        }
284        ptr = endptr;
285          }
286    }
287  }
288}
289
290void
291X3dParseHandlers::StartMaterial(
292                                AttributeList&  attributes)
293{
294  int len = attributes.getLength();
295  int i;
296  if (!mCurrentMesh->mMaterial)
297    mCurrentMesh->mMaterial = new Material;
298  for (i=0; i < len; i++) {
299    string attrName(StrX(attributes.getName(i)).LocalForm());
300    StrX attrValue(attributes.getValue(i));
301    const char *ptr = attrValue.LocalForm();
302    if (attrName == "diffuseColor") {
303      float r, g, b;
304      if (sscanf(ptr, "%f %f %f", &r, &g, &b) == 3)
305        mCurrentMesh->mMaterial->mDiffuseColor = RgbColor(r, g, b);
306    }
307  }
308}
309
310void
311X3dParseHandlers::StartCoordinate(
312                                  AttributeList&  attributes)
313{
314        int len = attributes.getLength();
315       
316        int i;
317        VertexContainer vertices;
318       
319        for (i=0; i < len; i++)
320        {
321                string attrName(StrX(attributes.getName(i)).LocalForm());
322         
323                if (attrName == "point")
324                {
325                        StrX attrValue(attributes.getValue(i));
326                 
327
328                        const char *ptr = attrValue.LocalForm();
329                        char *endptr;
330
331
332                        while (1)
333                        {
334                                float x = (float)strtod(ptr, &endptr);
335               
336                                if (ptr == endptr)
337                                  break;
338                         
339                                ptr = endptr;
340                               
341                                float y = (float)strtod(ptr, &endptr);
342                         
343                                if (ptr == endptr)
344                                        break;
345
346                                ptr = endptr;
347                         
348                                float z = (float)strtod(ptr, &endptr);
349                                if (ptr == endptr)
350                                        break;
351                         
352                                ptr = endptr;
353                         
354                                if (*ptr == ',')
355                                        ptr ++;
356
357                                Vector3 v(x, y, z);
358                                vertices.push_back(v);
359                        }
360                        if (mLoadPolygonsAsMeshes)
361                        {
362                                mCurrentVertices = vertices;
363                        }
364                        else
365                        {
366                                mCurrentMesh->mVertices = vertices;
367                        }
368                }
369        }
370}
371
372
373void
374X3dParseHandlers::startElement(const XMLCh* const name,
375                                                           AttributeList&  attributes)
376{
377  StrX lname(name);
378  string element(lname.LocalForm());
379 
380  if (element == "IndexedFaceSet") {
381    // create a new mesh node in the scene graph
382    StartIndexedFaceSet(attributes);
383  }
384 
385  if (element == "Shape") {
386    cout << "+";
387        mCurrentMesh = new Mesh;
388  }
389 
390  if (element == "Coordinate") {
391    if (mCurrentMesh)
392      StartCoordinate(attributes);
393  }
394 
395  if (element == "Material") {
396    StartMaterial(attributes);
397  }
398if (1)
399  if (element == "Transform") {
400          StartTransform(attributes);
401  }
402
403  mElementCount++;
404  mAttrCount += attributes.getLength();
405}
406
407void
408X3dParseHandlers::characters(const XMLCh* const chars,
409                             const unsigned int length)
410{
411  mCharacterCount += length;
412}
413
414void
415X3dParseHandlers::ignorableWhitespace(const XMLCh* const chars,
416                                      const unsigned int length)
417{
418  mSpaceCount += length;
419}
420
421void
422X3dParseHandlers::resetDocument()
423{
424  mAttrCount = 0;
425  mCharacterCount = 0;
426  mElementCount = 0;
427  mSpaceCount = 0;
428}
429
430
431
432// ---------------------------------------------------------------------------
433//  StdInParseHandlers: Overrides of the SAX ErrorHandler interface
434// ---------------------------------------------------------------------------
435void
436X3dParseHandlers::error(const SAXParseException& e)
437{
438  XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
439                            << ", line " << e.getLineNumber()
440                            << ", char " << e.getColumnNumber()
441                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
442}
443
444void
445X3dParseHandlers::fatalError(const SAXParseException& e)
446{
447  XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
448                            << ", line " << e.getLineNumber()
449                            << ", char " << e.getColumnNumber()
450                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
451}
452
453void
454X3dParseHandlers::warning(const SAXParseException& e)
455{
456  XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
457                            << ", line " << e.getLineNumber()
458                            << ", char " << e.getColumnNumber()
459                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
460}
461
462
463bool
464X3dParser::ParseFile(const string filename,
465                                         SceneGraphNode **root,
466                                         const bool loadPolygonsAsMeshes)
467{
468  // Initialize the XML4C system
469  try {
470    XMLPlatformUtils::Initialize();
471  }
472 
473  catch (const XMLException& toCatch)
474    {
475      XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
476                                << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
477      return false;
478    }
479 
480 
481  //
482  //  Create a SAX parser object. Then, according to what we were told on
483  //  the command line, set the options.
484  //
485  SAXParser* parser = new SAXParser;
486  parser->setValidationScheme(valScheme);
487  parser->setDoNamespaces(doNamespaces);
488  parser->setDoSchema(doSchema);
489  parser->setValidationSchemaFullChecking(schemaFullChecking);
490 
491
492  //
493  //  Create our SAX handler object and install it on the parser, as the
494  //  document and error handler. We are responsible for cleaning them
495  //  up, but since its just stack based here, there's nothing special
496  //  to do.
497  //
498  *root = new SceneGraphNode;
499  X3dParseHandlers handler(*root, loadPolygonsAsMeshes);
500  parser->setDocumentHandler(&handler);
501  parser->setErrorHandler(&handler);
502 
503  unsigned long duration;
504  int errorCount = 0;
505  // create a faux scope so that 'src' destructor is called before
506  // XMLPlatformUtils::Terminate
507  {
508    //
509    //  Kick off the parse and catch any exceptions. Create a standard
510    //  input input source and tell the parser to parse from that.
511    //
512    //    StdInInputSource src;
513    try
514      {
515        const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
516        parser->parse(filename.c_str());
517        const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
518        duration = endMillis - startMillis;
519        errorCount = parser->getErrorCount();
520      }
521    catch (const OutOfMemoryException&)
522      {
523        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
524        errorCount = 2;
525        return false;
526      }
527    catch (const XMLException& e)
528      {
529        XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
530                                  << StrX(e.getMessage())
531                                  << "\n" << XERCES_STD_QUALIFIER endl;
532        errorCount = 1;
533        return false;
534      }
535
536   
537    // Print out the stats that we collected and time taken
538    if (!errorCount) {
539      XERCES_STD_QUALIFIER cout << filename << ": " << duration << " ms ("
540                                << handler.GetElementCount() << " elems, "
541                                << handler.GetAttrCount() << " attrs, "
542                                << handler.GetSpaceCount() << " spaces, "
543                                << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
544    }
545  }
546 
547  //
548  //  Delete the parser itself.  Must be done prior to calling Terminate, below.
549  //
550  delete parser;
551 
552  XMLPlatformUtils::Terminate();
553 
554  if (errorCount > 0)
555    return false;
556  else
557    return true;
558}
559
560
561
562/************************************************************************/
563/*             class X3dViewCellsParseHandlers implementation           */
564/************************************************************************/
565
566
567// ---------------------------------------------------------------------------
568//  StdInParseHandlers: Constructors and Destructor
569// ---------------------------------------------------------------------------
570X3dViewCellsParseHandlers::X3dViewCellsParseHandlers(ViewCellsManager *viewCellsManager,
571                                                                                                         const float viewCellHeight):
572mElementCount(0),
573mAttrCount(0),
574mCharacterCount(0),
575mSpaceCount(0),
576mViewCellsManager(viewCellsManager),
577mViewCellHeight(viewCellHeight)
578{
579}
580
581X3dViewCellsParseHandlers::~X3dViewCellsParseHandlers()
582{
583}
584
585
586// ---------------------------------------------------------------------------
587//  StdInParseHandlers: Implementation of the SAX DocumentHandler interface
588// ---------------------------------------------------------------------------
589void X3dViewCellsParseHandlers::endElement(const XMLCh* const name)
590{
591  StrX lname(name);
592  string element(lname.LocalForm());
593 
594  if (element == "Shape")
595    EndShape();
596}
597
598void
599X3dViewCellsParseHandlers::EndShape()
600{
601}
602
603void
604X3dViewCellsParseHandlers::StartIndexedFaceSet(
605                                      AttributeList&  attributes)
606{
607        int len = attributes.getLength();
608        int i;
609        // clear previous vertex indices
610        mCurrentVertexIndices.clear();
611        for (i=0; i < len; i++)
612        {
613                string attrName(StrX(attributes.getName(i)).LocalForm());
614           
615                if (attrName == "coordIndex")
616                {
617                        StrX attrValue(attributes.getValue(i));
618                       
619                        // handle coordIndex
620                        const char *ptr = attrValue.LocalForm();
621                        char *endptr;
622               
623                        while (1)
624                        {
625                                int index = strtol(ptr, &endptr, 10);
626                               
627                                if (ptr == endptr)
628                                        break;
629
630                                if (index != -1)
631                                {
632                                        mCurrentVertexIndices.push_back(index);
633                                }
634                   
635                                ptr = endptr;
636                        }
637                }
638        }
639}
640
641
642void
643X3dViewCellsParseHandlers::StartCoordinate(AttributeList&  attributes)
644{
645        int len = attributes.getLength();
646       
647        VertexContainer vertices;
648        int i;
649        for (i=0; i < len; i++)
650        {
651                string attrName(StrX(attributes.getName(i)).LocalForm());
652               
653                if (attrName == "point")
654                {
655                        StrX attrValue(attributes.getValue(i));
656                       
657                        const char *ptr = attrValue.LocalForm();
658                       
659                        char *endptr;
660                       
661                        while (1)
662                        {
663                                float x = (float)strtod(ptr, &endptr);
664               
665                                if (ptr == endptr)
666                                        break;
667                                ptr = endptr;
668                               
669                                float y = (float)(float)strtod(ptr, &endptr);
670
671                               
672                                if (ptr == endptr)
673                                        break;
674                                ptr = endptr;
675
676                                float z = (float)(float)strtod(ptr, &endptr);
677
678                                if (ptr == endptr)
679                                        break;
680
681                                ptr = endptr;
682                                if (*ptr == ',')
683                                        ptr++;
684
685                                Vector3 v(x, y, z);
686                                vertices.push_back(v);                         
687                        }
688                }
689        }
690
691        for (i = 0; i < mCurrentVertexIndices.size(); i += 3)
692        {
693                Triangle3 baseTri(vertices[mCurrentVertexIndices[i + 0]],
694                                                  vertices[mCurrentVertexIndices[i + 1]],
695                                                  vertices[mCurrentVertexIndices[i + 2]]);
696
697                // create view cell from base triangle
698                mViewCellsManager->AddViewCell(
699                        mViewCellsManager->ExtrudeViewCell(baseTri,
700                        mViewCellHeight));
701        }
702}
703
704
705void
706X3dViewCellsParseHandlers::startElement(const XMLCh* const name,
707                                                                                AttributeList&  attributes)
708{
709  StrX lname(name);
710  string element(lname.LocalForm());
711 
712  if (element == "IndexedFaceSet") {
713    // create the viewcells from individual triangles
714    StartIndexedFaceSet(attributes);
715  }
716       
717  if (element == "Coordinate") {
718          // add coordinates to the triangles
719      StartCoordinate(attributes);
720  }
721  // do nothing
722  //if (element == "Shape") {}
723  // ignore material
724  //if (element == "Material") {}
725
726  ++ mElementCount;
727  mAttrCount += attributes.getLength();
728}
729
730void
731X3dViewCellsParseHandlers::characters(const XMLCh* const chars,
732                             const unsigned int length)
733{
734  mCharacterCount += length;
735}
736
737void
738X3dViewCellsParseHandlers::ignorableWhitespace(const XMLCh* const chars,
739                                      const unsigned int length)
740{
741  mSpaceCount += length;
742}
743
744void
745X3dViewCellsParseHandlers::resetDocument()
746{
747  mAttrCount = 0;
748  mCharacterCount = 0;
749  mElementCount = 0;
750  mSpaceCount = 0;
751}
752
753
754
755// ---------------------------------------------------------------------------
756//  StdInParseHandlers: Overrides of the SAX ErrorHandler interface
757// ---------------------------------------------------------------------------
758void
759X3dViewCellsParseHandlers::error(const SAXParseException& e)
760{
761  XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
762                            << ", line " << e.getLineNumber()
763                            << ", char " << e.getColumnNumber()
764                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
765}
766
767void
768X3dViewCellsParseHandlers::fatalError(const SAXParseException& e)
769{
770  XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
771                            << ", line " << e.getLineNumber()
772                            << ", char " << e.getColumnNumber()
773                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
774}
775
776void
777X3dViewCellsParseHandlers::warning(const SAXParseException& e)
778{
779  XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
780                            << ", line " << e.getLineNumber()
781                            << ", char " << e.getColumnNumber()
782                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
783}
784
785
786bool
787X3dParser::ParseFile(const string filename, ViewCellsManager &viewCells)
788{
789  // Initialize the XML4C system
790  try {
791    XMLPlatformUtils::Initialize();
792  }
793 
794  catch (const XMLException& toCatch)
795    {
796      XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
797                                << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
798      return false;
799    }
800 
801 
802  //
803  //  Create a SAX parser object. Then, according to what we were told on
804  //  the command line, set the options.
805  //
806  SAXParser* parser = new SAXParser;
807  parser->setValidationScheme(valScheme);
808  parser->setDoNamespaces(doNamespaces);
809  parser->setDoSchema(doSchema);
810  parser->setValidationSchemaFullChecking(schemaFullChecking);
811 
812
813  //
814  //  Create our SAX handler object and install it on the parser, as the
815  //  document and error handler. We are responsible for cleaning them
816  //  up, but since its just stack based here, there's nothing special
817  //  to do.
818  //
819  X3dViewCellsParseHandlers handler(&viewCells, mViewCellHeight);
820  parser->setDocumentHandler(&handler);
821  parser->setErrorHandler(&handler);
822 
823  unsigned long duration;
824  int errorCount = 0;
825  // create a faux scope so that 'src' destructor is called before
826  // XMLPlatformUtils::Terminate
827  {
828    //
829    //  Kick off the parse and catch any exceptions. Create a standard
830    //  input input source and tell the parser to parse from that.
831    //
832    //    StdInInputSource src;
833    try
834      {
835        const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
836        parser->parse(filename.c_str());
837        const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
838        duration = endMillis - startMillis;
839        errorCount = parser->getErrorCount();
840      }
841    catch (const OutOfMemoryException&)
842      {
843        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
844        errorCount = 2;
845        return false;
846      }
847    catch (const XMLException& e)
848      {
849        XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
850                                  << StrX(e.getMessage())
851                                  << "\n" << XERCES_STD_QUALIFIER endl;
852        errorCount = 1;
853        return false;
854      }
855
856   
857    // Print out the stats that we collected and time taken
858    if (!errorCount) {
859      XERCES_STD_QUALIFIER cout << filename << ": " << duration << " ms ("
860                                << handler.GetElementCount() << " elems, "
861                                << handler.GetAttrCount() << " attrs, "
862                                << handler.GetSpaceCount() << " spaces, "
863                                << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
864    }
865  }
866 
867  //
868  //  Delete the parser itself.  Must be done prior to calling Terminate, below.
869  //
870  delete parser;
871 
872  XMLPlatformUtils::Terminate();
873 
874  if (errorCount > 0)
875    return false;
876  else
877    return true;
878}
Note: See TracBrowser for help on using the repository browser.