00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026
00028
00029 #ifndef __X3DSAX2HANDLERS_H__
00030 #define __X3DSAX2HANDLERS_H__
00031
00032 #include <xercesc/sax2/Attributes.hpp>
00033 #include <xercesc/sax2/DefaultHandler.hpp>
00034 #include <xercesc/sax/Locator.hpp>
00035 #include <stack>
00036 #include <list>
00037 #include "Field.h"
00038 #include "Node.h"
00039 #include "Exception.h"
00040 #include "DEFNodes.h"
00041 #include "AutoRef.h"
00042 #include "ProtoDeclaration.h"
00043 #include "AutoPtrVector.h"
00044 #include "X3D.h"
00045
00046 #ifdef _MSC_VER
00047 #pragma comment( lib, "xerces-c_2.lib" )
00048 #endif
00049
00050
00051 XERCES_CPP_NAMESPACE_USE
00052 namespace H3D {
00053 namespace X3D {
00058 H3D_API_EXCEPTION( XMLParseError );
00059
00063 class X3DSAX2Handlers : public DefaultHandler {
00064 public:
00068 X3DSAX2Handlers( DEFNodes *dn = NULL,
00069 DEFNodes *_exported_nodes = NULL,
00070 PrototypeVector *_proto_declarations = NULL ):
00071 proto_instance( NULL ),
00072 proto_declaration( NULL ),
00073 defining_proto_body( false ),
00074 defining_proto_interface( false ),
00075 defining_proto_connections( false ),
00076 defining_extern_proto( false ),
00077 delete_DEF_map( dn == NULL ),
00078 delete_exported_map( _exported_nodes == NULL ),
00079 delete_proto_declarations( _proto_declarations == NULL ),
00080 DEF_map( dn ),
00081 exported_nodes( _exported_nodes ),
00082 proto_declarations( _proto_declarations ),
00083 locator( NULL ){
00084 if( !DEF_map ) {
00085 DEF_map = new DEFNodes();
00086 }
00087 if( !_exported_nodes ) {
00088 exported_nodes = new DEFNodes();
00089 }
00090 if( !_proto_declarations ) {
00091 proto_declarations = new PrototypeVector;
00092 }
00093 };
00094
00096 ~X3DSAX2Handlers() {
00097 if( delete_DEF_map )
00098 delete DEF_map;
00099 if( delete_exported_map )
00100 delete exported_nodes;
00101 if( delete_proto_declarations )
00102 delete proto_declarations;
00103 };
00104
00105
00106 void setDocumentLocator( const Locator *const _locator ) {
00107 locator = (Locator *)_locator;
00108 }
00109
00113 inline const AutoRef<Node> &getResultingNode() {
00114 return resulting_node;
00115 }
00116
00119 virtual void startDocument () {
00120 resulting_node.reset( NULL );
00121 }
00122
00131 void startElement(const XMLCh* const uri,
00132 const XMLCh* const localname,
00133 const XMLCh* const qname,
00134 const Attributes& attrs);
00135
00143 void endElement (const XMLCh *const uri,
00144 const XMLCh *const localname,
00145 const XMLCh *const qname);
00146
00150 void warning(const SAXParseException& exc);
00151
00155 void error(const SAXParseException& exc);
00156
00160 void fatalError(const SAXParseException& exc);
00161
00164 X3DPrototypeInstance *proto_instance;
00165
00166 protected:
00167
00171 void protoStartElement( const XMLCh* const uri,
00172 const XMLCh* const localname,
00173 const XMLCh* const qname,
00174 const Attributes& attrs );
00175
00179 void protoEndElement( const XMLCh* const uri,
00180 const XMLCh* const localname,
00181 const XMLCh* const qname );
00182
00186 void handleProtoInterfaceFieldElement( const Attributes &attrs );
00187
00192 void handleConnectElement( const Attributes &attrs, Node *parent );
00193
00196 void handleRouteElement( const Attributes &attrs,
00197 bool route_no_event = false );
00198
00202 Field * handleFieldElement( const Attributes &attrs, Node *parent );
00203
00205 void handleImportElement( const Attributes &attrs );
00206
00208 void handleExportElement( const Attributes &attrs );
00209
00213 X3DPrototypeInstance* handleProtoInstanceElement( const Attributes &attrs );
00214
00217 void handleFieldValueElement( const Attributes &attrs, Node *parent );
00218
00220 void handleExternProtoDeclareElement( const Attributes &attrs );
00221
00224 class NodeElement {
00225 public:
00227 NodeElement( Node *_node,
00228 const string &_container_field = "" ):
00229 container_field( _container_field ),
00230 node( _node ) {
00231 if( node ) node->ref();
00232 }
00233
00235 NodeElement( const NodeElement& ne ) {
00236 node = ne.node;
00237 container_field = ne.container_field;
00238 if( node ) node->ref();
00239 }
00240
00242 ~NodeElement() {
00243 if( node ) node->unref();
00244 }
00245
00247 void setNode( Node *n ) {
00248 if( n )
00249 n->ref();
00250 if( node )
00251 node->unref();
00252 node = n;
00253 }
00254
00256 Node *getNode() {
00257 return node;
00258 }
00259
00261 void setContainerField( const string &s ) {
00262 container_field = s;
00263 }
00264
00266 const string &getContainerField() {
00267 return container_field;
00268 }
00269 private:
00270 string container_field;
00271 Node *node;
00272
00273 };
00274
00278 string getLocationString();
00279
00282 typedef std::stack< NodeElement > NodeElementStack;
00283
00288 ProtoDeclaration *proto_declaration;
00289
00292 string proto_body;
00293
00295 bool defining_proto_body;
00296
00298 bool defining_proto_interface;
00299
00301 bool defining_proto_connections;
00302
00304 bool defining_extern_proto;
00305
00308 NodeElementStack node_stack;
00309
00312 bool delete_DEF_map;
00313
00316 bool delete_exported_map;
00317
00320 bool delete_proto_declarations;
00321
00324 DEFNodes *DEF_map;
00325
00328 DEFNodes *exported_nodes;
00329
00331 PrototypeVector *proto_declarations;
00332
00334 AutoRef<Node> resulting_node;
00335
00339 Locator *locator;
00340
00341 };
00342 }
00343 }
00344 #endif