Coverage Report

Created: 2025-12-31 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/MapServer/src/maplibxml2.c
Line
Count
Source
1
/******************************************************************************
2
 * $Id$
3
 *
4
 * Project:  MapServer
5
 * Purpose:  libxml2 convenience wrapper functions
6
 * Author:   Tom Kralidis (tomkralidis@gmail.com)
7
 *
8
 ******************************************************************************
9
 * Copyright (c) 2007, Tom Kralidis
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a
12
 * copy of this software and associated documentation files (the "Software"),
13
 * to deal in the Software without restriction, including without limitation
14
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15
 * and/or sell copies of the Software, and to permit persons to whom the
16
 * Software is furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies of this Software or works derived from this Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
 * DEALINGS IN THE SOFTWARE.
28
 ****************************************************************************/
29
30
#include "mapserver.h"
31
32
#ifdef USE_LIBXML2
33
34
#include <libxml/parser.h>
35
#include <libxml/tree.h>
36
#include <libxml/xpath.h>
37
#include <libxml/xpathInternals.h>
38
39
/**
40
 * msLibXml2GenerateList()
41
 *
42
 * Convenience function to produce a series of XML elements from a delimited
43
 * list
44
 *
45
 * @param xmlNodePtr psParent the encompassing node
46
 * @param xmlNsPtr psNs the namespace object
47
 * @param const char *elname the list member element name
48
 * @param const char *values the list member element values
49
 * @param char delim the delimiter
50
 *
51
 */
52
53
void msLibXml2GenerateList(xmlNodePtr psParent, xmlNsPtr psNs,
54
0
                           const char *elname, const char *values, char delim) {
55
0
  char **tokens = NULL;
56
0
  int n = 0;
57
0
  int i = 0;
58
0
  tokens = msStringSplit(values, delim, &n);
59
0
  for (i = 0; i < n; i++) {
60
    // Not sure we really need to distinguish empty vs non-empty case, but
61
    // this does change the result of
62
    // msautotest/wxs/expected/wcs_empty_cap111.xml otherwise
63
0
    if (tokens[i] && tokens[i][0] != '\0')
64
0
      xmlNewTextChild(psParent, psNs, BAD_CAST elname, BAD_CAST tokens[i]);
65
0
    else
66
0
      xmlNewChild(psParent, psNs, BAD_CAST elname, BAD_CAST tokens[i]);
67
0
  }
68
0
  msFreeCharArray(tokens, n);
69
0
}
70
71
/**
72
 * msLibXml2GetXPath()
73
 *
74
 * Convenience function to fetch an XPath
75
 *
76
 * @param xmlDocPtr doc the XML doc pointer
77
 * @param xmlXPathContextPtr the context pointer
78
 * @param xmlChar *xpath the xpath value
79
 *
80
 * @return result xmlXPathObjectPtr pointer
81
 *
82
 */
83
84
xmlXPathObjectPtr msLibXml2GetXPath(xmlDocPtr doc, xmlXPathContextPtr context,
85
0
                                    xmlChar *xpath) {
86
0
  (void)doc;
87
0
  xmlXPathObjectPtr result;
88
0
  result = xmlXPathEval(xpath, context);
89
0
  if (result == NULL) {
90
0
    return NULL;
91
0
  }
92
0
  if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
93
0
    xmlXPathFreeObject(result);
94
0
    return NULL;
95
0
  }
96
0
  return result;
97
0
}
98
99
/**
100
 * msLibXml2GetXPathTree
101
 *
102
 * Convenience function to fetch an XPath and children
103
 *
104
 * @param xmlDocPtr doc the XML doc pointer
105
 * @param xmlXPathObjectPtr the xpath object
106
 *
107
 * @return result string
108
 *
109
 */
110
111
0
char *msLibXml2GetXPathTree(xmlDocPtr doc, xmlXPathObjectPtr xpath) {
112
0
  xmlBufferPtr xbuf;
113
0
  char *result = NULL;
114
115
0
  xbuf = xmlBufferCreate();
116
117
0
  if (xpath) {
118
0
    if (xmlNodeDump(xbuf, doc, xpath->nodesetval->nodeTab[0], 0, 0) == -1) {
119
0
      return NULL;
120
0
    }
121
0
    result = msStrdup((char *)xbuf->content);
122
0
  }
123
0
  xmlBufferFree(xbuf);
124
0
  return result;
125
0
}
126
127
#endif /* defined(USE_LIBXML2) */