VectorParameterParser.h 5.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef VECTOR_VALUE_PARAMETER_PARSER_H_
#define VECTOR_VALUE_PARAMETER_PARSER_H_

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidKernel/System.h"

#include "MantidAPI/ImplicitFunctionParameterParser.h"

#include <Poco/DOM/DOMParser.h>
#include <Poco/DOM/Document.h>
#include <Poco/DOM/Element.h>
#include <Poco/DOM/NodeList.h>
#include <Poco/DOM/NodeIterator.h>
#include <Poco/DOM/NodeFilter.h>
#include <Poco/File.h>
#include <Poco/Path.h>
19
#ifndef Q_MOC_RUN
20
21
#include <boost/lexical_cast.hpp>
#endif
22

23
24
namespace Mantid {
namespace API {
25
26
27
28
29
30
/**

 XML parser for vector value (n elements) parameter types.

 @author Owen Arnold, Tessella plc
 @date 21/07/2011
31
32
 Copyright &copy; 2010 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
 National Laboratory & European Spallation Source
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

 This file is part of Mantid.

 Mantid is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.

 Mantid is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

49
 File change history is stored at: <https://github.com/mantidproject/mantid>
50
51
 Code Documentation is available at: <http://doxygen.mantidproject.org>
 */
52
53
54
template <class VectorValueParameterType>
class DLLExport VectorParameterParser
    : public Mantid::API::ImplicitFunctionParameterParser {
55
public:
56
  VectorValueParameterType *parseVectorParameter(std::string sValue);
57

58
  Mantid::API::ImplicitFunctionParameter *
59
  createParameter(Poco::XML::Element *parameterElement) override;
60

61
62
  VectorValueParameterType *
  createWithoutDelegation(Poco::XML::Element *parameterElement);
63

64
65
  void setSuccessorParser(
      Mantid::API::ImplicitFunctionParameterParser *paramParser) override;
66
67
68
69
70
71
72
73
74
};

////////////////////////////////////////////////////////////////////

//----------------------------------------------------------------------
/* Parse the value aspect of the parameter only.
@param sValue : parameter value in string form.
@return new parameter object.
*/
75
76
77
78
template <typename VectorValueParameterType>
VectorValueParameterType *
VectorParameterParser<VectorValueParameterType>::parseVectorParameter(
    std::string sValue) {
79
80
81
  std::vector<std::string> strs;
  boost::split(strs, sValue, boost::is_any_of(","));

82
  VectorValueParameterType *product = new VectorValueParameterType(strs.size());
83
84
  typedef typename VectorValueParameterType::ValueType ValType;
  ValType value = 0;
85

86
  for (size_t i = 0; i < strs.size(); i++) {
87
88
    boost::erase_all(strs[i], " ");
    value = boost::lexical_cast<ValType>(strs[i]);
89
    product->addValue(i, value);
90
91
92
93
94
95
96
97
98
  }
  return product;
}

//----------------------------------------------------------------------
/* Create parameter from xml element.
@param parameterElement : XML element containing the parameter info.
@return new parameter object.
*/
99
100
101
102
template <typename VectorValueParameterType>
Mantid::API::ImplicitFunctionParameter *
VectorParameterParser<VectorValueParameterType>::createParameter(
    Poco::XML::Element *parameterElement) {
103
  std::string typeName = parameterElement->getChildElement("Type")->innerText();
104
105
  if (VectorValueParameterType::parameterName() != typeName) {
    if (!m_successor) {
106
107
108
      throw std::runtime_error("No successor ParameterParser!");
    }
    return m_successor->createParameter(parameterElement);
109
110
111
  } else {
    std::string sParameterValue =
        parameterElement->getChildElement("Value")->innerText();
112
113
114
115
    return parseVectorParameter(sParameterValue);
  }
}

116
//------------------------------------------------------------------------------
117
118
/* Creates a parameter from an xml element. This is single-shot. Does not defer
to successor if it fails!.
119
120
121
@param parameterElement : xml Element
@return A fully constructed VectorValueParameterType.
*/
122
123
124
125
template <class VectorValueParameterType>
VectorValueParameterType *
VectorParameterParser<VectorValueParameterType>::createWithoutDelegation(
    Poco::XML::Element *parameterElement) {
126
  std::string typeName = parameterElement->getChildElement("Type")->innerText();
127
128
129
130
131
132
  if (VectorValueParameterType::parameterName() != typeName) {
    throw std::runtime_error("The attempted ::createWithoutDelegation failed. "
                             "The type provided does not match this parser.");
  } else {
    std::string sParameterValue =
        parameterElement->getChildElement("Value")->innerText();
133
134
135
136
    return parseVectorParameter(sParameterValue);
  }
}

137
138
139
140
//----------------------------------------------------------------------
/* Setter for a successor parser.
@param paramParser : successor
*/
141
template <typename VectorValueParameterType>
142
void VectorParameterParser<VectorValueParameterType>::setSuccessorParser(
143
    Mantid::API::ImplicitFunctionParameterParser *paramParser) {
144
145
146
147
148
149
150
  Mantid::API::ImplicitFunctionParameterParser::SuccessorType temp(paramParser);
  m_successor.swap(temp);
}
}
}

#endif