OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESXMLUtils.cc
Go to the documentation of this file.
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
47 BESXMLUtils::XMLErrorFunc( void *context, const char *msg, ... )
48 {
49  va_list args ;
50  va_start( args, msg ) ;
51  char mymsg[1024] ;
52  vsnprintf( mymsg, sizeof mymsg, msg, args ) ;
53  vector<string> *myerrors = (vector<string> *)context ;
54  myerrors->push_back( mymsg ) ;
55 }
56 
65 void
66 BESXMLUtils::GetProps( xmlNode *node, map< string, string> &props )
67 {
68  if( !node )
69  {
70  return ;
71  }
72 
73  if( node->properties == NULL )
74  {
75  return ;
76  }
77 
78  xmlAttr *curr_prop = node->properties ;
79  while( curr_prop )
80  {
81  string prop_name = (char *)curr_prop->name ;
83  string prop_val ;
84  xmlNode *curr_val = curr_prop->children ;
85  if( curr_val && curr_val->content )
86  {
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 
104 void
106  string &name,
107  string &value,
108  map<string, string> &props )
109 {
110  if( node )
111  {
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  {
119  if( child_node->type == XML_TEXT_NODE )
120  {
121  if( child_node->content )
122  {
123  value = BESUtil::xml2id( (char *)child_node->content ) ;
125  }
126  else
127  {
128  value = "" ;
129  }
130  done = true ;
131  }
132  child_node = child_node->next ;
133  }
134  }
135 }
136 
144 xmlNode *
146  string &child_name,
147  string &child_value,
148  map<string, string> &child_props )
149 {
150  xmlNode *child_node = NULL ;
151  if( node )
152  {
153  child_node = node->children ;
154  bool done = false ;
155  while( child_node && !done )
156  {
157  if( child_node->type == XML_ELEMENT_NODE )
158  {
159  done = true ;
160  BESXMLUtils::GetNodeInfo( child_node, child_name,
161  child_value, child_props ) ;
162  }
163  else
164  {
165  child_node = child_node->next ;
166  }
167  }
168  }
169  return child_node ;
170 }
171 
179 xmlNode *
180 BESXMLUtils::GetNextChild( xmlNode *child_node,
181  string &next_name,
182  string &next_value,
183  map<string, string> &next_props )
184 {
185  if( child_node )
186  {
187  child_node = child_node->next ;
188  bool done = false ;
189  while( child_node && !done )
190  {
191  if( child_node->type == XML_ELEMENT_NODE )
192  {
193  done = true ;
194  BESXMLUtils::GetNodeInfo( child_node, next_name,
195  next_value, next_props ) ;
196  }
197  else
198  {
199  child_node = child_node->next ;
200  }
201  }
202  }
203  return child_node ;
204 }
205 
213 xmlNode *
214 BESXMLUtils::GetChild( xmlNode *node,
215  const string &child_name,
216  string &child_value,
217  map<string, string> &child_props )
218 {
219  xmlNode *child_node = NULL ;
220  if( node )
221  {
222  child_node = node->children ;
223  bool done = false ;
224  while( child_node && !done )
225  {
226  if( child_node->type == XML_ELEMENT_NODE )
227  {
228  string name = (char *)child_node->name ;
230  if( name == child_name )
231  {
232  done = true ;
233  BESXMLUtils::GetNodeInfo( child_node, name,
234  child_value, child_props ) ;
235  }
236  else
237  {
238  child_node = child_node->next ;
239  }
240  }
241  else
242  {
243  child_node = child_node->next ;
244  }
245  }
246  }
247  return child_node ;
248 }
249 
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:145
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:180
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:105
static void removeLeadingAndTrailingBlanks(string &key)
remove leading and trailing blanks from a string
Definition: BESUtil.cc:366
static void XMLErrorFunc(void *context, const char *msg,...)
error function used by libxml2 to report errors
Definition: BESXMLUtils.cc:47
static void GetProps(xmlNode *node, map< string, string > &props)
given an xml node, build the map of properties for that node
Definition: BESXMLUtils.cc:66
static string xml2id(string in)
unescape xml escaped characters
Definition: BESUtil.cc:419
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:214