bes  Updated for version 3.20.6
VariableAggElement.cc
1 // This file is part of the "NcML Module" project, a BES module designed
3 // to allow NcML files to be used to be used as a wrapper to add
4 // AIS to existing datasets of any format.
5 //
6 // Copyright (c) 2009 OPeNDAP, Inc.
7 // Author: Michael Johnson <m.johnson@opendap.org>
8 //
9 // For more information, please also see the main website: http://opendap.org/
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // Please see the files COPYING and COPYRIGHT for more information on the GLPL.
26 //
27 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
29 #include "VariableAggElement.h"
30 #include "AggregationElement.h"
31 #include "NCMLDebug.h"
32 #include "NCMLParser.h"
33 #include "NCMLUtil.h"
34 
35 namespace ncml_module {
36 const string VariableAggElement::_sTypeName = "variableAgg";
37 const vector<string> VariableAggElement::_sValidAttributes = getValidAttributes();
38 
39 VariableAggElement::VariableAggElement() :
40  RCObjectInterface(), NCMLElement(0), _name("")
41 {
42 }
43 
44 VariableAggElement::VariableAggElement(const VariableAggElement& proto) :
45  RCObjectInterface(), NCMLElement(proto), _name(proto._name)
46 {
47 }
48 
49 VariableAggElement::~VariableAggElement()
50 {
51  _name.clear();
52 }
53 
54 const string&
55 VariableAggElement::getTypeName() const
56 {
57  return _sTypeName;
58 }
59 
61 VariableAggElement::clone() const
62 {
63  return new VariableAggElement(*this);
64 }
65 
66 void VariableAggElement::setAttributes(const XMLAttributeMap& attrs)
67 {
68  validateAttributes(attrs, _sValidAttributes);
69  _name = attrs.getValueForLocalNameOrDefault("name", "");
70 }
71 
72 void VariableAggElement::handleBegin()
73 {
74  VALID_PTR(_parser);
75 
76  // Make sure the name is not empty or this is uselss.
77  if (_name.empty()) {
78  THROW_NCML_PARSE_ERROR(_parser->getParseLineNumber(),
79  "Cannot have variableAgg@name empty! Scope=" + _parser->getScopeString());
80  }
81 
82  // Also make sure we are the direct child of an aggregation or it's an error as well!
83  if (!_parser->isScopeAggregation()) {
84  THROW_NCML_PARSE_ERROR(_parser->getParseLineNumber(),
85  "Got a variableAgg element not as a direct child of an aggregation! elt=" + toString() + " at scope="
86  + _parser->getScopeString());
87  }
88 
89  AggregationElement& parentAgg = getParentAggregation();
90  parentAgg.addAggregationVariable(_name);
91  parentAgg.setVariableAggElement(); // let the agg know we're adding to it.
92 }
93 
94 void VariableAggElement::handleEnd()
95 {
96 }
97 
98 string VariableAggElement::toString() const
99 {
100  return (string("<") + _sTypeName + printAttributeIfNotEmpty("name", _name) + "/>");
101 }
102 
104 VariableAggElement::getParentAggregation() const
105 {
106  AggregationElement* pAgg = dynamic_cast<AggregationElement*>(_parser->getCurrentElement());
107  NCML_ASSERT_MSG(pAgg, "VariableAggElement::getParentAggregation(): "
108  "Expected current top of stack was AggregationElement*, but it wasn't! Logic error!");
109  return *pAgg;
110 }
111 
113 
114 vector<string> VariableAggElement::getValidAttributes()
115 {
116  vector<string> validAttrs;
117  validAttrs.reserve(1);
118  validAttrs.push_back("name");
119  return validAttrs;
120 }
121 }
void addAggregationVariable(const string &name)
Element for the <variableAgg> element child of an <aggregation>.
const std::string getValueForLocalNameOrDefault(const std::string &localname, const std::string &defVal="") const
Definition: XMLHelpers.cc:181
NcML Parser for adding/modifying/removing metadata (attributes) to existing local datasets using NcML...