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 }
DDXMLElement::parent
const std::string & parent(void) const
access to parent element name
Definition: DDXMLElement.cc:201
DDXMLElement::clear
virtual void clear(void)
clear this element's contents.
Definition: DDXMLElement.cc:40
mps_fire.i
i
Definition: mps_fire.py:428
DDXMLElement::myElement_
std::string myElement_
Definition: DDXMLElement.h:185
MessageLogger.h
funct::false
false
Definition: Factorize.h:29
DDXMLElement::getVectorAttribute
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.
Definition: DDXMLElement.cc:104
DDXMLElement::throwError
void throwError(const std::string &keyMessage) const
format std::string for throw an error.
Definition: DDXMLElement.cc:209
DDName
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:17
DDXMLElement::stream
virtual void stream(std::ostream &os) const
Allow for the elements to have their own streaming method, but also provide a default.
Definition: DDXMLElement.cc:164
DDLElementRegistry
The main class for processing parsed elements.
Definition: DDLElementRegistry.h:23
DDXMLElement::end
virtual std::vector< DDXMLAttribute >::const_iterator end(void)
Definition: DDXMLElement.cc:191
mps_check.msg
tuple msg
Definition: mps_check.py:285
DDXMLElement::loadAttributes
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
DDXMLElement::operator++
std::vector< DDXMLAttribute >::const_iterator & operator++(int inc)
Allow the elements of this type to be iterated over using ++ operator.
Definition: DDXMLElement.cc:196
DDXMLElement::attributes_
std::vector< DDXMLAttribute > attributes_
Definition: DDXMLElement.h:180
operator<<
std::ostream & operator<<(std::ostream &os, const DDXMLElement &element)
Definition: DDXMLElement.cc:159
DDXMLElement::getAttributeSet
virtual const DDXMLAttribute & getAttributeSet(size_t aIndex=0) const
Get a "row" of attributes, i.e. one attribute set.
Definition: DDXMLElement.cc:54
DDXMLElement::appendText
void appendText(const std::string &inText)
append to the current (i.e. most recently added)
Definition: DDXMLElement.cc:134
names
const std::string names[nVars_]
Definition: PhotonIDValueMapProducer.cc:124
contentValuesCheck.values
values
Definition: contentValuesCheck.py:38
DDCompactView
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
DDXMLElement::DDXMLElement
DDXMLElement(DDLElementRegistry *myreg)
Constructor.
Definition: DDXMLElement.cc:14
DDXMLElement::processElement
virtual void processElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv)
Processing the element.
Definition: DDXMLElement.cc:126
DDXMLElement::getAttribute
virtual const std::string & getAttribute(const std::string &name) const
Access to attributes by name.
Definition: DDXMLElement.cc:47
DDXMLElement::parentElement_
std::string parentElement_
Definition: DDXMLElement.h:186
DDXMLElement::appendAttributes
void appendAttributes(std::vector< std::string > &tv, const std::string &name)
behind the scenes appending to pAttributes...
Definition: DDXMLElement.cc:173
DDXMLElement::attributeAccumulator_
AttrAccumType attributeAccumulator_
Definition: DDXMLElement.h:182
DDXMLAttribute
std::map< std::string, std::string > DDXMLAttribute
Definition: DDXMLElement.h:45
funct::true
true
Definition: Factorize.h:173
DDXMLElement::preProcessElement
virtual void preProcessElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv)
Called by loadAttributes AFTER attributes are loaded.
Definition: DDXMLElement.cc:20
DDXMLElement::autoClear_
bool autoClear_
Definition: DDXMLElement.h:183
DDXMLElement::loadText
void loadText(const std::string &inText)
Used to load both text and XML comments into this object.
Definition: DDXMLElement.cc:132
DDXMLElement::setSelf
void setSelf(const std::string &sename)
Set self element name to central list of names.
Definition: DDXMLElement.cc:205
DDXMLElement::get
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
DDXMLElement
This is a base class for processing XML elements in the DDD.
Definition: DDXMLElement.h:48
DDXMLElement::setParent
void setParent(const std::string &pename)
Set parent element name to central list of names.
Definition: DDXMLElement.cc:203
DDXMLElement::begin
virtual std::vector< DDXMLAttribute >::const_iterator begin(void)
Definition: DDXMLElement.cc:186
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
DDXMLElement::isEmpty
virtual bool isEmpty(void) const
Have any elements of this type been encountered but not processed?
Definition: DDXMLElement.cc:207
DDXMLElement::getDDName
const virtual DDName getDDName(const std::string &defaultNS, const std::string &attname=std::string("name"), size_t aIndex=0)
Definition: DDXMLElement.cc:56
DDXMLElement::gotText
virtual bool gotText(void) const
gotText()? kind of like gotMilk? Yes = text has already been encountered.
Definition: DDXMLElement.cc:153
DDXMLElement::getText
const std::string getText(size_t tindex=0) const
retrieve the text blob.
Definition: DDXMLElement.cc:145
DDXMLElement::text_
std::vector< std::string > text_
Definition: DDXMLElement.h:181
DDXMLElement.h
Exception
Definition: hltDiff.cc:245
DDXMLElement::size
virtual size_t size(void) const
Number of elements accumulated.
Definition: DDXMLElement.cc:184
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
Exception.h
DDXMLElement::myIter_
std::vector< DDXMLAttribute >::const_iterator myIter_
Definition: DDXMLElement.h:184