BOSS 6.6.4.p03
BESIII Offline Software System
Loading...
Searching...
No Matches
xmlBase::Dom Class Reference

#include <Dom.h>

Static Public Member Functions

static DOMElement * findFirstChildByName (const DOMElement *parent, const char *const name)
 
static DOMElement * findFirstChildByName (const DOMElement *parent, const std::string name)
 
static DOMElement * getSiblingElement (const DOMNode *child)
 Return next element sibling, if any.

 
static DOMElement * getFirstChildElement (const DOMNode *parent)
 Get first child which is an element node, if any.
 
static DOMElement * getElementById (const DOMDocument *doc, const std::string &id)
 
static std::string getNodeName (const DOMNode *elt)
 
static std::string getTagName (const DOMElement *node)
 
static bool checkTagName (const DOMElement *element, const std::string &tagName)
 
static void getChildrenByTagName (const DOMElement *parent, const std::string &tagName, std::vector< DOMElement * > &children, bool clear=true)
 
static void getDescendantsByTagName (const DOMElement *parent, const std::string &tagName, std::vector< DOMElement * > &children, bool clear=true)
 
static void getAttributeNodeMap (const DOMNode *elt, std::map< std::string, DOMNode * > &atts, bool clear=true)
 
static bool hasAttribute (const DOMNode *elt, const char *attName)
 
static std::string getAttribute (const DOMElement *elt, const char *attName)
 
static std::string getAttribute (const DOMElement *elt, std::string attName)
 
static std::string getAttribute (const DOMNode *elt, const char *attName)
 
static std::string getAttribute (const DOMNode *elt, std::string attName)
 
static double getDoubleAttribute (const DOMNode *elt, std::string attName)
 
static unsigned getDoublesAttribute (const DOMNode *elt, std::string attName, std::vector< double > &values, bool clear=true)
 
static unsigned getFloatsAttribute (const DOMNode *elt, std::string attName, std::vector< float > &values, bool clear=true)
 
static int getIntAttribute (const DOMNode *elt, std::string attName)
 
static unsigned getIntsAttribute (const DOMNode *elt, std::string attName, std::vector< int > &values, bool clear=true)
 
static std::string getText (const DOMNode *textNode)
 
static std::string getTextContent (const DOMElement *elt)
 
static void addAttribute (DOMElement *elt, std::string name, double value)
 Add attribute of type double to a DOM element, DOMString att name.
 
static void addAttribute (DOMElement *elt, std::string name, int value)
 Add attribute of type int to a DOM element.
 
static void addAttribute (DOMElement *elt, std::string name, unsigned int value)
 Add attribute of type unsigned int to a DOM element.
 
static void addAttribute (DOMElement *elt, std::string name, std::string value)
 Add an attribute of type string to a DOM element.
 
static void addAttribute (DOMElement *elt, std::string name, const char *const value)
 Add an attribute of type char* to a DOM element.
 
static void printElement (DOMNode *elt, std::ostream &out)
 
static void prettyPrintElement (DOMNode *elt, std::ostream &out, std::string prefix)
 
static void prune (DOMElement *elt)
 

Detailed Description

This class is just a convenient place for some static functions and a static buffer to be used as temp space for transcoding which beef up the DOM interface a bit

Definition at line 80 of file Dom.h.

Member Function Documentation

◆ addAttribute() [1/5]

void xmlBase::Dom::addAttribute ( DOMElement *  elt,
std::string  name,
const char *const  value 
)
static

Add an attribute of type char* to a DOM element.

Definition at line 500 of file Dom.cxx.

501 {
502
503 if (elt == 0) {
504 throw NullNode("from xmlBase::Dom::addAttribute. Null argument");
505 }
506
507 XMLCh* xmlchValue = XMLString::transcode(value);
508 XMLCh* xmlchName = XMLString::transcode(name.c_str());
509
510 try {
511 elt->setAttribute(xmlchName, xmlchValue);
512 }
513 catch (DOMException ex) {
514 char* msg = XMLString::transcode(ex.msg);
515 std::cerr << "DOMException in xmlBase::Dom::addAttribute(char*)"
516 << std::endl;
517 std::cerr << "Msg: " << std::string(msg) << "Code: "
518 << ex.code << std::endl;
519 std::cerr.flush();
520 XMLString::release(&msg);
521 throw ex;
522 }
523
524 XMLString::release(&xmlchName);
525 XMLString::release(&xmlchValue);
526
527 }

