bes  Updated for version 3.20.10
BESXMLUtils.cc
1 // BESXMLUtils.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include "BESXMLUtils.h"
34 #include "BESUtil.h"
35 #include "BESDebug.h"
36 
37 using std::vector;
38 using std::string;
39 using std::map;
40 
41 #define MODULE "xmlutils"
42 #define prolog string("BESXMLUtils::").append(__func__).append("() - ")
43 
54 void BESXMLUtils::XMLErrorFunc(void *context, const char *msg, ...)
55 {
56  va_list args;
57  va_start( args, msg );
58  char mymsg[1024];
59  vsnprintf(mymsg, sizeof mymsg, msg, args);
60  va_end(args); // Added jhrg 9/17/15
61  vector<string> *myerrors = (vector<string> *) context;
62  myerrors->push_back(mymsg);
63 }
64 
74 void BESXMLUtils::GetProps(xmlNode *node, map<string, string> &props)
75 {
76  if (!node) {
77  return;
78  }
79 
80  if (node->properties == nullptr) {
81  return;
82  }
83 
84  xmlAttr *curr_prop = node->properties;
85  while (curr_prop) {
86  string prop_name = (char *) curr_prop->name;
88  string prop_val;
89  xmlNode *curr_val = curr_prop->children;
90  if (curr_val && curr_val->content) {
91  prop_val = BESUtil::xml2id((char *) curr_val->content);
93  }
94  props[prop_name] = prop_val;
95 
96  curr_prop = curr_prop->next;
97  }
98 }
99 
109 void BESXMLUtils::GetNodeInfo(xmlNode *node, string &name, string &value, map<string, string> &props)
110 {
111  if (node) {
112  name = (char *) node->name;
114  BESXMLUtils::GetProps(node, props);
115  xmlNode *child_node = node->children;
116  bool done = false;
117  while (child_node && !done) {
118  if (child_node->type == XML_TEXT_NODE) {
119  if (child_node->content) {
120  BESDEBUG(MODULE, prolog << "child_node->content: " << child_node->content << std::endl);
121  value = BESUtil::xml2id((char *)child_node->content);
123  BESDEBUG(MODULE, prolog << "value: " << value << std::endl);
124  }
125  else {
126  value = "";
127  }
128  done = true;
129  }
130  child_node = child_node->next;
131  }
132  }
133 }
134 
142 xmlNode *
143 BESXMLUtils::GetFirstChild(xmlNode *node, string &child_name, string &child_value, map<string, string> &child_props)
144 {
145  xmlNode *child_node = nullptr;
146  if (node) {
147  child_node = node->children;
148  bool done = false;
149  while (child_node && !done) {
150  if (child_node->type == XML_ELEMENT_NODE) {
151  done = true;
152  BESXMLUtils::GetNodeInfo(child_node, child_name, child_value, child_props);
153  }
154  else {
155  child_node = child_node->next;
156  }
157  }
158  }
159  return child_node;
160 }
161 
169 xmlNode *
170 BESXMLUtils::GetNextChild(xmlNode *child_node, string &next_name, string &next_value, map<string, string> &next_props)
171 {
172  if (child_node) {
173  child_node = child_node->next;
174  bool done = false;
175  while (child_node && !done) {
176  if (child_node->type == XML_ELEMENT_NODE) {
177  done = true;
178  BESXMLUtils::GetNodeInfo(child_node, next_name, next_value, next_props);
179  }
180  else {
181  child_node = child_node->next;
182  }
183  }
184  }
185  return child_node;
186 }
187 
195 xmlNode *
196 BESXMLUtils::GetChild(xmlNode *node, const string &child_name, string &child_value, map<string, string> &child_props)
197 {
198  xmlNode *child_node = nullptr;
199  if (node) {
200  child_node = node->children;
201  bool done = false;
202  while (child_node && !done) {
203  if (child_node->type == XML_ELEMENT_NODE) {
204  string name = (char *) child_node->name;
206  if (name == child_name) {
207  done = true;
208  BESXMLUtils::GetNodeInfo(child_node, name, child_value, child_props);
209  }
210  else {
211  child_node = child_node->next;
212  }
213  }
214  else {
215  child_node = child_node->next;
216  }
217  }
218  }
219  return child_node;
220 }
221 
static std::string xml2id(std::string in)
Definition: BESUtil.cc:541
static void removeLeadingAndTrailingBlanks(std::string &key)
Definition: BESUtil.cc:485
static void GetNodeInfo(xmlNode *node, std::string &name, std::string &value, std::map< std::string, std::string > &props)
get the name, value if any, and any properties for the specified node
Definition: BESXMLUtils.cc:109
static void GetProps(xmlNode *node, std::map< std::string, std::string > &props)
given an xml node, build the map of properties (xml attributes) for that node
Definition: BESXMLUtils.cc:74
static xmlNode * GetFirstChild(xmlNode *node, std::string &child_name, std::string &child_value, std::map< std::string, std::string > &child_props)
get the first element child node for the given node
Definition: BESXMLUtils.cc:143
static xmlNode * GetChild(xmlNode *node, const std::string &child_name, std::string &child_value, std::map< std::string, std::string > &child_props)
get the element child node of the given node with the given name
Definition: BESXMLUtils.cc:196
static xmlNode * GetNextChild(xmlNode *child_node, std::string &next_name, std::string &next_value, std::map< std::string, std::string > &next_props)
get the next element child node after the given child node
Definition: BESXMLUtils.cc:170
static void XMLErrorFunc(void *context, const char *msg,...)
error function used by libxml2 to report errors
Definition: BESXMLUtils.cc:54