Skip to content
Snippets Groups Projects
Commit 614d7c27 authored by Martin Killenberg's avatar Martin Killenberg
Browse files

factored out the actual importing into a function with a plain C++ interface...

factored out the actual importing into a function with a plain C++ interface so it can be used without XML file
parent e60d7a70
No related branches found
No related tags found
No related merge requests found
......@@ -68,9 +68,11 @@ namespace ChimeraTK{
std::set< std::string > _inputVariables;
void processLocation(xmlpp::Node const * locationNode);
void processProperty(xmlpp::Node const * propertyNode, std::string locationName);
void processImport(xmlpp::Node const * importNode, std::string importLocationName=std::string());
void processLocationNode(xmlpp::Node const * locationNode);
void processPropertyNode(xmlpp::Node const * propertyNode, std::string locationName);
void processImportNode(xmlpp::Node const * importNode, std::string importLocationName=std::string());
void import(std::string importSource, std::string importLocationName);
std::map<std::string, PropertyAttributes> _locationDefaults;
PropertyAttributes _globalDefaults;
......
......@@ -19,7 +19,7 @@ namespace ChimeraTK{
return false;
}
void VariableMapper::processLocation(xmlpp::Node const * locationNode){
void VariableMapper::processLocationNode(xmlpp::Node const * locationNode){
const xmlpp::Element* location = dynamic_cast<const xmlpp::Element*>(locationNode);
std::string locationName = location->get_attribute("name")->get_value();
......@@ -27,16 +27,16 @@ namespace ChimeraTK{
if (nodeIsWhitespace(node)) continue;
if (node->get_name() == "property"){
processProperty(node, locationName);
processPropertyNode(node, locationName);
}else if (node->get_name() == "import"){
processImport(node, locationName);
processImportNode(node, locationName);
}else{
throw std::invalid_argument(std::string("Error parsing xml file in location ") + locationName + ": Unknown node '"+node->get_name()+"'");
}
}
}
void VariableMapper::processProperty(xmlpp::Node const * propertyNode, std::string locationName){
void VariableMapper::processPropertyNode(xmlpp::Node const * propertyNode, std::string locationName){
const xmlpp::Element* property = dynamic_cast<const xmlpp::Element*>(propertyNode);
std::string source = property->get_attribute("source")->get_value();
......@@ -72,43 +72,47 @@ namespace ChimeraTK{
}
void VariableMapper::processImport(xmlpp::Node const * importNode, std::string importLocationName){
void VariableMapper::processImportNode(xmlpp::Node const * importNode, std::string importLocationName){
for (auto const & node : importNode->get_children()){
const xmlpp::TextNode* nodeAsText = dynamic_cast<const xmlpp::TextNode*>(node);
std::string importSource = nodeAsText->get_content();
// a slash will be added after the source, so we make the source empty for an import of everything
if (importSource == "/"){
importSource = "";
}
import(importSource, importLocationName);
}
}
void VariableMapper::import(std::string importSource, std::string importLocationName){
// a slash will be added after the source, so we make the source empty for an import of everything
if (importSource == "/"){
importSource = "";
}
// loop source tree, cut beginning, replace / with _ and add a property
for (auto const & processVariable : _inputVariables){
if (_inputSortedDescriptions.find(processVariable) != _inputSortedDescriptions.end()){
continue;
}
// loop source tree, cut beginning, replace / with _ and add a property
for (auto const & processVariable : _inputVariables){
if (_inputSortedDescriptions.find(processVariable) != _inputSortedDescriptions.end()){
continue;
}
if ( processVariable.find( importSource+"/") == 0 ){
// processVariable starts with wanted source
auto nameSource = processVariable.substr( importSource.size() + 1); // add the slash to be removed
std::string propertyName;
std::string locationName;
if (importLocationName.empty()){
// a global import, try to get the location name from the source
auto locationAndPropertyName = splitStringAtFirstSlash(nameSource);
locationName = locationAndPropertyName.first;
propertyName = locationAndPropertyName.second;
if (locationName.empty() ){
throw std::logic_error(std::string("Invalid XML content in global import of ") + importSource + ": Cannot create location name from '" + nameSource + "', one hirarchy level is missing.");
}
}else{
// import into a location, we know the location name. Just replace / with .
propertyName = std::regex_replace(nameSource, std::regex("/"), ".");
locationName = importLocationName;
if ( processVariable.find( importSource+"/") == 0 ){
// processVariable starts with wanted source
auto nameSource = processVariable.substr( importSource.size() + 1); // add the slash to be removed
std::string propertyName;
std::string locationName;
if (importLocationName.empty()){
// a global import, try to get the location name from the source
auto locationAndPropertyName = splitStringAtFirstSlash(nameSource);
locationName = locationAndPropertyName.first;
propertyName = locationAndPropertyName.second;
if (locationName.empty() ){
throw std::logic_error(std::string("Invalid XML content in global import of ") + importSource + ": Cannot create location name from '" + nameSource + "', one hirarchy level is missing.");
}
_inputSortedDescriptions[processVariable] = PropertyDescription(locationName, propertyName);
}else{
// import into a location, we know the location name. Just replace / with .
propertyName = std::regex_replace(nameSource, std::regex("/"), ".");
locationName = importLocationName;
}
_inputSortedDescriptions[processVariable] = PropertyDescription(locationName, propertyName);
}
}
}
......@@ -133,9 +137,9 @@ namespace ChimeraTK{
if (nodeIsWhitespace(mainNode)) continue;
if (mainNode->get_name() == "location"){
processLocation(mainNode);
processLocationNode(mainNode);
}else if (mainNode->get_name() == "import"){
processImport(mainNode);
processImportNode(mainNode);
}else{
throw std::invalid_argument(std::string("Error parsing xml file ") + xmlFile + ": Unknown node '"+mainNode->get_name()+"'");
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment