Skip to content
Snippets Groups Projects
Commit 7ad68ea5 authored by Julien Osman's avatar Julien Osman
Browse files

ENH: Add methods to access lists in XMLMetadataSupplier

parent 6ed37fe8
No related branches found
No related tags found
No related merge requests found
......@@ -49,6 +49,26 @@ public:
* If band >= 0, the metadata value is looked in the specified band*/
const std::string GetMetadataValue(const std::string path, bool& hasValue, int band=1) const override;
const std::string GetFirstMetadataValue(const std::string paths, bool& hasValue) const;
template <typename T> T GetFirstAs(std::string path) const
{
bool hasValue;
std::string ret = GetFirstMetadataValue(path, hasValue);
if (!hasValue)
{
otbGenericExceptionMacro(MissingMetadataException,<<"Missing metadata '"<<path<<"'")
}
try
{
return boost::lexical_cast<T>(ret);
}
catch (boost::bad_lexical_cast&)
{
otbGenericExceptionMacro(MissingMetadataException,<<"Bad metadata value for '"<<path<<"', got: "<<ret)
}
}
std::string GetResourceFile(std::string="") const override;
int GetNbBands() const override;
......@@ -70,6 +90,8 @@ protected:
virtual char** ReadXMLToList(CPLXMLNode* psNode, char** papszList,
const char* pszName = "");
char **CSLFetchPartialNameValueMultiple(CSLConstList papszStrList, const char *pszName) const;
private:
/** List of resource files */
std::string m_FileName;
......
......@@ -54,6 +54,34 @@ const std::string XMLMetadataSupplier::GetMetadataValue(const std::string path,
return std::string(ret);
}
const std::string XMLMetadataSupplier::GetFirstMetadataValue(const std::string path, bool& hasValue) const
{
std::size_t found = path.find("_#");
char ** values = this->CSLFetchPartialNameValueMultiple(m_MetadataDic, path.substr(0, found).c_str());
std::size_t start = found + 2;
if (found != std::string::npos)
found = path.find("_#", found);
while(found != std::string::npos)
{
values = this->CSLFetchPartialNameValueMultiple(m_MetadataDic, path.substr(start, found).c_str());
start = found;
found = path.find("_#", found + 2);
}
if (values[0] != nullptr)
{
hasValue = true;
std::string ret = std::string(values[0]);
return ret.substr(ret.find('=') + 1);
}
else
{
hasValue = false;
return "";
}
}
std::string XMLMetadataSupplier::GetResourceFile(std::string) const
{
return m_FileName;
......@@ -165,6 +193,18 @@ char** XMLMetadataSupplier::ReadXMLToList(CPLXMLNode* psNode, char** papszList,
return papszList;
}
char ** XMLMetadataSupplier::CSLFetchPartialNameValueMultiple(CSLConstList papszStrList, const char *pszName) const
{
if( papszStrList == nullptr || pszName == nullptr )
return nullptr;
char **papszValues = nullptr;
for( ; *papszStrList != nullptr ; ++papszStrList )
if( strstr(*papszStrList, pszName) )
papszValues = CSLAddString(papszValues, *papszStrList);
return papszValues;
}
int XMLMetadataSupplier::GetNbBands() const
{
return 0;
......
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