bes  Updated for version 3.20.6
XDSequence.cc
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of asciival, software which can return an XML data
5 // representation of the data read from a DAP server.
6 
7 // Copyright (c) 2010 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 // Authors:
27 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
28 
29 // Implementation for the class XDStructure. See XDByte.cc
30 //
31 // 3/12/98 jhrg
32 
33 #include "config.h"
34 
35 #include <iostream>
36 #include <string>
37 
38 #include <BESDebug.h>
39 
40 #include <InternalErr.h>
41 #include <util.h>
42 #include <debug.h>
43 
44 #include "XDSequence.h"
45 #include "XDStructure.h"
46 #include "get_xml_data.h"
47 
48 using std::endl ;
49 using namespace xml_data;
50 using namespace libdap;
51 
52 BaseType *
53 XDSequence::ptr_duplicate()
54 {
55  return new XDSequence(*this);
56 }
57 
58 XDSequence::XDSequence(const string &n) : Sequence(n)
59 {
60 }
61 
62 XDSequence::XDSequence(Sequence * bt)
63  : Sequence( bt->name() ), XDOutput( bt )
64 {
65  // Let's make the alternative structure of XD types now so that we
66  // don't have to do it on the fly.
67  Vars_iter p = bt->var_begin();
68  while (p != bt->var_end()) {
69  BaseType *new_bt = basetype_to_xd(*p);
70  add_var(new_bt);
71  delete new_bt;
72  p++;
73  }
74 
75  BaseType::set_send_p(bt->send_p());
76 }
77 
78 XDSequence::~XDSequence()
79 {
80 }
81 
82 int
83 XDSequence::length() const
84 {
85  return -1;
86 }
87 
88 // This specialization is different from the Sequence version only in that
89 // it tests '(*iter)->send_p()' before incrementing 'i' by
90 // '(*iter)->element_count(true)'.
91 int
92 XDSequence::element_count(bool leaves)
93 {
94  if (!leaves)
95  return d_vars.size();
96  else {
97  int i = 0;
98  for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
99  if ((*iter)->send_p())
100  i += (*iter)->element_count(true);
101  }
102  return i;
103  }
104 }
105 void
106 XDSequence::start_xml_declaration(XMLWriter *writer, const char *element)
107 {
108  XDOutput::start_xml_declaration(writer);
109 
110  for (Vars_iter p = var_begin(); p != var_end(); ++p) {
111  if ((*p)->send_p()) {
112  dynamic_cast<XDOutput&>(**p).start_xml_declaration(writer, element);
113  dynamic_cast<XDOutput&>(**p).end_xml_declaration(writer);
114  }
115  }
116 }
117 
118 void
119 XDSequence::print_xml_data(XMLWriter *writer, bool show_type)
120 {
121  // Forcing the use of the generic version prints just the <Structure>
122  // element w/o the type information of the components. That will be printed
123  // by the embedded print_xml_data calls.
124  if (show_type)
125  XDOutput::start_xml_declaration(writer);
126 
127  Sequence *seq = dynamic_cast<Sequence*>(d_redirect);
128  if (!seq)
129  seq = this;
130 
131  const int rows = seq->number_of_rows() /*- 1*/;
132  const int elements = seq->element_count() /*- 1*/;
133 
134  // For each row of the Sequence...
135  for (int i = 0; i < rows; ++i) {
136  BESDEBUG("yd", "Working on the " << i << "th row" << endl);
137  // Print the row information
138  if (xmlTextWriterStartElement(writer->get_writer(), (const xmlChar*) "row") < 0)
139  throw InternalErr(__FILE__, __LINE__, "Could not write Array element for " + name());
140  if (xmlTextWriterWriteFormatAttribute(writer->get_writer(), (const xmlChar*) "number", "%d", i) < 0)
141  throw InternalErr(__FILE__, __LINE__, "Could not write number attribute for " + name());
142 
143  // For each variable of the row...
144  for (int j = 0; j < elements; ++j) {
145  BESDEBUG("yd", "Working on the " << j << "th field" << endl);
146  BaseType *bt_ptr = seq->var_value(i, j);
147  BaseType *abt_ptr = basetype_to_xd(bt_ptr);
148  dynamic_cast<XDOutput&>(*abt_ptr).print_xml_data(writer, true);
149  BESDEBUG("yd", "Back from print xml data." << endl);
150  // abt_ptr is not stored for future use, so delete it
151  delete abt_ptr;
152  }
153 
154  // Close the row element
155  if (xmlTextWriterEndElement(writer->get_writer()) < 0)
156  throw InternalErr(__FILE__, __LINE__, "Could not end element for " + name());
157  }
158 
159  // End the <Structure> element
160  if (show_type)
161  end_xml_declaration(writer);
162 }
XDSequence
Definition: XDSequence.h:39
libdap
Definition: BESDapFunctionResponseCache.h:35
XDOutput
Definition: XDOutput.h:42