VICI  0.11.815
Visual Chart Interpreter
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
xml.h
Go to the documentation of this file.
1 /*
2  * xml.h
3  *
4  * Copyright 2001 - 2016 Brenton Ross
5  *
6  * This file is part of VICI.
7  *
8  * VICI is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * VICI is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with VICI. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef CFI_XML_H
28 #define CFI_XML_H
29 
30 #include <libxml/tree.h>
31 #include <libxml/xpath.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include "stringy.h"
35 #include <vector>
36 
38 #define VICI_RELEASE "V0.1"
39 
40 namespace VICI
41 {
42 
43 namespace cfi
44 {
45 
47 
54 class XmlBuffer
55 {
56 public:
57  virtual ~XmlBuffer(){}
58  virtual char *addr() = 0;
59  virtual int len() = 0;
60  virtual void addr(char *) = 0;
61  virtual void len(int) = 0;
62 };
63 
65 
73 class Xml
74 {
75 protected:
76 
78  static const int UMASK_RW_RW_R = 0664 ^ 0777;
79 
81  struct stat xmlStat;
82  xmlDocPtr mDoc;
83 
84  //
85  // Support for XPath and namespaces
86  //
87  xmlXPathContextPtr mCtx;
88  static bool xpathinit;
89 
91  void createXPathContext();
92 
96  void registerNamespace( const std::string &prefix, csr uri );
97 
98 protected:
100  Xml();
101 
103 
106  void open( const Path &fname );
107 
109 
113  void create( const Path &fname, csr root_element );
114 
116  void unpack( XmlBuffer & );
117 
118  bool mDirty;
119  bool mIsOpen;
120 
122 
123  void freeDoc();
124 
126 
131  void setProp( xmlNodePtr node, csr prop, csr val );
132 
134 
140  void setProp( xmlNodePtr node, csr prop, int val );
141 
143 
149  void setProp( xmlNodePtr node, csr prop, double val );
150 
152 
157  void setContent( xmlNodePtr node, csr text );
158 
160 
164  void setCDATAContent( xmlNodePtr node, csr text );
165 
166 
168 
174  void getPropString( xmlNodePtr node, csr prop, std::string & val );
175 
177 
183  void getPropInt( xmlNodePtr node, csr prop, int & val );
184 
186 
192  void getPropShort( xmlNodePtr node, csr prop, short & val );
193 
195 
201  void getPropDouble( xmlNodePtr node, csr prop, double & val );
202 
203 
205 
210  std::string getNodeContent( xmlNodePtr p, int expand=1) const;
211 
213 
217  std::string getNodeName( xmlNodePtr p ) const;
218 
220  xmlNodePtr getRoot();
221 
223 
227  xmlNodePtr getChild( xmlNodePtr node, csr name ) const;
228 
230 
235  int getChildren( xmlNodePtr node, std::vector< xmlNodePtr > & nodes ) const;
236 
238 
242  xmlNodePtr newNode( xmlNodePtr node, csr name );
243 
245 
250  static xmlNodePtr newTextChild( xmlNodePtr node, csr name, csr text );
251 
253 
257  void deleteNode( xmlNodePtr node); // does a deep free
258 
259  // searches for nodes that match the XPath expression.
260  // returns the number found.
261 
263 
267  int find( csr xpath_expression, std::vector< xmlNodePtr > & nodes );
268 
269 public:
270 
272  virtual ~Xml();
273 
275 
279  void setDtd( csr name, const Path &dtd );
280 
282 
287  void setDtd( csr name, csr ident, csr url );
288 
289 
291 
296  void getDtdIdentifiers(csr dtdName, std::string & publicId, std::string & systemId);
297 
298  // set compression - valid values are 0 to 9, where 0 is none, 9 is max
299 
301 
304  void setCompression( int rate );
305 
306  // save the xml back to disk
307 
309  void save();
310 
312 
315  void save( const Path &fname ); //associate a (possibly new)
316  //file with this
317 
319  void pack( XmlBuffer & );
320 
322 
325  bool dirty() { return mDirty; }
326 
328 
331  void setDirty( bool dt ) { mDirty = dt; };
332 
334 
337  bool safeToSave();
338 
340  void reload();
341 };
342 
343 } // namespace cfi
344 } // end namespace VICI
345 
346 #endif /* XML_H_ */
int find(csr xpath_expression, std::vector< xmlNodePtr > &nodes)
Find nodes matching an XPath.
Definition: xml.cpp:368
bool mDirty
true when an update has been made, and saving is required.
Definition: xml.h:118
Path mFilename
the XML file that the object represents
Definition: xml.h:80
void setDirty(bool dt)
Access to dirty flag if we change document outside of this interface.
Definition: xml.h:331
An abstract class defining a buffer object.
Definition: xml.h:54
static xmlNodePtr newTextChild(xmlNodePtr node, csr name, csr text)
Create a new node with text content.
Definition: xml.cpp:347
void setDtd(csr name, const Path &dtd)
Set DTD identifier to dtd, which is just the dtd file name.
Definition: xml.cpp:425
Useful string functions.
virtual char * addr()=0
Get the address of the data.
xmlNodePtr newNode(xmlNodePtr node, csr name)
Create a new node.
Definition: xml.cpp:336
bool dirty()
Check if the document has been modified.
Definition: xml.h:325
virtual ~XmlBuffer()
Ensure derived classes have a destructor.
Definition: xml.h:57
static bool xpathinit
flag to indicate if xpath has been initialized
Definition: xml.h:88
void unpack(XmlBuffer &)
Create a document from the content of a buffer.
Definition: xml.cpp:125
void registerNamespace(const std::string &prefix, csr uri)
Definition: xml.cpp:54
xmlNodePtr getChild(xmlNodePtr node, csr name) const
Get a child node by name.
Definition: xml.cpp:309
int mCompression
0 is uncompressed, thru 9 for max zlib compression
Definition: xml.h:121
void setProp(xmlNodePtr node, csr prop, csr val)
Set a string property.
Definition: xml.cpp:160
xmlXPathContextPtr mCtx
XML XPath context.
Definition: xml.h:87
void getPropInt(xmlNodePtr node, csr prop, int &val)
Get the property value as an integer.
Definition: xml.cpp:219
void getPropShort(xmlNodePtr node, csr prop, short &val)
Get the property value as a short integer.
Definition: xml.cpp:232
std::string getNodeContent(xmlNodePtr p, int expand=1) const
Get the content of the node.
Definition: xml.cpp:268
bool mIsOpen
true if the file is open
Definition: xml.h:119
xmlNodePtr getRoot()
Get the root node.
Definition: xml.cpp:300
void getPropString(xmlNodePtr node, csr prop, std::string &val)
Get the property value as a string.
Definition: xml.cpp:206
Manipulate path strings.
Definition: stringy.h:74
void getDtdIdentifiers(csr dtdName, std::string &publicId, std::string &systemId)
Get the public identifier for the DTD.
Definition: xml.cpp:493
void create(const Path &fname, csr root_element)
Create a new xml file and document.
Definition: xml.cpp:108
void deleteNode(xmlNodePtr node)
Delete a node and all of its children.
Definition: xml.cpp:356
virtual ~Xml()
Destructor.
Definition: xml.cpp:395
void reload()
Reload the XML file.
Definition: xml.cpp:604
virtual int len()=0
Get the length of the data.
void pack(XmlBuffer &)
Save the document into a buffer.
Definition: xml.cpp:566
void setContent(xmlNodePtr node, csr text)
Set the content for the node.
Definition: xml.cpp:189
void getPropDouble(xmlNodePtr node, csr prop, double &val)
Get the property value as a double.
Definition: xml.cpp:252
struct stat xmlStat
time the file was last accessed by us
Definition: xml.h:81
xmlDocPtr mDoc
pointer to the root document object
Definition: xml.h:82
void setCDATAContent(xmlNodePtr node, csr text)
Set the content for the node using CDATA.
Definition: xml.cpp:197
void open(const Path &fname)
Open an existing XML file.
Definition: xml.cpp:86
static const int UMASK_RW_RW_R
Default umask for xml files created.
Definition: xml.h:78
const std::string & csr
short cut for string constants
Definition: vici.h:80
void save()
Save the document.
Definition: xml.cpp:513
void freeDoc()
release the document from memory
Definition: xml.cpp:146
A C++ wrapper for libxml2.
Definition: xml.h:73
int getChildren(xmlNodePtr node, std::vector< xmlNodePtr > &nodes) const
Get the children of the node.
Definition: xml.cpp:322
std::string getNodeName(xmlNodePtr p) const
Get the name of the node.
Definition: xml.cpp:289
bool safeToSave()
Check if can save without clobbering other input.
Definition: xml.cpp:591
void setCompression(int rate)
Set the compression rate for saving the document.
Definition: xml.cpp:506
void createXPathContext()
Call this whenever mDoc is explicitly updated.
Definition: xml.cpp:41
Xml()
Constructor.
Definition: xml.cpp:66