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