Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_XMLParameterListReader.cpp
1// @HEADER
2// *****************************************************************************
3// Teuchos: Common Tools Package
4//
5// Copyright 2004 NTESS and the Teuchos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
12#include "Teuchos_Assert.hpp"
13#include "Teuchos_ParameterEntryXMLConverterDB.hpp"
16
17
18namespace Teuchos {
19
20
22: _allowDuplicateSublists(true)
23{;}
24
26{ return _allowDuplicateSublists; }
27
29{ _allowDuplicateSublists = policy; }
30
33{
35 xml.getTag()
36 !=
39 "XMLParameterListReader expected tag " <<
41 <<", found " << xml.getTag());
44 int validatorsIndex =
46 if(validatorsIndex != -1){
47 convertValidators(xml.getChild(validatorsIndex), validatorIDsMap);
48 }
50 convertParameterList(xml, rtn, entryIDsMap, validatorIDsMap);
51
52 int dependencyIndex = xml.findFirstChild(
54 if(dependencyIndex != -1){
55 convertDependencies(
57 xml.getChild(dependencyIndex),
60 }
61 return rtn;
62}
63
64
88
89
90void XMLParameterListReader::convertValidators(
92{
93 std::set<const XMLObject*> validatorsWithPrototypes;
94 for (int i=0; i<xml.numChildren(); ++i){
95 if (xml.getChild(i).hasAttribute(
97 {
98 validatorsWithPrototypes.insert(&xml.getChild(i));
99 }
100 else{
101 RCP<ParameterEntryValidator> insertedValidator =
103 xml.getChild(i), validatorIDsMap);
107 testForDuplicateValidatorIDs(xmlID, validatorIDsMap);
109 xmlID,
110 insertedValidator));
111 }
112 }
113
114 for (
115 std::set<const XMLObject*>::const_iterator it =
116 validatorsWithPrototypes.begin();
117 it!=validatorsWithPrototypes.end();
118 ++it)
119 {
120 RCP<ParameterEntryValidator> insertedValidator =
121 ValidatorXMLConverterDB::convertXML(*(*it), validatorIDsMap);
123 (*it)->getRequired<ParameterEntryValidator::ValidatorID>(
125 testForDuplicateValidatorIDs(xmlID, validatorIDsMap);
127 xmlID, insertedValidator));
128 }
129}
130
131
132void
133XMLParameterListReader::convertParameterList(const XMLObject& xml,
134 RCP<ParameterList> parentList,
135 EntryIDsMap& entryIDsMap, const IDtoValidatorMap& validatorIDsMap) const
136{
139 BadParameterListElementException,
140 "XMLParameterListReader expected tag " <<
142 <<", found the tag "
143 << xml.getTag());
144
145 if(xml.hasAttribute(XMLParameterListWriter::getNameAttributeName())){
146 parentList->setName(
148 }
149
150 for (int i=0; i<xml.numChildren(); i++) {
151
152 XMLObject child = xml.getChild(i);
153
156 &&
157 child.getTag() != ParameterEntry::getTagName()
158 &&
160 &&
162 BadParameterListElementException,
163 "XMLParameterListReader expected tag "
165 << ParameterEntry::getTagName() << ", but found "
166 << child.getTag() << " tag.");
167
168
169 if(
171 ||
172 child.getTag() == ParameterEntry::getTagName()
173 )
174 {
175
176 std::string name;
178 if ( child.hasAttribute(XMLParameterListWriter::getNameAttributeName()) ) {
179 name = child.getRequired(XMLParameterListWriter::getNameAttributeName());
180 }
181 else {
182 // the name needs to be unique: generate one
183 std::ostringstream ss;
184 ss << "child" << i;
185 name = ss.str();
186 }
188 _allowDuplicateSublists == false
189 &&
190 parentList->isSublist(name) == true,
191 DuplicateParameterSublist,
192 "XMLParameterListReader encountered duplicate sublist \"" << name << "\", in violation"
193 << " of the policy specified by XMLParameterListReader::setAllowsDuplicateSublists()." );
194 RCP<ParameterList> newList = sublist(parentList, name);
195 convertParameterList(child, newList, entryIDsMap, validatorIDsMap);
196 }
197 else if (child.getTag() == ParameterEntry::getTagName()) {
200 NoNameAttributeExecption,
201 "All child nodes of a ParameterList must have a name attribute!" <<
202 std::endl << std::endl);
203 name = child.getRequired(XMLParameterListWriter::getNameAttributeName());
204 parentList->setEntry(
206 if(child.hasAttribute(ValidatorXMLConverter::getIdAttributeName())){
207 IDtoValidatorMap::const_iterator result = validatorIDsMap.find(
208 child.getRequired<ParameterEntryValidator::ValidatorID>(
210 TEUCHOS_TEST_FOR_EXCEPTION(result == validatorIDsMap.end(),
211 MissingValidatorDefinitionException,
212 "Could not find validator with id: "
213 << child.getRequired(
215 << std::endl <<
216 "Bad Parameter: " << name << std::endl << std::endl);
217 parentList->getEntryRCP(name)->setValidator(result->second);
218 }
219 }
220 if(child.hasAttribute(ParameterEntryXMLConverter::getIdAttributeName())){
221 insertEntryIntoMap(child, parentList->getEntryRCP(name), entryIDsMap);
222 }
223 }
224 }
225}
226
227void XMLParameterListReader::testForDuplicateValidatorIDs(
229 const IDtoValidatorMap& currentMap) const
230{
231 TEUCHOS_TEST_FOR_EXCEPTION(currentMap.find(potentialNewID) != currentMap.end(),
232 DuplicateValidatorIDsException,
233 "Validators with duplicate ids found!" << std::endl <<
234 "Bad ID: " << potentialNewID);
235}
236
237void XMLParameterListReader::convertDependencies(
238 RCP<DependencySheet> depSheet,
239 const XMLObject& xml,
240 const EntryIDsMap& entryIDsMap,
241 const IDtoValidatorMap& validatorIDsMap) const
242{
243 if(xml.hasAttribute(DependencySheet::getNameAttributeName())){
244 depSheet->setName(
245 xml.getAttribute(DependencySheet::getNameAttributeName()));
246 }
247 for(int i = 0; i < xml.numChildren(); ++i){
248 RCP<Dependency> currentDep = DependencyXMLConverterDB::convertXML(
249 xml.getChild(i),
250 entryIDsMap,
251 validatorIDsMap);
252 depSheet->addDependency(currentDep);
253 }
254}
255
256void XMLParameterListReader::insertEntryIntoMap(
257 const XMLObject& xmlObj,
258 RCP<ParameterEntry> entryToInsert,
259 EntryIDsMap& entryIDsMap) const
260{
261 if(xmlObj.hasAttribute(ParameterEntryXMLConverter::getIdAttributeName()))
262 {
264 xmlObj.getRequired<ParameterEntry::ParameterEntryID>(
266 TEUCHOS_TEST_FOR_EXCEPTION(entryIDsMap.find(xmlID) != entryIDsMap.end(),
267 DuplicateParameterIDsException,
268 "Parameters/ParameterList with duplicate ids found!" << std::endl <<
269 "Bad ID: " << xmlID << std::endl << std::endl);
270 entryIDsMap.insert(EntryIDsMap::value_type(xmlID, entryToInsert));
271 }
272}
273
274
275} // namespace Teuchos
276
A database for DependencyXMLConverters.
A database for ValidatorXMLConverters.
Writes an XML object to a parameter list.
Writes a ParameterList to an XML object.
Thrown when the root xml tag for a parameter list is incorrect.
static const std::string & getNameAttributeName()
When serializing to XML, this string should be used as the name of the name attribute.
static RCP< Dependency > convertXML(const XMLObject &xmlObject, const XMLParameterListReader::EntryIDsMap &entryIDsMap, const IDtoValidatorMap &validatorIDsMap)
Given an XMLObject converts the XMLObject to a Dependency.
Maps Validators to integers.
ValidatorMap::const_iterator const_iterator
std::pair< ParameterEntryValidator::ValidatorID, RCP< ParameterEntryValidator > > IDValidatorPair
void insert(IDValidatorPair toInsert)
inserts an IDValidatorPair into the map.
static ParameterEntry convertXML(const XMLObject &xmlObj)
Converts XML to a ParameterEntry.
static const std::string & getTagName()
Get the string that should be used as the tag name for all parameters when they are serialized to xml...
A list of parameters of arbitrary type.
Smart reference counting pointer class for automatic garbage collection.
static RCP< ParameterEntryValidator > convertXML(const XMLObject &xmlObject, const IDtoValidatorMap &validatorIDsMap)
Given an XMLObject converts the XMLObject to a ParameterEntryValidator and inserts the validator into...
static const std::string & getIdAttributeName()
static const std::string & getPrototypeIdAttributeName()
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object,...
const XMLObject & getChild(int i) const
Return the i-th child node.
bool getAllowsDuplicateSublists() const
Specifies the current policy regarding duplicated sublists. See setAllowsDuplicateSublists() for more...
void setAllowsDuplicateSublists(bool policy)
Set policy regarding duplicated sublists.
RCP< ParameterList > toParameterList(const XMLObject &xml, RCP< DependencySheet > depSheet) const
std::map< ParameterEntry::ParameterEntryID, RCP< ParameterEntry > > EntryIDsMap
Convenience typedef.
static const std::string & getDependenciesTagName()
static const std::string & getValidatorsTagName()
static const std::string & getParameterListTagName()
static const std::string & getNameAttributeName()
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.