SASSY  0.0
Software Architecture Support System
 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 - 2017 Brenton Ross
5  *
6  * This file is part of SASSY.
7  *
8  * SASSY 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  * SASSY 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 SASSY. 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 SASSY_RELEASE "V0.1"
39 
40 namespace SASSY
41 {
42 
43 namespace cfi
44 {
45 
47 
55 class Xml
56 {
57 protected:
58 
60  static const int UMASK_RW_RW_R = 0664 ^ 0777;
61 
63  struct stat xmlStat;
64  xmlDocPtr mDoc;
65 
66  //
67  // Support for XPath and namespaces
68  //
69  xmlXPathContextPtr mCtx;
70  static bool xpathinit;
71 
73  void createXPathContext();
74 
78  void registerNamespace( const std::string &prefix, csr uri );
79 
80 protected:
82  Xml();
83 
85 
88  void open( const Path &fname );
89 
91 
95  void create( const Path &fname, csr root_element );
96 
97  bool mDirty;
98  bool mIsOpen;
99 
101 
102  void freeDoc();
103 
105 
110  void setProp( xmlNodePtr node, csr prop, csr val );
111 
113 
119  void setProp( xmlNodePtr node, csr prop, int val );
120 
122 
128  void setProp( xmlNodePtr node, csr prop, double val );
129 
131 
136  void setContent( xmlNodePtr node, csr text );
137 
139 
143  void setCDATAContent( xmlNodePtr node, csr text );
144 
145 
147 
153  void getPropString( xmlNodePtr node, csr prop, std::string & val );
154 
156 
162  void getPropInt( xmlNodePtr node, csr prop, int & val );
163 
165 
171  void getPropShort( xmlNodePtr node, csr prop, short & val );
172 
174 
180  void getPropDouble( xmlNodePtr node, csr prop, double & val );
181 
182 
184 
189  std::string getNodeContent( xmlNodePtr p, int expand=1) const;
190 
192 
196  std::string getNodeName( xmlNodePtr p ) const;
197 
199  xmlNodePtr getRoot();
200 
202 
206  xmlNodePtr getChild( xmlNodePtr node, csr name ) const;
207 
209 
214  int getChildren( xmlNodePtr node, std::vector< xmlNodePtr > & nodes ) const;
215 
217 
221  xmlNodePtr newNode( xmlNodePtr node, csr name );
222 
224 
229  static xmlNodePtr newTextChild( xmlNodePtr node, csr name, csr text );
230 
232 
236  void deleteNode( xmlNodePtr node); // does a deep free
237 
238  // searches for nodes that match the XPath expression.
239  // returns the number found.
240 
242 
246  int find( csr xpath_expression, std::vector< xmlNodePtr > & nodes );
247 
248 public:
249 
251  virtual ~Xml();
252 
254 
258  void setDtd( csr name, const Path &dtd );
259 
260 
262 
267  void getDtdIdentifiers(csr dtdName, std::string & publicId, std::string & systemId);
268 
269  // set compression - valid values are 0 to 9, where 0 is none, 9 is max
270 
272 
275  void setCompression( int rate );
276 
277  // save the xml back to disk
278 
280  void save();
281 
283 
286  void save( const Path &fname ); //associate a (possibly new)
287  //file with this
288 
289 
291 
294  bool dirty() { return mDirty; }
295 
297 
300  void setDirty( bool dt ) { mDirty = dt; };
301 
303 
306  bool safeToSave();
307 
309  void reload();
310 };
311 
312 } // namespace cfi
313 } // end namespace SASSY
314 
315 #endif /* XML_H_ */
A C++ wrapper for libxml2.
Definition: xml.h:55
Path mFilename
the XML file that the object represents
Definition: xml.h:62
void registerNamespace(const std::string &prefix, csr uri)
Definition: xml.cpp:54
void getDtdIdentifiers(csr dtdName, std::string &publicId, std::string &systemId)
Get the public identifier for the DTD.
Definition: xml.cpp:440
void setProp(xmlNodePtr node, csr prop, csr val)
Set a string property.
Definition: xml.cpp:137
void getPropString(xmlNodePtr node, csr prop, std::string &val)
Get the property value as a string.
Definition: xml.cpp:183
void getPropShort(xmlNodePtr node, csr prop, short &val)
Get the property value as a short integer.
Definition: xml.cpp:209
xmlNodePtr getRoot()
Get the root node.
Definition: xml.cpp:277
void getPropDouble(xmlNodePtr node, csr prop, double &val)
Get the property value as a double.
Definition: xml.cpp:229
void create(const Path &fname, csr root_element)
Create a new xml file and document.
Definition: xml.cpp:106
void freeDoc()
release the document from memory
Definition: xml.cpp:123
Useful string functions.
void setContent(xmlNodePtr node, csr text)
Set the content for the node.
Definition: xml.cpp:166
Manipulate path strings.
Definition: stringy.h:67
virtual ~Xml()
Destructor.
Definition: xml.cpp:372
Xml()
Constructor.
Definition: xml.cpp:66
xmlDocPtr mDoc
pointer to the root document object
Definition: xml.h:64
void getPropInt(xmlNodePtr node, csr prop, int &val)
Get the property value as an integer.
Definition: xml.cpp:196
void open(const Path &fname)
Open an existing XML file.
Definition: xml.cpp:84
bool safeToSave()
Check if can save without clobbering other input.
Definition: xml.cpp:513
void createXPathContext()
Call this whenever mDoc is explicitly updated.
Definition: xml.cpp:41
int mCompression
0 is uncompressed, thru 9 for max zlib compression
Definition: xml.h:100
xmlXPathContextPtr mCtx
XML XPath context.
Definition: xml.h:69
void setCDATAContent(xmlNodePtr node, csr text)
Set the content for the node using CDATA.
Definition: xml.cpp:174
void deleteNode(xmlNodePtr node)
Delete a node and all of its children.
Definition: xml.cpp:333
std::string getNodeContent(xmlNodePtr p, int expand=1) const
Get the content of the node.
Definition: xml.cpp:245
int getChildren(xmlNodePtr node, std::vector< xmlNodePtr > &nodes) const
Get the children of the node.
Definition: xml.cpp:299
xmlNodePtr newNode(xmlNodePtr node, csr name)
Create a new node.
Definition: xml.cpp:313
void setDtd(csr name, const Path &dtd)
Set DTD identifier to dtd, which is just the dtd file name.
Definition: xml.cpp:402
void reload()
Reload the XML file.
Definition: xml.cpp:526
struct stat xmlStat
time the file was last accessed by us
Definition: xml.h:63
static bool xpathinit
flag to indicate if xpath has been initialized
Definition: xml.h:70
int find(csr xpath_expression, std::vector< xmlNodePtr > &nodes)
Find nodes matching an XPath.
Definition: xml.cpp:345
bool mIsOpen
true if the file is open
Definition: xml.h:98
static xmlNodePtr newTextChild(xmlNodePtr node, csr name, csr text)
Create a new node with text content.
Definition: xml.cpp:324
void setCompression(int rate)
Set the compression rate for saving the document.
Definition: xml.cpp:453
const std::string & csr
Save some time typing and shorten parameter lines.
Definition: stringy.h:35
xmlNodePtr getChild(xmlNodePtr node, csr name) const
Get a child node by name.
Definition: xml.cpp:286
std::string getNodeName(xmlNodePtr p) const
Get the name of the node.
Definition: xml.cpp:266
static const int UMASK_RW_RW_R
Default umask for xml files created.
Definition: xml.h:60
bool dirty()
Check if the document has been modified.
Definition: xml.h:294
bool mDirty
true when an update has been made, and saving is required.
Definition: xml.h:97
void save()
Save the document.
Definition: xml.cpp:460
void setDirty(bool dt)
Access to dirty flag if we change document outside of this interface.
Definition: xml.h:300