◆ addAttribute() [2/5]

void xmlBase::Dom::addAttribute ( DOMElement *  elt,
std::string  name,
double  value 
)
static

Add attribute of type double to a DOM element, DOMString att name.

Add attribute of type double to a DOM element, std::string att name

Definition at line 392 of file Dom.cxx.

393 {
394 if (elt == 0) {
395 throw NullNode("from xmlBase::Dom::addAttribute. null argument");
396 }
397
398 //#ifdef DEFECT_NO_STRINGSTREAM
399 // std::strstream s;
400 // s << value << '\0';
401 //#else
402 std::ostringstream s;
403 s << value;
404 //#endif
405
406 std::string str = s.str();
407 XMLCh* xmlchValue = XMLString::transcode(str.c_str());
408 XMLCh* xmlchName = XMLString::transcode(name.c_str());
409
410 try {
411 elt->setAttribute(xmlchName, xmlchValue);
412 }
413 catch (DOMException ex) {
414 char* msg = XMLString::transcode(ex.msg);
415 std::cerr << "DOMException in xmlBase::Dom::addAttribute(double)" << std::endl;
416 std::cerr << "Msg: " << std::string(msg)
417 << "Code: " << ex.code << std::endl;
418 XMLString::release(&msg);
419 throw ex;
420 }
421 XMLString::release(&xmlchName);
422 XMLString::release(&xmlchValue);
423 }
XmlRpcServer s
Definition: HelloServer.cpp:11

Referenced by addAttribute().

◆ addAttribute() [3/5]

void xmlBase::Dom::addAttribute ( DOMElement *  elt,
std::string  name,
int  value 
)
static

Add attribute of type int to a DOM element.

Definition at line 426 of file Dom.cxx.

427 {
428 if (elt == 0) {
429 throw NullNode("from xmlBase::Dom::addAttribute. Null argument");
430 }
431
432
433 //#ifdef DEFECT_NO_STRINGSTREAM
434 // std::strstream s;
435 // s << value << '\0';
436 //#else
437 std::ostringstream s;
438 s << value;
439 //#endif
440
441 std::string str = s.str();
442
443 XMLCh* xmlchValue = XMLString::transcode(str.c_str());
444 XMLCh* xmlchName = XMLString::transcode(name.c_str());
445
446 try {
447 elt->setAttribute(xmlchName, xmlchValue);
448 }
449 catch (DOMException ex) {
450 char* msg = XMLString::transcode(ex.msg);
451 std::cerr << "DOMException in xmlBase::Dom::addAttribute(int)"
452 << std::endl;
453 std::cerr << "Msg: " << std::string(msg)
454 << "Code: " << ex.code << std::endl;
455 XMLString::release(&msg);
456 throw ex;
457 }
458
459 XMLString::release(&xmlchName);
460 XMLString::release(&xmlchValue);
461
462 }

◆ addAttribute() [4/5]

void xmlBase::Dom::addAttribute ( DOMElement *  elt,
std::string  name,
std::string  value 
)
static

Add an attribute of type string to a DOM element.

Definition at line 529 of file Dom.cxx.

530 {
531
532 if (elt == 0)
533 throw NullNode("from xmlBase::Dom::addAttribute. Null argument");
534
535 addAttribute(elt, name, value.c_str());
536 }
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

◆ addAttribute() [5/5]

void xmlBase::Dom::addAttribute ( DOMElement *  elt,
std::string  name,
unsigned int  value 
)
static

Add attribute of type unsigned int to a DOM element.

Definition at line 464 of file Dom.cxx.

465 {
466 if (elt == 0) {
467 throw NullNode("from xmlBase::Dom::addAttribute. Null argument");
468 }
469
470 //#ifdef DEFECT_NO_STRINGSTREAM
471 // std::strstream s;
472 // s << value << '\0';
473 //#else
474 std::ostringstream s;
475 s << value;
476 //#endif
477
478 std::string str = s.str();
479
480 XMLCh* xmlchValue = XMLString::transcode(str.c_str());
481 XMLCh* xmlchName = XMLString::transcode(name.c_str());
482
483 try {
484 elt->setAttribute(xmlchName, xmlchValue);
485 }
486 catch (DOMException ex) {
487 char* msg = XMLString::transcode(ex.msg);
488 std::cerr << "DOMException in xmlBase::Dom::addAttribute(unsigned int)"
489 << std::endl;
490 std::cerr << "Msg: " << std::string(msg)
491 << "Code: " << ex.code << std::endl;
492 XMLString::release(&msg);
493 throw ex;
494 }
495 XMLString::release(&xmlchName);
496 XMLString::release(&xmlchValue);
497
498 }

