bes  Updated for version 3.20.5
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 
46 void BESXMLUtils::XMLErrorFunc(void *context, const char *msg, ...)
47 {
48  va_list args;
49  va_start( args, msg );
50  char mymsg[1024];
51  vsnprintf(mymsg, sizeof mymsg, msg, args);
52  va_end(args); // Added jhrg 9/17/15
53  vector<string> *myerrors = (vector<string> *) context;
54  myerrors->push_back(mymsg);
55 }
56 
66 void BESXMLUtils::GetProps(xmlNode *node, map<string, string> &props)
67 {
68  if (!node) {
69  return;
70  }
71 
72  if (node->properties == NULL) {
73  return;
74  }
75 
76  xmlAttr *curr_prop = node->properties;
77  while (curr_prop) {
78  string prop_name = (char *) curr_prop->name;
80  string prop_val;
81  xmlNode *curr_val = curr_prop->children;
82  if (curr_val && curr_val->content) {
83  prop_val = BESUtil::xml2id((char *) curr_val->content);
85  }
86  props[prop_name] = prop_val;
87 
88  curr_prop = curr_prop->next;
89  }
90 }
91 
101 void BESXMLUtils::GetNodeInfo(xmlNode *node, string &name, string &value, map<string, string> &props)
102 {
103  if (node) {
104  name = (char *) node->name;
106  BESXMLUtils::GetProps(node, props);
107  xmlNode *child_node = node->children;
108  bool done = false;
109  while (child_node && !done) {
110  if (child_node->type == XML_TEXT_NODE) {
111  if (child_node->content) {
112  value = BESUtil::xml2id((char *)child_node->content);
114  }
115  else {
116  value = "";
117  }
118  done = true;
119  }
120  child_node = child_node->next;
121  }
122  }
123 }
124 
132 xmlNode *
133 BESXMLUtils::GetFirstChild(xmlNode *node, string &child_name, string &child_value, map<string, string> &child_props)
134 {
135  xmlNode *child_node = NULL;
136  if (node) {
137  child_node = node->children;
138  bool done = false;
139  while (child_node && !done) {
140  if (child_node->type == XML_ELEMENT_NODE) {
141  done = true;
142  BESXMLUtils::GetNodeInfo(child_node, child_name, child_value, child_props);
143  }
144  else {
145  child_node = child_node->next;
146  }
147  }
148  }
149  return child_node;
150 }
151 
159 xmlNode *
160 BESXMLUtils::GetNextChild(xmlNode *child_node, string &next_name, string &next_value, map<string, string> &next_props)
161 {
162  if (child_node) {
163  child_node = child_node->next;
164  bool done = false;
165  while (child_node && !done) {
166  if (child_node->type == XML_ELEMENT_NODE) {
167  done = true;
168  BESXMLUtils::GetNodeInfo(child_node, next_name, next_value, next_props);
169  }
170  else {
171  child_node = child_node->next;
172  }
173  }
174  }
175  return child_node;
176 }
177 
185 xmlNode *
186 BESXMLUtils::GetChild(xmlNode *node, const string &child_name, string &child_value, map<string, string> &child_props)
187 {
188  xmlNode *child_node = NULL;
189  if (node) {
190  child_node = node->children;
191  bool done = false;
192  while (child_node && !done) {
193  if (child_node->type == XML_ELEMENT_NODE) {
194  string name = (char *) child_node->name;
196  if (name == child_name) {
197  done = true;
198  BESXMLUtils::GetNodeInfo(child_node, name, child_value, child_props);
199  }
200  else {
201  child_node = child_node->next;
202  }
203  }
204  else {
205  child_node = child_node->next;
206  }
207  }
208  }
209  return child_node;
210 }
211 
BESXMLUtils::GetChild
static xmlNode * GetChild(xmlNode *node, const string &child_name, string &child_value, map< string, string > &child_props)
get the element child node of the given node with the given name
Definition: BESXMLUtils.cc:186
BESXMLUtils::GetNodeInfo
static void GetNodeInfo(xmlNode *node, string &name, string &value, map< string, string > &props)
get the name, value if any, and any properties for the specified node
Definition: BESXMLUtils.cc:101
BESUtil::removeLeadingAndTrailingBlanks
static void removeLeadingAndTrailingBlanks(string &key)
Definition: BESUtil.cc:463
BESXMLUtils::GetNextChild
static xmlNode * GetNextChild(xmlNode *child_node, string &next_name, string &next_value, map< string, string > &next_props)
get the next element child node after the given child node
Definition: BESXMLUtils.cc:160
BESXMLUtils::GetProps
static void GetProps(xmlNode *node, map< string, string > &props)
given an xml node, build the map of properties (xml attributes) for that node
Definition: BESXMLUtils.cc:66
BESXMLUtils::XMLErrorFunc
static void XMLErrorFunc(void *context, const char *msg,...)
error function used by libxml2 to report errors
Definition: BESXMLUtils.cc:46
BESUtil::xml2id
static string xml2id(string in)
Definition: BESUtil.cc:519
BESXMLUtils::GetFirstChild
static xmlNode * GetFirstChild(xmlNode *node, string &child_name, string &child_value, map< string, string > &child_props)
get the first element child node for the given node
Definition: BESXMLUtils.cc:133