BOSS 7.1.1
BESIII Offline Software System
Loading...
Searching...
No Matches
Dom.h
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/xmlBase/xmlBase/Dom.h,v 1.1.1.1 2005/10/17 06:10:27 maqm Exp $
2// Author: J. Bogart
3
4#ifndef xmlBase_Dom_h
5#define xmlBase_Dom_h
6#include <xercesc/util/XercesDefs.hpp>
7
8XERCES_CPP_NAMESPACE_BEGIN
9class DOMElement;
10class DOMNode;
11class XMLLCPTranscoder;
12// class XERCES_CPP_NAMESPACE_QUALIFIER DOMNodeList;
13class DOMDocument;
14XERCES_CPP_NAMESPACE_END
15#include <string>
16#include <iostream>
17#include <vector>
18#include <map>
19
20// typedef DOM_Element DomElement;
21// typedef XERCES_CPP_NAMESPACE_QUALIFIER DOMElement DomElement
22// typedef XERCES_CPP_NAMESPACE_QUALIFIER DOMNode DomNode;
23// typedef XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument DomDocuemnt;
24// typedef DOMString DomString;
25// typedef DOMNodeList DomNodeList;
26// typedef XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument DomDocument;
27namespace xmlBase {
28 //! Base exception class for Dom
29 class DomException : std::exception {
30 public:
31 DomException(const std::string& extraInfo = "") : std::exception(),
32 m_name("DomException"), m_extra(extraInfo) {}
33 virtual ~DomException() throw() {}
34 virtual std::string getMsg() {
35 std::string msg = m_name + ": " + m_extra;
36 return msg;}
37 virtual const char* what() {
38 return m_extra.c_str();
39 }
40 protected:
41 std::string m_name;
42 private:
43 std::string m_extra;
44 };
45
46 class NullNode : public DomException {
47 public:
48 NullNode(const std::string& extraInfo = "") : DomException(extraInfo)
49 {m_name = "NullNode";}
50 virtual ~NullNode() throw() {}
51 };
52
54 public:
55 WrongAttributeType(const std::string& extraInfo = "") :
56 DomException(extraInfo)
57 {
58 m_name = "WrongAttributeType";
59 }
60 virtual ~WrongAttributeType() throw() {}
61 };
62
63 class WrongNodeType : public DomException {
64 public:
65 WrongNodeType(const std::string& extraInfo = "") :
66 DomException(extraInfo)
67 {
68 m_name = "WrongNodeType";
69 }
70 virtual ~WrongNodeType() throw() {}
71 };
72
73 // XERCES_CPP_NAMESPACE_USE
74 using XERCES_CPP_NAMESPACE_QUALIFIER DOMElement;
75 using XERCES_CPP_NAMESPACE_QUALIFIER DOMNode;
76 using XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument;
77 //! This class is just a convenient place for some static functions
78 //! and a static buffer to be used as temp space for transcoding
79 //! which beef up the DOM interface a bit
80 class Dom {
81
82 public:
83 /*! Find first child with given tagname. Only immediate children
84 qualify.
85 \param parent Element whose child is to be found
86 \param name name to look for (may be *, in which case
87 first child, whatever its tagname, will
88 be returned)
89 */
90 static DOMElement* findFirstChildByName(const DOMElement* parent,
91 const char* const name);
92 static DOMElement* findFirstChildByName(const DOMElement* parent,
93 const std::string name);
94
95 //! Return next element sibling, if any.
96 /*! DOM api doesn't provide a direct way to get next sibling which
97 is also an element.
98 */
99 static DOMElement* getSiblingElement(const DOMNode* child);
100
101 //! Get first child which is an element node, if any.
102 static DOMElement* getFirstChildElement(const DOMNode* parent);
103
104 static DOMElement* getElementById(const DOMDocument* doc,
105 const std::string& id);
106
107 //! Create std::string holding node name (if that makes sense
108 //! for this node). Throws NullNode exception if argument is null
109 static std::string getNodeName(const DOMNode* elt);
110
111 //! Create std::string holding tag name (= node name for
112 //! the base DomNode). Throws NullNode exception if argument is null.
113 static std::string getTagName(const DOMElement* node);
114
115
116 //! See if element has expected tag name or not.
117 //! Throws NullNode exception if argument is null
118 static bool checkTagName(const DOMElement* element,
119 const std::string& tagName);
120
121 //! Fill supplied vector with all (immediate) child elements matching
122 //! requested tagname. If tagName == "*" returns all immediate
123 //! children. By default clear vector first.
124 static void getChildrenByTagName(const DOMElement* parent,
125 const std::string& tagName,
126 std::vector<DOMElement*>& children,
127 bool clear = true);
128
129 //! Fill supplied vector with all descendant elements matching
130 //! requested tagname. if tagNmae = "*" returns all descendant
131 //! elements. By default clear vector first.
132 static void getDescendantsByTagName(const DOMElement* parent,
133 const std::string& tagName,
134 std::vector<DOMElement*>& children,
135 bool clear = true);
136
137 //! Fill supplied map with all attribute nodes, where key is
138 //! attribute name. By default clear map first.
139 static void getAttributeNodeMap(const DOMNode* elt,
140 std::map<std::string, DOMNode*>& atts,
141 bool clear = true);
142
143 //! Return true if arg is valid element and has specified
144 //! attribute, else false
145 static bool hasAttribute(const DOMNode* elt, const char* attName);
146
147 //! Return std::string form of attribute value (standard DOM
148 //! interface deals only in DOMString type). Return null string
149 //! if attribute isn't found.
150 static std::string getAttribute(const DOMElement* elt,
151 const char* attName);
152 //! Return std::string form of attribute value (standard DOM
153 //! interface deals only in DOMString type). Return null string
154 //! if attribute isn't found.
155 static std::string getAttribute(const DOMElement* elt,
156 std::string attName);
157
158 //! Return std::string form of attribute value (standard DOM
159 //! interface deals only in DOMString type) or null if \b elt
160 //! is not a DOM_Element or attribute is not found.
161 static std::string getAttribute(const DOMNode* elt,
162 const char* attName);
163 //! Return std::string form of attribute value (standard DOM
164 //! interface deals only in DOMString type) or null if \b elt
165 //! is not a DOM_Element or attribute isn't found
166 static std::string getAttribute(const DOMNode* elt,
167 std::string attName);
168
169 //! Return double attribute value. Throw exception if 1) not element
170 //! node 2) attribute doesn't exist 3) value isn't really a double
171 static double getDoubleAttribute(const DOMNode* elt, std::string attName);
172
173 //! Return doubles in user-supplied vector. Attribute value
174 //! should be a string consisting of valid ascii representation of
175 //! floating point numbers, separated by blanks; else the method
176 //! will throw an exception (xml::WrongAttributeType)
177 //! @returns Number of good values found
178 static unsigned getDoublesAttribute(const DOMNode* elt,
179 std::string attName,
180 std::vector<double>& values,
181 bool clear=true);
182
183 //! Return doubles in user-supplied vector. Attribute value
184 //! should be a string consisting of valid ascii representation of
185 //! floating point numbers, separated by blanks; else the method
186 //! will throw an exception (xml::WrongAttributeType)
187 //! @returns Number of good values found
188 static unsigned getFloatsAttribute(const DOMNode* elt,
189 std::string attName,
190 std::vector<float>& values,
191 bool clear=true);
192
193 //! Return int attribute value. Throw exception if 1) not element
194 //! node 2) attribute doesn't exist 3) value isn't really an int
195 static int getIntAttribute(const DOMNode* elt, std::string attName);
196
197 //! Return ints in user-supplied vector. Attribute value
198 //! should be a string consisting of valid ascii representation of
199 //! floating point numbers, separated by blanks; else the method
200 //! will throw an exception (xml::WrongAttributeType)
201 //! @returns Number of good values found
202 static unsigned getIntsAttribute(const DOMNode* elt,
203 std::string attName,
204 std::vector<int>& values,
205 bool clear=true);
206
207 //! Return value of a text node (the text) as a standard string;
208 //! throw an exception if it's not a text-type node (TEXT_NODE,
209 //! CDATA_SECTION_NODE, COMMENT_NODE are ok)
210 static std::string getText(const DOMNode* textNode);
211
212 //! Return value of text node (the text) for text node which
213 //! is child of the supplied element argument. Element should
214 //! have only text content, not mixed content.
215 static std::string getTextContent(const DOMElement* elt);
216
217 //! Add attribute of type double to a DOM element, DOMString att name
218 // static void addAttribute(DOMElement* elt, const DomString& name,
219 // double value);
220
221 //! Add attribute of type double to a DOM element, std::string att name
222 static void addAttribute(DOMElement* elt, std::string name,
223 double value);
224
225 //! Add attribute of type \c int to a DOM element
226 static void addAttribute(DOMElement* elt, std::string name,
227 int value);
228
229 //! Add attribute of type \c unsigned \c int to a DOM element
230 static void addAttribute(DOMElement* elt, std::string name,
231 unsigned int value);
232
233 //! Add an attribute of type \c string to a DOM element
234 static void addAttribute(DOMElement* elt, std::string name,
235 std::string value);
236
237 //! Add an attribute of type \c char* to a DOM element
238 static void addAttribute(DOMElement* elt, std::string name,
239 const char * const value);
240
241
242
243 // Could get rid of prefix argument for non-pretty print
244 //! (Re)serialize and output a DOM node and its children
245 //! to the specified stream.
246 /*! To output an entire document, invoke this routine with
247 the document element as first argument.
248
249 This routine works best for DOM representations which
250 already include formatting (end-of-lines and indentation).
251 */
252 static void printElement(DOMNode* elt, std::ostream& out);
253
254 //! (Re)serialize and output a DOM node and its children
255 //! to the specified stream.
256 /*! To output an entire document, invoke this routine with
257 the document element as first argument.
258
259 This routine works best for DOM representations which
260 do not already include formatting (end-of-lines and indentation).
261 */
262 static void prettyPrintElement(DOMNode* elt, std::ostream& out,
263 std::string prefix);
264
265 /** Utility to remove all children of elt (but not elt itself).
266 */
267 static void prune(DOMElement* elt);
268
269 // Make EResolver, XmlParser and XmlErrorHandler friends so they can
270 // use transcode methods
271 // friend class EResolver;
272 // friend class XmlErrorHandler;
273 // friend class XmlParser;
274 private:
275 /// Return pointer to transcoded DOMString.
276 /** For temporary use only, such as immediately writing to some
277 output. Applications needing permanence should copy the char array.
278 Passing in a null argument is a fatal error */
279
280 /// Return pointer to transcoded XMLCh string.
281 static char* transToChar(const XMLCh* const str);
282 static char* transToChar(const XMLCh* const str, unsigned int len);
283
284
285 /// Transcode \b from character string \b to XMLCh string.
286 /** Passing in a null argument is a fatal error. */
287 static XMLCh* transToXMLCh(const char* const src);
288
289
290 static unsigned int transBufSize;
291 static char* transBuf; /*< output buffer for \b transToChar */
292 static unsigned int xmlchBufSize;
293 static XMLCh* xmlchBuf; /*< output buffer for \b transToXMLCh */
294 static XERCES_CPP_NAMESPACE_QUALIFIER XMLLCPTranscoder* transcoder;
295 static int initTrans(); /*< set up buffers, make transcoder */
296
297 static XMLCh* xmlchStar;
298
299
300 };
301} // end namespace
302#endif
Base exception class for Dom.
Definition Dom.h:29
DomException(const std::string &extraInfo="")
Definition Dom.h:31
virtual ~DomException()
Definition Dom.h:33
std::string m_name
Definition Dom.h:41
virtual std::string getMsg()
Definition Dom.h:34
virtual const char * what()
Definition Dom.h:37
static std::string getTagName(const DOMElement *node)
Definition Dom.cxx:142
static void getChildrenByTagName(const DOMElement *parent, const std::string &tagName, std::vector< DOMElement * > &children, bool clear=true)
Definition Dom.cxx:160
static void addAttribute(DOMElement *elt, std::string name, double value)
Add attribute of type double to a DOM element, DOMString att name.
Definition Dom.cxx:392
static int getIntAttribute(const DOMNode *elt, std::string attName)
Definition Dom.cxx:326
static DOMElement * getSiblingElement(const DOMNode *child)
Return next element sibling, if any.
Definition Dom.cxx:96
static unsigned getFloatsAttribute(const DOMNode *elt, std::string attName, std::vector< float > &values, bool clear=true)
Definition Dom.cxx:302
static std::string getTextContent(const DOMElement *elt)
Definition Dom.cxx:385
static void prettyPrintElement(DOMNode *elt, std::ostream &out, std::string prefix)
Definition Dom.cxx:644
static void getAttributeNodeMap(const DOMNode *elt, std::map< std::string, DOMNode * > &atts, bool clear=true)
Definition Dom.cxx:196
static void printElement(DOMNode *elt, std::ostream &out)
Definition Dom.cxx:570
static bool hasAttribute(const DOMNode *elt, const char *attName)
Definition Dom.cxx:210
static double getDoubleAttribute(const DOMNode *elt, std::string attName)
Definition Dom.cxx:263
static DOMElement * getElementById(const DOMDocument *doc, const std::string &id)
Definition Dom.cxx:120
static std::string getNodeName(const DOMNode *elt)
Definition Dom.cxx:128
static std::string getAttribute(const DOMElement *elt, const char *attName)
Definition Dom.cxx:222
static DOMElement * findFirstChildByName(const DOMElement *parent, const char *const name)
Definition Dom.cxx:60
static void getDescendantsByTagName(const DOMElement *parent, const std::string &tagName, std::vector< DOMElement * > &children, bool clear=true)
Definition Dom.cxx:178
static unsigned getDoublesAttribute(const DOMNode *elt, std::string attName, std::vector< double > &values, bool clear=true)
Definition Dom.cxx:279
static DOMElement * getFirstChildElement(const DOMNode *parent)
Get first child which is an element node, if any.
Definition Dom.cxx:115
static unsigned getIntsAttribute(const DOMNode *elt, std::string attName, std::vector< int > &values, bool clear=true)
Definition Dom.cxx:342
static void prune(DOMElement *elt)
Definition Dom.cxx:732
static std::string getText(const DOMNode *textNode)
Definition Dom.cxx:366
static bool checkTagName(const DOMElement *element, const std::string &tagName)
Definition Dom.cxx:146
NullNode(const std::string &extraInfo="")
Definition Dom.h:48
virtual ~NullNode()
Definition Dom.h:50
WrongAttributeType(const std::string &extraInfo="")
Definition Dom.h:55
virtual ~WrongAttributeType()
Definition Dom.h:60
WrongNodeType(const std::string &extraInfo="")
Definition Dom.h:65
virtual ~WrongNodeType()
Definition Dom.h:70