Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
// -*- C++ -*-
//
// Package:     Core
// Class  :     FWConfiguration
//
// Implementation:
//     <Notes on implementation>
//
// Original Author:  Chris Jones
//         Created:  Fri Feb 22 15:54:29 EST 2008
//

// system include files
#include <memory>

#include <stdexcept>
#include <algorithm>
#include <functional>
// user include files
#include "Fireworks/Core/interface/FWConfiguration.h"

//
// constants, enums and typedefs
//

//
// static data member definitions
//

//
// constructors and destructor
//
//FWConfiguration::FWConfiguration()
//{
//}

FWConfiguration::FWConfiguration(const FWConfiguration& rhs)
    : m_stringValues(rhs.m_stringValues ? new std::vector<std::string>(*(rhs.m_stringValues)) : nullptr),
      m_keyValues(rhs.m_keyValues ? new KeyValues(*(rhs.m_keyValues)) : nullptr),
      m_version(rhs.m_version) {}

FWConfiguration::~FWConfiguration() {}

//
// assignment operators
//
FWConfiguration& FWConfiguration::operator=(const FWConfiguration& rhs) {
  //An exception safe implementation is
  FWConfiguration temp(rhs);
  swap(temp);

  return *this;
}

//
// member functions
//
FWConfiguration& FWConfiguration::addKeyValue(const std::string& iKey, const FWConfiguration& iConfig) {
  if (m_stringValues) {
    throw std::runtime_error("adding key/value to configuration containing string values");
  }
  if (not m_keyValues) {
    m_keyValues = std::make_unique<KeyValues>(1, std::make_pair(iKey, iConfig));
  } else {
    m_keyValues->push_back(std::make_pair(iKey, iConfig));
  }
  return *this;
}
FWConfiguration& FWConfiguration::addKeyValue(const std::string& iKey, FWConfiguration& iConfig, bool iDoSwap) {
  if (m_stringValues) {
    throw std::runtime_error("adding key/value to configuration containing string values");
  }
  if (m_stringValues) {
    throw std::runtime_error("adding key/value to configuration containing string values");
  }
  if (not m_keyValues) {
    if (not iDoSwap) {
      m_keyValues = std::make_unique<KeyValues>(1, std::make_pair(iKey, iConfig));
    } else {
      m_keyValues = std::make_unique<KeyValues>(1, std::make_pair(iKey, FWConfiguration()));
      m_keyValues->back().second.swap(iConfig);
    }
  } else {
    if (not iDoSwap) {
      m_keyValues->push_back(std::make_pair(iKey, iConfig));
    } else {
      m_keyValues->push_back(std::make_pair(iKey, FWConfiguration()));
      m_keyValues->back().second.swap(iConfig);
    }
  }
  return *this;
}

FWConfiguration& FWConfiguration::addValue(const std::string& iValue) {
  if (m_keyValues) {
    throw std::runtime_error("adding string value to configuration containing key/value pairs");
  }
  if (not m_stringValues) {
    m_stringValues = std::make_unique<std::vector<std::string>>(1, iValue);
  } else {
    m_stringValues->push_back(iValue);
  }
  return *this;
}

void FWConfiguration::swap(FWConfiguration& iRHS) {
  std::swap(m_version, iRHS.m_version);
  m_stringValues.swap(iRHS.m_stringValues);
  m_keyValues.swap(iRHS.m_keyValues);
}

//
// const member functions
//
const std::string& FWConfiguration::value(unsigned int iIndex) const {
  if (not m_stringValues) {
    throw std::runtime_error("no string values set");
  }
  return m_stringValues->at(iIndex);
}

const FWConfiguration* FWConfiguration::valueForKey(const std::string& iKey) const {
  if (m_stringValues) {
    throw std::runtime_error("valueFoKey fails because configuration containing string values");
  }
  if (not m_keyValues) {
    throw std::runtime_error("valueForKey fails becuase no key/values set");
  }
  KeyValues::iterator itFind =
      std::find_if(m_keyValues->begin(),
                   m_keyValues->end(),
                   std::bind(std::equal_to<void>(),
                             std::bind(&std::pair<std::string, FWConfiguration>::first, std::placeholders::_1),
                             iKey));
  if (itFind == m_keyValues->end()) {
    return nullptr;
  }
  return &(itFind->second);
}

/** Helper function to make sure we escape correctly xml entities embedded in
    the string @a value.
  */
std::string attrEscape(std::string value) {
  std::string::size_type index = 0;
  index = 0;
  while (std::string::npos != (index = value.find('&', index))) {
    value.replace(index, 1, "&amp;", 5);
    // Do not check "&quot;" for quotes.
    index += 5;
  }

  while (std::string::npos != (index = value.find('"', index))) {
    value.replace(index, 1, "&quot;", 6);
    // Do not check "&quot;" for quotes.
    index += 6;
  }

  index = 0;
  while (std::string::npos != (index = value.find('<', index))) {
    value.replace(index, 1, "&lt;", 4);
    // Do not check "&quot;" for quotes.
    index += 4;
  }

  index = 0;
  while (std::string::npos != (index = value.find('>', index))) {
    value.replace(index, 1, "&gt;", 4);
    // Do not check "&quot;" for quotes.
    index += 4;
  }

  return value;
}

/** Streaming FWConfiguration objects to xml.

    Example of dump is:
    
    <config name="top" version="1">
      <string value="S1">
      <string value="S2">
      ...
      <string value="SN">
      <config name="c1">
         ...
      </configuration>
      <config name="c2">
         ...
      </config>
      ...
    </config>
   
    Streaming the top level configuration item will stream all its children.
  */
void FWConfiguration::streamTo(std::ostream& oTo, const FWConfiguration& iConfig, const std::string& name) {
  static int recursionLevel = -1;
  recursionLevel += 1;
  std::string indentation(recursionLevel, ' ');
  oTo << indentation << "<config name=\"" << name << "\" version=\"" << iConfig.version() << "\">\n";
  if (iConfig.stringValues()) {
    for (FWConfiguration::StringValues::const_iterator it = iConfig.stringValues()->begin();
         it != iConfig.stringValues()->end();
         ++it) {
      oTo << indentation << "  <string>" << attrEscape(*it) << "</string>\n";
    }
  }
  if (iConfig.keyValues()) {
    for (FWConfiguration::KeyValues::const_iterator it = iConfig.keyValues()->begin(); it != iConfig.keyValues()->end();
         ++it) {
      FWConfiguration::streamTo(oTo, it->second, attrEscape(it->first));
    }
  }
  oTo << indentation << "</config>" << std::endl;
  recursionLevel -= 1;
}

//
// static member functions
//