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 
14 DDXMLElement::DDXMLElement(DDLElementRegistry* myreg) : myRegistry_(myreg), attributes_(), text_(), autoClear_(false) {}
15 
16 DDXMLElement::DDXMLElement(DDLElementRegistry* myreg, const bool& clearme)
17  : myRegistry_(myreg), attributes_(), text_(), autoClear_(clearme) {}
18 
19 // For pre-processing, after attributes are loaded. Default, do nothing!
21 
22 // This loads the attributes into the attributes_ std::vector.
24  const std::vector<std::string>& names,
25  const std::vector<std::string>& values,
26  const std::string& nmspace,
27  DDCompactView& cpv) {
28  attributes_.resize(attributes_.size() + 1);
29  DDXMLAttribute& tAttributes = attributes_.back();
30 
31  // adds attributes
32  for (size_t i = 0; i < names.size(); ++i) {
33  tAttributes.insert(std::make_pair(names[i], values[i]));
34  }
35 
36  preProcessElement(elemName, nmspace, cpv);
37 }
38 
39 // clear data.
40 void DDXMLElement::clear(void) {
41  text_.clear();
42  attributes_.clear();
43  attributeAccumulator_.clear();
44 }
45 
46 // Access to current attributes by name.
48  static const std::string ldef;
49  if (!attributes_.empty())
50  return get(name, attributes_.size() - 1);
51  return ldef;
52 }
53 
54 const DDXMLAttribute& DDXMLElement::getAttributeSet(size_t aIndex) const { return attributes_[aIndex]; }
55 
56 const DDName DDXMLElement::getDDName(const std::string& defaultNS, const std::string& attname, size_t aIndex) {
57  if (aIndex < attributes_.size() && attributes_[aIndex].find(attname) != attributes_[aIndex].end()) {
58  std::string ns = defaultNS;
59  // For the user to fully control namespaces they must provide for
60  // all name attributes something of the form, for example:
61  // <Solid name="ns:name" ...
62  // If defaultNS is "!" (magic I don't like) then find and set
63  // the namespace properly.
64  if (defaultNS == "!") {
65  ns = "";
66  }
67  const std::string& name = attributes_[aIndex].find(attname)->second;
68  std::string rn = name;
69  size_t foundColon = name.find(':');
70  if (foundColon != std::string::npos) {
71  ns = name.substr(0, foundColon);
72  rn = name.substr(foundColon + 1);
73  }
74  return DDName(rn, ns);
75  }
76  std::string msg = "DDXMLElement:getDDName failed. It was asked to make ";
77  msg += "a DDName using attribute: " + attname;
78  msg += " in position: " + std::to_string(aIndex) + ". There are ";
79  msg += std::to_string(attributes_.size()) + " entries in the element.";
80  throwError(msg);
81  return DDName("justToCompile", "justToCompile"); // used to make sure it compiles
82 }
83 
84 // Returns a specific value from the aIndex set of attributes.
85 const std::string& DDXMLElement::get(const std::string& name, const size_t aIndex) const {
86  static const std::string sts;
87  if (aIndex < attributes_.size()) {
88  DDXMLAttribute::const_iterator it = attributes_[aIndex].find(name);
89  if (attributes_[aIndex].end() == it) {
90  return sts;
91  } else
92  return (it->second);
93  }
94  std::string msg = "DDXMLElement:get failed. It was asked for attribute " + name;
95  msg += " in position " + std::to_string(aIndex) + " when there are only ";
96  msg += std::to_string(attributes_.size()) + " in the element storage.\n";
97  throwError(msg);
98  // meaningless...
99  return sts;
100 }
101 
102 // Returns a specific set of values as a std::vector of std::strings,
103 // given the attribute name.
104 std::vector<std::string> DDXMLElement::getVectorAttribute(const std::string& name) {
105  // The idea here is that the attributeAccumulator_ is a cache of
106  // on-the-fly generation from the std::vector<DDXMLAttribute> and the
107  // reason is simply to speed things up if it is requested more than once.
108  std::vector<std::string> tv;
109  AttrAccumType::const_iterator ita = attributeAccumulator_.find(name);
110  if (ita != attributeAccumulator_.end()) {
112  if (tv.size() < attributes_.size()) {
113  appendAttributes(tv, name);
114  }
115  } else {
116  if (!attributes_.empty()) {
117  appendAttributes(tv, name);
118  } else {
119  // throw cms::Exception("DDException") << msg;
120  }
121  }
122  return tv;
123 }
124 
125 // Default do-nothing processElementBases.
128  if (autoClear_)
129  clear();
130 }
131 
132 void DDXMLElement::loadText(const std::string& inText) { text_.emplace_back(inText); }
133 
135  static const std::string cr("\n");
136  if (!text_.empty()) {
137  text_[text_.size() - 1] += cr;
138  text_[text_.size() - 1] += inText;
139  } else {
140  std::string msg = "DDXMLElement::appendText could not append to non-existent text.";
141  throwError(msg);
142  }
143 }
144 
145 const std::string DDXMLElement::getText(size_t tindex) const {
146  if (tindex > text_.size()) {
147  std::string msg = "DDXMLElement::getText tindex is greater than text_.size()).";
148  throwError(msg);
149  }
150  return text_[tindex];
151 }
152 
153 bool DDXMLElement::gotText(void) const {
154  if (!text_.empty())
155  return true;
156  return false;
157 }
158 
159 std::ostream& operator<<(std::ostream& os, const DDXMLElement& element) {
160  element.stream(os);
161  return os;
162 }
163 
164 void DDXMLElement::stream(std::ostream& os) const {
165  os << "Output of current element attributes:" << std::endl;
166  for (const auto& attribute : attributes_) {
167  for (DDXMLAttribute::const_iterator it = attribute.begin(); it != attribute.end(); ++it)
168  os << it->first << " = " << it->second << "\t";
169  os << std::endl;
170  }
171 }
172 
173 void DDXMLElement::appendAttributes(std::vector<std::string>& tv, const std::string& name) {
174  for (size_t i = tv.size(); i < attributes_.size(); ++i) {
175  DDXMLAttribute::const_iterator itnv = attributes_[i].find(name);
176  if (itnv != attributes_[i].end())
177  tv.emplace_back(itnv->second);
178  else
179  tv.emplace_back("");
180  }
181 }
182 
183 // Number of elements accumulated.
184 size_t DDXMLElement::size(void) const { return attributes_.size(); }
185 
186 std::vector<DDXMLAttribute>::const_iterator DDXMLElement::begin(void) {
187  myIter_ = attributes_.begin();
188  return attributes_.begin();
189 }
190 
191 std::vector<DDXMLAttribute>::const_iterator DDXMLElement::end(void) {
192  myIter_ = attributes_.end();
193  return attributes_.end();
194 }
195 
196 std::vector<DDXMLAttribute>::const_iterator& DDXMLElement::operator++(int inc) {
197  myIter_ = myIter_ + inc;
198  return myIter_;
199 }
200 
201 const std::string& DDXMLElement::parent(void) const { return parentElement_; }
202 
203 void DDXMLElement::setParent(const std::string& pename) { parentElement_ = pename; }
204 
205 void DDXMLElement::setSelf(const std::string& sename) { myElement_ = sename; }
206 
207 bool DDXMLElement::isEmpty(void) const { return (attributes_.empty() ? true : false); }
208 
209 void DDXMLElement::throwError(const std::string& keyMessage) const {
210  std::string msg = keyMessage + "\n";
211  msg += " Element " + myElement_ + "\n";
212 
213  throw cms::Exception("DDException") << msg;
214 }
std::string parentElement_
Definition: DDXMLElement.h:186
std::vector< DDXMLAttribute >::const_iterator & operator++(int inc)
Allow the elements of this type to be iterated over using ++ operator.
virtual const std::string & get(const std::string &name, size_t aIndex=0) const
Returns a specific value from the aIndex set of attributes.
Definition: DDXMLElement.cc:85
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.
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:17
std::string to_string(const V &value)
Definition: OMSAccess.h:71
virtual void processElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv)
Processing the element.
virtual bool gotText(void) const
gotText()? kind of like gotMilk? Yes = text has already been encountered.
void appendAttributes(std::vector< std::string > &tv, const std::string &name)
behind the scenes appending to pAttributes...
virtual bool isEmpty(void) const
Have any elements of this type been encountered but not processed?
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
virtual size_t size(void) const
Number of elements accumulated.
const std::string names[nVars_]
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:20
std::ostream & operator<<(std::ostream &os, const DDXMLElement &element)
void loadText(const std::string &inText)
Used to load both text and XML comments into this object.
virtual void stream(std::ostream &os) const
Allow for the elements to have their own streaming method, but also provide a default.
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:23
std::vector< DDXMLAttribute >::const_iterator myIter_
Definition: DDXMLElement.h:184
virtual const DDXMLAttribute & getAttributeSet(size_t aIndex=0) const
Get a "row" of attributes, i.e. one attribute set.
Definition: DDXMLElement.cc:54
std::vector< DDXMLAttribute > attributes_
Definition: DDXMLElement.h:180
std::string myElement_
Definition: DDXMLElement.h:185
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:182
This is a base class for processing XML elements in the DDD.
Definition: DDXMLElement.h:48
tuple msg
Definition: mps_check.py:285
virtual std::vector< DDXMLAttribute >::const_iterator begin(void)
const std::string & parent(void) const
access to parent element name
The main class for processing parsed elements.
virtual void clear(void)
clear this element&#39;s contents.
Definition: DDXMLElement.cc:40
void throwError(const std::string &keyMessage) const
format std::string for throw an error.
virtual const std::string & getAttribute(const std::string &name) const
Access to attributes by name.
Definition: DDXMLElement.cc:47
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:56
const std::string getText(size_t tindex=0) const
retrieve the text blob.
std::vector< std::string > text_
Definition: DDXMLElement.h:181