bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
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 <sstream>
27#include <vector>
28
29#include <libdap/DDS.h>
30#include <libdap/XMLWriter.h>
31#include <libdap/D4Group.h>
32#include <libdap/D4BaseTypeFactory.h>
33#include <libdap/InternalErr.h>
34
35#include "DMRpp.h"
36#include "DmrppCommon.h"
37#include "DmrppTypeFactory.h"
38
39using namespace libdap;
40using namespace std;
41
42namespace dmrpp {
43
44DMRpp::DMRpp(DmrppTypeFactory *factory, const std::string &name) : DMR(factory, name)
45{
46}
47
74
75void DMRpp::print_dmrpp(XMLWriter &xml, const string &href, bool constrained, bool print_chunks)
76{
77 bool pc_initial_value = DmrppCommon::d_print_chunks;
78 DmrppCommon::d_print_chunks = print_chunks;
79
80 try {
81 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "Dataset") < 0)
82 throw InternalErr(__FILE__, __LINE__, "Could not write Dataset element");
83
84 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "xmlns",
85 (const xmlChar*) get_namespace().c_str()) < 0)
86 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for xmlns");
87
88 // The dmrpp namespace
90 if (xmlTextWriterWriteAttribute(xml.get_writer(),
91 (const xmlChar*)string("xmlns:").append(DmrppCommon::d_ns_prefix).c_str(),
92 (const xmlChar*)DmrppCommon::d_dmrpp_ns.c_str()) < 0)
93 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for xmlns:dmrpp");
94
95 if (!request_xml_base().empty()) {
96 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "xml:base",
97 (const xmlChar*) request_xml_base().c_str()) < 0)
98 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for xml:base");
99 }
100
101 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "dapVersion",
102 (const xmlChar*) dap_version().c_str()) < 0)
103 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for dapVersion");
104
105 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "dmrVersion",
106 (const xmlChar*) dmr_version().c_str()) < 0)
107 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for dapVersion");
108
109 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name", (const xmlChar*) name().c_str()) < 0)
110 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
111
112 // cerr << "DMRpp::" <<__func__ << "() href: "<< href << endl;
113 if (!href.empty())
114 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*)string(DmrppCommon::d_ns_prefix).append(":href").c_str(),
115 (const xmlChar*) href.c_str()) < 0)
116 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for href");
117
118 if (!get_version().empty())
119 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*)string(DmrppCommon::d_ns_prefix).append(":version").c_str(),
120 (const xmlChar*) get_version().c_str()) < 0)
121 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for version");
122
123 root()->print_dap4(xml, constrained);
124
125 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
126 throw InternalErr(__FILE__, __LINE__, "Could not end the top-level Group element");
127 }
128 catch (...) {
129 DmrppCommon::d_print_chunks = pc_initial_value;
130 throw;
131 }
132
133 DmrppCommon::d_print_chunks = pc_initial_value;
134}
135
142void DMRpp::print_dap4(XMLWriter &xml, bool constrained /* false */)
143{
144 print_dmrpp(xml, get_href(), constrained, get_print_chunks());
145}
146
147libdap::DDS *DMRpp::getDDS()
148{
149 DmrppTypeFactory factory;
150 unique_ptr<DDS> dds(new DDS(&factory, name()));
151 dds->filename(filename());
152
153 // Now copy the global attributes
154 unique_ptr<vector<BaseType *>> top_vars(root()->transform_to_dap2(&(dds->get_attr_table())/*, true*/));
155 for (auto i = top_vars->begin(), e = top_vars->end(); i != e; i++) {
156 dds->add_var_nocopy(*i);
157 }
158
159 dds->set_factory(nullptr);
160 return dds.release();
161}
162
163} /* namespace dmrpp */
virtual void print_dmrpp(libdap::XMLWriter &xml, const std::string &href="", bool constrained=false, bool print_chunks=true)
Print the DMR++ response.
Definition DMRpp.cc:75
void print_dap4(libdap::XMLWriter &xml, bool constrained=false)
override DMR::print_dap4() so the chunk info will print too.
Definition DMRpp.cc:142
static std::string d_ns_prefix
The XML namespace prefix to use.
static bool d_print_chunks
if true, print_dap4() prints chunk elements
static std::string d_dmrpp_ns
The DMR++ XML namespace.