Coverage Report

Created: 2025-06-13 06:18

/src/gdal/gcore/gdalmultidomainmetadata.cpp
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Project:  GDAL Core
4
 * Purpose:  Implementation of GDALMultiDomainMetadata class.  This class
5
 *           manages metadata items for a variable list of domains.
6
 * Author:   Frank Warmerdam, warmerdam@pobox.com
7
 *
8
 ******************************************************************************
9
 * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
10
 * Copyright (c) 2009-2011, Even Rouault <even dot rouault at spatialys.com>
11
 *
12
 * SPDX-License-Identifier: MIT
13
 ****************************************************************************/
14
15
#include "cpl_port.h"
16
#include "gdal_priv.h"
17
18
#include <cstring>
19
20
#include "cpl_conv.h"
21
#include "cpl_error.h"
22
#include "cpl_minixml.h"
23
#include "cpl_string.h"
24
#include "gdal_pam.h"
25
26
//! @cond Doxygen_Suppress
27
/************************************************************************/
28
/*                      GDALMultiDomainMetadata()                       */
29
/************************************************************************/
30
31
0
GDALMultiDomainMetadata::GDALMultiDomainMetadata() = default;
32
33
/************************************************************************/
34
/*                      ~GDALMultiDomainMetadata()                      */
35
/************************************************************************/
36
37
0
GDALMultiDomainMetadata::~GDALMultiDomainMetadata() = default;
38
39
/************************************************************************/
40
/*                               Clear()                                */
41
/************************************************************************/
42
43
void GDALMultiDomainMetadata::Clear()
44
45
0
{
46
0
    aosDomainList.clear();
47
0
    oMetadata.clear();
48
0
}
49
50
/************************************************************************/
51
/*                           SanitizeDomain()                           */
52
/************************************************************************/
53
54
static inline const char *SanitizeDomain(const char *pszDomain)
55
0
{
56
0
    return pszDomain ? pszDomain : "";
57
0
}
58
59
/************************************************************************/
60
/*                            GetMetadata()                             */
61
/************************************************************************/
62
63
char **GDALMultiDomainMetadata::GetMetadata(const char *pszDomain)
64
65
0
{
66
0
    const auto oIter = oMetadata.find(SanitizeDomain(pszDomain));
67
0
    if (oIter == oMetadata.end())
68
0
        return nullptr;
69
0
    return oIter->second.List();
70
0
}
71
72
/************************************************************************/
73
/*                            SetMetadata()                             */
74
/************************************************************************/
75
76
CPLErr GDALMultiDomainMetadata::SetMetadata(CSLConstList papszMetadata,
77
                                            const char *pszDomain)
78
79
0
{
80
0
    pszDomain = SanitizeDomain(pszDomain);
81
82
0
    auto oIter = oMetadata.find(pszDomain);
83
0
    if (oIter == oMetadata.end())
84
0
    {
85
0
        aosDomainList.AddString(pszDomain);
86
0
        oIter =
87
0
            oMetadata.insert(std::pair(aosDomainList.back(), CPLStringList()))
88
0
                .first;
89
0
    }
90
91
0
    auto &oMDList = oIter->second;
92
0
    oMDList = papszMetadata;
93
94
    // we want to mark name/value pair domains as being sorted for fast
95
    // access.
96
0
    if (!STARTS_WITH_CI(pszDomain, "xml:") &&
97
0
        !STARTS_WITH_CI(pszDomain, "json:") &&
98
0
        !EQUAL(pszDomain, "SUBDATASETS")
99
        // The IMD metadata domain should not be sorted, as order matters
100
        // when writing it back. Cf https://github.com/OSGeo/gdal/issues/11470
101
0
        && !EQUAL(pszDomain, "IMD"))
102
0
    {
103
0
        oMDList.Sort();
104
0
    }
105
106
0
    return CE_None;
107
0
}
108
109
/************************************************************************/
110
/*                          GetMetadataItem()                           */
111
/************************************************************************/
112
113
const char *GDALMultiDomainMetadata::GetMetadataItem(const char *pszName,
114
                                                     const char *pszDomain)
115
116
0
{
117
0
    const auto oIter = oMetadata.find(SanitizeDomain(pszDomain));
118
0
    if (oIter == oMetadata.end())
119
0
        return nullptr;
120
0
    return oIter->second.FetchNameValue(pszName);
121
0
}
122
123
/************************************************************************/
124
/*                          SetMetadataItem()                           */
125
/************************************************************************/
126
127
CPLErr GDALMultiDomainMetadata::SetMetadataItem(const char *pszName,
128
                                                const char *pszValue,
129
                                                const char *pszDomain)