◆ checkTagName()

bool xmlBase::Dom::checkTagName ( const DOMElement *  element,
const std::string &  tagName 
)
static

See if element has expected tag name or not. Throws NullNode exception if argument is null

Definition at line 146 of file Dom.cxx.

147 {
148 if (!element) {
149 throw NullNode("Null dom element argument to xmlBase::Dom::checkTagName");
150 }
151 if (tagName == std::string("*")) return true;
152 XMLCh* xmlchTagName = XMLString::transcode(tagName.c_str());
153 bool ret = XMLString::equals(xmlchTagName, element->getTagName());
154 XMLString::release(&xmlchTagName);
155 return ret;
156 }

Referenced by XmlBaseCnv::findNextDacCol(), and getChildrenByTagName().

◆ findFirstChildByName() [1/2]

DOMElement * xmlBase::Dom::findFirstChildByName ( const DOMElement *  parent,
const char *const  name 
)
static

Find first child with given tagname. Only immediate children qualify.

Parameters
parentElement whose child is to be found
namename to look for (may be *, in which case first child, whatever its tagname, will be returned)

Definition at line 60 of file Dom.cxx.

61 {
62
63 if (!xmlchStar) xmlchStar = XMLString::transcode("*");
64 if (!(parent)) {
65 throw NullNode("from xmlBase::Dom::findFirstChildByName. Null parent arg.");
66 }
67
68 XMLCh* xmlchName = XMLString::transcode(name);
69
70 DOMTreeWalker* walk = makeWalker(parent);
71 DOMElement* child = static_cast<DOMElement*>(walk->firstChild());
72 if (XMLString::equals(xmlchName, xmlchStar)) return child;
73
74 if (!child) return 0;
75 // otherwise cycle through children, looking for first with right tag name
76 while (! XMLString::equals(child->getNodeName(), xmlchName)) {
77 child = static_cast<DOMElement*>(walk->nextSibling());
78 if (!child) return 0;
79 }
80
81
82 XMLString::release(&xmlchName);
83
84 walk->release();
85 return child;
86 }

Referenced by findFirstChildByName(), XmlBaseCnv::findFirstDacCol(), getFirstChildElement(), main(), and prune().

◆ findFirstChildByName() [2/2]

DOMElement * xmlBase::Dom::findFirstChildByName ( const DOMElement *  parent,
const std::string  name 
)
static

Definition at line 88 of file Dom.cxx.

89 {
90 return findFirstChildByName(parent, name.c_str());
91 }
static DOMElement * findFirstChildByName(const DOMElement *parent, const char *const name)
Definition: Dom.cxx:60

◆ getAttribute() [1/4]

std::string xmlBase::Dom::getAttribute ( const DOMElement *  elt,
const char *  attName 
)
static

Return std::string form of attribute value (standard DOM interface deals only in DOMString type). Return null string if attribute isn't found.

Definition at line 222 of file Dom.cxx.

222 {
223 if (elt == 0) {
224 throw NullNode("from xmlBase::Dom::getAttribute. Null argument");
225 }
226
227 XMLCh* xmlchAttName = XMLString::transcode(attName);
228 const XMLCh* attValue = elt->getAttribute(xmlchAttName);
229 XMLString::release(&xmlchAttName);
230
231 if (attValue == 0) return std::string("");
232
233 char* chrAttValue = XMLString::transcode(attValue);
234 std::string strValue = std::string(chrAttValue);
235 XMLString::release(&chrAttValue);
236 return strValue;
237 }

Referenced by getAttribute(), getDoubleAttribute(), getDoublesAttribute(), getFloatsAttribute(), getIntAttribute(), getIntsAttribute(), rdbModel::MysqlConnection::open(), prettyPrintElement(), and printElement().

◆ getAttribute() [2/4]

