/
DMRpp.cc
118 lines (96 loc) · 4.71 KB
/
DMRpp.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of the BES
// Copyright (c) 2016 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
#include "config.h"
#include <XMLWriter.h>
#include <D4Group.h>
#include <InternalErr.h>
#include "DMRpp.h"
#include "DmrppCommon.h"
using namespace libdap;
namespace dmrpp {
#if 0
/**
* @brief The DMR++ namespace.
*/
const string dmrpp_namespace = "http://xml.opendap.org/dap/dmrpp/1.0.0#";
#endif
/**
* @brief Print the DMR++ response
*
* This is a clone of DMR::print_dap4() modified to include the DMR++ namespace
* and to print the DMR++ XML which is the standard DMR XML with the addition of
* new elements that include information about the 'chunks' that hold data values.
* It uses a static field defined in DmrppCommon to control whether the
* chunk information should be printed. The third argument \arg print_chunks
* will set this static class member to true or false, which controls the
* output of the chunk information. This method resets the field to its previous
* value on exit.
*
* @param xml Writer the XML to this instance of XMLWriter
* @param constrained Should the DMR++ be constrained, in the same sense that the
* DMR::print_dap4() method can print a constrained DMR. False by default.
* @param print_chunks Print the chunks? This parameter sets the DmrppCommon::d_print_chunks
* static field. That field is used by other methods in the Dmrpp<Type> classes to
* control if they print the chunk information. True by default.
*
* @see DmrppArray::print_dap4()
* @see DmrppCommon::print_dmrpp()
*/
void DMRpp::print_dmrpp(XMLWriter &xml, bool constrained, bool print_chunks)
{
bool pc_initial_value = DmrppCommon::d_print_chunks;
DmrppCommon::d_print_chunks = print_chunks;
try {
if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "Dataset") < 0)
throw InternalErr(__FILE__, __LINE__, "Could not write Dataset element");
if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "xmlns",
(const xmlChar*) get_namespace().c_str()) < 0)
throw InternalErr(__FILE__, __LINE__, "Could not write attribute for xmlns");
// The dmrpp namespace
if (DmrppCommon::d_print_chunks)
if (xmlTextWriterWriteAttribute(xml.get_writer(),
(const xmlChar*)string("xmlns:").append(DmrppCommon::d_ns_prefix).c_str(),
(const xmlChar*)DmrppCommon::d_dmrpp_ns.c_str()) < 0)
throw InternalErr(__FILE__, __LINE__, "Could not write attribute for xmlns:dmrpp");
if (!request_xml_base().empty()) {
if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "xml:base",
(const xmlChar*) request_xml_base().c_str()) < 0)
throw InternalErr(__FILE__, __LINE__, "Could not write attribute for xml:base");
}
if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "dapVersion",
(const xmlChar*) dap_version().c_str()) < 0)
throw InternalErr(__FILE__, __LINE__, "Could not write attribute for dapVersion");
if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "dmrVersion",
(const xmlChar*) dmr_version().c_str()) < 0)
throw InternalErr(__FILE__, __LINE__, "Could not write attribute for dapVersion");
if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name", (const xmlChar*) name().c_str()) < 0)
throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
root()->print_dap4(xml, constrained);
if (xmlTextWriterEndElement(xml.get_writer()) < 0)
throw InternalErr(__FILE__, __LINE__, "Could not end the top-level Group element");
}
catch (...) {
DmrppCommon::d_print_chunks = pc_initial_value;
throw;
}
DmrppCommon::d_print_chunks = pc_initial_value;
}
} /* namespace dmrpp */