1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 #ifndef ZORBA_CONTEXT_STATIC_CONTEXT_H
18 #define ZORBA_CONTEXT_STATIC_CONTEXT_H
19 
20 #include <deque>
21 #include <map>
22 #include <memory>
23 #include <set>
24 #include <vector>
25 
26 #include <zorba/config.h>
27 #include <zorba/api_shared_types.h>
28 #include <zorba/function.h>
29 #include <zorba/error.h>
30 #include <zorba/diagnostic_list.h>
31 
32 #ifdef WIN32
33 #include "store/api/item.h"
34 #include "compiler/expression/expr_base.h"
35 #include "types/typemanager.h"
36 #endif
37 
38 #include "context/static_context_consts.h"
39 #include "context/decimal_format.h"
40 #include "context/uri_resolver.h"
41 #include "context/features.h"
42 
43 #include "zorbautils/hashmap_zstring.h"
44 #include "zorbautils/hashmap_itemp.h"
45 
46 #include "common/shared_types.h"
47 
48 #include "util/stl_util.h"
49 #include "util/auto_vector.h"
50 
51 
52 namespace zorba
53 {
54 
55 class expr;
56 #ifndef ZORBA_NO_FULL_TEXT
57 class ftmatch_options;
58 #endif /* ZORBA_NO_FULL_TEXT */
59 class var_expr;
60 class function;
61 class XQType;
62 class ExternalModule;
63 class IndexDecl;
64 class QueryLoc;
65 class namespace_node;
66 class user_function;
67 class TypeManager;
68 class XQPCollator;
69 
70 template <class V> class serializable_ItemPointerHashMap;
71 
72 class IndexDecl;
73 typedef rchandle<IndexDecl> IndexDecl_t;
74 
75 class ValueIC;
76 typedef rchandle<ValueIC> ValueIC_t;
77 
78 class StaticallyKnownCollection;
79 typedef rchandle<StaticallyKnownCollection> StaticallyKnownCollection_t;
80 
81 
82 /*******************************************************************************
83   Base Uri Computation
84 
85   thePrologBaseUri is the one declared in the prolog. theApplicationBaseUri
86   is set explicitly from the C++/C api. If both thePrologBaseUri and
87   theApplicationBaseUri are set, thePrologBaseUri hides theApplicationBaseUri.
88 
89   theEncapsulatingEntityUri is currently not used, except in one special case:
90   to make the baseURI property of the file module sctx be the full pathname of
91   the query file (see translator.cpp).
92 
93   theEntityRetrievalUri is set by default to the full pathname of the file
94   containing the query we are running (for the main module) or the url of
95   the module component (for lib modules). It may also be set explicitly
96   from the C++/C api.
97 ********************************************************************************/
98 struct BaseUriInfo : public ::zorba::serialization::SerializeBaseClass
99 {
100   zstring thePrologBaseUri;
101   zstring theApplicationBaseUri;
102   zstring theEncapsulatingEntityUri;
103   zstring theEntityRetrievalUri;
104 
105   zstring theBaseUri;
106 
107   bool    theHavePrologBaseUri;
108   bool    theHaveApplicationBaseUri;
109   bool    theHaveEncapsulatingEntityUri;
110   bool    theHaveEntityRetrievalUri;
111   bool    theHaveBaseUri;
112 
113 public:
114   SERIALIZABLE_CLASS(BaseUriInfo)
115   SERIALIZABLE_CLASS_CONSTRUCTOR(BaseUriInfo)
116   void serialize(::zorba::serialization::Archiver& ar);
117 
118 public:
BaseUriInfoBaseUriInfo119   BaseUriInfo()
120     :
121     theHavePrologBaseUri(false),
122     theHaveApplicationBaseUri(false),
123     theHaveEncapsulatingEntityUri(false),
124     theHaveEntityRetrievalUri(false),
125     theHaveBaseUri(false)
126   {
127   }
128 };
129 
130 
131 /*******************************************************************************
132 
133 ********************************************************************************/
134 struct FunctionInfo : public ::zorba::serialization::SerializeBaseClass
135 {
136   function_t  theFunction;
137   bool        theIsDisabled;
138 
139 public:
140   SERIALIZABLE_CLASS(FunctionInfo)
141   FunctionInfo(::zorba::serialization::Archiver& ar);
142   void serialize(::zorba::serialization::Archiver& ar);
143 
144 public:
145   FunctionInfo();
146 
147   FunctionInfo(const function_t& f, bool disabled = false);
148 
149   ~FunctionInfo();
150 };
151 
152 
153 
154 /*******************************************************************************
155 
156 ********************************************************************************/
157 class VarInfo : public SimpleRCObject
158 {
159 protected:
160   store::Item_t  theName;
161 
162   ulong          theId;
163 
164   int            theKind;
165 
166   xqtref_t       theType;
167 
168   bool           theIsExternal;
169 
170   bool           theHasInitializer;
171 
172   var_expr     * theVarExpr;
173 
174 public:
175   SERIALIZABLE_CLASS(VarInfo)
176   VarInfo(::zorba::serialization::Archiver& ar);
177   void serialize(::zorba::serialization::Archiver& ar);
178 
179 public:
180   VarInfo();
181 
182   VarInfo(var_expr* v);
183 
184   ~VarInfo();
185 
getName()186   const store::Item_t& getName() const { return theName; }
187 
getId()188   ulong getId() const { return theId; }
189 
setId(ulong id)190   void setId(ulong id) { theId = id; }
191 
getKind()192   int getKind() const { return theKind; }
193 
getType()194   const XQType* getType() const { return theType.getp(); }
195 
196   void setType(const xqtref_t& t);
197 
isExternal()198   bool isExternal() const { return theIsExternal; }
199 
setIsExternal(bool v)200   void setIsExternal(bool v) { theIsExternal = v; }
201 
hasInitializer()202   bool hasInitializer() const { return theHasInitializer; }
203 
setHasInitializer(bool v)204   void setHasInitializer(bool v) { theHasInitializer = v; }
205 
getVar()206   var_expr* getVar() const { return theVarExpr; }
207 
clearVar()208   void clearVar() { theVarExpr = NULL; }
209 };
210 
211 
212 typedef rchandle<VarInfo> VarInfo_t;
213 
214 
215 /*******************************************************************************
216 
217 ********************************************************************************/
218 struct PrologOption : public ::zorba::serialization::SerializeBaseClass
219 {
220   store::Item_t  theName;
221   zstring        theValue;
222 
223 public:
224   SERIALIZABLE_CLASS(PrologOption)
225   SERIALIZABLE_CLASS_CONSTRUCTOR(PrologOption)
226   void serialize(::zorba::serialization::Archiver& ar);
227 
228 public:
PrologOptionPrologOption229   PrologOption() {}
230 
PrologOptionPrologOption231   PrologOption(const store::Item* name, const zstring& value)
232     :
233     theName(const_cast<store::Item*>(name)),
234     theValue(const_cast<zstring&>(value))
235   {
236   }
237 };
238 
239 
240 /***************************************************************************//**
241   XQuery 3.0 static context
242 	[http://www.w3.org/TR/2010/WD-xquery-30-20101214/#static_context]
243 
244   Class static_context implements a node in the hierarchy of static contextes.
245   The root of this hierarchy is a global (singleton) sctx that is shared by all
246   queries. This global root sctx is created during engine statrtup and is
247   initialized with the default values specified by the W3C spec. After its
248   creation, it is accessible via the singleton GlobalEnvironment obj (see
249   GENV_ROOT_STATIC_CONTEXT macro in src/system/globalenv.h). We call this the
250   "zorba root sctx".
251 
252   An application can create sctx objs using the Zorba::createStaticContext()
253   API method. The application can then set various components of such an sctx
254   obj using the API methods provided by the StaticContext class (which is a
255   wrapper of the internal static_context class). All application-created sctx
256   objs are created as children of the zorba root sctx.
257 
258   For a particular query, its root sctx may be (a) a child of an application-
259   created sctx obj, or (b) if the query is a load-prolog query, the application
260   created sctx obj itself, or (c) if the app did not provide any sctx, a child
261   of zorba's root sctx. In cases (a) and (b) the application provides its sctx
262   to the query via the Zorba:compileQuery() or the XQuery::compile() API methods.
263   The query root sctx is populated with the declarations found in the prolog of
264   the main module and the declarations that are imported from the modules that
265   the main module imports.
266 
267   Each library module participating in a query has its a root sctx as well,
268   which is a child of the zorba root sctx, or the application provided sctx
269   (if any). (This implies that an application provided sctx is "visible" to
270   all the modules of a query). A module root sctx contains the declarations
271   found in the prolog of the associated module and the declarations that are
272   imported from other modules.
273 
274   Additional sctx objs may be created under the query or module root sctxs. Such
275   expression-level sctx objs provide scoping contextes for variables, namespace
276   bindings, etc. They both inherit and hide information from their ancestor sctxs.
277 
278   Expression-level sctx objs are created and maintained only during the
279   translation of a module; afterwards they are discarded. In contrast, query
280   and module root sctx objs stay around for the whole life of a query (including
281   runtime). Such sctxs are registered in XQueryImpl::theSctxMap.
282 
283 
284   Data Members of Class static_context:
285 
286 
287   theParent :
288   -----------
289   Pointer to the parent sctx object in the sctx hierarchy.
290 
291   theTraceStream :
292   ----------------
293   Output stream that is used by the fn:trace function. std::cerr is the default
294   if the user didn't provide one.
295 
296   theQueryExpr :
297   --------------
298   If this sctx is the root sctx for a mainModule, theQueryExpr is the result
299   of the translation of that mainModule. It is used in implementing the
300   loadProlog api.
301 
302   theModuleNamespace :
303   --------------------
304   If this is the root sctx of a library module, theModuleNamespace stores the
305   target namespace URI of that module.
306 
307   theImportedBuiltinModules :
308   ---------------------------
309   If this is the root sctx of a module, theImportedBuiltinModules stores the
310   namespaces of "builtin" modules that have been imported by this module.
311   Builtin modules are modules that declare user-visible zorba builtin functions
312   (see also ns_consts.h)
313 
314   theBaseUriInfo :
315   ----------------
316   Stores various URIs that participate in the computation of the module's base
317   URI, as described in the W3C spec.
318 
319   theDocResolver :
320   ----------------
321   URI resolver used for retrieving documents (used by fn:doc and fn:doc-available).
322 
323   theColResolver :
324   ----------------
325   URI resolver used for retrieving W3C collections (used by fn:collection).
326 
327   theModulePaths :
328   ----------------
329   Vector of absolute directory pathnames for directories that contain module
330   files and/or schema files and/or library files that contain the implementations
331   of external functions. The Zorba root sctx stores a number of predefined paths.
332   Applications may register additional paths in their own sctx objs. These
333   directory pathnames are used by the StandardModuleURIResolver and the
334   StandardSchemaURIResolver, which implement the full resulution protocol for
335   modules and schemas, respectively.
336 
337   theExternalModulesMap :
338   -----------------------
339 
340   theTypeManager :
341   ---------------
342   If non NULL, then "this" is the root sctx of a module, and theTypeManager stores
343   the schemas that are imported by the associated module (in-scope element
344   declarations, in-scope attribute declarations and in-scope type declarations).
345 
346   theNamespaceBindings :
347   ----------------------
348   A hash map whose entries map qname prefixes to namespace URIs.
349 
350   theDefaultElementNamespace :
351   ----------------------------
352   The namespace URI to be used for element and type qnames whose prefix is empty.
353 
354   theDefaultFunctionNamespace :
355   -----------------------------
356   The namespace URI to be used for function qnames whose prefix is empty.
357 
358   theContextItemType :
359   --------------------
360 
361   theVariablesMap :
362   -----------------
363 
364   theImportedPrivateVariablesMap :
365   --------------------------------
366 
367   theFunctionMap :
368   ----------------
369   An entry of this hash map maps an expanded qname to the function object with
370   that qname. The qname of a function does not fully identify the function; the
371   arity is needed together with the qname for a full id. But, most functions
372   have a single arity, so for better performance, such functions are registered
373   in this map. Functions that have multiple arities, have one of their associated
374   function objs registered here and the rest are stored in theFunctionArityMap.
375 
376   theFunctionArityMap :
377   ---------------------
378   An entry of this hash map maps an expanded qname to a vector of function
379   objects with that qname. The function ojbects in this vector correspond to
380   different versions of a function that all share the same qname but have
381   different arities. One of these versions is stored in the theFunctionMap,
382   and the rest are regisreded in theFunctionArityMap.
383 
384   theCollectionMap :
385   ------------------
386   A hash mash map mapping XQDDF collection qnames to the objs storing the info
387   from the declaration of the associated collections.
388 
389   theW3CCollectionMap :
390   ---------------------
391 
392   theDefaultW3CCollectionType :
393   -----------------------------
394 
395   theIndexMap :
396   -------------
397   A hash mash map mapping XQDDF index qnames to the objs storing the info from
398   the declaration of the associated index.
399 
400   theIndexCallback :
401   ------------------
402 
403   theIndexCallbackData :
404   ----------------------
405 
406   theICMap :
407   ----------
408   A hash mash map mapping XQDDF integrity constraint qnames to the objs storing
409   the info from the declaration of the associated integrity constraint.
410 
411   theDocumentMap :
412   ----------------
413 
414   theCollationMap :
415   -----------------
416 
417   theDefaultCollation :
418   ---------------------
419 
420   theDecimalFormats :
421   -------------------
422   Set of decimal formats. Each decimal format is identified by a qname.
423   theDecimalFormats must not contain two decimal formats with the same qname.
424   If an ancestor sctx contains a decimal format with the same qname as a
425   decimal format in "this" sctx, then the decimal format of the ancestor is
426   hidden by the decimal format in "this". The set of in-scope decimal formats
427   is the union of all decimal foramts in "this" and its ancestors, minus the
428   decimal foramts that are hidden by inner-scope decimal formats.
429 
430 
431   Note: URI resolvers are not serialized if the plan is serialized. Instead,
432   they are set again if the query is loaded. If the user has provided a resolver
433   before, he needs to make sure that the resolver is available using the
434   SerializationCallback class. The latter also yields for ExternalModules and
435   the TraceStream.
436 
437   theFeatures:
438   ------------
439   Feature flags that are used when a particular feature (e.g. scripting
440   or ddl) is enabled. The definition of the features is located in
441   context/featueres.h.
442 ********************************************************************************/
443 
444 class static_context : public SimpleRCObject
445 {
446   ITEM_PTR_HASH_MAP(StaticallyKnownCollection_t, CollectionMap);
447 
448   ITEM_PTR_HASH_MAP(IndexDecl_t, IndexMap);
449 
450   ITEM_PTR_HASH_MAP(ValueIC_t, ICMap);
451 
452   ITEM_PTR_HASH_MAP(VarInfo_t, VariableMap);
453 
454   ITEM_PTR_HASH_MAP(FunctionInfo, FunctionMap);
455 
456   ITEM_PTR_HASH_MAP(std::vector<FunctionInfo>*, FunctionArityMap);
457 
458   ITEM_PTR_HASH_MAP(PrologOption, OptionMap);
459 
460   ZSTRING_HASH_MAP(zstring, NamespaceBindings);
461 
462   ZSTRING_HASH_MAP(xqtref_t, DocumentMap);
463 
464   ZSTRING_HASH_MAP(xqtref_t, W3CCollectionMap);
465 
466   typedef std::map<std::string, XQPCollator*> CollationMap;
467 
468 public:
469 
470   struct ctx_module_t : public ::zorba::serialization::SerializeBaseClass
471   {
472     ExternalModule * module;
473     bool             dyn_loaded_module;
474     static_context * sctx;
475 
476   public:
477     SERIALIZABLE_CLASS(ctx_module_t)
478     SERIALIZABLE_CLASS_CONSTRUCTOR(ctx_module_t)
479     void serialize(::zorba::serialization::Archiver& ar);
480 
ctx_module_tctx_module_t481     ctx_module_t() : module(0), dyn_loaded_module(false) {}
482 
~ctx_module_tctx_module_t483     virtual ~ctx_module_t() {}
484   };
485 
486   ZSTRING_HASH_MAP(ctx_module_t, ExternalModuleMap);
487 
488 public:
489   static const zstring DOT_VAR_NAME;
490   static const zstring DOT_POS_VAR_NAME;
491   static const zstring DOT_SIZE_VAR_NAME;
492 
493 
494   //
495   // W3C namespaces
496   //
497   static const char* W3C_NS_PREFIX; // http://www.w3.org/
498 
499   static const char* W3C_XML_NS;    // http://www.w3.org/XML/1998/namespace
500 
501   static const char* W3C_FN_NS;     // http://www.w3.org/2005/xpath-functions
502 
503   //
504   // Zorba namespaces
505   //
506 
507   static const char* ZORBA_NS_PREFIX; // http://www.zorba-xquery.com/
508 
509   // Namespaces of external modules declaring zorba builtin functions
510   static const char* ZORBA_MATH_FN_NS;
511   static const char* ZORBA_BASE64_FN_NS;
512 
513   static const char* ZORBA_JSON_FN_NS;
514 
515   static const char* ZORBA_NODEREF_FN_NS;
516   static const char* ZORBA_NODEPOS_FN_NS;
517   static const char* ZORBA_STORE_DYNAMIC_COLLECTIONS_DDL_FN_NS;
518   static const char* ZORBA_STORE_DYNAMIC_COLLECTIONS_DML_FN_NS;
519   static const char* ZORBA_STORE_STATIC_COLLECTIONS_DDL_FN_NS;
520   static const char* ZORBA_STORE_STATIC_COLLECTIONS_DML_FN_NS;
521   static const char* ZORBA_STORE_STATIC_INDEXES_DDL_FN_NS;
522   static const char* ZORBA_STORE_STATIC_INDEXES_DML_FN_NS;
523   static const char* ZORBA_STORE_STATIC_INTEGRITY_CONSTRAINTS_DDL_FN_NS;
524   static const char* ZORBA_STORE_STATIC_INTEGRITY_CONSTRAINTS_DML_FN_NS;
525   static const char* ZORBA_STORE_DYNAMIC_DOCUMENTS_FN_NS;
526   static const char* ZORBA_STORE_DYNAMIC_UNORDERED_MAP_FN_NS;
527 
528 #ifdef ZORBA_WITH_JSON
529   static const char* JSONIQ_DM_NS;
530   static const char* JSONIQ_FN_NS;
531 #endif
532 
533   static const char* ZORBA_SCHEMA_FN_NS;
534   static const char* ZORBA_XQDOC_FN_NS;
535   static const char* ZORBA_RANDOM_FN_NS;
536   static const char* ZORBA_INTROSP_SCTX_FN_NS;
537   static const char* ZORBA_REFLECTION_FN_NS;
538   static const char* ZORBA_STRING_FN_NS;
539 
540   static const char* ZORBA_URI_FN_NS;
541 
542   static const char* ZORBA_FETCH_FN_NS;
543   static const char* ZORBA_NODE_FN_NS;
544   static const char* ZORBA_XML_FN_NS;
545 #ifndef ZORBA_NO_FULL_TEXT
546   static const char* ZORBA_FULL_TEXT_FN_NS;
547 #endif /* ZORBA_NO_FULL_TEXT */
548   static const char* ZORBA_XML_FN_OPTIONS_NS;
549 
550   // Namespaces of virtual modules declaring zorba builtin functions
551   static const char* ZORBA_UTIL_FN_NS;
552   static const char* ZORBA_SCRIPTING_FN_NS;
553 
554   // Namespaces of virtual modules declaring internal builtin functions of
555   // XQUERY or zorba. Internal functions are not visible to xquery programs.
556   static const char* XQUERY_OP_NS;
557   static const char* ZORBA_OP_NS;
558 
559   // options-related namepsaces
560   static const char* ZORBA_OPTIONS_NS;
561   static const char* ZORBA_OPTION_WARN_NS;
562   static const char* ZORBA_OPTION_FEATURE_NS;
563   static const char* ZORBA_OPTION_OPTIM_NS;
564   static const char* ZORBA_VERSIONING_NS;
565 
566 protected:
567   static_context                        * theParent;
568 
569   std::ostream                          * theTraceStream;
570 
571   expr*                                  theQueryExpr;
572 
573   std::string                             theModuleNamespace;
574 
575   std::vector<zstring>                    theImportedBuiltinModules;
576 
577   BaseUriInfo                           * theBaseUriInfo;
578 
579   ztd::auto_vector<internal::URIMapper>   theURIMappers;
580 
581   ztd::auto_vector<internal::URLResolver> theURLResolvers;
582 
583   checked_vector<zstring>                 theURIPath;
584 
585   checked_vector<zstring>                 theLibPath;
586 
587   ExternalModuleMap                     * theExternalModulesMap;
588 
589   rchandle<TypeManager>                   theTypeManager;
590 
591   NamespaceBindings                     * theNamespaceBindings;
592 
593   zstring                                 theDefaultElementNamespace;
594   bool                                    theHaveDefaultElementNamespace;
595 
596   zstring                                 theDefaultFunctionNamespace;
597   bool                                    theHaveDefaultFunctionNamespace;
598 
599   xqtref_t                                theContextItemType;
600 
601   VariableMap                           * theVariablesMap;
602 
603   VariableMap                           * theImportedPrivateVariablesMap;
604 
605   FunctionMap                           * theFunctionMap;
606   FunctionArityMap                      * theFunctionArityMap;
607 
608   CollectionMap                         * theCollectionMap;
609 
610   W3CCollectionMap                      * theW3CCollectionMap;
611   xqtref_t                                theDefaultW3CCollectionType;
612 
613   IndexMap                              * theIndexMap;
614 
615   ICMap                                 * theICMap;
616 
617   DocumentMap                           * theDocumentMap;
618 
619   CollationMap                          * theCollationMap;
620   std::string                           * theDefaultCollation;
621   mutable XQPCollator                   * theCachedDefaultCollator;
622 
623   OptionMap                             * theOptionMap;
624 
625   audit::Event                          * theAuditEvent;
626 
627 #ifndef ZORBA_NO_FULL_TEXT
628   ftmatch_options                       * theFTMatchOptions;
629 #endif /* ZORBA_NO_FULL_TEXT */
630 
631   StaticContextConsts::xquery_version_t      theXQueryVersion;
632 
633   StaticContextConsts::xpath_compatibility_t theXPathCompatibility;
634 
635   StaticContextConsts::construction_mode_t   theConstructionMode;
636 
637   bool                                       theInheritNamespaces;
638 
639   bool                                       thePreserveNamespaces;
640 
641   StaticContextConsts::ordering_mode_t       theOrderingMode;
642 
643   StaticContextConsts::empty_order_mode_t    theEmptyOrderMode;
644 
645   StaticContextConsts::boundary_space_mode_t theBoundarySpaceMode;
646 
647   StaticContextConsts::validation_mode_t     theValidationMode;
648 
649   std::vector<DecimalFormat_t>               theDecimalFormats;
650 
651   bool                                       theAllWarningsDisabled;
652 
653   bool                                       theAllWarningsErrors;
654 
655   std::vector<store::Item_t>                 theWarningsAreErrors;
656 
657   std::vector<store::Item_t>                 theDisabledWarnings;
658 
659   uint32_t                                   theFeatures;
660 
661 public:
662   static bool is_builtin_module(const zstring& ns);
663 
664   static bool is_builtin_virtual_module(const zstring& ns);
665 
666   static bool is_non_pure_builtin_module(const zstring& ns);
667 
668   static bool is_reserved_module(const zstring& ns);
669 
670   static zstring var_name(const store::Item*);
671 
672 public:
673   SERIALIZABLE_CLASS(static_context);
674 
675   void serialize_resolvers(serialization::Archiver& ar);
676 
677   void serialize_tracestream(serialization::Archiver& ar);
678 
679   void serialize(serialization::Archiver& ar);
680 
681 public:
682   static_context(::zorba::serialization::Archiver& ar);
683 
684   ~static_context();
685 
get_parent()686   static_context* get_parent() const { return theParent; }
687 
688   static_context* create_child_context();
689 
690   bool is_global_root_sctx() const;
691 
692   expr* get_query_expr() const;
693 
694   void set_query_expr(expr* expr);
695 
696   void set_trace_stream(std::ostream&);
697 
698   std::ostream* get_trace_stream() const;
699 
set_module_namespace(const zstring & ns)700   void set_module_namespace(const zstring& ns) { theModuleNamespace = ns.str(); }
701 
get_module_namespace()702   const std::string& get_module_namespace() const { return theModuleNamespace; }
703 
704   void add_imported_builtin_module(const zstring& ns);
705 
706   bool is_imported_builtin_module(const zstring& ns);
707 
708   //
709   // Base uri
710   //
711   zstring get_implementation_baseuri() const;
712 
713   bool get_encapsulating_entity_uri(zstring& res) const;
714 
715   void set_encapsulating_entity_uri(const zstring& uri);
716 
717   bool get_entity_retrieval_uri(zstring& res) const;
718 
719   void set_entity_retrieval_uri(const zstring& uri);
720 
721   zstring get_base_uri() const;
722 
723   void clear_base_uri();
724 
725   void set_base_uri(const zstring& uri, bool from_prolog = true);
726 
727   void compute_base_uri();
728 
729   zstring
730   resolve_relative_uri(
731       const zstring& aUri,
732       bool aValidate = true) const;
733 
734   zstring
735   resolve_relative_uri(
736       const zstring&  aRelativeUri,
737       const zstring&  aBaseUri,
738       bool            validate = true) const;
739 
740   //
741   // URI Resolution
742   //
743 
744   /**
745    * Add a URIMapper to be used by this static context when resolving
746    * URIs to resources.
747    */
748   void add_uri_mapper(internal::URIMapper* aMapper);
749 
750   /**
751    * Add a URLResolver to be used by this static context when
752    * resolving URIs to resources.
753    */
754   void add_url_resolver(internal::URLResolver* aResolver);
755 
756   /**
757    * Given a URI, return a Resource for that URI.
758    * @param aEntityKind the expected kind of entity expected at this aUri
759    */
760   std::auto_ptr<internal::Resource> resolve_uri
761   (zstring const& aUri, internal::EntityData::Kind aEntityKind, zstring& oErrorMessage) const;
762 
763   /**
764    * Given a URI, return a Resource for that URI.
765    * @param aEntityData an EntityData object to pass to the mappers/resolvers.
766    */
767   std::auto_ptr<internal::Resource> resolve_uri
768   (zstring const& aUri, internal::EntityData const& aEntityData, zstring& oErrorMessage) const;
769 
770   /**
771    * Given a URI, populate a vector with a list of component URIs.  If
772    * no component URIs are available, the vector will be populated
773    * with (only) the input URI.
774    */
775   void get_component_uris
776   (zstring const& aUri, internal::EntityData::Kind aEntityKind,
777     std::vector<zstring>& oComponents) const;
778 
779   /**
780    * Given a URI, populate a vector with a list of candidate URIs.  If
781    * no candidate URIs are available, the vector will be populated
782    * with (only) the input URI.
783    */
784   void get_candidate_uris
785   (zstring const& aUri, internal::EntityData::Kind aEntityKind,
786     std::vector<zstring>& oComponents) const;
787 
788   void set_uri_path(const std::vector<zstring>& aURIPath);
789 
790   void get_uri_path(std::vector<zstring>& oURIPath) const;
791 
792   void get_full_uri_path(std::vector<zstring>& oURIPath) const;
793 
794   void set_lib_path(const std::vector<zstring>& aLibPath);
795 
796   void get_lib_path(std::vector<zstring>& oLibPath) const;
797 
798   void get_full_lib_path(std::vector<zstring>& oLibPath) const;
799 
800   //
801   // Validating Items
802   //
803   bool validate(
804         store::Item* rootElement,
805         store::Item_t& validatedResult,
806         StaticContextConsts::validation_mode_t validationMode =
807             StaticContextConsts::strict_validation) const;
808 
809   bool validate(
810         store::Item* rootElement,
811         store::Item_t& validatedResult,
812         const zstring& targetNamespace,
813         StaticContextConsts::validation_mode_t validationMode =
814             StaticContextConsts::strict_validation) const ;
815 
816   bool validateSimpleContent(
817         zstring& textValue,
818         store::Item* typeQName,
819         std::vector<store::Item_t>& resultList) const;
820 
821   //
822   // Type management
823   //
824   TypeManager* get_typemanager() const;
825 
826   TypeManager* get_local_typemanager() const;
827 
828   void set_typemanager(rchandle<TypeManager>);
829 
830   //
831   // Namespace Bindings
832   //
833   const zstring& default_elem_type_ns() const;
834 
835   void set_default_elem_type_ns(
836       const zstring& ns,
837       bool raiseError,
838       const QueryLoc& loc);
839 
840   const zstring& default_function_ns() const;
841 
842   void set_default_function_ns(
843       const zstring& ns,
844       bool raiseError,
845       const QueryLoc& loc);
846 
847   void bind_ns(
848         const zstring& prefix,
849         const zstring& ns,
850         const QueryLoc& loc,
851         const Error& err = err::XQST0033);
852 
853   bool lookup_ns(
854         zstring& ns,
855         const zstring& prefix,
856         const QueryLoc& loc,
857         const Error& err = err::XPST0081) const;
858 
859   void expand_qname(
860         store::Item_t& qname,
861         const zstring& default_ns,
862         const zstring& pfx,
863         const zstring& local,
864         const QueryLoc& loc) const;
865 
866   void get_namespace_bindings(store::NsBindings& bindings) const;
867 
868   //
869   // Variables
870   //
871   void bind_var(var_expr* expr, const QueryLoc& loc, const Error& err);
872 
873   VarInfo* lookup_var(const store::Item* qname) const;
874 
875   void getVariables(
876       std::vector<VarInfo*>& variableList,
877       bool localsOnly = false,
878       bool returnPrivateVars = false,
879       bool externalVarsOnly = false) const;
880 
881   void set_context_item_type(const xqtref_t& t, const QueryLoc& loc);
882 
883   const XQType* get_context_item_type() const;
884 
885   //
886   // Functions
887   //
888   void bind_fn(function_t& f, ulong arity, const QueryLoc& loc);
889 
890   void unbind_fn(const store::Item* qname, ulong arity);
891 
892   function* lookup_fn(
893       const store::Item* qname,
894       ulong arity,
895       bool skipDisabled = true);
896 
897   function* lookup_local_fn(
898       const store::Item* qname,
899       ulong arity,
900       bool skipDisabled = true);
901 
902   void get_functions(std::vector<function*>& functions) const;
903 
904   void find_functions(
905         const store::Item* qname,
906         std::vector<function*>& functions) const;
907 
908   void bind_external_module(
909         ExternalModule* aModule,
910         bool aDynamicallyLoaded = false);
911 
912   ExternalFunction* lookup_external_function(
913         const zstring& prefix,
914         const zstring& local);
915 
916 
917   //
918   // Documents
919   //
920   void bind_document(const zstring& uri, xqtref_t& t);
921 
922   const XQType* lookup_document(const zstring& uri);
923 
924   void get_all_documents(std::vector<zstring>& documents);
925 
926   //
927   // W3C Collections
928   //
929   void bind_w3c_collection(zstring& uri, xqtref_t& t);
930 
931   const XQType* lookup_w3c_collection(const zstring& uri);
932 
933   void set_default_w3c_collection_type(xqtref_t& t);
934 
935   const XQType* get_default_w3c_collection_type();
936 
937   //
938   // XQDDF Collections
939   //
940   void bind_collection(StaticallyKnownCollection_t& aCollection, const QueryLoc& aLoc);
941 
942   const StaticallyKnownCollection* lookup_collection(const store::Item* aName) const;
943 
944   store::Iterator_t collection_names() const;
945 
946   //
947   // XQDDF Indexes
948   //
949   void bind_index(IndexDecl_t& vi, const QueryLoc& loc);
950 
951   IndexDecl* lookup_index(const store::Item* qname) const;
952 
953   store::Iterator_t index_names() const;
954 
955 
956   //
957   // XQDDF Integrity Constraints
958   //
959   void bind_ic(ValueIC_t& vic, const QueryLoc& loc);
960 
961   ValueIC_t lookup_ic(const store::Item* qname) const;
962 
963   store::Iterator_t ic_names() const;
964 
965 
966   //
967   // Collations
968   //
969   void add_collation(const std::string& uri, const QueryLoc& loc);
970 
971   bool is_known_collation(const std::string& uri) const;
972 
973   XQPCollator* get_collator(const std::string& uri, const QueryLoc& loc) const;
974 
975   void set_default_collation(const std::string& uri, const QueryLoc& loc);
976 
977   const std::string& get_default_collation(const QueryLoc& loc) const;
978 
979   XQPCollator* get_default_collator(const QueryLoc& loc) const;
980 
981   void get_collations(std::vector<std::string>& collations) const;
982 
983   //
984   // Options
985   //
986   void bind_option(
987       const store::Item* qname,
988       const zstring& option,
989       const QueryLoc& loc);
990 
991   bool lookup_option(const store::Item* qname, zstring& option) const;
992 
993 protected:
994   void process_feature_option(
995     const zstring& value,
996     bool  enable,
997     const QueryLoc& loc);
998 
999   void process_optim_option(
1000     const zstring& value,
1001     bool  enable,
1002     const QueryLoc& loc);
1003 
1004   void process_warning_option(
1005     const zstring& value,
1006     const zstring& name,
1007     const QueryLoc& loc);
1008 
1009   store::Item_t
1010   parse_and_expand_qname(
1011       const zstring& value,
1012       const char* default_ns,
1013       const QueryLoc& loc
1014     ) const;
1015 
1016 public:
1017 
1018   //
1019   // Auditing
1020   //
1021   void set_audit_event(audit::Event* ae);
1022 
1023   audit::Event* get_audit_event() const;
1024 
1025 
1026   //
1027   //  Misc
1028   //
1029   StaticContextConsts::xquery_version_t xquery_version() const;
1030 
1031   void set_xquery_version(StaticContextConsts::xquery_version_t v);
1032 
1033   StaticContextConsts::xpath_compatibility_t xpath_compatibility() const;
1034 
1035   void set_xpath_compatibility(StaticContextConsts::xpath_compatibility_t v);
1036 
1037   StaticContextConsts::construction_mode_t construction_mode() const;
1038 
1039   void set_construction_mode(StaticContextConsts::construction_mode_t v);
1040 
1041   bool inherit_ns() const;
1042 
1043   void set_inherit_ns(bool v);
1044 
1045   bool preserve_ns() const;
1046 
1047   void set_preserve_ns(bool);
1048 
1049   StaticContextConsts::ordering_mode_t ordering_mode() const;
1050 
1051   bool is_in_ordered_mode() const;
1052 
1053   void set_ordering_mode(StaticContextConsts::ordering_mode_t v);
1054 
1055   StaticContextConsts::empty_order_mode_t empty_order_mode() const;
1056 
1057   void set_empty_order_mode(StaticContextConsts::empty_order_mode_t v);
1058 
1059   StaticContextConsts::boundary_space_mode_t boundary_space_mode() const;
1060 
1061   void set_boundary_space_mode(StaticContextConsts::boundary_space_mode_t v);
1062 
1063   StaticContextConsts::validation_mode_t validation_mode() const;
1064 
1065   void set_validation_mode(StaticContextConsts::validation_mode_t v);
1066 
1067   void add_decimal_format(const DecimalFormat_t& format, const QueryLoc& loc);
1068 
1069   DecimalFormat_t get_decimal_format(const store::Item_t& qname);
1070 
1071 #ifndef ZORBA_NO_FULL_TEXT
get_match_options()1072   ftmatch_options const* get_match_options() const {
1073     return theFTMatchOptions;
1074   }
1075 
set_match_options(ftmatch_options * mo)1076   void set_match_options( ftmatch_options *mo ) {
1077     theFTMatchOptions = mo;
1078   }
1079 #endif /* ZORBA_NO_FULL_TEXT */
1080 
1081   //
1082   // Merge in the static context of a module
1083   //
1084   void import_module (const static_context* module, const QueryLoc& loc);
1085 
1086   //
1087   // Warnings
1088   //
1089   void disableWarning(store::Item_t qname);
1090 
1091   void disableAllWarnings();
1092 
1093   void setWarningAsError(store::Item_t qname);
1094 
1095   bool isWarningDisabled(const char* ns, const char* localname);
1096 
1097   bool isWarningAnError(const char* ns, const char* localname);
1098 
1099 
1100 protected:
1101   static_context();
1102 
1103   static_context(static_context* parent);
1104 
collection_map()1105   CollectionMap* collection_map() const { return theCollectionMap; }
1106 
index_map()1107   IndexMap* index_map() const { return theIndexMap; }
1108 
ic_map()1109   ICMap* ic_map() const { return theICMap; }
1110 
1111   //serialization helpers
1112   bool check_parent_is_root();
1113 
1114   void set_parent_as_root();
1115 
1116 private:
1117 
1118   void apply_uri_mappers(zstring const& aUri,
1119     internal::EntityData const* aEntityData,
1120     internal::URIMapper::Kind aMapperKind,
1121     std::vector<zstring>& oUris) const;
1122 
1123   void apply_url_resolvers(std::vector<zstring>& aUrls,
1124     internal::EntityData const* aEntityData,
1125     std::auto_ptr<internal::Resource>& oResource,
1126     zstring& oErrorMessage) const;
1127 
1128 public:
is_feature_set(feature::kind k)1129   bool is_feature_set( feature::kind k ) const
1130   {
1131     return (theFeatures & k) != 0;
1132   }
1133 
set_feature(feature::kind k)1134   void set_feature( feature::kind k )
1135   {
1136     theFeatures |= k;
1137   }
1138 
unset_feature(feature::kind k)1139   void unset_feature( feature::kind k )
1140   {
1141     theFeatures &= ~k;
1142   }
1143 
1144 };
1145 
1146 
1147 }
1148 #endif /* ZORBA_CONTEXT_STATIC_CONTEXT_H */
1149 
1150 /*
1151  * Local variables:
1152  * mode: c++
1153  * End:
1154  */
1155 /* vim:set et sw=2 ts=2: */
1156