std::string xmlBase::Dom::getAttribute ( const DOMElement *  elt,
std::string  attName 
)
static

Return std::string form of attribute value (standard DOM interface deals only in DOMString type). Return null string if attribute isn't found.

Definition at line 240 of file Dom.cxx.

241 {
242
243 return getAttribute(elt, attName.c_str());
244 }
static std::string getAttribute(const DOMElement *elt, const char *attName)
Definition: Dom.cxx:222

◆ getAttribute() [3/4]

std::string xmlBase::Dom::getAttribute ( const DOMNode *  elt,
const char *  attName 
)
static

Return std::string form of attribute value (standard DOM interface deals only in DOMString type) or null if elt is not a DOM_Element or attribute is not found.

Definition at line 246 of file Dom.cxx.

246 {
247 if (elt == 0) {
248 throw NullNode("from xmlBase::Dom::getAttribute. Null argument");
249 }
250
251 if (elt->getNodeType() != DOMNode::ELEMENT_NODE) {
252 throw NullNode("from xmlBase::Dom::getAttribute. Non-element argument");
253 }
254 return getAttribute(static_cast<const DOMElement*>(elt), attName);
255 }

◆ getAttribute() [4/4]

std::string xmlBase::Dom::getAttribute ( const DOMNode *  elt,
std::string  attName 
)
static

Return std::string form of attribute value (standard DOM interface deals only in DOMString type) or null if elt is not a DOM_Element or attribute isn't found

Definition at line 257 of file Dom.cxx.

258 {
259 return getAttribute(elt, attName.c_str());
260 }

◆ getAttributeNodeMap()

void xmlBase::Dom::getAttributeNodeMap ( const DOMNode *  elt,
std::map< std::string, DOMNode * > &  atts,
bool  clear = true 
)
static

Fill supplied map with all attribute nodes, where key is attribute name. By default clear map first.

Definition at line 196 of file Dom.cxx.

198 {
199
200 if (clear) atts.clear();
201 DOMNamedNodeMap* nodeMap = node->getAttributes();
202 unsigned int nAtts = nodeMap->getLength();
203
204 for (unsigned iAtt = 0; iAtt < nAtts; iAtt++) {
205 DOMNode* attNode = nodeMap->item(iAtt);
206 atts[xmlBase::Dom::getNodeName(attNode)] = attNode;
207 }
208 }
static std::string getNodeName(const DOMNode *elt)
Definition: Dom.cxx:128

Referenced by prettyPrintElement(), and printElement().

◆ getChildrenByTagName()

void xmlBase::Dom::getChildrenByTagName ( const DOMElement *  parent,
const std::string &  tagName,
std::vector< DOMElement * > &  children,
bool  clear = true 
)
static

Fill supplied vector with all (immediate) child elements matching requested tagname. If tagName == "*" returns all immediate children. By default clear vector first.

Definition at line 160 of file Dom.cxx.

163 {
164 if (!parent) {
165 throw NullNode("from xmlBase::Dom::getChildrenByTagName. Null parent arg");
166 }
167 if (clear) children.clear();
168 DOMElement* child = getFirstChildElement(parent);
169 while (child != 0) {
170 if (checkTagName(child, tagName)) {
171 children.push_back(child);
172 }
173 child = getSiblingElement(child);
174 }
175 }
static DOMElement * getSiblingElement(const DOMNode *child)
Return next element sibling, if any.
Definition: Dom.cxx:96
static DOMElement * getFirstChildElement(const DOMNode *parent)
Get first child which is an element node, if any.
Definition: Dom.cxx:115
static bool checkTagName(const DOMElement *element, const std::string &tagName)
Definition: Dom.cxx:146

◆ getDescendantsByTagName()

void xmlBase::Dom::getDescendantsByTagName ( const DOMElement *  parent,
const std::string &  tagName,
std::vector< DOMElement * > &  children,
bool  clear = true 
)
static

Fill supplied vector with all descendant elements matching requested tagname. if tagNmae = "*" returns all descendant elements. By default clear vector first.

Definition at line 178 of file Dom.cxx.

