1 | // ---------------------------------------------------------------------------
|
---|
2 | // Includes for all the program files to see
|
---|
3 | // ---------------------------------------------------------------------------
|
---|
4 | #include <string.h>
|
---|
5 | #include <stdlib.h>
|
---|
6 | #include <iostream>
|
---|
7 | using 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 |
|
---|
30 |
|
---|
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 | // ---------------------------------------------------------------------------
|
---|
51 | static bool doNamespaces = false;
|
---|
52 | static bool doSchema = false;
|
---|
53 | static bool schemaFullChecking = false;
|
---|
54 | static SAXParser::ValSchemes valScheme = SAXParser::Val_Auto;
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 | // ---------------------------------------------------------------------------
|
---|
61 | // StdInParseHandlers: Constructors and Destructor
|
---|
62 | // ---------------------------------------------------------------------------
|
---|
63 | X3dParseHandlers::X3dParseHandlers(SceneGraphNode *root) :
|
---|
64 | fElementCount(0)
|
---|
65 | , fAttrCount(0)
|
---|
66 | , fCharacterCount(0)
|
---|
67 | , fSpaceCount(0)
|
---|
68 | {
|
---|
69 | currentNode = root;
|
---|
70 | }
|
---|
71 |
|
---|
72 | X3dParseHandlers::~X3dParseHandlers()
|
---|
73 | {
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | // ---------------------------------------------------------------------------
|
---|
78 | // StdInParseHandlers: Implementation of the SAX DocumentHandler interface
|
---|
79 | // ---------------------------------------------------------------------------
|
---|
80 | void X3dParseHandlers::endElement(const XMLCh* const name)
|
---|
81 | {
|
---|
82 | StrX lname(name);
|
---|
83 | string element(lname.localForm());
|
---|
84 | if (element == "Shape")
|
---|
85 | EndShape();
|
---|
86 | }
|
---|
87 |
|
---|
88 | void
|
---|
89 | X3dParseHandlers::EndShape()
|
---|
90 | {
|
---|
91 | currentMesh->Preprocess();
|
---|
92 | // make an instance of this mesh
|
---|
93 | MeshInstance *mi = new MeshInstance(currentMesh);
|
---|
94 | currentNode->mGeometry.push_back(mi);
|
---|
95 | currentMesh = NULL;
|
---|
96 | }
|
---|
97 |
|
---|
98 | void
|
---|
99 | X3dParseHandlers::StartIndexedFaceSet(
|
---|
100 | AttributeList& attributes)
|
---|
101 | {
|
---|
102 | int len = attributes.getLength();
|
---|
103 | int i;
|
---|
104 | VertexIndexContainer vertices;
|
---|
105 |
|
---|
106 | for (i=0; i < len; i++) {
|
---|
107 | string attrName(StrX(attributes.getName(i)).localForm());
|
---|
108 | if (attrName == "coordIndex") {
|
---|
109 | StrX attrValue(attributes.getValue(i));
|
---|
110 | // handle coordIndex
|
---|
111 | vertices.clear();
|
---|
112 | const char *ptr = attrValue.localForm();
|
---|
113 | char *endptr;
|
---|
114 | while(1) {
|
---|
115 | int index = strtol(ptr, &endptr, 10);
|
---|
116 | if (ptr == endptr || index == -1) {
|
---|
117 | if (vertices.size() > 2) {
|
---|
118 | Face *face = new Face(vertices);
|
---|
119 | currentMesh->mFaces.push_back(face);
|
---|
120 | }
|
---|
121 | vertices.clear();
|
---|
122 | if (ptr == endptr)
|
---|
123 | break;
|
---|
124 | } else {
|
---|
125 | vertices.push_back(index);
|
---|
126 | }
|
---|
127 | ptr = endptr;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | void
|
---|
134 | X3dParseHandlers::StartMaterial(
|
---|
135 | AttributeList& attributes)
|
---|
136 | {
|
---|
137 | int len = attributes.getLength();
|
---|
138 | int i;
|
---|
139 | if (!currentMesh->mMaterial)
|
---|
140 | currentMesh->mMaterial = new Material;
|
---|
141 | for (i=0; i < len; i++) {
|
---|
142 | string attrName(StrX(attributes.getName(i)).localForm());
|
---|
143 | StrX attrValue(attributes.getValue(i));
|
---|
144 | const char *ptr = attrValue.localForm();
|
---|
145 | if (attrName == "diffuseColor") {
|
---|
146 | float r, g, b;
|
---|
147 | if (sscanf(ptr, "%f %f %f", &r, &g, &b) == 3)
|
---|
148 | currentMesh->mMaterial->mDiffuseColor = RgbColor(r, g, b);
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | void
|
---|
154 | X3dParseHandlers::StartCoordinate(
|
---|
155 | AttributeList& attributes)
|
---|
156 | {
|
---|
157 | int len = attributes.getLength();
|
---|
158 | int i;
|
---|
159 | VertexContainer vertices;
|
---|
160 | for (i=0; i < len; i++) {
|
---|
161 | string attrName(StrX(attributes.getName(i)).localForm());
|
---|
162 | if (attrName == "point") {
|
---|
163 | StrX attrValue(attributes.getValue(i));
|
---|
164 | const char *ptr = attrValue.localForm();
|
---|
165 | char *endptr;
|
---|
166 | while(1) {
|
---|
167 | float x = strtod(ptr, &endptr);
|
---|
168 | if (ptr == endptr)
|
---|
169 | break;
|
---|
170 | ptr = endptr;
|
---|
171 | float y = strtod(ptr, &endptr);
|
---|
172 | if (ptr == endptr)
|
---|
173 | break;
|
---|
174 | ptr = endptr;
|
---|
175 | float z = strtod(ptr, &endptr);
|
---|
176 | if (ptr == endptr)
|
---|
177 | break;
|
---|
178 | ptr = endptr;
|
---|
179 | if (*ptr == ',')
|
---|
180 | ptr++;
|
---|
181 | Vector3 v(x, y, z);
|
---|
182 | vertices.push_back(v);
|
---|
183 | }
|
---|
184 | currentMesh->mVertices = vertices;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | void
|
---|
191 | X3dParseHandlers::startElement(const XMLCh* const name,
|
---|
192 | AttributeList& attributes)
|
---|
193 | {
|
---|
194 | StrX lname(name);
|
---|
195 | string element(lname.localForm());
|
---|
196 |
|
---|
197 | if (element == "IndexedFaceSet") {
|
---|
198 | // create a new mesh node in the scene graph
|
---|
199 | StartIndexedFaceSet(attributes);
|
---|
200 | }
|
---|
201 |
|
---|
202 | if (element == "Shape") {
|
---|
203 | cout<<"+";
|
---|
204 | currentMesh = new Mesh;
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (element == "Coordinate") {
|
---|
208 | if (currentMesh)
|
---|
209 | StartCoordinate(attributes);
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (element == "Material") {
|
---|
213 | StartMaterial(attributes);
|
---|
214 | }
|
---|
215 |
|
---|
216 | fElementCount++;
|
---|
217 | fAttrCount += attributes.getLength();
|
---|
218 | }
|
---|
219 |
|
---|
220 | void
|
---|
221 | X3dParseHandlers::characters(const XMLCh* const chars,
|
---|
222 | const unsigned int length)
|
---|
223 | {
|
---|
224 | fCharacterCount += length;
|
---|
225 | }
|
---|
226 |
|
---|
227 | void
|
---|
228 | X3dParseHandlers::ignorableWhitespace(const XMLCh* const chars,
|
---|
229 | const unsigned int length)
|
---|
230 | {
|
---|
231 | fSpaceCount += length;
|
---|
232 | }
|
---|
233 |
|
---|
234 | void
|
---|
235 | X3dParseHandlers::resetDocument()
|
---|
236 | {
|
---|
237 | fAttrCount = 0;
|
---|
238 | fCharacterCount = 0;
|
---|
239 | fElementCount = 0;
|
---|
240 | fSpaceCount = 0;
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 |
|
---|
245 | // ---------------------------------------------------------------------------
|
---|
246 | // StdInParseHandlers: Overrides of the SAX ErrorHandler interface
|
---|
247 | // ---------------------------------------------------------------------------
|
---|
248 | void
|
---|
249 | X3dParseHandlers::error(const SAXParseException& e)
|
---|
250 | {
|
---|
251 | XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
|
---|
252 | << ", line " << e.getLineNumber()
|
---|
253 | << ", char " << e.getColumnNumber()
|
---|
254 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
255 | }
|
---|
256 |
|
---|
257 | void
|
---|
258 | X3dParseHandlers::fatalError(const SAXParseException& e)
|
---|
259 | {
|
---|
260 | XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
|
---|
261 | << ", line " << e.getLineNumber()
|
---|
262 | << ", char " << e.getColumnNumber()
|
---|
263 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
264 | }
|
---|
265 |
|
---|
266 | void
|
---|
267 | X3dParseHandlers::warning(const SAXParseException& e)
|
---|
268 | {
|
---|
269 | XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
|
---|
270 | << ", line " << e.getLineNumber()
|
---|
271 | << ", char " << e.getColumnNumber()
|
---|
272 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | bool
|
---|
277 | X3dParser::ParseFile(const string filename,
|
---|
278 | SceneGraphNode **root)
|
---|
279 | {
|
---|
280 | // Initialize the XML4C system
|
---|
281 | try {
|
---|
282 | XMLPlatformUtils::Initialize();
|
---|
283 | }
|
---|
284 |
|
---|
285 | catch (const XMLException& toCatch)
|
---|
286 | {
|
---|
287 | XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
|
---|
288 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
289 | return false;
|
---|
290 | }
|
---|
291 |
|
---|
292 |
|
---|
293 | //
|
---|
294 | // Create a SAX parser object. Then, according to what we were told on
|
---|
295 | // the command line, set the options.
|
---|
296 | //
|
---|
297 | SAXParser* parser = new SAXParser;
|
---|
298 | parser->setValidationScheme(valScheme);
|
---|
299 | parser->setDoNamespaces(doNamespaces);
|
---|
300 | parser->setDoSchema(doSchema);
|
---|
301 | parser->setValidationSchemaFullChecking(schemaFullChecking);
|
---|
302 |
|
---|
303 |
|
---|
304 | //
|
---|
305 | // Create our SAX handler object and install it on the parser, as the
|
---|
306 | // document and error handler. We are responsible for cleaning them
|
---|
307 | // up, but since its just stack based here, there's nothing special
|
---|
308 | // to do.
|
---|
309 | //
|
---|
310 | *root = new SceneGraphNode;
|
---|
311 | X3dParseHandlers handler(*root);
|
---|
312 | parser->setDocumentHandler(&handler);
|
---|
313 | parser->setErrorHandler(&handler);
|
---|
314 |
|
---|
315 | unsigned long duration;
|
---|
316 | int errorCount = 0;
|
---|
317 | // create a faux scope so that 'src' destructor is called before
|
---|
318 | // XMLPlatformUtils::Terminate
|
---|
319 | {
|
---|
320 | //
|
---|
321 | // Kick off the parse and catch any exceptions. Create a standard
|
---|
322 | // input input source and tell the parser to parse from that.
|
---|
323 | //
|
---|
324 | // StdInInputSource src;
|
---|
325 | try
|
---|
326 | {
|
---|
327 | const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
328 | parser->parse(filename.c_str());
|
---|
329 | const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
330 | duration = endMillis - startMillis;
|
---|
331 | errorCount = parser->getErrorCount();
|
---|
332 | }
|
---|
333 | catch (const OutOfMemoryException&)
|
---|
334 | {
|
---|
335 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
336 | errorCount = 2;
|
---|
337 | return false;
|
---|
338 | }
|
---|
339 | catch (const XMLException& e)
|
---|
340 | {
|
---|
341 | XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
|
---|
342 | << StrX(e.getMessage())
|
---|
343 | << "\n" << XERCES_STD_QUALIFIER endl;
|
---|
344 | errorCount = 1;
|
---|
345 | return false;
|
---|
346 | }
|
---|
347 |
|
---|
348 | // Print out the stats that we collected and time taken
|
---|
349 | if (!errorCount) {
|
---|
350 | XERCES_STD_QUALIFIER cout << filename << ": " << duration << " ms ("
|
---|
351 | << handler.getElementCount() << " elems, "
|
---|
352 | << handler.getAttrCount() << " attrs, "
|
---|
353 | << handler.getSpaceCount() << " spaces, "
|
---|
354 | << handler.getCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | //
|
---|
359 | // Delete the parser itself. Must be done prior to calling Terminate, below.
|
---|
360 | //
|
---|
361 | delete parser;
|
---|
362 |
|
---|
363 | XMLPlatformUtils::Terminate();
|
---|
364 |
|
---|
365 | if (errorCount > 0)
|
---|
366 | return false;
|
---|
367 | else
|
---|
368 | return true;
|
---|
369 | }
|
---|
370 |
|
---|
371 |
|
---|