bes  Updated for version 3.19.1
BESXMLInfo.cc
1 // BESXMLInfo.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 <sstream>
34 
35 using std::ostringstream ;
36 
37 #include "BESXMLInfo.h"
38 #include "BESUtil.h"
39 #include "BESDataNames.h"
40 
41 #define MY_ENCODING "ISO-8859-1"
42 #define BES_SCHEMA "http://xml.opendap.org/ns/bes/1.0#"
43 
50  : BESInfo( ),
51  _writer( 0 ),
52  _doc_buf( 0 ),
53  _started( false ),
54  _ended( false )
55 {
56 }
57 
58 BESXMLInfo::~BESXMLInfo()
59 {
60  cleanup() ;
61 }
62 
63 void
64 BESXMLInfo::cleanup()
65 {
66  // make sure the buffer and writer are all cleaned up
67  if( _writer )
68  {
69  xmlFreeTextWriter( _writer ) ;
70  _writer = 0 ;
71  _doc_buf = 0 ;
72  }
73  if( _doc_buf )
74  {
75  xmlBufferFree( _doc_buf ) ;
76  _doc_buf = 0 ;
77  }
78 
79  // this always seems to be causing a memory fault
80  // xmlCleanupParser();
81 
82  _started = false ;
83  _ended = false ;
84  if( _strm )
85  {
86  ((ostringstream *)_strm)->str( "" ) ;
87  }
88 }
89 
98 void
99 BESXMLInfo::begin_response( const string &response_name,
101 {
102  BESInfo::begin_response( response_name, dhi ) ;
103 
104  _response_name = response_name ;
105 
106  LIBXML_TEST_VERSION
107 
108  int rc = 0 ;
109 
110  /* Create a new XML buffer, to which the XML document will be
111  * written */
112  _doc_buf = xmlBufferCreate() ;
113  if( _doc_buf == NULL )
114  {
115  cleanup() ;
116  string err = (string)"Error creating the xml buffer for response "
117  + _response_name ;
118  throw BESInternalError( err, __FILE__, __LINE__ ) ;
119  }
120 
121  /* Create a new XmlWriter for memory, with no compression.
122  * Remark: there is no compression for this kind of xmlTextWriter */
123  _writer = xmlNewTextWriterMemory( _doc_buf, 0 ) ;
124  if( _writer == NULL )
125  {
126  cleanup() ;
127  string err = (string)"Error creating the xml writer for response "
128  + _response_name ;
129  throw BESInternalError( err, __FILE__, __LINE__ ) ;
130  }
131 
132  rc = xmlTextWriterSetIndent( _writer, 4 ) ;
133  if( rc < 0 )
134  {
135  cleanup() ;
136  string err = (string)"Error starting indentation for response document "
137  + _response_name ;
138  throw BESInternalError( err, __FILE__, __LINE__ ) ;
139  }
140 
141  rc = xmlTextWriterSetIndentString( _writer, BAD_CAST " " ) ;
142  if( rc < 0 )
143  {
144  cleanup() ;
145  string err = (string)"Error setting indentation for response document "
146  + _response_name ;
147  throw BESInternalError( err, __FILE__, __LINE__ ) ;
148  }
149 
150  _started = true ;
151 
152  /* Start the document with the xml default for the version,
153  * encoding ISO 8859-1 and the default for the standalone
154  * declaration. MY_ENCODING defined at top of this file*/
155  rc = xmlTextWriterStartDocument( _writer, NULL, MY_ENCODING, NULL ) ;
156  if( rc < 0 )
157  {
158  cleanup() ;
159  string err = (string)"Error starting xml response document for "
160  + _response_name ;
161  throw BESInternalError( err, __FILE__, __LINE__ ) ;
162  }
163 
164  /* Start an element named "response". Since this is the first element,
165  * this will be the root element of the document */
166  rc = xmlTextWriterStartElementNS( _writer, NULL,
167  BAD_CAST "response",
168  BAD_CAST BES_SCHEMA ) ;
169  if( rc < 0 )
170  {
171  cleanup() ;
172  string err = (string)"Error starting the response element for response "
173  + _response_name ;
174  throw BESInternalError( err, __FILE__, __LINE__ ) ;
175  }
176 
177  /* Add the request id attribute */
178  string reqid = dhi.data[REQUEST_ID] ;
179  if( !reqid.empty() )
180  {
181  rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST REQUEST_ID,
182  BAD_CAST reqid.c_str() ) ;
183  if( rc < 0 )
184  {
185  cleanup() ;
186  string err = (string)"Error adding attribute " + REQUEST_ID
187  + " for response " + _response_name ;
188  throw BESInternalError( err, __FILE__, __LINE__ ) ;
189  }
190  }
191 
192  /* Start an element for the specific response. */
193  rc = xmlTextWriterStartElement( _writer, BAD_CAST _response_name.c_str() ) ;
194  if( rc < 0 )
195  {
196  cleanup() ;
197  string err = (string)"Error creating root element for response "
198  + _response_name ;
199  throw BESInternalError( err, __FILE__, __LINE__ ) ;
200  }
201 }
202 
210 void
212 {
213  BESInfo::end_response() ;
214 
215  int rc = 0 ;
216 
217  // this should end the response element
218  rc = xmlTextWriterEndElement( _writer ) ;
219  if( rc < 0 )
220  {
221  cleanup() ;
222  string err = (string)"Error ending response element for response "
223  + _response_name ;
224  throw BESInternalError( err, __FILE__, __LINE__ ) ;
225  }
226 
227  // this should end the specific response element, like showVersion
228  rc = xmlTextWriterEndElement( _writer ) ;
229  if( rc < 0 )
230  {
231  cleanup() ;
232  string err = (string)"Error ending specific response element "
233  + "for response " + _response_name ;
234  throw BESInternalError( err, __FILE__, __LINE__ ) ;
235  }
236 
237  rc = xmlTextWriterEndDocument( _writer ) ;
238  if( rc < 0 )
239  {
240  cleanup() ;
241  string err = (string)"Error ending the response document for response "
242  + _response_name ;
243  throw BESInternalError( err, __FILE__, __LINE__ ) ;
244  }
245 
246  // must call this before getting the buffer content
247  xmlFreeTextWriter( _writer ) ;
248  _writer = 0 ;
249 
250  // get the xml document as a string and return
251  if( !_doc_buf->content )
252  {
253  cleanup() ;
254  string err = (string)"Error retrieving response document as string "
255  + "for response " + _response_name ;
256  throw BESInternalError( err, __FILE__, __LINE__ ) ;
257  }
258  else
259  {
260  _doc = (char *)_doc_buf->content ;
261  }
262 
263  _ended = true ;
264 
265  cleanup() ;
266 }
267 
274 void
275 BESXMLInfo::add_tag( const string &tag_name,
276  const string &tag_data,
277  map<string,string> *attrs )
278 {
279  /* Start an element named tag_name. */
280  int rc = xmlTextWriterStartElement( _writer, BAD_CAST tag_name.c_str() ) ;
281  if( rc < 0 )
282  {
283  cleanup() ;
284  string err = (string)"Error starting element " + tag_name
285  + " for response " + _response_name ;
286  throw BESInternalError( err, __FILE__, __LINE__ ) ;
287  }
288 
289  if( attrs )
290  {
291  map<string,string>::const_iterator i = attrs->begin() ;
292  map<string,string>::const_iterator e = attrs->end() ;
293  for( ; i != e; i++ )
294  {
295  string name = (*i).first ;
296  string val = (*i).second ;
297 
298  // FIXME: is there one with no value?
299  /* Add the attributes */
300  rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST name.c_str(),
301  BAD_CAST val.c_str() ) ;
302  if( rc < 0 )
303  {
304  cleanup() ;
305  string err = (string)"Error adding attribute " + name
306  + " for response " + _response_name ;
307  throw BESInternalError( err, __FILE__, __LINE__ ) ;
308  }
309  }
310  }
311 
312  /* Write the value of the element */
313  if( !tag_data.empty() )
314  {
315  rc = xmlTextWriterWriteString( _writer, BAD_CAST tag_data.c_str() ) ;
316  if( rc < 0 )
317  {
318  cleanup() ;
319  string err = (string)"Error writing the value for element "
320  + tag_name + " for response " + _response_name ;
321  throw BESInternalError( err, __FILE__, __LINE__ ) ;
322  }
323  }
324 
325  // this should end the tag_name element
326  rc = xmlTextWriterEndElement( _writer ) ;
327  if( rc < 0 )
328  {
329  cleanup() ;
330  string err = (string)"Error ending element " + tag_name
331  + " for response " + _response_name ;
332  throw BESInternalError( err, __FILE__, __LINE__ ) ;
333  }
334 }
335 
341 void
342 BESXMLInfo::begin_tag( const string &tag_name,
343  map<string,string> *attrs )
344 {
345  begin_tag( tag_name, "", "", attrs ) ;
346 }
347 
355 void
356 BESXMLInfo::begin_tag( const string &tag_name,
357  const string &ns,
358  const string &uri,
359  map<string,string> *attrs )
360 {
361  BESInfo::begin_tag( tag_name ) ;
362 
363  /* Start an element named tag_name. */
364  int rc = 0 ;
365  if( ns.empty() && uri.empty() )
366  {
367  rc = xmlTextWriterStartElement( _writer, BAD_CAST tag_name.c_str());
368  if( rc < 0 )
369  {
370  cleanup() ;
371  string err = (string)"Error starting element " + tag_name
372  + " for response " + _response_name ;
373  throw BESInternalError( err, __FILE__, __LINE__ ) ;
374  }
375  }
376  else
377  {
378  const char *cns = NULL ;
379  if( !ns.empty() ) cns = ns.c_str() ;
380  rc = xmlTextWriterStartElementNS( _writer,
381  BAD_CAST cns,
382  BAD_CAST tag_name.c_str(),
383  BAD_CAST uri.c_str() ) ;
384  if( rc < 0 )
385  {
386  cleanup() ;
387  string err = (string)"Error starting element " + tag_name
388  + " for response " + _response_name ;
389  throw BESInternalError( err, __FILE__, __LINE__ ) ;
390  }
391  }
392 
393  if( attrs )
394  {
395  map<string,string>::const_iterator i = attrs->begin() ;
396  map<string,string>::const_iterator e = attrs->end() ;
397  for( ; i != e; i++ )
398  {
399  string name = (*i).first ;
400  string val = (*i).second ;
401 
402  /* Add the attributes */
403  rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST name.c_str(),
404  BAD_CAST val.c_str() ) ;
405  if( rc < 0 )
406  {
407  cleanup() ;
408  string err = (string)"Error adding attribute " + name
409  + " for response " + _response_name ;
410  throw BESInternalError( err, __FILE__, __LINE__ ) ;
411  }
412  }
413  }
414 }
415 
422 void
423 BESXMLInfo::end_tag( const string &tag_name )
424 {
425  BESInfo::end_tag( tag_name ) ;
426 
427  int rc = 0 ;
428 
429  string s = ((ostringstream *)_strm)->str() ;
430  if( !s.empty() )
431  {
432  /* Write the value of the element */
433  rc = xmlTextWriterWriteString( _writer, BAD_CAST s.c_str() ) ;
434  if( rc < 0 )
435  {
436  cleanup() ;
437  string err = (string)"Error writing the value for element "
438  + tag_name + " for response " + _response_name ;
439  throw BESInternalError( err, __FILE__, __LINE__ ) ;
440  }
441 
442  ((ostringstream *)_strm)->str( "" ) ;
443  }
444 
445  // this should end the tag_name element
446  rc = xmlTextWriterEndElement( _writer ) ;
447  if( rc < 0 )
448  {
449  cleanup() ;
450  string err = (string)"Error ending element " + tag_name
451  + " for response " + _response_name ;
452  throw BESInternalError( err, __FILE__, __LINE__ ) ;
453  }
454 }
455 
460 void
461 BESXMLInfo::add_space( unsigned long num_spaces )
462 {
463  string to_add ;
464  for( unsigned long i = 0; i < num_spaces; i++ )
465  {
466  to_add += " " ;
467  }
468  BESInfo::add_data( to_add ) ;
469 }
470 
475 void
476 BESXMLInfo::add_break( unsigned long num_breaks )
477 {
478  string to_add ;
479  for( unsigned long i = 0; i < num_breaks; i++ )
480  {
481  to_add += "\n" ;
482  }
483  BESInfo::add_data( to_add ) ;
484 }
485 
486 void
487 BESXMLInfo::add_data( const string &s )
488 {
489  BESInfo::add_data( s ) ;
490 }
491 
500 void
501 BESXMLInfo::add_data_from_file( const string &key, const string &name )
502 {
503  // just add the html file with the <html ... wrapper around it
504  // <html xmlns="http://www.w3.org/1999/xhtml">
505  begin_tag( "html", "", "http://www.w3.org/1999/xhtml" ) ;
506 
507  string newkey = key + ".HTML" ;
508  BESInfo::add_data_from_file( newkey, name ) ;
509 
510  end_tag( "html" ) ;
511 }
512 
521 void
524 {
525  if( _started && !_ended )
526  {
527  end_response() ;
528  }
529  transmitter->send_text( *this, dhi ) ;
530 }
531 
537 void
538 BESXMLInfo::print( ostream &strm )
539 {
540  if( _started && !_ended )
541  {
542  end_response() ;
543  }
544  strm << _doc ;
545 }
546 
554 void
555 BESXMLInfo::dump( ostream &strm ) const
556 {
557  strm << BESIndent::LMarg << "BESXMLInfo::dump - ("
558  << (void *)this << ")" << endl ;
559  BESIndent::Indent() ;
560  BESInfo::dump( strm ) ;
561  BESIndent::UnIndent() ;
562 }
563 
564 BESInfo *
565 BESXMLInfo::BuildXMLInfo( const string &/*info_type*/ )
566 {
567  return new BESXMLInfo( ) ;
568 }
569 
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESXMLInfo.cc:555
virtual void end_response()
end the response
Definition: BESXMLInfo.cc:211
virtual void dump(ostream &strm) const
Displays debug information about this object.
Definition: BESInfo.cc:263
exception thrown if inernal error encountered
virtual void add_data_from_file(const string &key, const string &name)
add data from a file to the informational object.
Definition: BESInfo.cc:170
virtual void add_data_from_file(const string &key, const string &name)
add data from a file to the informational object
Definition: BESXMLInfo.cc:501
virtual void end_tag(const string &tag_name)
end a tagged part of the informational response
Definition: BESXMLInfo.cc:423
virtual void print(ostream &strm)
print the information from this informational object to the specified stream
Definition: BESXMLInfo.cc:538
virtual void begin_response(const string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESXMLInfo.cc:99
BESXMLInfo()
constructs an informational response object as an xml document
Definition: BESXMLInfo.cc:49
informational response object
Definition: BESInfo.h:68
virtual void add_space(unsigned long num_spaces)
add a space to the informational response
Definition: BESXMLInfo.cc:461
virtual void add_break(unsigned long num_breaks)
add a line break to the information
Definition: BESXMLInfo.cc:476
virtual void begin_tag(const string &tag_name, const string &ns, const string &uri, map< string, string > *attrs=0)
begin a tagged part of the information, information to follow
Definition: BESXMLInfo.cc:356
Structure storing information used by the BES to handle the request.
map< string, string > data
the map of string data that will be required for the current request.
virtual void add_tag(const string &tag_name, const string &tag_data, map< string, string > *attrs=0)
add tagged information to the inforamtional response
Definition: BESXMLInfo.cc:275
virtual void begin_response(const string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESInfo.cc:112
virtual void add_data(const string &s)
add data to this informational object. If buffering is not set then the information is output directl...
Definition: BESInfo.cc:148
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the text information as text
Definition: BESXMLInfo.cc:522
virtual void add_data(const string &s)
add data to this informational object. If buffering is not set then the information is output directl...
Definition: BESXMLInfo.cc:487