181 {
182 if (parent == 0) {
183 throw NullNode("from xmlBase::Dom::getChildrenByTagName. Null parent arg");
184 }
185 if (clear) children.clear();
186 XMLCh* xmlchTagName = XMLString::transcode(tagName.c_str());;
187 DOMNodeList* list =
188 parent->getElementsByTagName(xmlchTagName);
189 XMLString::release(&xmlchTagName);
190 unsigned int nItem = list->getLength();
191 for (unsigned int iItem = 0; iItem < nItem; iItem++) {
192 children.push_back(static_cast<DOMElement *>(list->item(iItem)));
193 }
194 }

◆ getDoubleAttribute()

double xmlBase::Dom::getDoubleAttribute ( const DOMNode *  elt,
std::string  attName 
)
static

Return double attribute value. Throw exception if 1) not element node 2) attribute doesn't exist 3) value isn't really a double

Definition at line 263 of file Dom.cxx.

263 {
264 std::string stringValue = getAttribute(elt, attName);
265
266 if (stringValue == std::string("") ) {
267 throw
268 NullNode("from xmlBase::Dom::getDoubleAttribute. non-existent attribute");
269 }
270 try {
271 return facilities::Util::stringToDouble(stringValue);
272 }
273 catch (facilities::WrongType ex) {
274 std::cerr << ex.getMsg();
275 throw WrongAttributeType("from xmlBase::Dom::getDoubleAttribute");
276 }
277 }
static double stringToDouble(const std::string &InStr)
Exception class used when converting from string to numeric type.

Referenced by main(), and XmlBaseCnv::processValSig().

◆ getDoublesAttribute()

unsigned xmlBase::Dom::getDoublesAttribute ( const DOMNode *  elt,
std::string  attName,
std::vector< double > &  values,
bool  clear = true 
)
static

Return doubles in user-supplied vector. Attribute value should be a string consisting of valid ascii representation of floating point numbers, separated by blanks; else the method will throw an exception (xml::WrongAttributeType)

Returns
Number of good values found

Definition at line 279 of file Dom.cxx.

280 {
281 unsigned nVals = 0;
282 std::vector<std::string> tokens;
283 if (clear) values.clear();
284 std::string stringValue = getAttribute(elt, attName);
285 if (stringValue.size() == 0) return nVals;
286
287 facilities::Util::stringTokenize(stringValue, std::string(" "), tokens);
288
289 try {
290 unsigned nToken = tokens.size();
291 for (unsigned iToken = 0; iToken < nToken; iToken++) {
292 values.push_back(facilities::Util::stringToDouble(tokens[iToken]));
293 nVals++;
294 }
295 }
296 catch (facilities::WrongType ex) {
297 std::cerr << ex.getMsg();
298 throw WrongAttributeType("from xmlBase::Dom::getDoublesAttribute");
299 }
300 return nVals;
301 }
static void stringTokenize(std::string input, const std::string &delimiters, std::vector< std::string > &tokens, bool clear=true)

Referenced by main().

◆ getElementById()

DOMElement * xmlBase::Dom::getElementById ( const DOMDocument *  doc,
const std::string &  id 
)
static

Definition at line 120 of file Dom.cxx.

121 {
122 XMLCh* idChars = XMLString::transcode(id.c_str());
123 DOMElement* elt = doc->getElementById(idChars);
124 XMLString::release(&idChars);
125 return elt;
126 }
char * c_str(Index i)
Definition: EvtCyclic3.cc:252

◆ getFirstChildElement()

DOMElement * xmlBase::Dom::getFirstChildElement ( const DOMNode *  parent)
static

Get first child which is an element node, if any.

Definition at line 115 of file Dom.cxx.

115 {
116 const DOMElement* elt = static_cast<const DOMElement*>(parent);
117 return findFirstChildByName(elt, "*");
118 }

Referenced by getChildrenByTagName(), and xmlBase::DocMan::parse().

◆ getFloatsAttribute()

unsigned xmlBase::Dom::getFloatsAttribute ( const DOMNode *  elt,
std::string  attName,
std::vector< float > &  values,
bool  clear = true 
)
static

Return doubles in user-supplied vector. Attribute value should be a string consisting of valid ascii representation of floating point numbers, separated by blanks; else the method will throw an exception (xml::WrongAttributeType)

Returns
Number of good values found

Definition at line 302 of file Dom.cxx.

