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 // $Id: FWConfiguration.cc,v 1.7 2011/11/18 02:57:07 amraktad Exp $
12 //
13 
14 // system include files
15 #include <stdexcept>
16 #include <algorithm>
17 #include <boost/bind.hpp>
18 
19 // user include files
21 
22 
23 //
24 // constants, enums and typedefs
25 //
26 
27 //
28 // static data member definitions
29 //
30 
31 //
32 // constructors and destructor
33 //
34 //FWConfiguration::FWConfiguration()
35 //{
36 //}
37 
39  m_stringValues( rhs.m_stringValues ? new std::vector<std::string>(*(rhs.m_stringValues)) : 0),
40  m_keyValues( rhs.m_keyValues ? new KeyValues(*(rhs.m_keyValues)) : 0),
41  m_version(rhs.m_version)
42 {
43 }
44 
46 {
47 }
48 
49 //
50 // assignment operators
51 //
53 {
54  //An exception safe implementation is
55  FWConfiguration temp(rhs);
56  swap(temp);
57 
58  return *this;
59 }
60 
61 //
62 // member functions
63 //
65 FWConfiguration::addKeyValue(const std::string& iKey, const FWConfiguration& iConfig)
66 {
67  if( m_stringValues ) {
68  throw std::runtime_error("adding key/value to configuration containing string values");
69  }
70  if(not m_keyValues) {
71  m_keyValues.reset(new KeyValues(1, std::make_pair(iKey,iConfig) ) );
72  } else {
73  m_keyValues->push_back(std::make_pair(iKey,iConfig));
74  }
75  return *this;
76 }
78 FWConfiguration::addKeyValue(const std::string&iKey, FWConfiguration& iConfig, bool iDoSwap)
79 {
80  if( m_stringValues ) {
81  throw std::runtime_error("adding key/value to configuration containing string values");
82  }
83  if( m_stringValues ) {
84  throw std::runtime_error("adding key/value to configuration containing string values");
85  }
86  if(not m_keyValues) {
87  if(not iDoSwap) {
88  m_keyValues.reset(new KeyValues(1, std::make_pair(iKey,iConfig) ) );
89  } else {
90  m_keyValues.reset(new KeyValues(1, std::make_pair(iKey,FWConfiguration()) ) );
91  m_keyValues->back().second.swap(iConfig);
92  }
93  } else {
94  if(not iDoSwap) {
95  m_keyValues->push_back(std::make_pair(iKey,iConfig));
96  } else {
97  m_keyValues->push_back(std::make_pair(iKey,FWConfiguration()));
98  m_keyValues->back().second.swap(iConfig);
99  }
100  }
101  return *this;
102 }
103 
105 FWConfiguration::addValue(const std::string& iValue)
106 {
107  if( m_keyValues ) {
108  throw std::runtime_error("adding string value to configuration containing key/value pairs");
109  }
110  if( not m_stringValues ) {
111  m_stringValues.reset( new std::vector<std::string>(1,iValue) );
112  } else {
113  m_stringValues->push_back(iValue);
114  }
115  return *this;
116 }
117 
118 void
120 {
122  m_stringValues.swap(iRHS.m_stringValues);
123  m_keyValues.swap(iRHS.m_keyValues);
124 }
125 
126 //
127 // const member functions
128 //
129 const std::string&
130 FWConfiguration::value(unsigned int iIndex) const
131 {
132  if( not m_stringValues ) {
133  throw std::runtime_error("no string values set");
134  }
135  return m_stringValues->at(iIndex);
136 }
137 
138 const FWConfiguration*
139 FWConfiguration::valueForKey(const std::string& iKey) const
140 {
141  if( m_stringValues ) {
142  throw std::runtime_error("valueFoKey fails because configuration containing string values");
143  }
144  if(not m_keyValues) {
145  throw std::runtime_error("valueForKey fails becuase no key/values set");
146  }
147  KeyValues::iterator itFind = std::find_if(m_keyValues->begin(),
148  m_keyValues->end(),
149  boost::bind(&std::pair<std::string,FWConfiguration>::first,_1) == iKey);
150  if(itFind == m_keyValues->end()) {
151  return 0;
152  }
153  return &(itFind->second);
154 }
155 
156 
160 std::string
161 attrEscape(std::string value)
162 {
164  index = 0;
165  while (std::string::npos != (index=value.find('&',index))){
166  value.replace(index, 1,"&amp;", 5);
167  // Do not check "&quot;" for quotes.
168  index += 5;
169  }
170 
171  while (std::string::npos != (index=value.find('"',index))){
172  value.replace(index, 1,"&quot;", 6);
173  // Do not check "&quot;" for quotes.
174  index += 6;
175  }
176 
177  index = 0;
178  while (std::string::npos != (index=value.find('<',index))){
179  value.replace(index, 1,"&lt;", 4);
180  // Do not check "&quot;" for quotes.
181  index += 4;
182  }
183 
184  index = 0;
185  while (std::string::npos != (index=value.find('>',index))){
186  value.replace(index, 1,"&gt;", 4);
187  // Do not check "&quot;" for quotes.
188  index += 4;
189  }
190 
191  return value;
192 }
193 
214 void
215 streamTo(std::ostream& oTo, const FWConfiguration& iConfig, const std::string &name)
216 {
217  static int recursionLevel = -1;
218  recursionLevel += 1;
219  std::string indentation(recursionLevel, ' ');
220  oTo << indentation << "<config name=\"" << name
221  << "\" version=\"" << iConfig.version() << "\">\n";
222  if(iConfig.stringValues()) {
223  for(FWConfiguration::StringValues::const_iterator it = iConfig.stringValues()->begin();
224  it != iConfig.stringValues()->end();
225  ++it) {
226  oTo << indentation << " <string>" << attrEscape(*it) << "</string>\n";
227  }
228  }
229  if(iConfig.keyValues()) {
230  for(FWConfiguration::KeyValues::const_iterator it = iConfig.keyValues()->begin();
231  it != iConfig.keyValues()->end();
232  ++it) {
233  streamTo(oTo, it->second, attrEscape(it->first));
234  }
235  }
236  oTo << indentation << "</config>" << std::endl;
237  recursionLevel -= 1;
238 }
239 
240 //
241 // static member functions
242 //
std::vector< std::pair< std::string, FWConfiguration > > KeyValues
const StringValues * stringValues() const
FWConfiguration(unsigned int iVersion=1)
const KeyValues * keyValues() const
void streamTo(std::ostream &, const FWConfiguration &, const std::string &name)
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 &)
bool first
Definition: L1TdeRCT.cc:94
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