1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/*
* Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otbXMLMetadataSupplier_h
#define otbXMLMetadataSupplier_h
#include "cpl_minixml.h"
#include "cpl_string.h"
#include "OTBMetadataExport.h"
#include "otbMetadataSupplierInterface.h"
#include "otbStringUtilities.h"
namespace otb
{
/** \class XMLMetadataSupplier
*
* \brief Class to access metadata information in a XML file
*
* \ingroup OTBMetadata
*/
class OTBMetadata_EXPORT XMLMetadataSupplier
: public MetadataSupplierInterface
{
public:
XMLMetadataSupplier(const std::string &);
XMLMetadataSupplier(const XMLMetadataSupplier &) = delete;
XMLMetadataSupplier& operator=(const XMLMetadataSupplier&) = delete;
/**
* @brief Get the metadata value corresponding to a given path
*
* @param path The path to look for
* @param hasValue True if path is found
* @param band not used
* @return The value corresponding to path. Empty string if not found.
*/
std::string GetMetadataValue(std::string const& path, bool& hasValue, int band=1) const override;
/**
* @brief Get the first metadata value corresponding to a given path
*
* @param path The path to look for
* @param hasValue True if path is found
* @return The value corresponding to path. Empty string if not found.
*/
std::string GetFirstMetadataValue(std::string const& paths, bool& hasValue) const;
/**
* @brief Get the metadata value corresponding to a given path
* converted to the given type
*
* This method can look for a value in a list, using the _# jocker. For example,
* looking for "value" in a dictionary like this :
* foo_1.bar=42
* foo_1.doo=99
* foo_2.value=8
* One can specify this path : foo_#.value, the method will then return 8.
*
* @param path The path to look for.
* @return The value corresponding to path.
* @raises otb::Error if path not found
*/
template <typename T> T GetFirstAs(std::string const& 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& s="") const override;
int GetNbBands() const override;
/** Get the number of keys starting with path */
unsigned int GetNumberOf(std::string const& path) const override;
/** If multiple keys have the same path, gives the position of the one with value value*/
unsigned int GetAttributId(std::string const& path, std::string const& value) const override;
/**
* @brief Writes the content of the XML file into a string
*
* @return A std::string
*/
std::string PrintSelf() const;
~XMLMetadataSupplier() = default;
protected:
/**
* @brief ReadXMLToList Transform xml to list of NULL terminated name=value
* strings
* @param psNode A xml node to process
* @param papszList A list to fill with name=value strings
* @param pszName A name of parent node. For root xml node should be empty.
* If name is not empty, the sibling nodes will not proceed
* @return An input list filled with values
*
* This method originates from a work by GDAL in the class GDALMDReaderBase.
*/
virtual char** ReadXMLToList(CPLXMLNode* psNode, char** papszList,
const char* pszName = "");
/**
* @brief In a StringList of “Name=Value” pairs, look for the values
* associated with a name containing the specified string
*
* @param papszStrList A StringList that will be searched
* @param pszName A string that will be looked for in the keys
* @return A StringList containing only the pairs from papszStrList whose key
* contain pszName
*/
std::vector<std::string> FetchPartialNameValueMultiple(char** papszStrList, const char *pszName) const;
/**
* @brief In a StringList of “Name=Value” pairs, look for the values
* associated with a name containing the specified string
*
* @param StringVector A std::vector of std::string that will be searched
* @param Name A std::string that will be looked for in the keys
* @return A std::vector of std::string containing only the pairs from StringVector whose key
* contain Name
*/
std::vector<std::string> FetchPartialNameValueMultiple(const std::vector<std::string> &StringVector,
const std::string &Name) const;
/**
* @brief In a StringList of “Name=Value” pairs, look for the keys equal to the specified string
* @param papszStrList A StringList that will be searched
* @param pszName A string that will be looked for at the beginning of the keys
* @return A StringList containing only the pairs from papszStrList whose key
* start with pszName
*/
std::vector<std::string> GetAllStartWith(char** papszStrList, const char *pszName) const;
private:
/** List of resource files */
std::string m_FileName;
/** Dictionary containing the metadata */
char** m_MetadataDic = nullptr;
};
} // end namespace otb
#endif
|