303 {
304 unsigned nVals = 0;
305 std::vector<std::string> tokens;
306 if (clear) values.clear();
307 std::string stringValue = getAttribute(elt, attName);
308 if (stringValue.size() == 0) return nVals;
309
310 facilities::Util::stringTokenize(stringValue, std::string(" "), tokens);
311
312 try {
313 unsigned nToken = tokens.size();
314 for (unsigned iToken = 0; iToken < nToken; iToken++) {
315 values.push_back(facilities::Util::stringToDouble(tokens[iToken]));
316 nVals++;
317 }
318 }
319 catch (facilities::WrongType ex) {
320 std::cerr << ex.getMsg();
321 throw WrongAttributeType("from xmlBase::Dom::getDoublesAttribute");
322 }
323 return nVals;
324 }

Referenced by XmlBaseCnv::processValSigs().

◆ getIntAttribute()

int xmlBase::Dom::getIntAttribute ( const DOMNode *  elt,
std::string  attName 
)
static

Return int attribute value. Throw exception if 1) not element node 2) attribute doesn't exist 3) value isn't really an int

Definition at line 326 of file Dom.cxx.

326 {
327 std::string stringValue = getAttribute(elt, attName);
328
329 if (stringValue == std::string("") ) {
330 throw
331 NullNode("from xmlBase::Dom::getIntAttribute. non-existent attribute");
332 }
333 try {
334 return facilities::Util::stringToInt(stringValue);
335 }
336 catch (facilities::WrongType ex) {
337 std::cerr << ex.getMsg();
338 throw WrongAttributeType("from xmlBase::Dom::getIntAttribute");
339 }
340 }
static int stringToInt(const std::string &InStr)

Referenced by main().

◆ getIntsAttribute()

unsigned xmlBase::Dom::getIntsAttribute ( const DOMNode *  elt,
std::string  attName,
std::vector< int > &  values,
bool  clear = true 
)
static

Return ints in user-supplied vector. Attribute value should be a string consisting of valid ascii representation of floating point numbers, separated by blanks; else the method will throw an exception (xml::WrongAttributeType)

Returns
Number of good values found

Definition at line 342 of file Dom.cxx.

343 {
344 unsigned nVals = 0;
345 std::vector<std::string> tokens;
346 if (clear) values.clear();
347 std::string stringValue = getAttribute(elt, attName);
348 if (stringValue.size() == 0) return nVals;
349
350 facilities::Util::stringTokenize(stringValue, std::string(" "), tokens);
351
352 try {
353 unsigned nToken = tokens.size();
354 for (unsigned iToken = 0; iToken < nToken; iToken++) {
355 values.push_back(facilities::Util::stringToInt(tokens[iToken]));
356 nVals++;
357 }
358 }
359 catch (facilities::WrongType ex) {
360 std::cerr << ex.getMsg();
361 throw WrongAttributeType("from xmlBase::Dom::getIntsAttribute");
362 }
363 return nVals;
364 }

Referenced by main().

◆ getNodeName()

std::string xmlBase::Dom::getNodeName ( const DOMNode *  elt)
static

Create std::string holding node name (if that makes sense for this node). Throws NullNode exception if argument is null

Definition at line 128 of file Dom.cxx.

128 {
129 if (!node) {
130 throw NullNode("Null node argument supplied to xmlBase::Dom::getNodeName");
131 }
132 const XMLCh* name = node->getNodeName();
133 if (!name) return std::string("");
134
135 char* chrName = XMLString::transcode(name);
136
137 std::string strName = std::string(chrName);
138 XMLString::release(&chrName);
139 return strName;
140 }

Referenced by getAttributeNodeMap(), getTagName(), prettyPrintElement(), and printElement().

◆ getSiblingElement()

DOMElement * xmlBase::Dom::getSiblingElement ( const DOMNode *  child)
static

Return next element sibling, if any.

DOM api doesn't provide a direct way to get next sibling which is also an element.

Definition at line 96 of file Dom.cxx.

96 {
97 if (!child) {
98 throw NullNode("from xmlBase::Dom::getSiblingElement. Null argument");
99 }
100
101 DOMNode* sib = child->getNextSibling();
102
103 while (sib) {
104 if (sib->getNodeType() == DOMNode::ELEMENT_NODE) {
105 DOMElement* eltSib = static_cast<DOMElement*> (sib);
106
107 return eltSib;
108 }
109 sib = sib->getNextSibling();
110 }
111
112 return 0;
113 }

