CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWConfiguration.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWConfiguration
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Fri Feb 22 15:54:29 EST 2008
11 //
12 
13 // system include files
14 #include <stdexcept>
15 #include <algorithm>
16 #include <boost/bind.hpp>
17 
18 // user include files
20 
21 
22 //
23 // constants, enums and typedefs
24 //
25 
26 //
27 // static data member definitions
28 //
29 
30 //
31 // constructors and destructor
32 //
33 //FWConfiguration::FWConfiguration()
34 //{
35 //}
36 
38  m_stringValues( rhs.m_stringValues ? new std::vector<std::string>(*(rhs.m_stringValues)) : 0),
39  m_keyValues( rhs.m_keyValues ? new KeyValues(*(rhs.m_keyValues)) : 0),
40  m_version(rhs.m_version)
41 {
42 }
43 
45 {
46 }
47 
48 //
49 // assignment operators
50 //
52 {
53  //An exception safe implementation is
54  FWConfiguration temp(rhs);
55  swap(temp);
56 
57  return *this;
58 }
59 
60 //
61 // member functions
62 //
65 {
66  if( m_stringValues ) {
67  throw std::runtime_error("adding key/value to configuration containing string values");
68  }
69  if(not m_keyValues) {
70  m_keyValues.reset(new KeyValues(1, std::make_pair(iKey,iConfig) ) );
71  } else {
72  m_keyValues->push_back(std::make_pair(iKey,iConfig));
73  }
74  return *this;
75 }
77 FWConfiguration::addKeyValue(const std::string&iKey, FWConfiguration& iConfig, bool iDoSwap)
78 {
79  if( m_stringValues ) {
80  throw std::runtime_error("adding key/value to configuration containing string values");
81  }
82  if( m_stringValues ) {
83  throw std::runtime_error("adding key/value to configuration containing string values");
84  }
85  if(not m_keyValues) {
86  if(not iDoSwap) {
87  m_keyValues.reset(new KeyValues(1, std::make_pair(iKey,iConfig) ) );
88  } else {
89  m_keyValues.reset(new KeyValues(1, std::make_pair(iKey,FWConfiguration()) ) );
90  m_keyValues->back().second.swap(iConfig);
91  }
92  } else {
93  if(not iDoSwap) {
94  m_keyValues->push_back(std::make_pair(iKey,iConfig));
95  } else {
96  m_keyValues->push_back(std::make_pair(iKey,FWConfiguration()));
97  m_keyValues->back().second.swap(iConfig);
98  }
99  }
100  return *this;
101 }
102 
105 {
106  if( m_keyValues ) {
107  throw std::runtime_error("adding string value to configuration containing key/value pairs");
108  }
109  if( not m_stringValues ) {
110  m_stringValues.reset( new std::vector<std::string>(1,iValue) );
111  } else {
112  m_stringValues->push_back(iValue);
113  }
114  return *this;
115 }
116 
117 void
119 {
121  m_stringValues.swap(iRHS.m_stringValues);
122  m_keyValues.swap(iRHS.m_keyValues);
123 }
124 
125 //
126 // const member functions
127 //
128 const std::string&
129 FWConfiguration::value(unsigned int iIndex) const
130 {
131  if( not m_stringValues ) {
132  throw std::runtime_error("no string values set");
133  }
134  return m_stringValues->at(iIndex);
135 }
136 
137 const FWConfiguration*
139 {
140  if( m_stringValues ) {
141  throw std::runtime_error("valueFoKey fails because configuration containing string values");
142  }
143  if(not m_keyValues) {
144  throw std::runtime_error("valueForKey fails becuase no key/values set");
145  }
146  KeyValues::iterator itFind = std::find_if(m_keyValues->begin(),
147  m_keyValues->end(),
148  boost::bind(&std::pair<std::string,FWConfiguration>::first,_1) == iKey);
149  if(itFind == m_keyValues->end()) {
150  return 0;
151  }
152  return &(itFind->second);
153 }
154 
155 
161 {
163  index = 0;
164  while (std::string::npos != (index=value.find('&',index))){
165  value.replace(index, 1,"&amp;", 5);
166  // Do not check "&quot;" for quotes.
167  index += 5;
168  }
169 
170  while (std::string::npos != (index=value.find('"',index))){
171  value.replace(index, 1,"&quot;", 6);
172  // Do not check "&quot;" for quotes.
173  index += 6;
174  }
175 
176  index = 0;
177  while (std::string::npos != (index=value.find('<',index))){
178  value.replace(index, 1,"&lt;", 4);
179  // Do not check "&quot;" for quotes.
180  index += 4;
181  }
182 
183  index = 0;
184  while (std::string::npos != (index=value.find('>',index))){
185  value.replace(index, 1,"&gt;", 4);
186  // Do not check "&quot;" for quotes.
187  index += 4;
188  }
189 
190  return value;
191 }
192 
213 void
214 FWConfiguration::streamTo(std::ostream& oTo, const FWConfiguration& iConfig, const std::string &name)
215 {
216  static int recursionLevel = -1;
217  recursionLevel += 1;
218  std::string indentation(recursionLevel, ' ');
219  oTo << indentation << "<config name=\"" << name
220  << "\" version=\"" << iConfig.version() << "\">\n";
221  if(iConfig.stringValues()) {
222  for(FWConfiguration::StringValues::const_iterator it = iConfig.stringValues()->begin();
223  it != iConfig.stringValues()->end();
224  ++it) {
225  oTo << indentation << " <string>" << attrEscape(*it) << "</string>\n";
226  }
227  }
228  if(iConfig.keyValues()) {
229  for(FWConfiguration::KeyValues::const_iterator it = iConfig.keyValues()->begin();
230  it != iConfig.keyValues()->end();
231  ++it) {
232  FWConfiguration::streamTo(oTo, it->second, attrEscape(it->first));
233  }
234  }
235  oTo << indentation << "</config>" << std::endl;
236  recursionLevel -= 1;
237 }
238 
239 //
240 // static member functions
241 //
std::vector< std::pair< std::string, FWConfiguration > > KeyValues
const StringValues * stringValues() const
FWConfiguration(unsigned int iVersion=1)
const KeyValues * keyValues() const
boost::scoped_ptr< std::vector< std::string > > m_stringValues
unsigned int version() const
FWConfiguration & operator=(const FWConfiguration &)
uint16_t size_type
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
void swap(FWConfiguration &)
FWConfiguration & addKeyValue(const std::string &, const FWConfiguration &)
const std::string & value(unsigned int iIndex=0) const
FWConfiguration & addValue(const std::string &)
std::string attrEscape(std::string value)
virtual ~FWConfiguration()
unsigned int m_version
const FWConfiguration * valueForKey(const std::string &iKey) const
boost::scoped_ptr< std::vector< std::pair< std::string, FWConfiguration > > > m_keyValues
static void streamTo(std::ostream &oTo, const FWConfiguration &iConfig, const std::string &name)