bes  Updated for version 3.20.6
XDOutput.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of asciival, software which can return an XML data
4 // representation of the data read from a DAP server.
5 
6 // Copyright (c) 2010 OPeNDAP, Inc.
7 // Author: James Gallagher <jgallagher@opendap.org>
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 OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 // Authors:
26 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
27 
28 #include "config.h"
29 
30 #include <libxml/encoding.h>
31 #include <libxml/xmlwriter.h>
32 
33 #include <iostream>
34 #include <sstream>
35 
36 #include <vector>
37 #include <algorithm>
38 #include <iterator>
39 
40 //#define DODS_DEBUG
41 
42 #include <BaseType.h>
43 #include <debug.h>
44 
45 #include <BESInternalFatalError.h>
46 #include <BESDebug.h>
47 
48 #include "XDOutput.h"
49 #include "get_xml_data.h"
50 
51 //using namespace xml_data;
52 using namespace std;
53 using namespace libdap;
54 
55 void XDOutput::start_xml_declaration(XMLWriter *writer, const char *element)
56 {
57  BaseType *btp = dynamic_cast<BaseType *>(this);
58  if (!btp)
59  throw InternalErr(__FILE__, __LINE__, "Expected a BaseType instance");
60 
61  if (xmlTextWriterStartElement(writer->get_writer(),
62  (element != 0) ? (const xmlChar*) element : (const xmlChar*) btp->type_name().c_str()) < 0)
63  throw InternalErr(__FILE__, __LINE__, "Could not write element for " + btp->name());
64 
65  if (xmlTextWriterWriteAttribute(writer->get_writer(), (const xmlChar*) "name",
66  (const xmlChar*) (btp->name().c_str())) < 0)
67  throw InternalErr(__FILE__, __LINE__, "Could not write attribute 'name' for " + btp->name());
68 }
69 
70 void XDOutput::end_xml_declaration(XMLWriter *writer)
71 {
72  BaseType *btp = dynamic_cast<BaseType *>(this);
73  if (!btp)
74  throw InternalErr(__FILE__, __LINE__, "Expected a BaseType instance");
75 
76  if (xmlTextWriterEndElement(writer->get_writer()) < 0)
77  throw InternalErr(__FILE__, __LINE__, "Could not end element for " + btp->name());
78 }
79 
80 void XDOutput::print_xml_data(XMLWriter *writer, bool show_type)
81 {
82  BESDEBUG("xd", "Entering XDOutput::print_xml_data" << endl);
83 
84  BaseType *btp = d_redirect;
85  if (!btp) btp = dynamic_cast<BaseType *>(this);
86  if (!btp) throw BESInternalFatalError("Expected a valid BaseType instance", __FILE__, __LINE__);
87 
88  if (show_type)
89  start_xml_declaration(writer);
90 
91  // Write the element for the value, then the value
92  ostringstream oss;
93  btp->print_val(oss, "", false);
94  BESDEBUG("xd", "XDOutput::print_xml_data, value = '" << oss.str() << "'." << endl);
95  if (xmlTextWriterWriteElement(writer->get_writer(), (const xmlChar*) "value", (const xmlChar*) oss.str().c_str())
96  < 0)
97  throw InternalErr(__FILE__, __LINE__, "Could not write value element for " + btp->name());
98 
99  if (show_type)
100  end_xml_declaration(writer);
101 }
102 
124 bool XDOutput::increment_state(vector<int>*state, const vector<int>&shape)
125 {
126 
127  DBG(cerr << "Entering increment_state" << endl);
128 
129  vector<int>::reverse_iterator state_riter;
130  vector<int>::const_reverse_iterator shape_riter;
131  for (state_riter = state->rbegin(), shape_riter = shape.rbegin(); state_riter < state->rend();
132  state_riter++, shape_riter++) {
133  if (*state_riter == *shape_riter - 1) {
134  *state_riter = 0;
135  }
136  else {
137  *state_riter = *state_riter + 1;
138 #if 0
139  cerr << "New value of state: ";
140  copy(state->begin(), state->end(),
141  ostream_iterator<int>(cerr, ", "));
142  cerr << endl;
143 #endif
144  return true;
145  }
146  }
147 
148  return false;
149 }
BESInternalFatalError
exception thrown if an internal error is found and is fatal to the BES
Definition: BESInternalFatalError.h:43
libdap
Definition: BESDapFunctionResponseCache.h:35
XDOutput::increment_state
virtual bool increment_state(vector< int > *state, const vector< int > &shape)
Definition: XDOutput.cc:124