OTB  9.0.0
Orfeo Toolbox
otbXMLMetadataSupplier.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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 &);
46  XMLMetadataSupplier(const XMLMetadataSupplier &) = delete;
47  XMLMetadataSupplier& operator=(const XMLMetadataSupplier&) = delete;
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 GetAttributId(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
otb::XMLMetadataSupplier::GetFirstAs
T GetFirstAs(std::string const &path) const
Get the metadata value corresponding to a given path converted to the given type.
Definition: otbXMLMetadataSupplier.h:83
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbStringUtilities.h
otb::MetadataSupplierInterface
Base class to access metadata information in files/images.
Definition: otbMetadataSupplierInterface.h:40
otbGenericExceptionMacro
#define otbGenericExceptionMacro(T, x)
Definition: otbMacro.h:144
otbMetadataSupplierInterface.h
otb::MissingMetadataException
Exception to be used when metadata parsing fails.
Definition: otbMissingMetadataException.h:36
otb::XMLMetadataSupplier
Class to access metadata information in a XML file.
Definition: otbXMLMetadataSupplier.h:41
otb::XMLMetadataSupplier::m_FileName
std::string m_FileName
Definition: otbXMLMetadataSupplier.h:169