OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
RemoveElement.cc
Go to the documentation of this file.
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 "RemoveElement.h"
30 #include "NCMLDebug.h"
31 #include "NCMLParser.h"
32 #include "NCMLUtil.h"
33 
34 namespace ncml_module
35 {
36  // The element name
37  const string RemoveElement::_sTypeName = "remove";
38  const vector<string> RemoveElement::_sValidAttributes = getValidAttributes();
39 
41  : RCObjectInterface()
42  , NCMLElement(0)
43  , _name("")
44  , _type("")
45  {
46  }
47 
49  : RCObjectInterface()
50  , NCMLElement(proto)
51  {
52  _name = proto._name;
53  _type = proto._type;
54  }
55 
57  {
58  }
59 
60  const string&
62  {
63  return _sTypeName;
64  }
65 
68  {
69  // We rely on the copy ctor here, so make sure it's valid
70  RemoveElement* newElt = new RemoveElement(*this);
71  return newElt;
72  }
73 
74  void
76  {
78 
79  _name = attrs.getValueForLocalNameOrDefault("name");
80  _type = attrs.getValueForLocalNameOrDefault("type");
81 
82  // We do other validation on the actual values later, so no need here.
83  }
84 
85  void
87  {
89  processRemove(*_parser);
90  }
91 
92  void
93  RemoveElement::handleContent(const string& content)
94  {
95  if (!NCMLUtil::isAllWhitespace(content))
96  {
98  "Got non-whitespace for element content and didn't expect it. "
99  "Element=" + toString() + " content=\"" +
100  content + "\"");
101  }
102  }
103 
104  void
106  {
107  }
108 
109  string
111  {
112  return "<" + _sTypeName + " " + "name=\"" + _name + "\" type=\"" + _type + "\" >";
113  }
114 
117 
118  void
119  RemoveElement::processRemove(NCMLParser& p)
120  {
121  if ( !(_type.empty() || _type == "attribute" || _type == "variable") )
122  {
124  "Illegal type in remove element: type=" + _type +
125  " This version of the parser can only remove type=\"attribute\" or type=\"variable\".");
126  }
127 
128  if (_type == "attribute")
129  {
130  processRemoveAttribute(p);
131  }
132  else if (_type == "variable")
133  {
134  processRemoveVariable(p);
135  }
136  else
137  {
138  THROW_NCML_INTERNAL_ERROR(toString() + " had type that wasn't attribute or variable. We shouldn't be calling this if so.");
139  }
140  }
141 
142  void
143  RemoveElement::processRemoveAttribute(NCMLParser& p)
144  {
145  AttrTable::Attr_iter it;
146  bool gotIt = p.findAttribute(_name, it);
147  if (!gotIt)
148  {
150  "In remove element, could not find attribute to remove name=" + _name +
151  " at the current scope=" + p.getScopeString());
152  }
153 
154  // Nuke it. This call works with containers too, and will recursively delete the children.
155  BESDEBUG("ncml", "Removing attribute name=" << _name << " at scope=" << p.getScopeString() << endl);
156  AttrTable* pTab = p.getCurrentAttrTable();
157  VALID_PTR(pTab);
158  pTab->del_attr(_name);
159  }
160 
161  void
162  RemoveElement::processRemoveVariable(NCMLParser& p)
163  {
164  BESDEBUG("ncml", "Removing variable name=" + _name + " at scope=" + p.getScopeString());
165 
166  // Remove the variable from the current container scope, either the dataset variable list or the current variable container.
167  p.deleteVariableAtCurrentScope(_name);
168  }
169 
170  vector<string>
171  RemoveElement::getValidAttributes()
172  {
173  vector<string> validAttrs;
174  validAttrs.reserve(2);
175  validAttrs.push_back("name");
176  validAttrs.push_back("type");
177  return validAttrs;
178  }
179 
180 } // ncml_module
virtual bool validateAttributes(const XMLAttributeMap &attrs, const vector< string > &validAttrs, vector< string > *pInvalidAttrs=0, bool printInvalid=true, bool throwOnError=true)
Check that the given attributes are all in the valid set, otherwise fill in *pInvalidAttrs with the p...
Definition: NCMLElement.cc:191
virtual RemoveElement * clone() const
Make and return a copy of this.
virtual void setAttributes(const XMLAttributeMap &attrs)
Set the attributes of this from the map.
An abstract superclass for NCMLArray that handles the non-parameterized functionality and allows u...
virtual void handleBegin()
Handle a begin on this element.
const string getValueForLocalNameOrDefault(const string &localname, const string &defVal="") const
If there is an attribute with localname, return its value, else return default.
Definition: XMLHelpers.cc:209
static bool isAllWhitespace(const std::string &str)
Is all the string whitespace as defined by chars in WHITESPACE ?
Definition: NCMLUtil.cc:110
static const vector< string > _sValidAttributes
Definition: RemoveElement.h:60
virtual void handleContent(const std::string &content)
Handle the characters content for the element.
Concrete subclass for the NcML element.
Definition: RemoveElement.h:40
int getParseLineNumber() const
Get the line of the NCML file the parser is currently parsing.
Definition: NCMLParser.cc:222
#define THROW_NCML_PARSE_ERROR(parseLine, msg)
Definition: NCMLDebug.h:69
static const std::string _sTypeName
Definition: RemoveElement.h:59
Base class for NcML element concrete classes.
Definition: NCMLElement.h:64
#define THROW_NCML_INTERNAL_ERROR(msg)
Definition: NCMLDebug.h:61
virtual const std::string & getTypeName() const
Return the type of the element, which should be: the same as ConcreteClassName::getTypeName() ...
#define VALID_PTR(ptr)
Definition: NCMLDebug.h:88
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
virtual void handleEnd()
Handle the closing of this element.
virtual std::string toString() const
Return a string describing the element.