GEAR  1.9.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
XMLHandlerMgr.cc
1 #include "gearxml/XMLHandlerMgr.h"
2 
3 #include "gearxml/GearParametersXML.h"
4 #include "gearxml/TPCParametersXML.h"
5 #include "gearxml/CalorimeterParametersXML.h"
6 #include "gearxml/ZPlanarParametersXML.h"
7 #include "gearxml/FTDParametersXML.h"
8 #include "gearxml/TrackerPlanesParametersXML.h"
9 #include "gearxml/SiPlanesParametersXML.h"
10 #include "gearxml/tinyxml.h"
11 
12 #include "gear/GEAR.h"
13 
14 
15 #include <string>
16 #include <sstream>
17 
18 namespace gear{
19 
20  // XMLHandlerMgr* XMLHandlerMgr::_me = 0 ;
21 
22 
23 
24  XMLHandlerMgr* XMLHandlerMgr::instance() {
25 
26  static XMLHandlerMgr _me ;
27  return &_me ;
28 
29  // if( _me == 0 ) {
30  // _me = new XMLHandlerMgr ;
31  // }
32  // return _me ;
33  }
34 
36 
37  XMLHandlerMap::iterator it_end = _map.end() ;
38 
39  for( XMLHandlerMap::iterator it = _map.begin() ; it != it_end ; ++ it ) {
40 
41  delete it->second ;
42  }
43  }
44 
45 
46  XMLHandlerMgr::XMLHandlerMgr() {
47 
48  _map[ GEAR::GEARPARAMETERS ] = new GearParametersXML ;
49  _map[ GEAR::TPCPARAMETERS ] = new TPCParametersXML ;
50  _map[ GEAR::CALORIMETERPARAMETERS ] = new CalorimeterParametersXML ;
51  _map[ GEAR::ZPLANARPARAMETERS ] = new ZPlanarParametersXML ;
52  _map[ GEAR::FTDPARAMETERS ] = new FTDParametersXML ;
53  _map[ GEAR::TRACKERPLANESPARAMETERS ] = new TrackerPlanesParametersXML ;
54  _map[ GEAR::SIPLANESPARAMETERS ] = new SiPlanesParametersXML ;
55 
56  }
57 
58 
59  const XMLHandler* XMLHandlerMgr::getHandler( const std::string& type ) {
60 
61  return _map[ type ] ;
62  }
63 
64 
65  void XMLHandlerMgr::setHandler( const std::string& type , XMLHandler* handler ) {
66 
67  if( handler == 0 ) // don't allow null pointer
68  return ;
69 
70  XMLHandlerMap::iterator it = _map.find( type ) ;
71 
72  if( it != _map.end() ) {
73 
74  delete it->second ;
75 
76  it->second = handler ;
77 
78  } else {
79 
80  _map.insert( std::make_pair( type , handler ) ) ;
81  }
82 
83  }
84 
85 
86 
87  // helper function (declared in XMLHandler.h )
88 
89  std::string getXMLAttribute(const TiXmlNode* node , const std::string& name ) {
90 
91  const TiXmlElement* el = node->ToElement() ;
92  if( el == 0 ) {
93  std::stringstream str ;
94  str << "XMLParser::getAttribute [" << name << "] not an XMLElement " ;
95  throw ParseException( str.str() ) ;
96  }
97  const char* at = el->Attribute( name ) ;
98 
99  if( at == 0 ){
100 
101  std::stringstream str ;
102  str << "XMLParser::getAttribute missing attribute \"" << name
103  << "\" in element <" << el->Value() << "/> " ;
104  throw ParseException( str.str() ) ;
105  }
106 
107  return std::string( at ) ;
108 
109  }
110 
111  std::string getOptionalXMLAttribute(const TiXmlNode* node , const std::string& name ,
112  const std::string& defaultValue) {
113 
114  std::string result( defaultValue ) ;
115 
116  try{
117 
118  result = getXMLAttribute( node, name ) ;
119  }
120  catch( ParseException& p){
121  }
122 
123  return result ;
124  }
125 
126 
127 
128  // helper function (declared in XMLHandler.h )
129 
130  std::string getChildElementValue(const TiXmlNode* node , const std::string& name ) {
131 
132  const TiXmlElement* el = node->ToElement() ;
133  if( el == 0 ) {
134  std::stringstream str ;
135  str << "XMLParser::getAttribute [" << name << "] not an XMLElement " ;
136  throw ParseException( str.str() ) ;
137  }
138 
139  const TiXmlElement* cE = el->FirstChildElement( name );
140 
141  if( cE == 0 ) {
142 
143  std::stringstream str ;
144  str << "XMLParser::getChildElementValue missing element \"" << name
145  << "\" in element <" << el->Value() << "/> " ;
146 
147  throw ParseException( str.str() ) ;
148  }
149 
150  return getXMLAttribute( cE , "value" ) ;
151 
152  }
153 
154  // helper function (declared in XMLHandler.h )
155 
156  std::string getOptionalChildElementValue(const TiXmlNode* node , const std::string& name ,
157  const std::string& defaultValue)
158  {
159 
160  const TiXmlElement* el = node->ToElement() ;
161  if( el == 0 )
162  throw ParseException("XMLParser::getAttribute not an XMLElement " ) ;
163 
164  const TiXmlElement* cE = el->FirstChildElement( name );
165 
166  if( cE == 0 )
167  {
168  return defaultValue;
169  }
170 
171  return getXMLAttribute( cE , "value" ) ;
172 
173  }
174 
175 }
XML handler for ZPlanarParameters.
std::string getOptionalXMLAttribute(const TiXmlNode *node, const std::string &name, const std::string &defaultValue)
Helper method used for parsing XML.
Interface for XML handlers of GEAR classes.
Definition: XMLHandler.h:17
XML handler for SiPlanesParameters.
XML handler for CalorimeterParameters.
const char * Value() const
The meaning of &#39;value&#39; changes for the specific type of TiXmlNode.
Definition: tinyxml.h:437
Abstract XML handler for TPCParameters.
std::string getOptionalChildElementValue(const TiXmlNode *node, const std::string &name, const std::string &defaultValue)
Helper method used for parsing XML - returns the attribute &#39;value&#39; of the optional named child elemen...
XML handler for GearParameters.
XML handler for FTDParameters.
const XMLHandler * getHandler(const std::string &type)
XMLHandler for given type.
XML handler for TrackerPlanesParameters.
The parent class for everything in the Document Object Model.
Definition: tinyxml.h:370
const char * Attribute(const char *name) const
Given an attribute name, Attribute() returns the value for the attribute of that name, or null if none exists.
Definition: tinyxml.cc:671
ParseException used for parse errors, e.g.
Definition: GEAR.h:65
std::string getXMLAttribute(const TiXmlNode *node, const std::string &name)
Helper method used for parsing XML.
const TiXmlElement * ToElement() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:617
The element is a container class.
Definition: tinyxml.h:827
~XMLHandlerMgr()
the destructor
std::string getChildElementValue(const TiXmlNode *node, const std::string &name)
Helper method used for parsing XML - returns the attribute &#39;value&#39; of the named child element as doub...
void setHandler(const std::string &type, XMLHandler *handler)
Register XMLHandler for type.
const TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
Definition: tinyxml.cc:482