CMS 3D CMS Logo

DDXMLElement.cc
Go to the documentation of this file.
2 
3 #include <iostream>
4 #include <memory>
5 #include <utility>
6 #include <string>
7 
10 
11 class DDCompactView;
12 class DDLElementRegistry;
13 
15  : myRegistry_( myreg ),
16  attributes_(),
17  text_(),
18  autoClear_( false )
19 {}
20 
21 DDXMLElement::DDXMLElement( DDLElementRegistry* myreg, const bool& clearme )
22  : myRegistry_( myreg ),
23  attributes_(),
24  text_(),
25  autoClear_( clearme )
26 {}
27 
28 // For pre-processing, after attributes are loaded. Default, do nothing!
29 void
31 {}
32 
33 // This loads the attributes into the attributes_ std::vector.
34 void
36  const std::vector<std::string> & names,
37  const std::vector<std::string> & values,
38  const std::string& nmspace, DDCompactView& cpv )
39 {
40  attributes_.resize(attributes_.size()+1);
41  DDXMLAttribute & tAttributes = attributes_.back();
42 
43  // adds attributes
44  for (size_t i = 0; i < names.size(); ++i)
45  {
46  tAttributes.insert(std::make_pair(names[i], values[i]));
47  }
48 
49  preProcessElement( elemName, nmspace, cpv );
50 }
51 
52 // clear data.
53 void
55 {
56  text_.clear();
57  attributes_.clear();
58  attributeAccumulator_.clear();
59 }
60 
61 // Access to current attributes by name.
62 const std::string &
64 {
65  static const std::string ldef;
66  if (!attributes_.empty())
67  return get(name, attributes_.size() - 1);
68  return ldef;
69 }
70 
71 const DDXMLAttribute&
72 DDXMLElement::getAttributeSet( size_t aIndex ) const
73 {
74  return attributes_[aIndex];
75 }
76 
77 
78 const DDName
79 DDXMLElement::getDDName( const std::string& defaultNS, const std::string& attname, size_t aIndex )
80 {
81  if (aIndex < attributes_.size()
82  && attributes_[aIndex].find(attname) != attributes_[aIndex].end()) {
83  std::string ns = defaultNS;
84  // For the user to fully control namespaces they must provide for
85  // all name attributes something of the form, for example:
86  // <Solid name="ns:name" ...
87  // If defaultNS is "!" (magic I don't like) then find and set
88  // the namespace properly.
89  if ( defaultNS == "!" ) {
90  ns = "";
91  }
92  const std::string & name = attributes_[aIndex].find(attname)->second;
93  std::string rn = name;
94  size_t foundColon= name.find(':');
95  if (foundColon != std::string::npos) {
96  ns = name.substr(0,foundColon);
97  rn = name.substr(foundColon+1);
98 
99  }
100  return DDName(rn, ns);
101  }
102  std::string msg = "DDXMLElement:getDDName failed. It was asked to make ";
103  msg += "a DDName using attribute: " + attname;
104  msg += " in position: " + std::to_string(aIndex) + ". There are ";
105  msg += std::to_string(attributes_.size()) + " entries in the element.";
106  throwError(msg);
107  return DDName("justToCompile", "justToCompile"); // used to make sure it compiles
108 }
109 
110 // Returns a specific value from the aIndex set of attributes.
111 const std::string &
112 DDXMLElement::get( const std::string& name, const size_t aIndex ) const
113 {
114  static const std::string sts;
115  if (aIndex < attributes_.size())
116  {
117  DDXMLAttribute::const_iterator it = attributes_[aIndex].find(name);
118  if (attributes_[aIndex].end() == it)
119  {
120  return sts;
121  }
122  else
123  return (it->second);
124  }
125  std::string msg = "DDXMLElement:get failed. It was asked for attribute " + name;
126  msg += " in position " + std::to_string(aIndex) + " when there are only ";
127  msg += std::to_string(attributes_.size()) + " in the element storage.\n";
128  throwError(msg);
129  // meaningless...
130  return sts;
131 }
132 
133 // Returns a specific set of values as a std::vector of std::strings,
134 // given the attribute name.
135 std::vector<std::string>
137 {
138  // The idea here is that the attributeAccumulator_ is a cache of
139  // on-the-fly generation from the std::vector<DDXMLAttribute> and the
140  // reason is simply to speed things up if it is requested more than once.
141  std::vector<std::string> tv;
142  AttrAccumType::const_iterator ita = attributeAccumulator_.find(name);
143  if (ita != attributeAccumulator_.end())
144  {
146  if (tv.size() < attributes_.size())
147  {
148  appendAttributes(tv, name);
149  }
150  }
151  else
152  {
153  if (!attributes_.empty())
154  {
155  appendAttributes(tv, name);
156  }
157  else
158  {
159  // throw cms::Exception("DDException") << msg;
160  }
161  }
162  return tv;
163 }
164 
165 // Default do-nothing processElementBases.
166 void
168 {
170  if ( autoClear_ ) clear();
171 }
172 
173 void
175 {
176  text_.emplace_back(inText);
177 }
178 
179 void
181 {
182  static const std::string cr("\n");
183  if (!text_.empty()) {
184  text_[text_.size() - 1] += cr;
185  text_[text_.size() - 1] += inText ;
186  } else
187  {
188  std::string msg = "DDXMLElement::appendText could not append to non-existent text.";
189  throwError(msg);
190  }
191 }
192 
193 const std::string
194 DDXMLElement::getText( size_t tindex ) const
195 {
196  if (tindex > text_.size()) {
197  std::string msg = "DDXMLElement::getText tindex is greater than text_.size()).";
198  throwError(msg);
199  }
200  return text_[tindex];
201 }
202 
203 bool
205 {
206  if (!text_.empty())
207  return true;
208  return false;
209 }
210 
211 std::ostream & operator<<( std::ostream & os, const DDXMLElement & element )
212 {
213  element.stream(os);
214  return os;
215 }
216 
217 void
218 DDXMLElement::stream( std::ostream & os ) const
219 {
220  os << "Output of current element attributes:" << std::endl;
221  for (const auto & attribute : attributes_)
222  {
223  for (DDXMLAttribute::const_iterator it = attribute.begin();
224  it != attribute.end(); ++it)
225  os << it->first << " = " << it->second << "\t";
226  os << std::endl;
227  }
228 }
229 
230 void
231 DDXMLElement::appendAttributes( std::vector<std::string> & tv,
232  const std::string& name )
233 {
234  for (size_t i = tv.size(); i < attributes_.size(); ++i)
235  {
236  DDXMLAttribute::const_iterator itnv = attributes_[i].find(name);
237  if (itnv != attributes_[i].end())
238  tv.emplace_back(itnv->second);
239  else
240  tv.emplace_back("");
241  }
242 }
243 
244 // Number of elements accumulated.
245 size_t
246 DDXMLElement::size( void ) const
247 {
248  return attributes_.size();
249 }
250 
251 std::vector<DDXMLAttribute>::const_iterator
253 {
254  myIter_ = attributes_.begin();
255  return attributes_.begin();
256 }
257 
258 std::vector<DDXMLAttribute>::const_iterator
260 {
261  myIter_ = attributes_.end();
262  return attributes_.end();
263 }
264 
265 std::vector<DDXMLAttribute>::const_iterator&
267 {
268  myIter_ = myIter_ + inc;
269  return myIter_;
270 }
271 
272 const std::string&
273 DDXMLElement::parent( void ) const
274 {
275  return parentElement_;
276 }
277 
278 void
280 {
281  parentElement_ = pename;
282 }
283 
284 void
286 {
287  myElement_ = sename;
288 }
289 
290 bool
292 {
293  return (attributes_.empty() ? true : false);
294 }
295 
296 void
297 DDXMLElement::throwError( const std::string& keyMessage ) const
298 {
299  std::string msg = keyMessage + "\n";
300  msg += " Element " + myElement_ +"\n";
301 
302  throw cms::Exception("DDException") << msg;
303 }
virtual bool isEmpty(void) const
Have any elements of this type been encountered but not processed?
std::string parentElement_
Definition: DDXMLElement.h:184
std::vector< DDXMLAttribute >::const_iterator & operator++(int inc)
Allow the elements of this type to be iterated over using ++ operator.
DDLElementRegistry * myRegistry_
Definition: DDXMLElement.h:172
static const HistoName names[]
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 "row" of attributes, i.e. one attribute set.
Definition: DDXMLElement.cc:72
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:15
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...
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:83
std::map< std::string, std::string > DDXMLAttribute
Definition: DDXMLElement.h:45
virtual void preProcessElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv)
Called by loadAttributes AFTER attributes are loaded.
Definition: DDXMLElement.cc:30
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:35
virtual size_t size(void) const
Number of elements accumulated.
std::vector< DDXMLAttribute >::const_iterator myIter_
Definition: DDXMLElement.h:182
std::vector< DDXMLAttribute > attributes_
Definition: DDXMLElement.h:178
friend std::ostream & operator<<(std::ostream &os, const DDXMLElement &element)
std::string myElement_
Definition: DDXMLElement.h:183
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:180
This is a base class for processing XML elements in the DDD.
Definition: DDXMLElement.h:48
tuple msg
Definition: mps_check.py:277
virtual const std::string & getAttribute(const std::string &name) const
Access to attributes by name.
Definition: DDXMLElement.cc:63
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:54
DDXMLElement(DDLElementRegistry *myreg)
Constructor.
Definition: DDXMLElement.cc:14
virtual const DDName getDDName(const std::string &defaultNS, const std::string &attname=std::string("name"), size_t aIndex=0)
Definition: DDXMLElement.cc:79
const std::string getText(size_t tindex=0) const
retrieve the text blob.
std::vector< std::string > text_
Definition: DDXMLElement.h:179