bes  Updated for version 3.20.10
DMRpp.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of the BES
4 
5 // Copyright (c) 2016 OPeNDAP, Inc.
6 // Author: James Gallagher <jgallagher@opendap.org>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 //
22 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
23 
24 #include "config.h"
25 
26 #include <libdap/DDS.h>
27 #include <libdap/XMLWriter.h>
28 #include <libdap/D4Group.h>
29 #include <libdap/D4BaseTypeFactory.h>
30 #include <libdap/InternalErr.h>
31 
32 #include "DMRpp.h"
33 #include "DmrppCommon.h"
34 #include "DmrppTypeFactory.h"
35 
36 using namespace libdap;
37 
38 namespace dmrpp {
39 
40 DMRpp::DMRpp(DmrppTypeFactory *factory, const std::string &name) : DMR(factory, name)
41 {
42 }
43 
71 void DMRpp::print_dmrpp(XMLWriter &xml, const string &href, bool constrained, bool print_chunks)
72 {
73  bool pc_initial_value = DmrppCommon::d_print_chunks;
74  DmrppCommon::d_print_chunks = print_chunks;
75 
76  try {
77  if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "Dataset") < 0)
78  throw InternalErr(__FILE__, __LINE__, "Could not write Dataset element");
79 
80  if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "xmlns",
81  (const xmlChar*) get_namespace().c_str()) < 0)
82  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for xmlns");
83 
84  // The dmrpp namespace
85  if (DmrppCommon::d_print_chunks)
86  if (xmlTextWriterWriteAttribute(xml.get_writer(),
87  (const xmlChar*)string("xmlns:").append(DmrppCommon::d_ns_prefix).c_str(),
88  (const xmlChar*)DmrppCommon::d_dmrpp_ns.c_str()) < 0)
89  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for xmlns:dmrpp");
90 
91  if (!request_xml_base().empty()) {
92  if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "xml:base",
93  (const xmlChar*) request_xml_base().c_str()) < 0)
94  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for xml:base");
95  }
96 
97  if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "dapVersion",
98  (const xmlChar*) dap_version().c_str()) < 0)
99  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for dapVersion");
100 
101  if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "dmrVersion",
102  (const xmlChar*) dmr_version().c_str()) < 0)
103  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for dapVersion");
104 
105  if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name", (const xmlChar*) name().c_str()) < 0)
106  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
107 
108  // cerr << "DMRpp::" <<__func__ << "() href: "<< href << endl;
109  if (!href.empty())
110  if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*)string(DmrppCommon::d_ns_prefix).append(":href").c_str(),
111  (const xmlChar*) href.c_str()) < 0)
112  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for href");
113 
114  if (!get_version().empty())
115  if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*)string(DmrppCommon::d_ns_prefix).append(":version").c_str(),
116  (const xmlChar*) get_version().c_str()) < 0)
117  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for version");
118 
119 
120  root()->print_dap4(xml, constrained);
121 
122  if (xmlTextWriterEndElement(xml.get_writer()) < 0)
123  throw InternalErr(__FILE__, __LINE__, "Could not end the top-level Group element");
124  }
125  catch (...) {
126  DmrppCommon::d_print_chunks = pc_initial_value;
127  throw;
128  }
129 
130  DmrppCommon::d_print_chunks = pc_initial_value;
131 }
132 
139 void
140 DMRpp::print_dap4(XMLWriter &xml, bool constrained /* false */)
141 {
142  print_dmrpp(xml, get_href(), constrained, get_print_chunks());
143 }
144 
145 libdap::DDS *DMRpp::getDDS() {
146  DmrppTypeFactory factory;
147  unique_ptr<DDS> dds(new DDS(&factory, name()));
148  dds->filename(filename());
149 
150  // Now copy the global attributes
151  unique_ptr<vector<BaseType *>> top_vars(root()->transform_to_dap2(&(dds->get_attr_table())/*, true*/));
152  for (vector<BaseType *>::iterator i = top_vars->begin(), e = top_vars->end(); i != e; i++) {
153  dds->add_var_nocopy(*i);
154  }
155 
156  dds->set_factory(0);
157  return dds.release();
158 }
159 
160 } /* namespace dmrpp */