Referenced by XmlBaseCnv::findNextDacCol(), getChildrenByTagName(), xmlBase::DocMan::parse(), and prune().

◆ getTagName()

std::string xmlBase::Dom::getTagName ( const DOMElement *  node)
static

Create std::string holding tag name (= node name for the base DomNode). Throws NullNode exception if argument is null.

Definition at line 142 of file Dom.cxx.

142 {
143 return Dom::getNodeName(elt);
144 }

Referenced by xmlBase::DocMan::parse().

◆ getText()

std::string xmlBase::Dom::getText ( const DOMNode *  textNode)
static

Return value of a text node (the text) as a standard string; throw an exception if it's not a text-type node (TEXT_NODE, CDATA_SECTION_NODE, COMMENT_NODE are ok)

Definition at line 366 of file Dom.cxx.

366 {
367 short int nType = textNode->getNodeType();
368
369 if ( (nType != DOMNode::TEXT_NODE) &&
370 (nType != DOMNode::COMMENT_NODE) &&
371 (nType != DOMNode::CDATA_SECTION_NODE))
372 {
373 throw WrongNodeType("from xmlBase::Dom::getText, argument not text node");
374 }
375 const XMLCh* t = textNode->getNodeValue();
376 if (t == 0 ) return std::string("");
377 char* chrText = XMLString::transcode(t);
378 std::string text = std::string(chrText);
379 XMLString::release(&chrText);
380
381 return text;
382 }
int t()
Definition: t.c:1

Referenced by getTextContent(), prettyPrintElement(), and printElement().

◆ getTextContent()

std::string xmlBase::Dom::getTextContent ( const DOMElement *  elt)
static

Return value of text node (the text) for text node which is child of the supplied element argument. Element should have only text content, not mixed content.

Definition at line 385 of file Dom.cxx.

385 {
386 if (elt->getNodeType() != DOMNode::ELEMENT_NODE) {
387 throw WrongNodeType("from xmlBase::Dom::getTextContent, argument not element node");
388 }
389 return (getText(elt->getFirstChild()));
390 }
static std::string getText(const DOMNode *textNode)
Definition: Dom.cxx:366

◆ hasAttribute()

bool xmlBase::Dom::hasAttribute ( const DOMNode *  elt,
const char *  attName 
)
static

Return true if arg is valid element and has specified attribute, else false

Definition at line 210 of file Dom.cxx.

210 {
211 bool ret = false;
212 if (node == 0) return ret;
213 if (node->getNodeType() != DOMNode::ELEMENT_NODE ) return ret;
214
215 const DOMElement* elt = static_cast<const DOMElement*>(node);
216 XMLCh* xmlchAttName = XMLString::transcode(attName);
217 ret = (elt->getAttributeNode(xmlchAttName) != 0);
218 XMLString::release(&xmlchAttName);
219 return ret;
220 }

◆ prettyPrintElement()

void xmlBase::Dom::prettyPrintElement ( DOMNode *  elt,
std::ostream &  out,
std::string  prefix 
)
static

(Re)serialize and output a DOM node and its children to the specified stream.

To output an entire document, invoke this routine with the document element as first argument.

This routine works best for DOM representations which do not already include formatting (end-of-lines and indentation).

Definition at line 644 of file Dom.cxx.

