bes  Updated for version 3.20.6
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 
36 using std::vector;
37 using std::string;
38 using std::map;
39 
50 void BESXMLUtils::XMLErrorFunc(void *context, const char *msg, ...)
51 {
52  va_list args;
53  va_start( args, msg );
54  char mymsg[1024];
55  vsnprintf(mymsg, sizeof mymsg, msg, args);
56  va_end(args); // Added jhrg 9/17/15
57  vector<string> *myerrors = (vector<string> *) context;
58  myerrors->push_back(mymsg);
59 }
60 
70 void BESXMLUtils::GetProps(xmlNode *node, map<string, string> &props)
71 {
72  if (!node) {
73  return;
74  }
75 
76  if (node->properties == NULL) {
77  return;
78  }
79 
80  xmlAttr *curr_prop = node->properties;
81  while (curr_prop) {
82  string prop_name = (char *) curr_prop->name;
84  string prop_val;
85  xmlNode *curr_val = curr_prop->children;
86  if (curr_val && curr_val->content) {
87  prop_val = BESUtil::xml2id((char *) curr_val->content);
89  }
90  props[prop_name] = prop_val;
91 
92  curr_prop = curr_prop->next;
93  }
94 }
95 
105 void BESXMLUtils::GetNodeInfo(xmlNode *node, string &name, string &value, map<string, string> &props)
106 {
107  if (node) {
108  name = (char *) node->name;
110  BESXMLUtils::GetProps(node, props);
111  xmlNode *child_node = node->children;
112  bool done = false;
113  while (child_node && !done) {
114  if (child_node->type == XML_TEXT_NODE) {
115  if (child_node->content) {
116  value = BESUtil::xml2id((char *)child_node->content);
118  }
119  else {
120  value = "";
121  }
122  done = true;
123  }
124  child_node = child_node->next;
125  }
126  }
127 }
128 
136 xmlNode *
137 BESXMLUtils::GetFirstChild(xmlNode *node, string &child_name, string &child_value, map<string, string> &child_props)
138 {
139  xmlNode *child_node = NULL;
140  if (node) {
141  child_node = node->children;
142  bool done = false;
143  while (child_node && !done) {
144  if (child_node->type == XML_ELEMENT_NODE) {
145  done = true;
146  BESXMLUtils::GetNodeInfo(child_node, child_name, child_value, child_props);
147  }
148  else {
149  child_node = child_node->next;
150  }
151  }
152  }
153  return child_node;
154 }
155 
163 xmlNode *
164 BESXMLUtils::GetNextChild(xmlNode *child_node, string &next_name, string &next_value, map<string, string> &next_props)
165 {
166  if (child_node) {
167  child_node = child_node->next;
168  bool done = false;
169  while (child_node && !done) {
170  if (child_node->type == XML_ELEMENT_NODE) {
171  done = true;
172  BESXMLUtils::GetNodeInfo(child_node, next_name, next_value, next_props);
173  }
174  else {
175  child_node = child_node->next;
176  }
177  }
178  }
179  return child_node;
180 }
181 
189 xmlNode *
190 BESXMLUtils::GetChild(xmlNode *node, const string &child_name, string &child_value, map<string, string> &child_props)
191 {
192  xmlNode *child_node = NULL;
193  if (node) {
194  child_node = node->children;
195  bool done = false;
196  while (child_node && !done) {
197  if (child_node->type == XML_ELEMENT_NODE) {
198  string name = (char *) child_node->name;
200  if (name == child_name) {
201  done = true;
202  BESXMLUtils::GetNodeInfo(child_node, name, child_value, child_props);
203  }
204  else {
205  child_node = child_node->next;
206  }
207  }
208  else {
209  child_node = child_node->next;
210  }
211  }
212  }
213  return child_node;
214 }
215 
BESXMLUtils::GetNodeInfo
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:105
BESXMLUtils::GetProps
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:70
BESXMLUtils::XMLErrorFunc
static void XMLErrorFunc(void *context, const char *msg,...)
error function used by libxml2 to report errors
Definition: BESXMLUtils.cc:50
BESXMLUtils::GetChild
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:190
BESUtil::xml2id
static std::string xml2id(std::string in)
Definition: BESUtil.cc:522
BESXMLUtils::GetFirstChild
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:137
BESXMLUtils::GetNextChild
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:164
BESUtil::removeLeadingAndTrailingBlanks
static void removeLeadingAndTrailingBlanks(std::string &key)
Definition: BESUtil.cc:466