bes  Updated for version 3.20.6
ScopeStack.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 
30 #include "ScopeStack.h"
31 #include "BESDebug.h"
32 #include "BESInternalError.h"
33 
34 using std::string;
35 using std::endl;
36 using std::vector;
37 
38 namespace ncml_module {
39 /* static */
40 /* enum ScopeType { GLOBAL=0, VARIABLE_ATOMIC, VARIABLE_CONSTRUCTOR, ATTRIBUTE_ATOMIC, ATTRIBUTE_CONTAINER, NUM_SCOPE_TYPES}; */
41 // Make sure these match!
42 const string ScopeStack::Entry::sTypeStrings[NUM_SCOPE_TYPES] = { "<GLOBAL>", "<Variable_Atomic>",
43  "<Variable_Constructor>", "<Attribute_Atomic>", "<Attribute_Container>", };
44 
45 ScopeStack::Entry::Entry(ScopeType theType, const string& theName) :
46  type(theType), name(theName)
47 {
48  if (theType < 0 || theType >= NUM_SCOPE_TYPES) {
49  BESDEBUG("ncml",
50  "ScopeStack::Entry(): Invalid scope type = " << theType << " for scope name=" << theName << endl);
51  throw BESInternalError("Invalid Scope Type!", __FILE__, __LINE__);
52  }
53 }
54 
56 
57 ScopeStack::ScopeStack() :
58  _scope(0)
59 {
60 }
61 
62 ScopeStack::~ScopeStack()
63 {
64  _scope.clear();
65  _scope.resize(0);
66 }
67 
68 void ScopeStack::clear()
69 {
70  _scope.clear();
71 }
72 
73 void ScopeStack::pop()
74 {
75  _scope.pop_back();
76 }
77 
78 const ScopeStack::Entry&
79 ScopeStack::top() const
80 {
81  return _scope.back();
82 }
83 
84 bool ScopeStack::empty() const
85 {
86  return _scope.empty();
87 }
88 
89 int ScopeStack::size() const
90 {
91  return _scope.size();
92 }
93 
94 string ScopeStack::getScopeString() const
95 {
96  string scope("");
97  vector<Entry>::const_iterator iter;
98  for (iter = _scope.begin(); iter != _scope.end(); iter++) {
99  if (iter != _scope.begin()) {
100  scope.append("."); // append scoping operator if not first entry
101  }
102  scope.append((*iter).name);
103  }
104  return scope;
105 }
106 
107 string ScopeStack::getTypedScopeString() const
108 {
109  string scope("");
110  vector<Entry>::const_iterator iter;
111  for (iter = _scope.begin(); iter != _scope.end(); iter++) {
112  if (iter != _scope.begin()) {
113  scope.append("."); // append scoping operator if not first entry
114  }
115  scope.append((*iter).getTypedName());
116  }
117  return scope;
118 }
119 
120 bool ScopeStack::isCurrentScope(ScopeType type) const
121 {
122  if (_scope.empty() && type == GLOBAL) {
123  return true;
124  }
125  else if (_scope.empty()) {
126  return false;
127  }
128  else {
129  return (_scope.back().type == type);
130  }
131 }
132 
134 
135 void ScopeStack::push(const Entry& entry)
136 {
137  if (entry.type == GLOBAL) {
138  BESDEBUG("ncml", "Logic error: can't push a GLOBAL scope type, ignoring." << endl);
139  }
140  else {
141  _scope.push_back(entry);
142  }
143 }
144 }
ncml_module::ScopeStack::ScopeType
ScopeType
Definition: ScopeStack.h:60
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
ncml_module
NcML Parser for adding/modifying/removing metadata (attributes) to existing local datasets using NcML...
Definition: AggregationElement.cc:72