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