BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Dom.cxx
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/xmlBase/src/Dom.cxx,v 1.2 2014/04/18 02:24:47 maqm Exp
2// $ Author: J. Bogart
3//
4// Implementation of xmlBase::Dom, a convenient place to put static
5// utilities which enhance DOM api.
6
7#include "xmlBase/Dom.h"
8#include <xercesc/dom/DOMCharacterData.hpp>
9#include <xercesc/dom/DOMDocument.hpp>
10#include <xercesc/dom/DOMElement.hpp>
11#include <xercesc/dom/DOMException.hpp>
12#include <xercesc/dom/DOMNamedNodeMap.hpp>
13#include <xercesc/dom/DOMNodeList.hpp>
14#include <xercesc/dom/DOMTreeWalker.hpp>
15#include <xercesc/util/PlatformUtils.hpp>
16#include <xercesc/util/TransService.hpp>
17#include <xercesc/util/XMLString.hpp>
18
19#include "facilities/Util.h"
20// #ifdef DEFECT_NO_STRINGSTREAM
21// #include <strstream>
22// #else
23#include <sstream>
24// #endif
25
26#include <cassert>
27#include <cstring>
28#include <string>
29
30namespace {
31 XERCES_CPP_NAMESPACE_USE
32
33 DOMTreeWalker* makeWalker( const DOMNode* node ) {
34 if ( !node ) return 0;
35 DOMDocument* doc = node->getOwnerDocument();
36 unsigned long whatToShow = DOMNodeFilter::SHOW_ELEMENT;
37
38 bool expand = true;
39 DOMNode* casted = const_cast<DOMNode*>( node );
40 // Show only elements; Expand entity references
41 DOMTreeWalker* walk = doc->createTreeWalker( casted, whatToShow, 0, expand );
42 return walk;
43 }
44} // namespace
45
46namespace xmlBase {
47 XERCES_CPP_NAMESPACE_USE
48
49 XMLLCPTranscoder* Dom::transcoder = 0;
50 char* Dom::transBuf = 0;
51
52 unsigned int Dom::transBufSize = 1000;
53 XMLCh* Dom::xmlchBuf = 0;
54
55 unsigned int Dom::xmlchBufSize = 200;
56 XMLCh* Dom::xmlchStar = 0;
57
58 DOMElement* Dom::findFirstChildByName( const DOMElement* parent, const char* const name ) {
59
60 if ( !xmlchStar ) xmlchStar = XMLString::transcode( "*" );
61 if ( !( parent ) )
62 { throw NullNode( "from xmlBase::Dom::findFirstChildByName. Null parent arg." ); }
63
64 XMLCh* xmlchName = XMLString::transcode( name );
65
66 DOMTreeWalker* walk = makeWalker( parent );
67 DOMElement* child = static_cast<DOMElement*>( walk->firstChild() );
68 if ( XMLString::equals( xmlchName, xmlchStar ) ) return child;
69
70 if ( !child ) return 0;
71 // otherwise cycle through children, looking for first with right tag name
72 while ( !XMLString::equals( child->getNodeName(), xmlchName ) )
73 {
74 child = static_cast<DOMElement*>( walk->nextSibling() );
75 if ( !child ) return 0;
76 }
77
78 XMLString::release( &xmlchName );
79
80 walk->release();
81 return child;
82 }
83
84 DOMElement* Dom::findFirstChildByName( const DOMElement* parent, const std::string name ) {
85 return findFirstChildByName( parent, name.c_str() );
86 }
87
88 // Leave this alone for now. Entities are not likely to cause
89 // trouble here.
90 DOMElement* Dom::getSiblingElement( const DOMNode* child ) {
91 if ( !child ) { throw NullNode( "from xmlBase::Dom::getSiblingElement. Null argument" ); }
92
93 DOMNode* sib = child->getNextSibling();
94
95 while ( sib )
96 {
97 if ( sib->getNodeType() == DOMNode::ELEMENT_NODE )
98 {
99 DOMElement* eltSib = static_cast<DOMElement*>( sib );
100
101 return eltSib;
102 }
103 sib = sib->getNextSibling();
104 }
105
106 return 0;
107 }
108
109 DOMElement* Dom::getFirstChildElement( const DOMNode* parent ) {
110 const DOMElement* elt = static_cast<const DOMElement*>( parent );
111 return findFirstChildByName( elt, "*" );
112 }
113
114 DOMElement* Dom::getElementById( const DOMDocument* doc, const std::string& id ) {
115 XMLCh* idChars = XMLString::transcode( id.c_str() );
116 DOMElement* elt = doc->getElementById( idChars );
117 XMLString::release( &idChars );
118 return elt;
119 }
120
121 std::string Dom::getNodeName( const DOMNode* node ) {
122 if ( !node )
123 { throw NullNode( "Null node argument supplied to xmlBase::Dom::getNodeName" ); }
124 const XMLCh* name = node->getNodeName();
125 if ( !name ) return std::string( "" );
126
127 char* chrName = XMLString::transcode( name );
128
129 std::string strName = std::string( chrName );
130 XMLString::release( &chrName );
131 return strName;
132 }
133
134 std::string Dom::getTagName( const DOMElement* elt ) { return Dom::getNodeName( elt ); }
135
136 bool Dom::checkTagName( const DOMElement* element, const std::string& tagName ) {
137 if ( !element )
138 { throw NullNode( "Null dom element argument to xmlBase::Dom::checkTagName" ); }
139 if ( tagName == std::string( "*" ) ) return true;
140 XMLCh* xmlchTagName = XMLString::transcode( tagName.c_str() );
141 bool ret = XMLString::equals( xmlchTagName, element->getTagName() );
142 XMLString::release( &xmlchTagName );
143 return ret;
144 }
145
146 void Dom::getChildrenByTagName( const DOMElement* parent, const std::string& tagName,
147 std::vector<DOMElement*>& children, bool clear ) {
148 if ( !parent )
149 { throw NullNode( "from xmlBase::Dom::getChildrenByTagName. Null parent arg" ); }
150 if ( clear ) children.clear();
151 DOMElement* child = getFirstChildElement( parent );
152 while ( child != 0 )
153 {
154 if ( checkTagName( child, tagName ) ) { children.push_back( child ); }
155 child = getSiblingElement( child );
156 }
157 }
158
159 void Dom::getDescendantsByTagName( const DOMElement* parent, const std::string& tagName,
160 std::vector<DOMElement*>& children, bool clear ) {
161 if ( parent == 0 )
162 { throw NullNode( "from xmlBase::Dom::getChildrenByTagName. Null parent arg" ); }
163 if ( clear ) children.clear();
164 XMLCh* xmlchTagName = XMLString::transcode( tagName.c_str() );
165 ;
166 DOMNodeList* list = parent->getElementsByTagName( xmlchTagName );
167 XMLString::release( &xmlchTagName );
168 unsigned int nItem = list->getLength();
169 for ( unsigned int iItem = 0; iItem < nItem; iItem++ )
170 { children.push_back( static_cast<DOMElement*>( list->item( iItem ) ) ); }
171 }
172
173 void Dom::getAttributeNodeMap( const DOMNode* node, std::map<std::string, DOMNode*>& atts,
174 bool clear ) {
175
176 if ( clear ) atts.clear();
177 DOMNamedNodeMap* nodeMap = node->getAttributes();
178 unsigned int nAtts = nodeMap->getLength();
179
180 for ( unsigned iAtt = 0; iAtt < nAtts; iAtt++ )
181 {
182 DOMNode* attNode = nodeMap->item( iAtt );
183 atts[xmlBase::Dom::getNodeName( attNode )] = attNode;
184 }
185 }
186
187 bool Dom::hasAttribute( const DOMNode* node, const char* attName ) {
188 bool ret = false;
189 if ( node == 0 ) return ret;
190 if ( node->getNodeType() != DOMNode::ELEMENT_NODE ) return ret;
191
192 const DOMElement* elt = static_cast<const DOMElement*>( node );
193 XMLCh* xmlchAttName = XMLString::transcode( attName );
194 ret = ( elt->getAttributeNode( xmlchAttName ) != 0 );
195 XMLString::release( &xmlchAttName );
196 return ret;
197 }
198
199 std::string Dom::getAttribute( const DOMElement* elt, const char* attName ) {
200 if ( elt == 0 ) { throw NullNode( "from xmlBase::Dom::getAttribute. Null argument" ); }
201
202 XMLCh* xmlchAttName = XMLString::transcode( attName );
203 const XMLCh* attValue = elt->getAttribute( xmlchAttName );
204 XMLString::release( &xmlchAttName );
205
206 if ( attValue == 0 ) return std::string( "" );
207
208 char* chrAttValue = XMLString::transcode( attValue );
209 std::string strValue = std::string( chrAttValue );
210 XMLString::release( &chrAttValue );
211 return strValue;
212 }
213
214 std::string Dom::getAttribute( const DOMElement* elt, std::string attName ) {
215
216 return getAttribute( elt, attName.c_str() );
217 }
218
219 std::string Dom::getAttribute( const DOMNode* elt, const char* attName ) {
220 if ( elt == 0 ) { throw NullNode( "from xmlBase::Dom::getAttribute. Null argument" ); }
221
222 if ( elt->getNodeType() != DOMNode::ELEMENT_NODE )
223 { throw NullNode( "from xmlBase::Dom::getAttribute. Non-element argument" ); }
224 return getAttribute( static_cast<const DOMElement*>( elt ), attName );
225 }
226
227 std::string Dom::getAttribute( const DOMNode* elt, std::string attName ) {
228 return getAttribute( elt, attName.c_str() );
229 }
230
231 double Dom::getDoubleAttribute( const DOMNode* elt, std::string attName ) {
232 std::string stringValue = getAttribute( elt, attName );
233
234 if ( stringValue == std::string( "" ) )
235 { throw NullNode( "from xmlBase::Dom::getDoubleAttribute. non-existent attribute" ); }
236 try
237 {
238 return facilities::Util::stringToDouble( stringValue );
239 } catch ( facilities::WrongType ex )
240 {
241 std::cerr << ex.getMsg();
242 throw WrongAttributeType( "from xmlBase::Dom::getDoubleAttribute" );
243 }
244 }
245
246 unsigned Dom::getDoublesAttribute( const DOMNode* elt, std::string attName,
247 std::vector<double>& values, bool clear ) {
248 unsigned nVals = 0;
249 std::vector<std::string> tokens;
250 if ( clear ) values.clear();
251 std::string stringValue = getAttribute( elt, attName );
252 if ( stringValue.size() == 0 ) return nVals;
253
254 facilities::Util::stringTokenize( stringValue, std::string( " " ), tokens );
255
256 try
257 {
258 unsigned nToken = tokens.size();
259 for ( unsigned iToken = 0; iToken < nToken; iToken++ )
260 {
261 values.push_back( facilities::Util::stringToDouble( tokens[iToken] ) );
262 nVals++;
263 }
264 } catch ( facilities::WrongType ex )
265 {
266 std::cerr << ex.getMsg();
267 throw WrongAttributeType( "from xmlBase::Dom::getDoublesAttribute" );
268 }
269 return nVals;
270 }
271 unsigned Dom::getFloatsAttribute( const DOMNode* elt, std::string attName,
272 std::vector<float>& values, bool clear ) {
273 unsigned nVals = 0;
274 std::vector<std::string> tokens;
275 if ( clear ) values.clear();
276 std::string stringValue = getAttribute( elt, attName );
277 if ( stringValue.size() == 0 ) return nVals;
278
279 facilities::Util::stringTokenize( stringValue, std::string( " " ), tokens );
280
281 try
282 {
283 unsigned nToken = tokens.size();
284 for ( unsigned iToken = 0; iToken < nToken; iToken++ )
285 {
286 values.push_back( facilities::Util::stringToDouble( tokens[iToken] ) );
287 nVals++;
288 }
289 } catch ( facilities::WrongType ex )
290 {
291 std::cerr << ex.getMsg();
292 throw WrongAttributeType( "from xmlBase::Dom::getDoublesAttribute" );
293 }
294 return nVals;
295 }
296
297 int Dom::getIntAttribute( const DOMNode* elt, std::string attName ) {
298 std::string stringValue = getAttribute( elt, attName );
299
300 if ( stringValue == std::string( "" ) )
301 { throw NullNode( "from xmlBase::Dom::getIntAttribute. non-existent attribute" ); }
302 try
303 { return facilities::Util::stringToInt( stringValue ); } catch ( facilities::WrongType ex )
304 {
305 std::cerr << ex.getMsg();
306 throw WrongAttributeType( "from xmlBase::Dom::getIntAttribute" );
307 }
308 }
309
310 unsigned Dom::getIntsAttribute( const DOMNode* elt, std::string attName,
311 std::vector<int>& values, bool clear ) {
312 unsigned nVals = 0;
313 std::vector<std::string> tokens;
314 if ( clear ) values.clear();
315 std::string stringValue = getAttribute( elt, attName );
316 if ( stringValue.size() == 0 ) return nVals;
317
318 facilities::Util::stringTokenize( stringValue, std::string( " " ), tokens );
319
320 try
321 {
322 unsigned nToken = tokens.size();
323 for ( unsigned iToken = 0; iToken < nToken; iToken++ )
324 {
325 values.push_back( facilities::Util::stringToInt( tokens[iToken] ) );
326 nVals++;
327 }
328 } catch ( facilities::WrongType ex )
329 {
330 std::cerr << ex.getMsg();
331 throw WrongAttributeType( "from xmlBase::Dom::getIntsAttribute" );
332 }
333 return nVals;
334 }
335
336 std::string Dom::getText( const DOMNode* textNode ) {
337 short int nType = textNode->getNodeType();
338
339 if ( ( nType != DOMNode::TEXT_NODE ) && ( nType != DOMNode::COMMENT_NODE ) &&
340 ( nType != DOMNode::CDATA_SECTION_NODE ) )
341 { throw WrongNodeType( "from xmlBase::Dom::getText, argument not text node" ); }
342 const XMLCh* t = textNode->getNodeValue();
343 if ( t == 0 ) return std::string( "" );
344 char* chrText = XMLString::transcode( t );
345 std::string text = std::string( chrText );
346 XMLString::release( &chrText );
347
348 return text;
349 }
350
351 std::string Dom::getTextContent( const DOMElement* elt ) {
352 if ( elt->getNodeType() != DOMNode::ELEMENT_NODE )
353 { throw WrongNodeType( "from xmlBase::Dom::getTextContent, argument not element node" ); }
354 return ( getText( elt->getFirstChild() ) );
355 }
356
357 void Dom::addAttribute( DOMElement* elt, std::string name, double value ) {
358 if ( elt == 0 ) { throw NullNode( "from xmlBase::Dom::addAttribute. null argument" ); }
359
360 // #ifdef DEFECT_NO_STRINGSTREAM
361 // std::strstream s;
362 // s << value << '\0';
363 // #else
364 std::ostringstream s;
365 s << value;
366 // #endif
367
368 std::string str = s.str();
369 XMLCh* xmlchValue = XMLString::transcode( str.c_str() );
370 XMLCh* xmlchName = XMLString::transcode( name.c_str() );
371
372 try
373 { elt->setAttribute( xmlchName, xmlchValue ); } catch ( DOMException ex )
374 {
375 char* msg = XMLString::transcode( ex.msg );
376 std::cerr << "DOMException in xmlBase::Dom::addAttribute(double)" << std::endl;
377 std::cerr << "Msg: " << std::string( msg ) << "Code: " << ex.code << std::endl;
378 XMLString::release( &msg );
379 throw ex;
380 }
381 XMLString::release( &xmlchName );
382 XMLString::release( &xmlchValue );
383 }
384
385 void Dom::addAttribute( DOMElement* elt, std::string name, int value ) {
386 if ( elt == 0 ) { throw NullNode( "from xmlBase::Dom::addAttribute. Null argument" ); }
387
388 // #ifdef DEFECT_NO_STRINGSTREAM
389 // std::strstream s;
390 // s << value << '\0';
391 // #else
392 std::ostringstream s;
393 s << value;
394 // #endif
395
396 std::string str = s.str();
397
398 XMLCh* xmlchValue = XMLString::transcode( str.c_str() );
399 XMLCh* xmlchName = XMLString::transcode( name.c_str() );
400
401 try
402 { elt->setAttribute( xmlchName, xmlchValue ); } catch ( DOMException ex )
403 {
404 char* msg = XMLString::transcode( ex.msg );
405 std::cerr << "DOMException in xmlBase::Dom::addAttribute(int)" << std::endl;
406 std::cerr << "Msg: " << std::string( msg ) << "Code: " << ex.code << std::endl;
407 XMLString::release( &msg );
408 throw ex;
409 }
410
411 XMLString::release( &xmlchName );
412 XMLString::release( &xmlchValue );
413 }
414
415 void Dom::addAttribute( DOMElement* elt, std::string name, unsigned int value ) {
416 if ( elt == 0 ) { throw NullNode( "from xmlBase::Dom::addAttribute. Null argument" ); }
417
418 // #ifdef DEFECT_NO_STRINGSTREAM
419 // std::strstream s;
420 // s << value << '\0';
421 // #else
422 std::ostringstream s;
423 s << value;
424 // #endif
425
426 std::string str = s.str();
427
428 XMLCh* xmlchValue = XMLString::transcode( str.c_str() );
429 XMLCh* xmlchName = XMLString::transcode( name.c_str() );
430
431 try
432 { elt->setAttribute( xmlchName, xmlchValue ); } catch ( DOMException ex )
433 {
434 char* msg = XMLString::transcode( ex.msg );
435 std::cerr << "DOMException in xmlBase::Dom::addAttribute(unsigned int)" << std::endl;
436 std::cerr << "Msg: " << std::string( msg ) << "Code: " << ex.code << std::endl;
437 XMLString::release( &msg );
438 throw ex;
439 }
440 XMLString::release( &xmlchName );
441 XMLString::release( &xmlchValue );
442 }
443
444 void Dom::addAttribute( DOMElement* elt, std::string name, const char* value ) {
445
446 if ( elt == 0 ) { throw NullNode( "from xmlBase::Dom::addAttribute. Null argument" ); }
447
448 XMLCh* xmlchValue = XMLString::transcode( value );
449 XMLCh* xmlchName = XMLString::transcode( name.c_str() );
450
451 try
452 { elt->setAttribute( xmlchName, xmlchValue ); } catch ( DOMException ex )
453 {
454 char* msg = XMLString::transcode( ex.msg );
455 std::cerr << "DOMException in xmlBase::Dom::addAttribute(char*)" << std::endl;
456 std::cerr << "Msg: " << std::string( msg ) << "Code: " << ex.code << std::endl;
457 std::cerr.flush();
458 XMLString::release( &msg );
459 throw ex;
460 }
461
462 XMLString::release( &xmlchName );
463 XMLString::release( &xmlchValue );
464 }
465
466 void Dom::addAttribute( DOMElement* elt, std::string name, std::string value ) {
467
468 if ( elt == 0 ) throw NullNode( "from xmlBase::Dom::addAttribute. Null argument" );
469
470 addAttribute( elt, name, value.c_str() );
471 }
472
473 /*
474 Serialize a node (and any children) to the specified ostream.
475 prefix is for now ignored, but see note following.
476
477 The only node types which actually do get written are
478 element (and its attributes and children)
479 text
480 comment
481
482 NB: For this first pass, printElement outputs all the
483 supported node types just as they appeared in the
484 serialization before parsing *if* the parse was
485 non-validating. If it *was* validating (or if
486 the DOM representation was built programmatically
487 rather than by parsing a file) then ignorable
488 white space will have been thrown away (in the
489 validated case) or there won't have been any to
490 begin with (programmatically-built case)
491 so the printed version will be a horrific single
492 line except that line breaks appearing within
493 comments or text nodes will be preserved.
494
495 Ideally would like to be able to query the DOM
496 or have an argument passed in to tell us whether
497 ignorable white space has been thrown away, in which
498 case we should attempt to pretty print by putting
499 newlines in in reasonable places and keeping track
500 of a sensible indentation level.
501
502 For now, make two different functions. See
503 prettyPrintElement below.
504 */
505 void Dom::printElement( DOMNode* node, std::ostream& out ) {
506
507 if ( node == 0 ) { throw NullNode( "from xmlBase::Dom::printElement. Null argument" ); }
508
509 switch ( node->getNodeType() )
510 {
511 case DOMNode::ELEMENT_NODE: {
512 // output start tag
513 {
514 std::string tagName = getNodeName( node );
515 out << '<' << tagName;
516 }
517 // ...with attributes
518
519 typedef std::map<std::string, DOMNode*> AttMap;
520 AttMap atts;
521 AttMap::const_iterator it;
522 getAttributeNodeMap( node, atts );
523
524 for ( it = atts.begin(); it != atts.end(); it++ )
525 {
526 std::string attName = ( *it ).first;
527 out << ' ' << attName << '=';
528 out << '"' << getAttribute( node, attName ) << '"';
529 }
530
531 // iterate through children
532 DOMNode* child = node->getFirstChild();
533 if ( child != 0 )
534 { // there are children
535 out << '>';
536 while ( child != 0 )
537 {
538 Dom::printElement( child, out );
539 child = child->getNextSibling();
540 }
541 // output end tag, long form
542 {
543 std::string endName = getNodeName( node );
544 out << "</" << endName << ">";
545 }
546 }
547 else
548 { // no children; use short form for the empty tag
549 out << " />";
550 }
551 }
552 break;
553 case DOMNode::TEXT_NODE:
554 // just put it out as is
555 { out << getText( node ); }
556 break;
557
558 case DOMNode::CDATA_SECTION_NODE: {
559 out << "<![CDATA[" << getText( node ) << "]]>";
560 }
561 break;
562
563 case DOMNode::COMMENT_NODE:
564 // glast.prs doesn't have any comments (but should!)
565 { out << "<!-- " << getText( node ) << "-->"; }
566 break;
567 default:
568 // ignore anything else
569 break;
570 }
571 }
572
573 // Assume we need to do the indenting and line breaks
574 void Dom::prettyPrintElement( DOMNode* node, std::ostream& out, std::string prefix ) {
575
576 if ( node == 0 )
577 { throw NullNode( "from xmlBase::Dom::prettyPrintElement. Null argument" ); }
578
579 out << prefix;
580 switch ( node->getNodeType() )
581 {
582
583 case DOMNode::ELEMENT_NODE: {
584 // output start tag
585 std::string tagName = getNodeName( node );
586 out << '<' << tagName;
587
588 // ...with attributes
589 typedef std::map<std::string, DOMNode*> AttMap;
590 AttMap atts;
591 AttMap::const_iterator it;
592
593 getAttributeNodeMap( node, atts );
594
595 for ( it = atts.begin(); it != atts.end(); it++ )
596 {
597 std::string attName = ( *it ).first;
598 out << ' ' << attName << '=';
599 out << '"' << getAttribute( node, attName ) << '"';
600 }
601
602 // iterate through children
603 DOMNode* child = node->getFirstChild();
604 if ( child != 0 )
605 { // there are children
606 out << '>' << std::endl;
607 while ( child != 0 )
608 {
609 // new indent level
610 Dom::prettyPrintElement( child, out, prefix + " " );
611 child = child->getNextSibling();
612 }
613 // output end tag, long form
614 {
615 std::string endName = getNodeName( node );
616 out << prefix << "</" << endName << ">" << std::endl;
617 }
618 }
619 else
620 { // no children; use short form for the empty tag
621 out << " />" << std::endl;
622 }
623 }
624 break;
625 case DOMNode::TEXT_NODE:
626 // just put it out as is
627 // Note this won't indent correctly if the text node
628 // contains multiple lines.
629 // Similarly, it's too much work to avoid sometimes putting out
630 // an "extra" blank line in the vicinity of text.
631 // Current code puts the extra <cr> before
632 // the text node.
633 { out << getText( node ); }
634
635 break;
636
637 case DOMNode::CDATA_SECTION_NODE: {
638 // Probably need to put opening and closing sequences in by hand..
639 out << "<![CDATA[" << getText( node ) << "]]>";
640 }
641 break;
642
643 case DOMNode::COMMENT_NODE:
644 // glast.prs doesn't have any comments (but should!)
645 // Note this won't indent correctly if the text node
646 // contains multiple lines. Could have utility routine
647 // to do this, to be called for comments and text nodes
648 { out << "<!-- " << getText( node ) << "-->" << std::endl; }
649
650 break;
651 default:
652 // ignore anything else
653 break;
654 }
655 }
656
657 void Dom::prune( DOMElement* elt ) {
658 DOMElement* child = findFirstChildByName( elt, "*" );
659 while ( child != 0 )
660 {
661 DOMElement* sib = getSiblingElement( child );
662 prune( child );
663 elt->removeChild( child );
664 child = sib;
665 }
666 }
667 char* Dom::transToChar( const XMLCh* const str ) {
668 return transToChar( str, XMLString::stringLen( str ) );
669 }
670
671 char* Dom::transToChar( const XMLCh* const str, unsigned int len ) {
672 // Passing null argument is a fatal error.
673 // No, it's not
674 // assert(str != 0);
675 if ( str == 0 ) return 0;
676
677 if ( !transcoder )
678 {
679 int status = initTrans();
680 if ( !status ) return 0;
681 }
682
683 // Find length of str to pass to transcode(..) rather than
684 // just passing output buffer size. This is important because
685 // (for Xerces 1.3 anyway) the transcode routine will try to read
686 // this many bytes from the input buffer, conceivably causing
687 // an access violation if it's more than the actual input
688 // buffer length
689
690 if ( len + 1 > transBufSize )
691 { // return old buffer; allocate a bigger one
692 char* tBuf = new char[len + 1];
693 if ( !tBuf ) return 0;
694 transBufSize = len + 1;
695 delete[] transBuf;
696 transBuf = tBuf;
697 }
698
699 bool ok;
700 ok = Dom::transcoder->transcode( str, transBuf, len );
701 return ( ok ? transBuf : 0 );
702 }
703
704 XMLCh* Dom::transToXMLCh( const char* const src ) {
705 // Passing null string is a fatal error
706 // No, it's not
707 if ( src == 0 ) return 0;
708 if ( !transcoder )
709 {
710 int status = initTrans();
711 if ( !status ) return 0;
712 }
713 // as with transToChar above, find actual length of char*
714 // and pass that to Xerces utility for 3rd (maxChars) argument.
715 unsigned int len = strlen( src ) + 1;
716 if ( len > xmlchBufSize )
717 {
718 XMLCh* tBuf = new XMLCh[len];
719 if ( !tBuf ) return 0;
720 xmlchBufSize = len;
721 delete[] xmlchBuf;
722 xmlchBuf = tBuf;
723 }
724
725 bool ok;
726
727 ok = transcoder->transcode( src, xmlchBuf, len );
728 return ( ok ? xmlchBuf : 0 );
729 }
730
731 int Dom::initTrans() {
732 transcoder = XMLPlatformUtils::fgTransService->makeNewLCPTranscoder(
733 XMLPlatformUtils::fgMemoryManager );
734 if ( !transcoder ) return 0; // and complain?!? Shouldn't ever happen
735 transBuf = new char[transBufSize];
736 if ( !transBuf )
737 {
738 delete transcoder;
739 return 0;
740 }
741 xmlchBuf = new XMLCh[xmlchBufSize];
742 if ( !xmlchBuf )
743 {
744 delete[] transBuf;
745 delete transcoder;
746 return 0;
747 }
748 return 1;
749 }
750
751} // end namespace xmlBase
XmlRpcServer s
static double stringToDouble(const std::string &InStr)
static void stringTokenize(std::string input, const std::string &delimiters, std::vector< std::string > &tokens, bool clear=true)
static int stringToInt(const std::string &InStr)
Exception class used when converting from string to numeric type.
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
int t()
Definition t.c:1