bes  Updated for version 3.17.4
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 
65 void BESXMLUtils::GetProps(xmlNode *node, map<string, string> &props)
66 {
67  if (!node) {
68  return;
69  }
70 
71  if (node->properties == NULL) {
72  return;
73  }
74 
75  xmlAttr *curr_prop = node->properties;
76  while (curr_prop) {
77  string prop_name = (char *) curr_prop->name;
79  string prop_val;
80  xmlNode *curr_val = curr_prop->children;
81  if (curr_val && curr_val->content) {
82  prop_val = BESUtil::xml2id((char *) curr_val->content);
84  }
85  props[prop_name] = prop_val;
86 
87  curr_prop = curr_prop->next;
88  }
89 }
90 
99 void BESXMLUtils::GetNodeInfo(xmlNode *node, string &name, string &value, map<string, string> &props)
100 {
101  if (node) {
102  name = (char *) node->name;
104  BESXMLUtils::GetProps(node, props);
105  xmlNode *child_node = node->children;
106  bool done = false;
107  while (child_node && !done) {
108  if (child_node->type == XML_TEXT_NODE) {
109  if (child_node->content) {
110  value = BESUtil::xml2id((char *) child_node->content);
112  }
113  else {
114  value = "";
115  }
116  done = true;
117  }
118  child_node = child_node->next;
119  }
120  }
121 }
122 
130 xmlNode *
131 BESXMLUtils::GetFirstChild(xmlNode *node, string &child_name, string &child_value, map<string, string> &child_props)
132 {
133  xmlNode *child_node = NULL;
134  if (node) {
135  child_node = node->children;
136  bool done = false;
137  while (child_node && !done) {
138  if (child_node->type == XML_ELEMENT_NODE) {
139  done = true;
140  BESXMLUtils::GetNodeInfo(child_node, child_name, child_value, child_props);
141  }
142  else {
143  child_node = child_node->next;
144  }
145  }
146  }
147  return child_node;
148 }
149 
157 xmlNode *
158 BESXMLUtils::GetNextChild(xmlNode *child_node, string &next_name, string &next_value, map<string, string> &next_props)
159 {
160  if (child_node) {
161  child_node = child_node->next;
162  bool done = false;
163  while (child_node && !done) {
164  if (child_node->type == XML_ELEMENT_NODE) {
165  done = true;
166  BESXMLUtils::GetNodeInfo(child_node, next_name, next_value, next_props);
167  }
168  else {
169  child_node = child_node->next;
170  }
171  }
172  }
173  return child_node;
174 }
175 
183 xmlNode *
184 BESXMLUtils::GetChild(xmlNode *node, const string &child_name, string &child_value, map<string, string> &child_props)
185 {
186  xmlNode *child_node = NULL;
187  if (node) {
188  child_node = node->children;
189  bool done = false;
190  while (child_node && !done) {
191  if (child_node->type == XML_ELEMENT_NODE) {
192  string name = (char *) child_node->name;
194  if (name == child_name) {
195  done = true;
196  BESXMLUtils::GetNodeInfo(child_node, name, child_value, child_props);
197  }
198  else {
199  child_node = child_node->next;
200  }
201  }
202  else {
203  child_node = child_node->next;
204  }
205  }
206  }
207  return child_node;
208 }
209 
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:131
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:158
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:99
static void removeLeadingAndTrailingBlanks(string &key)
Definition: BESUtil.cc:447
static void XMLErrorFunc(void *context, const char *msg,...)
error function used by libxml2 to report errors
Definition: BESXMLUtils.cc:46
static void GetProps(xmlNode *node, map< string, string > &props)
given an xml node, build the map of properties for that node
Definition: BESXMLUtils.cc:65
static string xml2id(string in)
Definition: BESUtil.cc:503
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:184