SourceForge.net Logo
ContextHelpers.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2008
3  * DecisionSoft Limited. All rights reserved.
4  * Copyright (c) 2004-2008
5  * Oracle. All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * $Id$
20  */
21 
22 #ifndef CONTEXTHELPERS_HPP
23 #define CONTEXTHELPERS_HPP
24 
25 #include <xqilla/framework/XQillaExport.hpp>
27 
28 class XQILLA_API AutoNodeSetOrderingReset
29 {
30 public:
32  {
33  context_ = context;
34  if(context_) {
35  ordering_ = context->getNodeSetOrdering();
36  context->setNodeSetOrdering(ordering);
37  }
38  }
39 
41  {
42  if(context_) {
43  context_->setNodeSetOrdering(ordering_);
44  }
45  }
46 
47 protected:
50 };
51 
52 class XQILLA_API AutoContextItemTypeReset
53 {
54 public:
56  {
57  context_ = context;
58  if(context_) {
59  sType_ = context->getContextItemType();
60  }
61  }
62 
64  {
65  context_ = context;
66  if(context_) {
67  sType_ = context->getContextItemType();
68  context->setContextItemType(sType);
69  }
70  }
71 
73  {
74  if(context_) {
75  context_->setContextItemType(sType_);
76  }
77  }
78 
79 protected:
82 };
83 
84 class XQILLA_API AutoNsScopeReset
85 {
86 public:
87  AutoNsScopeReset(StaticContext* context, XERCES_CPP_NAMESPACE_QUALIFIER DOMXPathNSResolver* newResolver)
88  {
89  context_=context;
90  if(context_) {
91  _oldNSResolver=context_->getNSResolver();
92  _defaultElementAndTypeNS=context->getDefaultElementAndTypeNS();
93  context_->setNSResolver(newResolver);
94  }
95  }
96 
98  {
99  if(context_) {
100  context_->setNSResolver(_oldNSResolver);
101  context_->setDefaultElementAndTypeNS(_defaultElementAndTypeNS);
102  }
103  }
104 
105 protected:
107  const XERCES_CPP_NAMESPACE_QUALIFIER DOMXPathNSResolver* _oldNSResolver;
109 
110 };
111 
112 class XQILLA_API AutoContextInfoReset
113 {
114 public:
116  : oldContextItem(context->getContextItem()),
117  oldContextPosition(context->getContextPosition()),
118  oldContextSize(context->getContextSize()),
119  context_(context)
120  {
121  }
122 
123  AutoContextInfoReset(DynamicContext *context, const Item::Ptr &contextItem, size_t contextPosition = 0, size_t contextSize = 0)
124  : oldContextItem(context->getContextItem()),
125  oldContextPosition(context->getContextPosition()),
126  oldContextSize(context->getContextSize()),
127  context_(context)
128  {
129  context->setContextItem(contextItem);
130  context->setContextPosition(contextPosition);
131  context->setContextSize(contextSize);
132  }
133 
135  {
136  resetContextInfo();
137  }
138 
139  void resetContextInfo()
140  {
141  context_->setContextItem(oldContextItem);
142  context_->setContextPosition(oldContextPosition);
143  context_->setContextSize(oldContextSize);
144  }
145 
149 
150 private:
151  DynamicContext* context_;
152 };
153 
154 class XQILLA_API AutoDocumentCacheReset
155 {
156 public:
158  : oldDC(const_cast<DocumentCache*>(context->getDocumentCache())),
159  context_ (context)
160  {
161  }
162 
164  {
165  context_->setDocumentCache(oldDC);
166  }
167 
169 
170 protected:
172 };
173 
174 class XQILLA_API AutoVariableStoreReset
175 {
176 public:
178  {
179  context_ = context;
180  _oldVarStore = context_->getVariableStore();
181  if(store)
182  context_->setVariableStore(store);
183  }
184 
186  {
187  context_->setVariableStore(_oldVarStore);
188  }
189 
190  void reset()
191  {
192  context_->setVariableStore(_oldVarStore);
193  }
194 
195 protected:
198 };
199 
200 class XQILLA_API AutoRegexGroupStoreReset
201 {
202 public:
204  {
205  context_ = context;
206  _oldRegexStore = context_->getRegexGroupStore();
207  if(store)
208  context_->setRegexGroupStore(store);
209  }
210 
212  {
213  context_->setRegexGroupStore(_oldRegexStore);
214  }
215 
216  void reset()
217  {
218  context_->setRegexGroupStore(_oldRegexStore);
219  }
220 
221 protected:
224 };
225 
226 class XQILLA_API AutoMessageListenerReset
227 {
228 public:
230  {
231  context_ = context;
232  if(context_) {
233  listener_ = context->getMessageListener();
234  context->setMessageListener(listener);
235  }
236  }
237 
239  {
240  if(context_) {
241  context_->setMessageListener(listener_);
242  }
243  }
244 
245 protected:
248 };
249 
250 class XQILLA_API AutoStackFrameReset
251 {
252 public:
254  {
255  context_ = context;
256  _oldFrame = context_->getStackFrame();
257  context_->setStackFrame(frame);
258  }
259 
261  {
262  context_->setStackFrame(_oldFrame);
263  }
264 
265  void reset()
266  {
267  context_->setStackFrame(_oldFrame);
268  }
269 
270 protected:
273 };
274 
275 template<typename T> class XQILLA_API AutoReset
276 {
277 public:
278  AutoReset(T &orig)
279  : orig_(orig)
280  {
281  old_ = orig;
282  }
283 
285  {
286  reset();
287  }
288 
289  void reset()
290  {
291  orig_ = old_;
292  }
293 
294 protected:
295  T &orig_;
296  T old_;
297 };
298 
299 #endif