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