libatomprobe
Library for Atom Probe Tomography (APT) computation
XMLHelper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Daniel Haley
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 /*
18  *
19  */
20 #ifndef ATOMPROBE_XMLHELPER_H
21 #define ATOMPROBE_XMLHELPER_H
22 
24 
25 #include <libxml/xmlreader.h>
26 #include <string>
27 #include <vector>
28 
29 #ifdef DEBUG
30 #include <iostream>
31 #endif
32 
33 
34 namespace AtomProbe {
35 
36 enum
37 {
40 };
41 
42 /* Cheat codes for XMLHelpNextType
43 Enum xmlElementType
44 {
45  XML_ELEMENT_NODE = 1,
46  XML_ATTRIBUTE_NODE = 2,
47  XML_TEXT_NODE = 3,
48  XML_CDATA_SECTION_NODE = 4,
49  XML_ENTITY_REF_NODE = 5,
50  XML_ENTITY_NODE = 6,
51  XML_PI_NODE = 7,
52  XML_COMMENT_NODE = 8,
53  XML_DOCUMENT_NODE = 9,
54  XML_DOCUMENT_TYPE_NODE = 10,
55  XML_DOCUMENT_FRAG_NODE = 11,
56  XML_NOTATION_NODE = 12,
57  XML_HTML_DOCUMENT_NODE = 13,
58  XML_DTD_NODE = 14,
59  XML_ELEMENT_DECL = 15,
60  XML_ATTRIBUTE_DECL = 16,
61  XML_ENTITY_DECL = 17,
62  XML_NAMESPACE_DECL = 18,
63  XML_XINCLUDE_START = 19,
64  XML_XINCLUDE_END = 20,
65  XML_DOCB_DOCUMENT_NODE = 21
66 }
67 */
68 
69 
70 //These functions return nonzero on failure,
71 //zero on success
72 //be warned that the node WILL be modified.
73 unsigned int XMLHelpNextType(xmlNodePtr &node,int);
74 //Jump forward to the element matching "nodename"
75 unsigned int XMLHelpFwdToElem(xmlNodePtr &node, const char *nodeName);
76 //Jump forward to an element that dooesn't match "nodeName"
77 unsigned int XMLHelpFwdNotElem(xmlNodePtr &node,const char *nodeName);
78 //this will match the node name against a list, stopping at the first matching
79 unsigned int XMLHelpFwdToList(xmlNodePtr &node, const std::vector<std::string> &nodeList);
80 //Get the text associated with this node (node->content as a string)
81 std::string XMLHelpGetText(xmlNodePtr &node);
82 
84 void XMLFreeDoc(void* data);
85 
86 //Returns 0 if successful, non zero if there is a property failure, or if the property is empty
87 template<class T> unsigned int XMLHelpGetProp(T &prop,xmlNodePtr node, std::string propName)
88 {
89  xmlChar *xmlString;
90 
91  //grab the xml property
92  xmlString = xmlGetProp(node,(const xmlChar *)propName.c_str());
93 
94  //Check string contents
95  if(!xmlString)
96  return PROP_PARSE_ERR;
97 
98  if(stream_cast(prop,xmlString))
99  {
100  xmlFree(xmlString);
101  return PROP_BAD_ATT;
102  }
103 
104  xmlFree(xmlString);
105 
106  return 0;
107 }
108 
109 
111 template<class T>
112 bool XMLGetNextElemAttrib(xmlNodePtr &nodePtr, T &v, const char *nodeName, const char *attrib)
113 {
114  std::string tmpStr;
115  xmlChar *xmlString;
116  //====
117  if(XMLHelpFwdToElem(nodePtr,nodeName))
118  return false;
119 
120  xmlString=xmlGetProp(nodePtr,(const xmlChar *)attrib);
121  if(!xmlString)
122  return false;
123  tmpStr=(char *)xmlString;
124 
125  if(stream_cast(v,tmpStr))
126  {
127  xmlFree(xmlString);
128  return false;
129  }
130 
131  xmlFree(xmlString);
132 
133  return true;
134 }
135 
136 /* Defined in the bowels of the xmlLib2 library
137  * Enum xmlElementType {
138  * XML_ELEMENT_NODE = 1
139  * XML_ATTRIBUTE_NODE = 2
140  * XML_TEXT_NODE = 3
141  * XML_CDATA_SECTION_NODE = 4
142  * XML_ENTITY_REF_NODE = 5
143  * XML_ENTITY_NODE = 6
144  * XML_PI_NODE = 7
145  * XML_COMMENT_NODE = 8
146  * XML_DOCUMENT_NODE = 9
147  * XML_DOCUMENT_TYPE_NODE = 10
148  * XML_DOCUMENT_FRAG_NODE = 11
149  * XML_NOTATION_NODE = 12
150  * XML_HTML_DOCUMENT_NODE = 13
151  * XML_DTD_NODE = 14
152  * XML_ELEMENT_DECL = 15
153  * XML_ATTRIBUTE_DECL = 16
154  * XML_ENTITY_DECL = 17
155  * XML_NAMESPACE_DECL = 18
156  * XML_XINCLUDE_START = 19
157  * XML_XINCLUDE_END = 20
158  * XML_DOCB_DOCUMENT_NODE = 21
159  * }
160  */
161 };
162 #endif
163 
unsigned int XMLHelpFwdToElem(xmlNodePtr &node, const char *nodeName)
Definition: XMLHelper.cpp:41
unsigned int XMLHelpNextType(xmlNodePtr &node, int)
Definition: XMLHelper.cpp:28
std::string XMLHelpGetText(xmlNodePtr &node)
void XMLFreeDoc(void *data)
Free a xmlDoc pointer. For use in conjunction with std::unique_ptr for auto-deallocation.
Definition: XMLHelper.cpp:23
unsigned int XMLHelpGetProp(T &prop, xmlNodePtr node, std::string propName)
Definition: XMLHelper.h:87
unsigned int XMLHelpFwdNotElem(xmlNodePtr &node, const char *nodeName)
Definition: XMLHelper.cpp:53
bool XMLGetNextElemAttrib(xmlNodePtr &nodePtr, T &v, const char *nodeName, const char *attrib)
Grab the specified attribute from the next element, then stream_cast() it into the passed-in object...
Definition: XMLHelper.h:112
bool stream_cast(T1 &result, const T2 &obj)
Template function to cast and object to another by the stringstream.
Definition: stringFuncs.h:32
unsigned int XMLHelpFwdToList(xmlNodePtr &node, const std::vector< std::string > &nodeList)