SourceForge.net Logo

ContextHelpers.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001, 2008,
00003  *     DecisionSoft Limited. All rights reserved.
00004  * Copyright (c) 2004, 2011,
00005  *     Oracle and/or its affiliates. All rights reserved.
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 
00020 #ifndef CONTEXTHELPERS_HPP
00021 #define CONTEXTHELPERS_HPP
00022 
00023 #include <xqilla/framework/XQillaExport.hpp>
00024 #include <xqilla/context/DynamicContext.hpp>
00025 
00026 class XQILLA_API AutoNodeSetOrderingReset
00027 {
00028 public:
00029   AutoNodeSetOrderingReset(StaticContext* context, StaticContext::NodeSetOrdering ordering = StaticContext::ORDERING_UNORDERED)
00030   {
00031     context_ = context;
00032     if(context_) {
00033       ordering_ = context->getNodeSetOrdering();
00034       context->setNodeSetOrdering(ordering);
00035     }
00036   }
00037 
00038   ~AutoNodeSetOrderingReset()
00039   {
00040     if(context_) {
00041       context_->setNodeSetOrdering(ordering_);
00042     }
00043   }
00044 
00045 protected:
00046   StaticContext* context_;
00047   StaticContext::NodeSetOrdering ordering_;  
00048 };
00049 
00050 class XQILLA_API AutoContextItemTypeReset
00051 {
00052 public:
00053   AutoContextItemTypeReset(StaticContext* context)
00054   {
00055     context_ = context;
00056     if(context_) {
00057       sType_ = context->getContextItemType();
00058     }
00059   }
00060 
00061   AutoContextItemTypeReset(StaticContext* context, const StaticType &sType)
00062   {
00063     context_ = context;
00064     if(context_) {
00065       sType_ = context->getContextItemType();
00066       context->setContextItemType(sType);
00067     }
00068   }
00069 
00070   ~AutoContextItemTypeReset()
00071   {
00072     if(context_) {
00073       context_->setContextItemType(sType_);
00074     }
00075   }
00076 
00077 protected:
00078   StaticContext* context_;
00079   StaticType sType_;
00080 };
00081 
00082 class XQILLA_API AutoNsScopeReset
00083 {
00084 public:
00085   AutoNsScopeReset(StaticContext* context, XERCES_CPP_NAMESPACE_QUALIFIER DOMXPathNSResolver* newResolver)
00086   {
00087     context_=context;
00088     if(context_) {
00089       _oldNSResolver=context_->getNSResolver();
00090       _defaultElementAndTypeNS=context->getDefaultElementAndTypeNS();
00091       context_->setNSResolver(newResolver);
00092     }
00093   }
00094 
00095   ~AutoNsScopeReset()
00096   {
00097     if(context_) {
00098       context_->setNSResolver(_oldNSResolver);
00099       context_->setDefaultElementAndTypeNS(_defaultElementAndTypeNS);
00100     }
00101   }
00102 
00103 protected:
00104   StaticContext* context_;
00105   const XERCES_CPP_NAMESPACE_QUALIFIER DOMXPathNSResolver* _oldNSResolver;
00106   const XMLCh *_defaultElementAndTypeNS;
00107   
00108 };
00109 
00110 class XQILLA_API AutoContextInfoReset
00111 {
00112 public:
00113   AutoContextInfoReset(DynamicContext *context)
00114     : oldContextItem(context->getContextItem()),
00115       oldContextPosition(context->getContextPosition()),
00116       oldContextSize(context->getContextSize()),
00117       context_(context)
00118   {
00119   }
00120 
00121   AutoContextInfoReset(DynamicContext *context, const Item::Ptr &contextItem, size_t contextPosition = 0, size_t contextSize = 0)
00122     : oldContextItem(context->getContextItem()),
00123       oldContextPosition(context->getContextPosition()),
00124       oldContextSize(context->getContextSize()),
00125       context_(context)
00126   {
00127     context->setContextItem(contextItem);
00128     context->setContextPosition(contextPosition);
00129     context->setContextSize(contextSize);
00130   }
00131 
00132   ~AutoContextInfoReset()
00133   {
00134     resetContextInfo();
00135   }
00136 
00137   void resetContextInfo()
00138   {
00139     context_->setContextItem(oldContextItem);
00140     context_->setContextPosition(oldContextPosition);
00141     context_->setContextSize(oldContextSize);
00142   }
00143 
00144   Item::Ptr oldContextItem;
00145   size_t oldContextPosition;
00146   size_t oldContextSize;
00147 
00148 private:
00149   DynamicContext* context_;
00150 };
00151 
00152 class XQILLA_API AutoDocumentCacheReset
00153 {
00154 public:
00155   AutoDocumentCacheReset(DynamicContext* context)
00156     : oldDC(const_cast<DocumentCache*>(context->getDocumentCache())),
00157       context_ (context)
00158   {
00159   }
00160 
00161   ~AutoDocumentCacheReset()
00162   {
00163     context_->setDocumentCache(oldDC);
00164   }
00165 
00166   DocumentCache *oldDC;
00167 
00168 protected:
00169   DynamicContext* context_;
00170 };
00171 
00172 class XQILLA_API AutoVariableStoreReset
00173 {
00174 public:
00175   AutoVariableStoreReset(DynamicContext *context, const VariableStore *store = 0)
00176   {
00177     context_ = context;
00178     _oldVarStore = context_->getVariableStore();
00179     if(store)
00180       context_->setVariableStore(store);
00181   }
00182 
00183   ~AutoVariableStoreReset()
00184   {
00185     context_->setVariableStore(_oldVarStore);
00186   }
00187 
00188   void reset()
00189   {
00190     context_->setVariableStore(_oldVarStore);
00191   }
00192 
00193 protected:
00194   DynamicContext *context_;
00195   const VariableStore *_oldVarStore;
00196 };
00197 
00198 class XQILLA_API AutoRegexGroupStoreReset
00199 {
00200 public:
00201   AutoRegexGroupStoreReset(DynamicContext *context, const RegexGroupStore *store = 0)
00202   {
00203     context_ = context;
00204     _oldRegexStore = context_->getRegexGroupStore();
00205     if(store)
00206       context_->setRegexGroupStore(store);
00207   }
00208 
00209   ~AutoRegexGroupStoreReset()
00210   {
00211     context_->setRegexGroupStore(_oldRegexStore);
00212   }
00213 
00214   void reset()
00215   {
00216     context_->setRegexGroupStore(_oldRegexStore);
00217   }
00218 
00219 protected:
00220   DynamicContext *context_;
00221   const RegexGroupStore *_oldRegexStore;
00222 };
00223 
00224 class XQILLA_API AutoMessageListenerReset
00225 {
00226 public:
00227   AutoMessageListenerReset(StaticContext* context, MessageListener *listener = 0)
00228   {
00229     context_ = context;
00230     if(context_) {
00231       listener_ = context->getMessageListener();
00232       context->setMessageListener(listener);
00233     }
00234   }
00235 
00236   ~AutoMessageListenerReset()
00237   {
00238     if(context_) {
00239       context_->setMessageListener(listener_);
00240     }
00241   }
00242 
00243 protected:
00244   StaticContext* context_;
00245   MessageListener *listener_;  
00246 };
00247 
00248 class XQILLA_API AutoStackFrameReset
00249 {
00250 public:
00251   AutoStackFrameReset(DynamicContext *context, const StackFrame *frame)
00252   {
00253     context_ = context;
00254     _oldFrame = context_->getStackFrame();
00255     context_->setStackFrame(frame);
00256   }
00257 
00258   ~AutoStackFrameReset()
00259   {
00260     context_->setStackFrame(_oldFrame);
00261   }
00262 
00263   void reset()
00264   {
00265     context_->setStackFrame(_oldFrame);
00266   }
00267 
00268 protected:
00269   DynamicContext *context_;
00270   const StackFrame *_oldFrame;
00271 };
00272 
00273 template<typename T> class XQILLA_API AutoReset
00274 {
00275 public:
00276   AutoReset(T &orig)
00277     : orig_(orig)
00278   {
00279     old_ = orig;
00280   }
00281 
00282   ~AutoReset()
00283   {
00284     reset();
00285   }
00286 
00287   void reset()
00288   {
00289     orig_ = old_;
00290   }
00291 
00292 protected:
00293   T &orig_;
00294   T old_;
00295 };
00296 
00297 #endif