645 {
646
647
648 if (node == 0) {
649 throw NullNode("from xmlBase::Dom::prettyPrintElement. Null argument");
650 }
651
652 out << prefix;
653 switch(node->getNodeType()) {
654
655 case DOMNode::ELEMENT_NODE:
656 {
657 // output start tag
658 std::string tagName = getNodeName(node);
659 out << '<' << tagName;
660
661 // ...with attributes
662 typedef std::map<std::string, DOMNode*> AttMap;
663 AttMap atts;
664 AttMap::const_iterator it;
665
666 getAttributeNodeMap(node, atts);
667
668 for (it = atts.begin();
669 it != atts.end(); it++) {
670 std::string attName = (*it).first;
671 out << ' ' << attName << '=';
672 out << '"' << getAttribute(node,attName) << '"';
673 }
674
675 // iterate through children
676 DOMNode* child = node->getFirstChild();
677 if (child != 0) { // there are children
678 out << '>' << std::endl;
679 while (child != 0) {
680 // new indent level
681 Dom::prettyPrintElement(child, out, prefix + " ");
682 child = child->getNextSibling();
683 }
684 // output end tag, long form
685 {
686 std::string endName = getNodeName(node);
687 out << prefix << "</" << endName << ">" << std::endl;
688 }
689 }
690 else { // no children; use short form for the empty tag
691 out << " />" << std::endl;
692 }
693 }
694 break;
695 case DOMNode::TEXT_NODE:
696 // just put it out as is
697 // Note this won't indent correctly if the text node
698 // contains multiple lines.
699 // Similarly, it's too much work to avoid sometimes putting out
700 // an "extra" blank line in the vicinity of text.
701 // Current code puts the extra <cr> before
702 // the text node.
703 {
704 out << getText(node);
705 }
706
707 break;
708
709 case DOMNode::CDATA_SECTION_NODE:
710 {
711 // Probably need to put opening and closing sequences in by hand..
712 out << "<![CDATA[" << getText(node) << "]]>";
713 }
714 break;
715
716 case DOMNode::COMMENT_NODE :
717 // glast.prs doesn't have any comments (but should!)
718 // Note this won't indent correctly if the text node
719 // contains multiple lines. Could have utility routine
720 // to do this, to be called for comments and text nodes
721 {
722 out << "<!-- " << getText(node) << "-->" << std::endl;
723 }
724
725 break;
726 default:
727 // ignore anything else
728 break;
729 }
730 }
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

Referenced by main(), and prettyPrintElement().

◆ printElement()

void xmlBase::Dom::printElement ( DOMNode *  elt,
std::ostream &  out 
)
static

(Re)serialize and output a DOM node and its children to the specified stream.

To output an entire document, invoke this routine with the document element as first argument.

This routine works best for DOM representations which already include formatting (end-of-lines and indentation).

Definition at line 570 of file Dom.cxx.

570 {
571
572 if (node == 0) {
573 throw NullNode("from xmlBase::Dom::printElement. Null argument");
574 }
575
576 switch(node->getNodeType()) {
577 case DOMNode::ELEMENT_NODE:
578 {
579 // output start tag
580 {
581 std::string tagName = getNodeName(node);
582 out << '<' << tagName;
583 }
584 // ...with attributes
585
586 typedef std::map<std::string, DOMNode*> AttMap;
587 AttMap atts;
588 AttMap::const_iterator it;
589 getAttributeNodeMap(node, atts);
590
591 for (it = atts.begin();
592 it != atts.end(); it++) {
593 std::string attName = (*it).first;
594 out << ' ' << attName << '=';
595 out << '"' << getAttribute(node,attName) << '"';
596 }
597
598 // iterate through children
599 DOMNode* child = node->getFirstChild();
600 if (child != 0) { // there are children
601 out << '>';
602 while (child != 0) {
603 Dom::printElement(child, out);
604 child = child->getNextSibling();
605 }
606 // output end tag, long form
607 {
608 std::string endName = getNodeName(node);
609 out << "</" << endName << ">";
610 }
611 }
612 else { // no children; use short form for the empty tag
613 out << " />";
614 }
615 }
616 break;
617 case DOMNode::TEXT_NODE:
618 // just put it out as is
619 {
620 out << getText(node);
621 }
622 break;
623
624 case DOMNode::CDATA_SECTION_NODE:
625 {
626 out << "<![CDATA[" << getText(node) << "]]>";
627 }
628 break;
629
630
631 case DOMNode::COMMENT_NODE :
632 // glast.prs doesn't have any comments (but should!)
633 {
634 out << "<!-- " << getText(node) << "-->";
635 }
636 break;
637 default:
638 // ignore anything else
639 break;
640 }
641 }
static void printElement(DOMNode *elt, std::ostream &out)
Definition: Dom.cxx:570

Referenced by main(), and printElement().

◆ prune()

void xmlBase::Dom::prune ( DOMElement *  elt)
static

Utility to remove all children of elt (but not elt itself).

Definition at line 732 of file Dom.cxx.

732 {
733 DOMElement* child = findFirstChildByName(elt, "*");
734 while (child != 0) {
735 DOMElement* sib = getSiblingElement(child);
736 prune(child);
737 elt->removeChild(child);
738 child = sib;
739 }
740 }
static void prune(DOMElement *elt)
Definition: Dom.cxx:732

Referenced by prune().


The documentation for this class was generated from the following files: