Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XMLDocument.h
Go to the documentation of this file.
1 #ifndef __XMLDOCUMENT_H__INCLUDED__
2 #define __XMLDOCUMENT_H__INCLUDED__
3 
4 #include <string>
5 #include <map>
6 #include <vector>
7 #include <iterator>
8 
9 #include <ostream>
10 
11 namespace XMLDOM
12 {
13 class XMLDocument;
14 
16 {
17 public:
18  enum Type
19  {
24  };
25 
26 private:
27  Type mType;
28  std::string mTagName;
29  std::string mContents; //for cdata elements
30 
31  typedef std::vector< XMLElement* > ElementList;
32  typedef std::map< std::string, XMLElement* >ElementMap;
33  typedef std::map< std::string, std::string >AttributeMap;
34 
35  ElementList mChildren;
36  ElementMap mById;
37  ElementMap mByName;
38  AttributeMap mAttributes;
39 
40  XMLElement *mParent;
41  XMLDocument *mDocument;
42 
43 public:
44  typedef ElementList::iterator child_iterator;
45  typedef ElementList::const_iterator const_child_iterator;
46 
47  typedef AttributeMap::iterator attribute_iterator;
48  typedef AttributeMap::const_iterator const_attribute_iterator;
49 
50 private:
51  const_attribute_iterator mIdAttribute;
52  const_attribute_iterator mNameAttribute;
53 
54 public:
55  //Constructors
56 
58  XMLElement();
59 
64  XMLElement( Type type, const std::string &data = std::string() );
65 
67  XMLElement( const std::string &cdata );
68 
70  XMLElement( const char *tagName, const char*const *attrValuePairList );
71 
73  XMLElement( const char *tagName, const char*const *attrValuePairList, unsigned int nAttr );
74 
76  XMLElement( const std::string &tagName, const std::vector< std::string > &attrValuePairList );
77 
79  XMLElement( const std::string &tagName, const std::map< std::string, std::string > &attrValuePairList );
80 
81  ~XMLElement();
82 
83 protected:
84  void setParent( XMLElement *parent );
85 
87  void setDocument( XMLDocument *document );
88 
89 public:
91  Type type() const
92  {
93  return mType;
94  }
95 
97  void setType( Type newType );
98 
100  const std::string& tagName() const
101  {
102  return mTagName;
103  }
104 
106  const XMLElement * getParent() const
107  {
108  return mParent;
109  }
110 
113  {
114  return mParent;
115  }
116 
118  const XMLDocument * getDocument() const
119  {
120  return mDocument;
121  }
122 
125  {
126  return mDocument;
127  }
128 
135  const std::string& contents() const;
136 
142  std::string& contents();
143 
152  void appendContents( const std::string &cont );
153 
155  unsigned int numChildren() const
156  {
157  return mChildren.size();
158  }
159 
161  const XMLElement * getChild( unsigned int idx ) const
162  {
163  return ( idx < mChildren.size() ) ? mChildren[idx] : 0;
164  }
165 
167  XMLElement * getChild( unsigned int idx )
168  {
169  return ( idx < mChildren.size() ) ? mChildren[idx] : 0;
170  }
171 
173  const XMLElement * getChildById( const std::string &id ) const;
174 
176  XMLElement * getChildById( const std::string &id );
177 
179  const XMLElement * getChildByName( const std::string &name ) const;
180 
182  XMLElement * getChildByName( const std::string &name );
183 
185  const std::string& getId() const
186  {
187  static std::string empty;
188  return ( mIdAttribute == mAttributes.end() ) ? empty : mIdAttribute->second;
189  }
190 
192  const std::string& getName() const
193  {
194  static std::string empty;
195  return ( mNameAttribute == mAttributes.end() ) ? empty : mNameAttribute->second;
196  }
197 
206  const XMLElement * getChildByHierarchicalId( const std::string &id ) const;
207 
216  XMLElement * getChildByHierarchicalId( const std::string &id );
217 
221  const XMLElement * getChildByHierarchicalName( const std::string &name ) const;
222 
226  XMLElement * getChildByHierarchicalName( const std::string &name );
227 
232  unsigned int appendChild( XMLElement *newElem );
233 
238  void removeChild( unsigned int idx );
239 
241  void removeChild( const XMLElement *which );
242 
244  void removeChildById( const std::string &id )
245  {
246  removeChild( getChildById( id ) );
247  }
248 
250  void removeChildByName( const std::string &name )
251  {
252  removeChild( getChildByName( name ) );
253  }
254 
256  void clear( bool doAttributes = true );
257 
259  const_attribute_iterator getAttribute( const std::string &name ) const
260  {
261  return mAttributes.find( name );
262  }
263 
265  attribute_iterator getAttribute( const std::string &name )
266  {
267  return mAttributes.find( name );
268  }
269 
271  void removeAttribute( const std::string &name );
272 
274  void setAttribute( const std::string &name, const std::string &value );
275 
277  const std::string& getAttributeValue( const std::string &name ) const
278  {
279  return getAttribute( name )->second;
280  }
281 
283  const std::string& getAttributeValue( const std::string &name, const std::string &def ) const
284  {
286  return ( it == attributesEnd() ) ? def : it->second;
287  }
288 
291  {
292  return mAttributes.begin();
293  }
294 
297  {
298  return mAttributes.begin();
299  }
302  {
303  return mAttributes.end();
304  }
305 
308  {
309  return mAttributes.end();
310  }
311 
314  {
315  return mChildren.begin();
316  }
317 
320  {
321  return mChildren.begin();
322  }
323 
326  {
327  return mChildren.end();
328  }
329 
332  {
333  return mChildren.end();
334  }
335 
343  void rebuildNamedBindings( bool deepScan = true );
344 
345 private:
346  static void JoinMaps( ElementMap &dst, const ElementMap &src );
347 };
348 
350 {
351 public:
353  std::string docType;
354  std::string sysId;
355  std::string pubId;
356 
358  bool dirty;
359 
362 
363 public: XMLDocument() : dirty( false )
364  , root( XMLElement::XET_ROOT ) {}
366 
367 public:
368 
369  //
370  //A series of handy wrappers, to mimic JavaScript.
371  //(mostly for Python GUI event handlers)
372  //
373 
375  void rebuildNamedBindings( bool deepScan = true )
376  {
377  root.rebuildNamedBindings( deepScan );
378  }
379 
381  void removeElementById( const std::string &id )
382  {
383  root.removeChildById( id );
384  }
385 
387  void removeElementByName( const std::string &name )
388  {
389  root.removeChildByName( name );
390  }
391 
393  void clear()
394  {
395  root.clear();
396  }
397 
399  const XMLElement * getElementById( const std::string &id ) const
400  {
401  return root.getChildById( id );
402  }
403 
405  XMLElement * getElementById( const std::string &id )
406  {
407  return root.getChildById( id );
408  }
409 
411  const XMLElement * getElementByName( const std::string &name ) const
412  {
413  return root.getChildByName( name );
414  }
415 
417  XMLElement * getElementByName( const std::string &name )
418  {
419  return root.getChildByName( name );
420  }
421 
423  const XMLElement * getElementByHierarchicalId( const std::string &id ) const
424  {
425  return root.getChildByHierarchicalId( id );
426  }
427 
429  XMLElement * getElementByHierarchicalId( const std::string &id )
430  {
431  return root.getChildByHierarchicalId( id );
432  }
433 
435  const XMLElement * getElementByHierarchicalName( const std::string &name ) const
436  {
437  return root.getChildByHierarchicalName( name );
438  }
439 
441  XMLElement * getElementByHierarchicalName( const std::string &name )
442  {
443  return root.getChildByHierarchicalName( name );
444  }
445 };
446 
447 class XMLProcessor;
448 
450 {
451 public:
452  enum Options
453  {
461  };
462  int options;
463 
464 private:
465  void *internals;
466 
467 public: XMLSerializer( const char *encoding = 0, XMLDocument *doc = 0, XMLElement *elem = 0 );
468  virtual ~XMLSerializer();
469 
478  virtual bool parse( const void *buf, unsigned int len );
479 
491  virtual bool importXML( const std::string &path );
492 
499  virtual bool exportXML( std::ostream &stream );
500 
502  bool exportXML( const std::string &filename );
503 
509  virtual void addProcessor( XMLProcessor *proc );
510 
533  virtual bool initialise( const char *encoding = 0, XMLDocument *doc = 0, XMLElement *elem = 0 );
534 
539  virtual XMLDocument * close();
540 };
541 
543 {
544 public: XMLProcessor() {}
545  virtual ~XMLProcessor() {}
546 
548  virtual const std::string& getTarget() const = 0;
549 
558  virtual std::pair< bool, std::string >execute( XMLSerializer *serializer,
559  XMLDocument *doc,
560  XMLElement *current,
561  const std::string &data ) = 0;
562 };
563 }
564 
565 #endif //__XMLDOCUMENT_H__INCLUDED__
566