130
131
0
{
132
0
    pszDomain = SanitizeDomain(pszDomain);
133
134
    /* -------------------------------------------------------------------- */
135
    /*      Create the domain if it does not already exist.                 */
136
    /* -------------------------------------------------------------------- */
137
138
0
    auto oIter = oMetadata.find(pszDomain);
139
0
    if (oIter == oMetadata.end())
140
0
    {
141
0
        aosDomainList.AddString(pszDomain);
142
0
        oIter =
143
0
            oMetadata.insert(std::pair(aosDomainList.back(), CPLStringList()))
144
0
                .first;
145
0
    }
146
147
    /* -------------------------------------------------------------------- */
148
    /*      Set the value in the domain list.                               */
149
    /* -------------------------------------------------------------------- */
150
0
    oIter->second.SetNameValue(pszName, pszValue);
151
152
0
    return CE_None;
153
0
}
154
155
/************************************************************************/
156
/*                              XMLInit()                               */
157
/*                                                                      */
158
/*      This method should be invoked on the parent of the              */
159
/*      <Metadata> elements.                                            */
160
/************************************************************************/
161
162
int GDALMultiDomainMetadata::XMLInit(const CPLXMLNode *psTree, int /* bMerge */)
163
0
{
164
0
    const CPLXMLNode *psMetadata = nullptr;
165
166
    /* ==================================================================== */
167
    /*      Process all <Metadata> elements, each for one domain.           */
168
    /* ==================================================================== */
169
0
    for (psMetadata = psTree->psChild; psMetadata != nullptr;
170
0
         psMetadata = psMetadata->psNext)
171
0
    {
172
0
        if (psMetadata->eType != CXT_Element ||
173
0
            !EQUAL(psMetadata->pszValue, "Metadata"))
174
0
            continue;
175
176
0
        const char *pszDomain = CPLGetXMLValue(psMetadata, "domain", "");
177
0
        const char *pszFormat = CPLGetXMLValue(psMetadata, "format", "");
178
179
        // Make sure we have a CPLStringList for this domain,
180
        // without wiping out an existing one.
181
0
        if (GetMetadata(pszDomain) == nullptr)
182
0
            SetMetadata(nullptr, pszDomain);
183
184
0
        auto oIter = oMetadata.find(pszDomain);
185
0
        CPLAssert(oIter != oMetadata.end());
186
187
0
        auto &oMDList = oIter->second;
188
189
        /* --------------------------------------------------------------------
190
         */
191
        /*      XML format subdocuments. */
192
        /* --------------------------------------------------------------------
193
         */
194
0
        if (EQUAL(pszFormat, "xml"))
195
0
        {
196
            // Find first non-attribute child of current element.
197
0
            const CPLXMLNode *psSubDoc = psMetadata->psChild;
198
0
            while (psSubDoc != nullptr && psSubDoc->eType == CXT_Attribute)
199
0
                psSubDoc = psSubDoc->psNext;
200
201
0
            char *pszDoc = CPLSerializeXMLTree(psSubDoc);
202
203
0
            oMDList.Clear();
204
0
            oMDList.AddStringDirectly(pszDoc);
205
0
        }
206
207
        /* --------------------------------------------------------------------
208
         */
209
        /*      JSon format subdocuments. */
210
        /* --------------------------------------------------------------------
211
         */
212
0
        else if (EQUAL(pszFormat, "json"))
213
0
        {
214
            // Find first text child of current element.
215
0
            const CPLXMLNode *psSubDoc = psMetadata->psChild;
216
0
            while (psSubDoc != nullptr && psSubDoc->eType != CXT_Text)
217
0
                psSubDoc = psSubDoc->psNext;
218
0
            if (psSubDoc)
219
0
            {
220
0
                oMDList.Clear();
221
0
                oMDList.AddString(psSubDoc->pszValue);
222
0
            }
223
0
        }
224
225
        /* --------------------------------------------------------------------
226
         */
227
        /*      Name value format. */
228
        /*      <MDI key="...">value_Text</MDI> */
229
        /* --------------------------------------------------------------------
230
         */
231
0
        else
232
0
        {
233
0
            for (const CPLXMLNode *psMDI = psMetadata->psChild;
234
0
                 psMDI != nullptr; psMDI = psMDI->psNext)
235
0
            {
236
0
                if (!EQUAL(psMDI->pszValue, "MDI") ||
237
0
                    psMDI->eType != CXT_Element || psMDI->psChild == nullptr ||
238
0
                    psMDI->psChild->psNext == nullptr ||
239
0
                    psMDI->psChild->eType != CXT_Attribute ||
240
0
                    psMDI->psChild->psChild == nullptr)
241
0
                    continue;
242
243
0
                char *pszName = psMDI->psChild->psChild->pszValue;
244
0
                char *pszValue = psMDI->psChild->psNext->pszValue;
245
0
                if (pszName != nullptr && pszValue != nullptr)
246
0
                    oMDList.SetNameValue(pszName, pszValue);
247
0
            }
248
0
        }
249
0
    }
250
251
0
    return !aosDomainList.empty();
252
0
}
253
254
/************************************************************************/
255
/*                             Serialize()                              */
256
/************************************************************************/
257
258
CPLXMLNode *GDALMultiDomainMetadata::Serialize() const
259
260
0
{
261
0
    CPLXMLNode *psFirst = nullptr;
262
263
0
    for (const auto &[pszDomainName, oList] : oMetadata)
264
0
    {
265
0
        CSLConstList papszMD = oList.List();
266
        // Do not serialize empty domains.
267
0
        if (papszMD == nullptr || papszMD[0] == nullptr)
268
0
            continue;
269
270
0
        CPLXMLNode *psMD = CPLCreateXMLNode(nullptr, CXT_Element, "Metadata");
271
272
0
        if (strlen(pszDomainName) > 0)
273
0
            CPLCreateXMLNode(CPLCreateXMLNode(psMD, CXT_Attribute, "domain"),
274
0
                             CXT_Text, pszDomainName);
275
276
0
        bool bFormatXMLOrJSon = false;
277
278
0
        if (STARTS_WITH_CI(pszDomainName, "xml:") && CSLCount(papszMD) == 1)
279
0
        {
280
0
            CPLXMLNode *psValueAsXML = CPLParseXMLString(papszMD[0]);
281
0
            if (psValueAsXML != nullptr)
282
0
            {
283
0
                bFormatXMLOrJSon = true;
284
285
0
                CPLCreateXMLNode(
286
0
                    CPLCreateXMLNode(psMD, CXT_Attribute, "format"), CXT_Text,
287
0
                    "xml");
288
289
0
                CPLAddXMLChild(psMD, psValueAsXML);
290
0
            }
291
0
        }
292
293
0
        if (STARTS_WITH_CI(pszDomainName, "json:") && CSLCount(papszMD) == 1)
294
0
        {
295
0
            bFormatXMLOrJSon = true;
296
297
0
            CPLCreateXMLNode(CPLCreateXMLNode(psMD, CXT_Attribute, "format"),
298
0
                             CXT_Text, "json");
299
0
            CPLCreateXMLNode(psMD, CXT_Text, *papszMD);
300
0
        }
301
302
0
        if (!bFormatXMLOrJSon)
303
0
        {
304
0
            CPLXMLNode *psLastChild = nullptr;
305
            // To go after domain attribute.
306
0
            if (psMD->psChild != nullptr)
307
0
            {
308
0
                psLastChild = psMD->psChild;
309
0
                while (psLastChild->psNext != nullptr)
310
0
                    psLastChild = psLastChild->psNext;
311
0
            }
312
0
            for (int i = 0; papszMD[i] != nullptr; i++)
313
0
            {
314
0
                char *pszKey = nullptr;
315
316
0
                const char *pszRawValue =
317
0
                    CPLParseNameValue(papszMD[i], &pszKey);
318
319
0
                CPLXMLNode *psMDI =
320
0
                    CPLCreateXMLNode(nullptr, CXT_Element, "MDI");
321
0
                if (psLastChild == nullptr)
322
0
                    psMD->psChild = psMDI;
323
0
                else
324
0
                    psLastChild->psNext = psMDI;
325
0
                psLastChild = psMDI;
326
327
0
                CPLSetXMLValue(psMDI, "#key", pszKey);
328
0
                CPLCreateXMLNode(psMDI, CXT_Text, pszRawValue);
329
330
0
                CPLFree(pszKey);
331
0
            }
332
0
        }
333
334
0
        if (psFirst == nullptr)
335
0
            psFirst = psMD;
336
0
        else
337
0
            CPLAddXMLSibling(psFirst, psMD);
338
0
    }
339
340
0
    return psFirst;
341
0
}
342
343
//! @endcond