diff --git a/Code/Wrappers/CommandLine/otbApplicationLauncherCommandLine.cxx b/Code/Wrappers/CommandLine/otbApplicationLauncherCommandLine.cxx index 35f7b0eed68eb944f4b69dfdca168861d86c4399..047ffa77447f15accee33e6efd2aff517b0cd8cc 100644 --- a/Code/Wrappers/CommandLine/otbApplicationLauncherCommandLine.cxx +++ b/Code/Wrappers/CommandLine/otbApplicationLauncherCommandLine.cxx @@ -22,6 +22,8 @@ const std::string GetChildNodeTextOf(TiXmlElement *parentElement, std::string key); std::string PrepareExpressionFromXML(std::string filename); +std::vector<std::string> PrepareVectorExpressionFromXML(std::string filename); +std::string CleanWord(const std::string & word); std::string PrepareExpressionFromXML(std::string filename) @@ -146,6 +148,106 @@ std::string PrepareExpressionFromXML(std::string filename) return expression; } +std::vector<std::string> PrepareVectorExpressionFromXML(std::string filename) +{ + std::vector<std::string> expression; + + if(filename.empty()) + { + std::cerr <<"Input XML Filename is empty" << std::endl; + return expression; + } + std::string ext = filename.substr(filename.size()-4,filename.size()); + if(ext != ".xml" ) + std::cerr << ext << " is a wrong extension: Expected .xml " << __FILE__ << std::endl; + + // Open the xml file + TiXmlDocument doc; + + //Use itksys::SystemTools::FOpen() and close it below because + //TiXmlDocument::TiXmlFileOpen( ) is not exposed from tinyXML library. Even + //though its available in the TiXmlDocument::SaveFile(). + FILE* fp = itksys::SystemTools::Fopen(filename.c_str(), "rb"); + + if (!doc.LoadFile(fp , TIXML_ENCODING_UTF8)) + { + std::cerr << "Can't open file " << filename << std::endl; + fclose(fp); + exit(1); + + } + + TiXmlHandle handle(&doc); + + TiXmlElement *n_OTB; + n_OTB = handle.FirstChild("OTB").Element(); + + if(!n_OTB) + { + std::string info = "Input XML file " + filename + " is invalid."; + std::cerr << info << std::endl; + fclose(fp); + exit(1); + } + + TiXmlElement *n_AppNode = n_OTB->FirstChildElement("application"); + + std::string moduleName; + moduleName = GetChildNodeTextOf(n_AppNode, "name"); + + expression.push_back(CleanWord(moduleName)); + + for( TiXmlElement* n_Parameter = n_AppNode->FirstChildElement("parameter"); n_Parameter != NULL; + n_Parameter = n_Parameter->NextSiblingElement() ) + { + std::string key="-"; + key.append(GetChildNodeTextOf(n_Parameter, "key")); + expression.push_back(CleanWord(key)); + + TiXmlElement* n_Values = NULL; + n_Values = n_Parameter->FirstChildElement("values"); + if(n_Values) + { + std::string values; + for(TiXmlElement* n_Value = n_Values->FirstChildElement("value"); n_Value != NULL; + n_Value = n_Value->NextSiblingElement()) + { + expression.push_back(CleanWord(n_Value->GetText())); + } + } + else + { + std::string value; + value = GetChildNodeTextOf(n_Parameter, "value"); + expression.push_back(CleanWord(value)); + + std::string type = GetChildNodeTextOf(n_Parameter, "type"); + if (type == "OutputImage") + { + std::string type = GetChildNodeTextOf(n_Parameter, "pixtype"); + expression.push_back(CleanWord(type)); + } + } + } + + fclose(fp); + + return expression; +} + +std::string CleanWord(const std::string & word) +{ + std::string res(""); + // Suppress whitespace characters at the beginning and ending of the string + std::string::size_type cleanStart = word.find_first_not_of(" \t"); + std::string::size_type cleanEnd = word.find_last_not_of(" \t\f\v\n\r"); + if (cleanEnd != std::string::npos) + { + res = word.substr(cleanStart,cleanEnd+1); + } + return res; +} + int main(int argc, char* argv[]) { if (argc < 2) @@ -159,7 +261,8 @@ int main(int argc, char* argv[]) std::string exp; if (strcmp(argv[1], "-inxml") == 0) { - exp = PrepareExpressionFromXML(argv[2]); + //exp = PrepareExpressionFromXML(argv[2]); + vexp = PrepareVectorExpressionFromXML(argv[2]); } else { @@ -175,9 +278,14 @@ int main(int argc, char* argv[]) { exp.append(argv[i]); }*/ - std::string strarg (argv[i]); - std::cout << strarg << std::endl; - vexp.push_back(strarg); + std::string strarg(argv[i]); + std::string cleanArg = CleanWord(strarg); + if (cleanArg.empty()) + { + // Empty argument ! + continue; + } + vexp.push_back(cleanArg); } } // std::cerr << exp << ":\n"; diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx index cc85fb6e58ff786460f50bd58b85b0dfa4199fe3..ecac446dd8b134d43f04fa6f6f1e3ba7b2816c17 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx @@ -99,7 +99,7 @@ bool CommandLineLauncher::Load(const std::string & exp) return false; } -bool CommandLineLauncher::Load(std::vector<std::string> vexp) +bool CommandLineLauncher::Load(const std::vector<std::string> &vexp) { m_VExpression = vexp; return this->Load(); @@ -907,18 +907,13 @@ bool CommandLineLauncher::CheckKeyValidity(std::string& refKey) appKeyList.push_back("testenv"); appKeyList.push_back("version"); - std::cout<< "toto" << std::endl; - // Check if each key in the expression exists in the application for (unsigned int i = 0; i < expKeyList.size(); i++) { - //remove first character - std::cout<< "expKey= " << expKeyList[i] << std::endl; - refKey = expKeyList[i].substr(1,expKeyList[i].length()-1); + refKey = expKeyList[i]; bool keyExist = false; for (unsigned int j = 0; j < appKeyList.size(); j++) { - std::cout<< "appKey= " << appKeyList[j] << std::endl; if (refKey == appKeyList[j]) { keyExist = true; diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h index 4ce5917b27fa392ffb39f90b480520a7b8ba9bc6..b0ce68a93b5e1bf4f9a30ad20c22c5df21874956 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.h @@ -88,7 +88,7 @@ public: bool Load( const std::string & exp ); /** same as Load method but set the expression before. */ - bool Load( std::vector<std::string> vexp); + bool Load(const std::vector<std::string> &vexp); /** Launch the process, using the Execute application method * The method will check if the user asked for help (looking at diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx index df4c449446f6769a1c6f3ad0b7f894005bae6628..f6313e76ec59284fd01d1f3a298365de35559e37 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx @@ -40,20 +40,23 @@ CommandLineParser::~CommandLineParser() } std::string -CommandLineParser::GetPathsAsString(std::vector<std::string> & vexp ) +CommandLineParser::GetPathsAsString(const std::vector<std::string> & vexp ) { std::string res; - // The first element must be the module name, non " -" allowed. - // The module path list element are the strings between the first - // element and the first key (ie. " -" string). - std::vector<std::string>::iterator it = vexp.begin(); - ++it; // first element is module name - - while (it->find("-") != std::string::npos) - { - ++it; - } + std::vector<std::string> pathList; + if (this->GetPaths(pathList,vexp) == OK) + { + std::vector<std::string>::iterator it; + for (it=pathList.begin() ; it!=pathList.end() ; ++it) + { + if (!res.empty()) + { + res.append(" "); + } + res.append(*it); + } + } return res; } @@ -87,30 +90,40 @@ CommandLineParser::GetPathsAsString( const std::string & exp ) } CommandLineParser::ParseResultType -CommandLineParser::GetPaths( std::vector<std::string> & paths, std::vector<std::string> & exp ) +CommandLineParser::GetPaths( std::vector<std::string> & paths, const std::vector<std::string> & exp ) { // The first element must be the module name, non " -" allowed. // The module path list elements are the strings between the first // element and the first key (ie. string which begins with "-"). - std::vector<std::string>::iterator it = exp.begin(); + std::vector<std::string>::const_iterator it = exp.begin(); ++it; // first element is module name - std::vector<itksys::String> pathlist; - - while (it->find("-") == std::string::npos) - { - std::string fullPath = itksys::SystemTools::CollapseFullPath((*it).c_str()); + std::string tmpPath; - if( !itksys::SystemTools::FileIsDirectory(fullPath.c_str()) ) + while (it != exp.end()) { + tmpPath = *it; + // If this is the first key : exit loop + if (tmpPath[0]=='-') + { + break; + } + // Add path to result + std::string fullPath = itksys::SystemTools::CollapseFullPath(tmpPath.c_str()); + if( !itksys::SystemTools::FileIsDirectory(fullPath.c_str()) ) + { std::cerr<<"Invalid module path: "<<fullPath<<std::endl; return INVALIDMODULEPATH; - } + } paths.push_back(fullPath); - ++it; - } - + } + + if (paths.empty()) + { + return NOMODULEPATH; + } + return OK; } @@ -165,12 +178,12 @@ CommandLineParser::GetPaths( std::vector<std::string> & paths, const std::string } CommandLineParser::ParseResultType -CommandLineParser::GetModuleName( std::string & modName, std::vector<std::string> & exp ) +CommandLineParser::GetModuleName( std::string & modName, const std::vector<std::string> & exp ) { itksys::RegularExpression reg; reg.compile("([^0-9a-zA-Z])"); // The first element must be the module path, non " -" allowed. - if( exp[0].find(" -") != std::string::npos) + if( exp[0][0] == '-') { return NOMODULENAME; } @@ -232,43 +245,43 @@ CommandLineParser::GetModuleName( std::string & modName, const std::string & exp } std::vector<std::string> -CommandLineParser::GetAttribut( const std::string & key, std::vector<std::string> & exp ) +CommandLineParser::GetAttribut( const std::string & key, const std::vector<std::string> & exp ) { - std::cout << "here1" << std::endl; - std::vector<std::string> res; - if (!this->IsAttributExists(key,exp)) - return res; - - std::cout << "here2: " << exp.size()<< std::endl; - - bool foundKey=false; - std::vector<std::string>::iterator it = exp.begin(); - while(it!=exp.end() ) - { - std::cout << key << "//" << *it<< std::endl; + std::vector<std::string> res; + if (!this->IsAttributExists(key,exp)) + return res; - if (foundKey) + bool foundKey=false; + std::vector<std::string>::const_iterator it = exp.begin(); + while(it!=exp.end() ) { - if (it->find("-") == 0) + if (foundKey) { - std::cout << res.size() << std::endl; - return res; - } + if (it->find("-") == 0) + { + std::string tmpKey = it->substr(1,std::string::npos); + if (this->IsAValidKey(tmpKey)) + { + break; + } + else + { + res.push_back(*it); + } + } else res.push_back(*it); + } + else + { + if (it->compare(key) == 0) + { + foundKey=true; + } + } + ++it; } - - if (it->find(key) != std::string::npos) - { foundKey=true; - std::cout << "foundkey true" << std::endl; - } - else - { - std::cout << "foundkey false" << std::endl; - } - ++it; - } - return res; + return res; } std::vector<std::string> @@ -362,7 +375,7 @@ CommandLineParser::GetAttribut( const std::string & key, const std::string & exp } std::string -CommandLineParser::GetAttributAsString( const std::string & key, std::vector<std::string> & exp ) +CommandLineParser::GetAttributAsString( const std::string & key, const std::vector<std::string> & exp ) { std::string res(""); std::vector<std::string> values = this->GetAttribut( key, exp ); @@ -435,29 +448,34 @@ CommandLineParser::IsAttributExists( const std::string key, const std::string & } bool -CommandLineParser::IsAttributExists( const std::string key, std::vector<std::string> & exp ) +CommandLineParser::IsAttributExists( const std::string key, const std::vector<std::string> & exp ) { - for (std::vector<std::string>::iterator it = exp.begin(); it != exp.end(); ++it) - { - if (it->find(key) != std::string::npos) + for (std::vector<std::string>::const_iterator it = exp.begin() ; it != exp.end(); ++it) + { + if (it->compare(key) == 0) return true; - } + } return false; } std::vector<std::string> -CommandLineParser::GetKeyList( std::vector<std::string> & exp ) +CommandLineParser::GetKeyList( const std::vector<std::string> & exp ) { - std::vector<std::string> keyList; - for (std::vector<std::string>::iterator it = exp.begin(); it != exp.end(); ++it) + std::vector<std::string> keyList; + for (std::vector<std::string>::const_iterator it = exp.begin() ; it != exp.end(); ++it) { - if (it->find("-") == 0 && this->IsAValidKey(*it)) + if (it->find("-") == 0) { - keyList.push_back(*it); + // Remove first character ('-') + std::string key = it->substr(1,std::string::npos); + if (this->IsAValidKey(key)) + { + keyList.push_back(key); + } } } - return keyList; + return keyList; } std::vector<std::string> @@ -498,13 +516,19 @@ CommandLineParser::IsAValidKey( const std::string & foundKey ) { bool res = false; std::string tmp = foundKey; - if( tmp.find(".") != std::string::npos ) - tmp.erase(tmp.find("."), tmp.find(".")); - - // To be a key, the string can't contain a number + // make sure the tested key ends with a dot. + // the starting dash should be already removed + tmp.append("."); + + // To be a key, the string must be a serie of groups separated by dots so that : + // - each group begins with an lower case letter + // - each group contains only alphanumeric characters (and lowercase) + // This also implies that each group is not empty and that the key doesn't + // start with a dot. These conditions shouldn't match any number even in + // scientific notation. + // The following regular expression says just that itksys::RegularExpression reg; - reg.compile("([^0-9])"); - + reg.compile("^([a-z][a-z0-9]*\\.)+$"); if( reg.find(tmp) ) { res = true; diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.h b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.h index 6f747680ccd66b85487f68e11a7c002cc34e0ead..83cca736364bad782204257bd2dcaacfcc43b6cf 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.h +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.h @@ -57,30 +57,30 @@ public: /** Get the attribut associatd to a key (list of element after the key and before the next "--") as a vector of string. */ std::vector<std::string> GetAttribut( const std::string & key, const std::string & exp ); - std::vector<std::string> GetAttribut( const std::string & key, std::vector<std::string> & exp ); + std::vector<std::string> GetAttribut( const std::string & key, const std::vector<std::string> & exp ); /** Get the attribut associatd to a key (list of element after the key and before the next "--") as a string separated by spaces. */ std::string GetAttributAsString( const std::string & key, const std::string & exp ); -std::string GetAttributAsString( const std::string & key, std::vector<std::string> & exp ); +std::string GetAttributAsString( const std::string & key, const std::vector<std::string> & exp ); /** Look if a given key is in an expression. */ bool IsAttributExists(const std::string key, const std::string & exp ); -bool IsAttributExists(const std::string key, std::vector<std::string> & exp); +bool IsAttributExists(const std::string key, const std::vector<std::string> & exp); /** Get the paths executables in an expression. Store the list in a vector of string. */ CommandLineParser::ParseResultType GetPaths( std::vector<std::string> & paths, const std::string & exp ); - CommandLineParser::ParseResultType GetPaths( std::vector<std::string> & paths, std::vector<std::string> & exp ); + CommandLineParser::ParseResultType GetPaths( std::vector<std::string> & paths, const std::vector<std::string> & exp ); /** Get the paths executables in an expression. Store the list in a vector of string. */ std::string GetPathsAsString( const std::string & exp ); - std::string GetPathsAsString( std::vector<std::string> & vexp ); + std::string GetPathsAsString( const std::vector<std::string> & vexp ); /** Get the module name in an expression. It can be the first element of the expression (if the expression doesn't start with a "--" or the attribut associated to the key m_ModuleNameKey). */ CommandLineParser::ParseResultType GetModuleName( std::string & modName, const std::string & exp ); -CommandLineParser::ParseResultType GetModuleName( std::string & modName, std::vector<std::string> & exp ); +CommandLineParser::ParseResultType GetModuleName( std::string & modName, const std::vector<std::string> & exp ); /** Get the list of keys in an expression. That is to say each word starting by "--". */ std::vector<std::string> GetKeyList( const std::string & exp ); -std::vector<std::string> GetKeyList( std::vector<std::string> & exp); + std::vector<std::string> GetKeyList( const std::vector<std::string> & exp); protected: /** Constructor */