test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DDXMLElement.cc
Go to the documentation of this file.
1 /***************************************************************************
2  DDXMLElement.cc - description
3  -------------------
4  begin : Fri Mar 15 2002
5  email : case@ucdhep.ucdavis.edu
6  ***************************************************************************/
7 
10 
11 #include <algorithm>
12 #include <iostream>
13 #include <sstream>
14 
16  : myRegistry_( myreg ),
17  attributes_(),
18  text_(),
19  autoClear_( false )
20 {}
21 
22 DDXMLElement::DDXMLElement( DDLElementRegistry* myreg, const bool& clearme )
23  : myRegistry_( myreg ),
24  attributes_(),
25  text_(),
26  autoClear_( clearme )
27 {}
28 
30 {}
31 
32 // For pre-processing, after attributes are loaded. Default, do nothing!
33 void
35 {
36  DCOUT_V('P', "DDXMLElement::preProcessElementBase default, do nothing) started-completed.");
37 }
38 
39 // This loads the attributes into the attributes_ std::vector.
40 void
42  const std::vector<std::string> & names,
43  const std::vector<std::string> & values,
44  const std::string& nmspace, DDCompactView& cpv )
45 {
46  attributes_.resize(attributes_.size()+1);
47  DDXMLAttribute & tAttributes = attributes_.back();
48 
49  // adds attributes
50  for (size_t i = 0; i < names.size(); ++i)
51  {
52  tAttributes.insert(std::make_pair(names[i], values[i]));
53  }
54 
55  preProcessElement( elemName, nmspace, cpv );
56  DCOUT_V('P', "DDXMLElement::loadAttributes completed. " << *this);
57 }
58 
59 // clear data.
60 void
62 {
63  text_.clear();
64  attributes_.clear();
65  attributeAccumulator_.clear();
66 }
67 
68 // Access to current attributes by name.
69 const std::string &
71 {
72  static const std::string ldef;
73  if (attributes_.size())
74  return get(name, attributes_.size() - 1);
75  return ldef;
76 }
77 
78 const DDXMLAttribute&
79 DDXMLElement::getAttributeSet( size_t aIndex ) const
80 {
81  return attributes_[aIndex];
82 }
83 
84 
85 const DDName
86 DDXMLElement::getDDName( const std::string& defaultNS, const std::string& attname, size_t aIndex )
87 {
88  if (aIndex < attributes_.size()
89  && attributes_[aIndex].find(attname) != attributes_[aIndex].end()) {
90  std::string ns = defaultNS;
91  // For the user to fully control namespaces they must provide for
92  // all name attributes something of the form, for example:
93  // <Solid name="ns:name" ...
94  // If defaultNS is "!" (magic I don't like) then find and set
95  // the namespace properly.
96  if ( defaultNS == "!" ) {
97  ns = "";
98  }
99  const std::string & name = attributes_[aIndex].find(attname)->second;
100  std::string rn = name;
101  size_t foundColon= name.find(':');
102  if (foundColon != std::string::npos) {
103  ns = name.substr(0,foundColon);
104  rn = name.substr(foundColon+1);
105 
106  }
107  return DDName(rn, ns);
108  }
109  std::string msg = "DDXMLElement:getDDName failed. It was asked to make ";
110  msg += "a DDName using attribute: " + attname;
111  msg += " in position: " + itostr(int(aIndex)) + ". There are ";
112  msg += itostr(int(attributes_.size())) + " entries in the element.";
113  throwError(msg);
114  return DDName("justToCompile", "justToCompile"); // used to make sure it compiles
115 }
116 
117 // Returns a specific value from the aIndex set of attributes.
118 const std::string &
119 DDXMLElement::get( const std::string& name, const size_t aIndex ) const
120 {
121  static const std::string sts;
122  if (aIndex < attributes_.size())
123  {
124  DDXMLAttribute::const_iterator it = attributes_[aIndex].find(name);
125  if (attributes_[aIndex].end() == it)
126  {
127  DCOUT_V('P', "WARNING: DDXMLElement::get did not find the requested attribute: " << name << std::endl << *this);
128  return sts;
129  }
130  else
131  return (it->second);
132  }
133  std::string msg = "DDXMLElement:get failed. It was asked for attribute " + name;
134  msg += " in position " + itostr(int(aIndex)) + " when there are only ";
135  msg += itostr(int(attributes_.size())) + " in the element storage.\n";
136  throwError(msg);
137  // meaningless...
138  return sts;
139 }
140 
141 // Returns a specific set of values as a std::vector of std::strings,
142 // given the attribute name.
143 std::vector<std::string>
145 {
146  // The idea here is that the attributeAccumulator_ is a cache of
147  // on-the-fly generation from the std::vector<DDXMLAttribute> and the
148  // reason is simply to speed things up if it is requested more than once.
149  std::vector<std::string> tv;
150  AttrAccumType::const_iterator ita = attributeAccumulator_.find(name);
151  if (ita != attributeAccumulator_.end())
152  {
154  if (tv.size() < attributes_.size())
155  {
156  appendAttributes(tv, name);
157  }
158  DCOUT_V('P', "DDXMLElement::getAttribute found attribute named " << name << " in a map of size " << size());
159  }
160  else
161  {
162  if (attributes_.size())
163  {
164  appendAttributes(tv, name);
165  }
166  else
167  {
168  DCOUT_V('P', "DDXMLAttributeAccumulator::getAttribute was asked to provide a std::vector of values for an attribute named " << name << " but there was no such attribute.");
169  // throw cms::Exception("DDException") << msg;
170  }
171  }
172  return tv;
173 }
174 
175 // Default do-nothing processElementBases.
176 void
178 {
179  DCOUT_V('P', "DDXMLElement::processElementBase (default, do nothing) started-completed");
181  if ( autoClear_ ) clear();
182 
183 }
184 
185 void
187 {
188  text_.push_back(inText);
189 }
190 
191 void
193 {
194  static const std::string cr("\n");
195  if (text_.size() > 0) {
196  text_[text_.size() - 1] += cr;
197  text_[text_.size() - 1] += inText ;
198  } else
199  {
200  std::string msg = "DDXMLElement::appendText could not append to non-existent text.";
201  throwError(msg);
202  }
203 }
204 
205 const std::string
206 DDXMLElement::getText( size_t tindex ) const
207 {
208  if (tindex > text_.size()) {
209  std::string msg = "DDXMLElement::getText tindex is greater than text_.size()).";
210  throwError(msg);
211  }
212  return text_[tindex];
213 }
214 
215 bool
217 {
218  if (text_.size() != 0)
219  return true;
220  return false;
221 }
222 
223 std::ostream & operator<<( std::ostream & os, const DDXMLElement & element )
224 {
225  element.stream(os);
226  return os;
227 }
228 
229 void
230 DDXMLElement::stream( std::ostream & os ) const
231 {
232  os << "Output of current element attributes:" << std::endl;
233  for (std::vector<DDXMLAttribute>::const_iterator itv = attributes_.begin();
234  itv != attributes_.end(); ++itv)
235  {
236  for (DDXMLAttribute::const_iterator it = itv->begin();
237  it != itv->end(); ++it)
238  os << it->first << " = " << it->second << "\t";
239  os << std::endl;
240  }
241 }
242 
243 void
244 DDXMLElement::appendAttributes( std::vector<std::string> & tv,
245  const std::string& name )
246 {
247  for (size_t i = tv.size(); i < attributes_.size(); ++i)
248  {
249  DDXMLAttribute::const_iterator itnv = attributes_[i].find(name);
250  if (itnv != attributes_[i].end())
251  tv.push_back(itnv->second);
252  else
253  tv.push_back("");
254  }
255 }
256 
257 // Number of elements accumulated.
258 size_t
259 DDXMLElement::size( void ) const
260 {
261  return attributes_.size();
262 }
263 
264 std::vector<DDXMLAttribute>::const_iterator
266 {
267  myIter_ = attributes_.begin();
268  return attributes_.begin();
269 }
270 
271 std::vector<DDXMLAttribute>::const_iterator
273 {
274  myIter_ = attributes_.end();
275  return attributes_.end();
276 }
277 
278 std::vector<DDXMLAttribute>::const_iterator&
280 {
281  myIter_ = myIter_ + inc;
282  return myIter_;
283 }
284 
285 const std::string&
286 DDXMLElement::parent( void ) const
287 {
288  return parentElement_;
289 }
290 
291 void
293 {
294  parentElement_ = pename;
295 }
296 
297 void
299 {
300  myElement_ = sename;
301 }
302 
303 // yet another :-)
306 {
307  std::ostringstream ostr;
308  ostr << in;
309  return ostr.str();
310 }
311 
312 bool
314 {
315  return (attributes_.size() == 0 ? true : false);
316 }
317 
318 void
319 DDXMLElement::throwError( const std::string& keyMessage ) const
320 {
321  std::string msg = keyMessage + "\n";
322  msg += " Element " + myElement_ +"\n";
323 
324  throw cms::Exception("DDException") << msg;
325 }
virtual bool isEmpty(void) const
Have any elements of this type been encountered but not processed?
int i
Definition: DBlmapReader.cc:9
std::string parentElement_
Definition: DDXMLElement.h:198
std::vector< DDXMLAttribute >::const_iterator & operator++(int inc)
Allow the elements of this type to be iterated over using ++ operator.
static const HistoName names[]
static std::string itostr(int i)
WARNING: abused by other classes in this system: yet another conversion from int to std::string...
void appendText(const std::string &inText)
append to the current (i.e. most recently added)
void setParent(const std::string &pename)
Set parent element name to central list of names.
virtual const DDXMLAttribute & getAttributeSet(size_t aIndex=0) const
Get a &quot;row&quot; of attributes, i.e. one attribute set.
Definition: DDXMLElement.cc:79
virtual const std::string & get(const std::string &name, size_t aIndex=0) const
Returns a specific value from the aIndex set of attributes.
void throwError(const std::string &keyMessage) const
format std::string for throw an error.
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:18
virtual void processElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv)
Processing the element.
const std::string & parent(void) const
access to parent element name
void appendAttributes(std::vector< std::string > &tv, const std::string &name)
behind the scenes appending to pAttributes...
std::ostream & operator<<(std::ostream &out, const ALILine &li)
Definition: ALILine.cc:187
type of data representation of DDCompactView
Definition: DDCompactView.h:77
std::map< std::string, std::string > DDXMLAttribute
Definition: DDXMLElement.h:55
virtual void preProcessElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv)
Called by loadAttributes AFTER attributes are loaded.
Definition: DDXMLElement.cc:34
void loadText(const std::string &inText)
Used to load both text and XML comments into this object.
void setSelf(const std::string &sename)
Set self element name to central list of names.
virtual std::vector< DDXMLAttribute >::const_iterator end(void)
void loadAttributes(const std::string &elemName, const std::vector< std::string > &names, const std::vector< std::string > &values, const std::string &nmspace, DDCompactView &cpv)
Load the element attributes.
Definition: DDXMLElement.cc:41
virtual size_t size(void) const
Number of elements accumulated.
std::vector< DDXMLAttribute >::const_iterator myIter_
Definition: DDXMLElement.h:196
std::vector< DDXMLAttribute > attributes_
Definition: DDXMLElement.h:192
std::string myElement_
Definition: DDXMLElement.h:197
virtual bool gotText(void) const
gotText()? kind of like gotMilk? Yes = text has already been encountered.
virtual std::vector< std::string > getVectorAttribute(const std::string &name)
Returns a set of values as a std::vector of strings, given the attribute name.
AttrAccumType attributeAccumulator_
Definition: DDXMLElement.h:194
#define DCOUT_V(M_v_Y, M_v_S)
Definition: DDdebug.h:54
virtual ~DDXMLElement(void)
Destructor.
Definition: DDXMLElement.cc:29
This is a base class for processing XML elements in the DDD.
Definition: DDXMLElement.h:58
virtual const std::string & getAttribute(const std::string &name) const
Access to attributes by name.
Definition: DDXMLElement.cc:70
virtual std::vector< DDXMLAttribute >::const_iterator begin(void)
virtual void stream(std::ostream &os) const
Allow for the elements to have their own streaming method, but also provide a default.
The main class for processing parsed elements.
virtual void clear(void)
clear this element&#39;s contents.
Definition: DDXMLElement.cc:61
volatile std::atomic< bool > shutdown_flag false
DDXMLElement(DDLElementRegistry *myreg)
Constructor.
Definition: DDXMLElement.cc:15
virtual const DDName getDDName(const std::string &defaultNS, const std::string &attname=std::string("name"), size_t aIndex=0)
Definition: DDXMLElement.cc:86
const std::string getText(size_t tindex=0) const
retrieve the text blob.
std::vector< std::string > text_
Definition: DDXMLElement.h:193