OTB  10.0.0
Orfeo Toolbox
otbXMLMetadataSupplier.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbXMLMetadataSupplier_h
22 #define otbXMLMetadataSupplier_h
23 
24 #include "cpl_minixml.h"
25 #include "cpl_string.h"
26 
27 #include "OTBMetadataExport.h"
29 #include "otbStringUtilities.h"
30 
31 
32 namespace otb
33 {
34 
41 class OTBMetadata_EXPORT XMLMetadataSupplier
43 {
44 public:
45  XMLMetadataSupplier(const std::string &);
48 
57  std::string GetMetadataValue(std::string const& path, bool& hasValue, int band=1) const override;
58 
66  std::string GetFirstMetadataValue(std::string const& paths, bool& hasValue) const;
67 
83  template <typename T> T GetFirstAs(std::string const& path) const
84  {
85  bool hasValue;
86  std::string ret = GetFirstMetadataValue(path, hasValue);
87  if (!hasValue)
88  {
89  otbGenericExceptionMacro(MissingMetadataException,<<"Missing metadata '"<<path<<"'")
90  }
91  try
92  {
93  return boost::lexical_cast<T>(ret);
94  }
95  catch (boost::bad_lexical_cast&)
96  {
97  otbGenericExceptionMacro(MissingMetadataException,<<"Bad metadata value for '"<<path<<"', got: "<<ret)
98  }
99  }
101 
102  std::string GetResourceFile(std::string const& s="") const override;
103 
104  int GetNbBands() const override;
105 
107  unsigned int GetNumberOf(std::string const& path) const override;
108 
110  unsigned int GetAttributeId(std::string const& path, std::string const& value) const override;
111 
117  std::string PrintSelf() const;
118 
119  ~XMLMetadataSupplier() = default;
120 protected:
132  virtual char** ReadXMLToList(CPLXMLNode* psNode, char** papszList,
133  const char* pszName = "");
134 
144  std::vector<std::string> FetchPartialNameValueMultiple(char** papszStrList, const char *pszName) const;
145 
155  std::vector<std::string> FetchPartialNameValueMultiple(const std::vector<std::string> &StringVector,
156  const std::string &Name) const;
157 
165  std::vector<std::string> GetAllStartWith(char** papszStrList, const char *pszName) const;
166 
167 private:
169  std::string m_FileName;
170 
172  char** m_MetadataDic = nullptr;
173 };
174 
175 } // end namespace otb
176 
177 #endif
Base class to access metadata information in files/images.
Exception to be used when metadata parsing fails.
Class to access metadata information in a XML file.
unsigned int GetNumberOf(std::string const &path) const override
XMLMetadataSupplier(const XMLMetadataSupplier &)=delete
std::string GetMetadataValue(std::string const &path, bool &hasValue, int band=1) const override
Get the metadata value corresponding to a given path.
XMLMetadataSupplier & operator=(const XMLMetadataSupplier &)=delete
virtual char ** ReadXMLToList(CPLXMLNode *psNode, char **papszList, const char *pszName="")
ReadXMLToList Transform xml to list of NULL terminated name=value strings.
std::vector< std::string > FetchPartialNameValueMultiple(char **papszStrList, const char *pszName) const
In a StringList of “Name=Value” pairs, look for the values associated with a name containing the spec...
int GetNbBands() const override
T GetFirstAs(std::string const &path) const
Get the metadata value corresponding to a given path converted to the given type.
std::vector< std::string > FetchPartialNameValueMultiple(const std::vector< std::string > &StringVector, const std::string &Name) const
In a StringList of “Name=Value” pairs, look for the values associated with a name containing the spec...
std::string GetFirstMetadataValue(std::string const &paths, bool &hasValue) const
Get the first metadata value corresponding to a given path.
std::string GetResourceFile(std::string const &s="") const override
std::vector< std::string > GetAllStartWith(char **papszStrList, const char *pszName) const
In a StringList of “Name=Value” pairs, look for the keys equal to the specified string.
XMLMetadataSupplier(const std::string &)
unsigned int GetAttributeId(std::string const &path, std::string const &value) const override
std::string PrintSelf() const
Writes the content of the XML file into a string.
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
#define otbGenericExceptionMacro(T, x)
Definition: otbMacro.h:196