H3D API  2.4.1
X3DSAX2Handlers.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
26 //
28 
29 #ifndef __X3DSAX2HANDLERS_H__
30 #define __X3DSAX2HANDLERS_H__
31 
32 #include <stack>
33 #include <list>
34 #include <H3D/Field.h>
35 #include <H3D/Node.h>
36 #include <H3D/DEFNodes.h>
37 #include <H3D/ProtoDeclaration.h>
38 #include <H3D/X3D.h>
39 #include <H3D/H3DScriptNode.h>
40 
41 #include <H3DUtil/Exception.h>
42 #include <H3DUtil/AutoRef.h>
43 #include <H3DUtil/AutoPtrVector.h>
44 
45 #ifdef HAVE_XERCES
46 
47 #include <xercesc/sax2/Attributes.hpp>
48 #include <xercesc/sax2/DefaultHandler.hpp>
49 #include <xercesc/sax/Locator.hpp>
50 #include <xercesc/sax2/XMLReaderFactory.hpp>
51 #include <xercesc/sax2/SAX2XMLReader.hpp>
52 
53 XERCES_CPP_NAMESPACE_USE
54 
55 #endif
56 
57 
58 namespace H3D {
59  namespace X3D {
65 
66 
67 #ifdef HAVE_XERCES
71  class X3DSAX2Handlers : public DefaultHandler {
72  public:
81  DEFNodes *_exported_nodes = NULL,
82  PrototypeVector *_proto_declarations = NULL,
83  bool _called_from_proto_declaration = false ):
84  proto_instance( NULL ),
85  proto_declaration( NULL ),
86  called_from_proto_declaration( _called_from_proto_declaration ),
87  proto_body_count( 0 ),
89  defining_proto_interface( false ),
91  defining_extern_proto( false ),
92  delete_DEF_map( dn == NULL ),
93  delete_exported_map( _exported_nodes == NULL ),
94  delete_proto_declarations( _proto_declarations == NULL ),
95  DEF_map( dn ),
96  exported_nodes( _exported_nodes ),
97  proto_declarations( _proto_declarations ),
98  locator( NULL ),
99  profile_set( false ),
100  meta_set( false ),
101  inside_head(false),
102  inside_cdata( false ) {
103 
104  if( !DEF_map ) {
105  DEF_map = new DEFNodes();
106  }
107  if( !_exported_nodes ) {
108  exported_nodes = new DEFNodes();
109  }
110  if( !_proto_declarations ) {
111  proto_declarations = new PrototypeVector;
112  }
113  };
114 
117  if( delete_DEF_map )
118  delete DEF_map;
119  if( delete_exported_map )
120  delete exported_nodes;
122  delete proto_declarations;
123  };
124 
125 
126  void setDocumentLocator( const Locator *const _locator ) {
127  locator = (Locator *)_locator;
128  }
129 
133  inline const AutoRef<Node> &getResultingNode() {
134  return resulting_node;
135  }
136 
139  virtual void startDocument () {
140  resulting_node.reset( NULL );
141  }
142 
146  virtual void endDocument();
147 
156  void startElement(const XMLCh* const uri,
157  const XMLCh* const localname,
158  const XMLCh* const qname,
159  const Attributes& attrs);
160 
162  //void comment( const XMLCh *const chars,
163  //const XMLSize_t length );
164 
167  void startCDATA();
168 
171  void endCDATA();
172 
175  void characters( const XMLCh *const chars,
176  const XMLSize_t length );
177 
185  void endElement (const XMLCh *const uri,
186  const XMLCh *const localname,
187  const XMLCh *const qname);
188 
192  void warning(const SAXParseException& exc);
193 
197  void error(const SAXParseException& exc);
198 
202  void fatalError(const SAXParseException& exc);
203 
207 
208  protected:
209 
213  void protoStartElement( const XMLCh* const uri,
214  const XMLCh* const localname,
215  const XMLCh* const qname,
216  const Attributes& attrs );
217 
221  void protoEndElement( const XMLCh* const uri,
222  const XMLCh* const localname,
223  const XMLCh* const qname );
224 
228  void handleProtoInterfaceFieldElement( const Attributes &attrs );
229 
234  void handleConnectElement( const Attributes &attrs, Node *parent );
235 
238  void handleRouteElement( const Attributes &attrs,
239  bool route_no_event = false );
240 
243  void handleProgramSettingElement( const Attributes &attrs );
244 
248  Field * handleFieldElement( const Attributes &attrs, Node *parent );
249 
251  void handleImportElement( const Attributes &attrs );
252 
254  void handleExportElement( const Attributes &attrs );
255 
259  X3DPrototypeInstance* handleProtoInstanceElement( const Attributes &attrs );
260 
263  void handleFieldValueElement( const Attributes &attrs, Node *parent );
264 
266  void handleExternProtoDeclareElement( const Attributes &attrs );
267 
270  class NodeElement {
271  public:
273  NodeElement( Node *_node,
274  const string &_container_field = "" ):
275  container_field( _container_field ),
276  node( _node ),
277  have_connect_element( false ) {
278  if( node ) node->ref();
279  }
280 
282  NodeElement( const NodeElement& ne ) {
283  node = ne.node;
284  container_field = ne.container_field;
285  cdata = ne.cdata;
286  if( node ) node->ref();
287  have_connect_element = ne.have_connect_element;
288  }
289 
292  if( node ) node->unref();
293  }
294 
296  void setNode( Node *n ) {
297  if( n )
298  n->ref();
299  if( node )
300  node->unref();
301  node = n;
302  }
303 
305  Node *getNode() {
306  return node;
307  }
308 
310  void setContainerField( const string &s ) {
311  container_field = s;
312  }
313 
315  void setCDATA( const string &s ) {
316  cdata = s;
317  }
318 
320  bool haveCDATA() {
321  return !cdata.empty();
322  }
323 
325  const string &getCDATA() {
326  return cdata;
327  }
328 
330  const string &getContainerField() {
331  return container_field;
332  }
333 
336  return have_connect_element;
337  }
338 
340  void setConnectElement( const bool &b ) {
341  have_connect_element = b;
342  }
343  private:
344  string container_field;
345  Node *node;
346  string cdata;
347  bool have_connect_element;
348  };
349 
353  string getLocationString();
354 
357  typedef std::stack< NodeElement > NodeElementStack;
358 
364 
368 
371  string proto_body;
372 
377  unsigned int proto_body_count;
378 
381 
384 
387 
390 
394 
398 
402 
406 
410 
414 
416  PrototypeVector *proto_declarations;
417 
421 
424 
428  Locator *locator;
429 
433 
435  bool meta_set;
436  bool inside_head;
437 
441 
445  string cdata;
446  };
447 
448  H3DAPI_API SAX2XMLReader* getNewXMLParser();
449 
450 #endif
451  }
452 }
453 #endif
454 
Contains the DEFNodes class.
Contains the Field class.
Base class for all scripting node interfaces.
Node base class.
Header file for ProtoDeclaration.
This file contains functions for creating H3D nodes from X3D input.
The Field class.
Definition: Field.h:46
Node is the base class for all classes that can be part of the H3D scene-graph.
Definition: Node.h:46
This abstract node type is the base type for all prototype instances in the X3D system.
Definition: X3DPrototypeInstance.h:65
Provides a mapping between defined DEF names in X3D and the nodes they refer to.
Definition: DEFNodes.h:92
Encapsulate a node with its XML containerField attribute.
Definition: X3DSAX2Handlers.h:270
bool haveConnectElement()
Returns true if the node have a connect element.
Definition: X3DSAX2Handlers.h:335
const string & getContainerField()
Get the container field value.
Definition: X3DSAX2Handlers.h:330
void setCDATA(const string &s)
Set CDATA associated with the node.
Definition: X3DSAX2Handlers.h:315
bool haveCDATA()
Returns true if there is any CDATA associated with the node.
Definition: X3DSAX2Handlers.h:320
Node * getNode()
Get the Node value.
Definition: X3DSAX2Handlers.h:305
NodeElement(Node *_node, const string &_container_field="")
Constructor.
Definition: X3DSAX2Handlers.h:273
void setConnectElement(const bool &b)
Set the have_connect_element variable.
Definition: X3DSAX2Handlers.h:340
~NodeElement()
Destructor.
Definition: X3DSAX2Handlers.h:291
NodeElement(const NodeElement &ne)
Copy constructor.
Definition: X3DSAX2Handlers.h:282
const string & getCDATA()
Get the CDATA associated with the node.
Definition: X3DSAX2Handlers.h:325
void setContainerField(const string &s)
Set the container field value.
Definition: X3DSAX2Handlers.h:310
void setNode(Node *n)
Set the Node value.
Definition: X3DSAX2Handlers.h:296
This class implements the SAX2 ContentHandler and ErrorHandler interface in order to build an H3D sce...
Definition: X3DSAX2Handlers.h:71
bool defining_extern_proto
true if we are inside an ExternProtoDeclare element.
Definition: X3DSAX2Handlers.h:389
void protoEndElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname)
This function will be called by endElement if the element is a ProtoDeclare element or a child of it.
Definition: X3DSAX2Handlers.cpp:904
void handleFieldValueElement(const Attributes &attrs, Node *parent)
This function will be called if a fieldValue element has been found in a ProtoInstance element.
Definition: X3DSAX2Handlers.cpp:1085
Locator * locator
The locator object of the parser.
Definition: X3DSAX2Handlers.h:428
H3DInt32 defining_proto_body
true if we are inside a ProtoBody element.
Definition: X3DSAX2Handlers.h:380
AutoRefVector< H3DScriptNode > script_nodes
All instances of H3DScriptNode that has been instanced so far during parsing.
Definition: X3DSAX2Handlers.h:420
void startElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const Attributes &attrs)
Handler for the SAX ContentHandler interface.
Definition: X3DSAX2Handlers.cpp:1208
PrototypeVector * proto_declarations
The ProtoDeclarations that have been defined with ProtoDeclare elements.
Definition: X3DSAX2Handlers.h:416
void warning(const SAXParseException &exc)
Handler for the SAX ErrorHandler interface.
Definition: X3DSAX2Handlers.cpp:1724
bool delete_exported_map
If true the exported_nodes member will be deleted when the X3DSAX2handlers object is destructed.
Definition: X3DSAX2Handlers.h:401
DEFNodes * DEF_map
A structure containing a map between DEF names and nodes that are found during parsing.
Definition: X3DSAX2Handlers.h:409
void handleImportElement(const Attributes &attrs)
This function will be called if a IMPORT element has been found.
Definition: X3DSAX2Handlers.cpp:544
unsigned int proto_body_count
Counter used know when a proto body part has finished.
Definition: X3DSAX2Handlers.h:377
void characters(const XMLCh *const chars, const XMLSize_t length)
Handler for the SAX DocumentHandler interface.
Definition: X3DSAX2Handlers.cpp:1200
string cdata
The characters collected inside a CDATA construct.
Definition: X3DSAX2Handlers.h:445
void endCDATA()
Handler for the SAX LexicalHandler interface.
Definition: X3DSAX2Handlers.cpp:1181
bool defining_proto_interface
true if we are inside a ProtoInterface element.
Definition: X3DSAX2Handlers.h:383
bool delete_DEF_map
If true the DEF_map member will be deleted when the X3DSAX2handlers object is destructed.
Definition: X3DSAX2Handlers.h:397
void handleExternProtoDeclareElement(const Attributes &attrs)
This function will be called if a ExternProtoDeclare element has been found.
Definition: X3DSAX2Handlers.cpp:993
void handleProtoInterfaceFieldElement(const Attributes &attrs)
This function is called when a field element has been found in a ProtoInterface element.
Definition: X3DSAX2Handlers.cpp:157
X3DSAX2Handlers(DEFNodes *dn=NULL, DEFNodes *_exported_nodes=NULL, PrototypeVector *_proto_declarations=NULL, bool _called_from_proto_declaration=false)
Constructor.
Definition: X3DSAX2Handlers.h:80
void handleConnectElement(const Attributes &attrs, Node *parent)
This function will be called if a connect element has been found in an IS element.
Definition: X3DSAX2Handlers.cpp:673
bool inside_cdata
Set to true while parser is parsing characters inside a CDATA construct.
Definition: X3DSAX2Handlers.h:440
virtual void endDocument()
Handler for the SAX ContentHandler interface.
Definition: X3DSAX2Handlers.cpp:1730
const AutoRef< Node > & getResultingNode()
Get the node that has been generated by the last parsing.
Definition: X3DSAX2Handlers.h:133
void handleProgramSettingElement(const Attributes &attrs)
This function will be called if a PROGRAM_SETTING element has been found.
Definition: X3DSAX2Handlers.cpp:477
bool called_from_proto_declaration
If true then this handler is created in proto_declaration so a ProtoDeclare element is allowed at top...
Definition: X3DSAX2Handlers.h:367
NodeElementStack node_stack
A stack of nodes that have been created while traversing the XML structure.
Definition: X3DSAX2Handlers.h:393
string proto_body
The string_body string is used when defining ProtoBody.
Definition: X3DSAX2Handlers.h:371
void endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname)
Handler for the SAX ContentHandler interface.
Definition: X3DSAX2Handlers.cpp:1586
void fatalError(const SAXParseException &exc)
Handler for the SAX ErrorHandler interface.
Definition: X3DSAX2Handlers.cpp:1713
X3DPrototypeInstance * handleProtoInstanceElement(const Attributes &attrs)
This function will be called if a ProtoInstance element has been found.
Definition: X3DSAX2Handlers.cpp:969
bool delete_proto_declarations
If true the proto_declarations member will be deleted when the X3DSAX2handlers object is destructed.
Definition: X3DSAX2Handlers.h:405
void handleExportElement(const Attributes &attrs)
This function will be called if a EXPORT element has been found.
Definition: X3DSAX2Handlers.cpp:629
bool defining_proto_connections
true if we are inside an IS element.
Definition: X3DSAX2Handlers.h:386
bool meta_set
Set after a META is set.
Definition: X3DSAX2Handlers.h:435
~X3DSAX2Handlers()
Destructor.
Definition: X3DSAX2Handlers.h:116
Field * handleFieldElement(const Attributes &attrs, Node *parent)
This function will be called if a "field" element has been found that is not part of a ProtoInterface...
Definition: X3DSAX2Handlers.cpp:247
AutoRef< Node > resulting_node
The Node that is created during parsing.
Definition: X3DSAX2Handlers.h:423
DEFNodes * exported_nodes
A list of DEFNodes that contain the nodes that have been exported using the EXPORT statement.
Definition: X3DSAX2Handlers.h:413
string getLocationString()
Returns a string with the current location as given by the current Locator object.
Definition: X3DSAX2Handlers.cpp:150
bool profile_set
Set after a profile is found (either in the X3D tag or in a PROFILE statement)
Definition: X3DSAX2Handlers.h:432
void protoStartElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const Attributes &attrs)
This function will be called by startElement if the element is a ProtoDeclare element or a child of i...
Definition: X3DSAX2Handlers.cpp:786
void error(const SAXParseException &exc)
Handler for the SAX ErrorHandler interface.
Definition: X3DSAX2Handlers.cpp:1704
AutoRef< ProtoDeclaration > proto_declaration
Will be set when a ProtoDeclare element is found and it is will be used in protoStartElement and prot...
Definition: X3DSAX2Handlers.h:363
virtual void startDocument()
Handler for the SAX ContentHandler interface.
Definition: X3DSAX2Handlers.h:139
void startCDATA()
Handler for the SAX LexicalHandler interface.
Definition: X3DSAX2Handlers.cpp:1171
std::stack< NodeElement > NodeElementStack
A stack of NodeElements.
Definition: X3DSAX2Handlers.h:357
void handleRouteElement(const Attributes &attrs, bool route_no_event=false)
This function will be called if a ROUTE, ROUTE_NO_EVENT or ROUTE_WITH_EVENT element has been found.
Definition: X3DSAX2Handlers.cpp:384
X3DPrototypeInstance * proto_instance
If this is set to non-NULL then the parser is used to create an X3DPrototypeInstance.
Definition: X3DSAX2Handlers.h:206
An exception for errors occuring while parsing an X3D XML file.
Definition: X3DSAX2Handlers.h:64
H3D_API_EXCEPTION(Vec2dNormalizeError)
H3DDouble length() const
int H3DInt32
H3D API namespace.
Definition: Anchor.h:38