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 #include "stdafx.h"
17 
18 #include "context/root_static_context.h"
19 #include "context/namespace_context.h"
20 #include "context/default_url_resolvers.h"
21 #include "context/default_uri_mappers.h"
22 
23 #include "types/root_typemanager.h"
24 
25 #include "system/globalenv.h"
26 
27 #include "compiler/parser/query_loc.h"
28 
29 #include "common/common.h"
30 #include "zorbamisc/ns_consts.h"
31 
32 #include "store/api/item_factory.h"
33 
34 namespace zorba
35 {
36 
37 
38 #ifdef WIN32
append_to_path(std::vector<zstring> & aPath,zstring & zorba_root,zstring & relpath)39 static void append_to_path(
40     std::vector<zstring>& aPath,
41     zstring& zorba_root,
42     zstring& relpath)
43 {
44   ascii::replace_all(relpath, '/', '\\');
45   zstring full_path(zorba_root);
46   full_path.append(relpath);
47   full_path.append("\\");
48   aPath.push_back(full_path);
49 }
50 #endif
51 
52 
53 
root_static_context()54 root_static_context::root_static_context()
55   :
56   static_context()
57 {
58   theTypeManager = &GENV_TYPESYSTEM;
59 }
60 
61 
init()62 void root_static_context::init()
63 {
64   QueryLoc loc;
65 
66   theImplementationBaseUri = ZORBA_NS;
67   compute_base_uri();
68 
69   set_xquery_version(StaticContextConsts::xquery_version_3_0);
70   set_xpath_compatibility(StaticContextConsts::xpath2_0);
71 
72   const char* const default_ns_initializers[] =
73   {
74     //"err", XQUERY_ERR_NS,
75     "fn", static_context::W3C_FN_NS,
76 #ifdef ZORBA_WITH_JSON
77     "jn", static_context::JSONIQ_FN_NS,
78 #endif
79     "local", XQUERY_LOCAL_FN_NS,
80     "xml", XML_NS,
81     "xs", XML_SCHEMA_NS,
82     "xsi", XSI_NS,
83     NULL, NULL
84   };
85 
86   for (const char* const* p = default_ns_initializers; *p != NULL; p += 2)
87   {
88     zstring pfx(p[0]);
89     zstring ns (p[1]);
90     bind_ns(pfx, ns, loc);
91   }
92 
93   set_default_elem_type_ns(zstring(), true, loc);
94 
95   set_default_function_ns(W3C_FN_NS, true, loc);
96 
97   add_collation(ZORBA_DEF_COLLATION_NS, QueryLoc::null);
98   add_collation(W3C_CODEPT_COLLATION_NS, QueryLoc::null);
99   add_collation("http://www.zorba-xquery.com/collations/SECONDARY/en/EN", QueryLoc::null);
100   set_default_collation(W3C_CODEPT_COLLATION_NS, QueryLoc::null);
101 
102   set_construction_mode(StaticContextConsts::cons_preserve);
103   set_inherit_ns(true);
104   set_preserve_ns(true);
105 
106   set_ordering_mode(StaticContextConsts::ordered);
107 
108   set_empty_order_mode(StaticContextConsts::empty_greatest);
109 
110   set_boundary_space_mode(StaticContextConsts::strip_space);
111 
112   set_default_w3c_collection_type(GENV_TYPESYSTEM.ITEM_TYPE_STAR);
113 
114   set_context_item_type(GENV_TYPESYSTEM.ITEM_TYPE_ONE, QueryLoc::null);
115 
116   // TODO move into globalenv? memory leaks?
117   add_url_resolver(new internal::FileURLResolver());
118   add_url_resolver(new internal::HTTPURLResolver());
119 #ifndef ZORBA_NO_FULL_TEXT
120   add_url_resolver(new internal::ThesaurusURLResolver());
121 #endif /* ZORBA_NO_FULL_TEXT */
122   add_uri_mapper(new internal::FileizeURIMapper());
123   add_uri_mapper(new internal::ModuleVersioningURIMapper());
124   add_uri_mapper(new internal::AutoFSURIMapper());
125   add_url_resolver(new internal::ZorbaCollectionURLResolver());
126   add_uri_mapper(new internal::ZorbaCollectionURIMapper());
127 
128   set_validation_mode(StaticContextConsts::lax_validation);
129 
130   std::vector<zstring> lRootURIPath;
131   std::vector<zstring> lRootLibPath;
132 #ifdef WIN32
133   // compute the relative path to zorba_simplestore.dll (this dll)
134   WCHAR  wdll_path[1024];
135   DWORD dll_path_size;
136   dll_path_size = GetModuleFileNameW(NULL,
137                                      wdll_path,
138                                      sizeof(wdll_path)/sizeof(wdll_path[0]));
139   if(dll_path_size)
140   {
141     wdll_path[dll_path_size] = 0;
142     char  dll_path[1024];
143     WideCharToMultiByte(CP_UTF8,
144                         0,
145                         wdll_path,
146                         -1,
147                         dll_path,
148                         sizeof(dll_path),
149                         NULL,
150                         NULL);
151 
152     char *last_slash = strrchr(dll_path, '\\');
153     if(last_slash)
154     {
155       last_slash[0] = 0;
156       last_slash = strrchr (dll_path, '\\');
157       if (last_slash)
158       {
159         last_slash[1] = 0;
160         zstring zorba_root_dir(dll_path);
161 
162         append_to_path(lRootURIPath, zorba_root_dir, zstring(ZORBA_CORE_URI_DIR));
163         append_to_path(lRootURIPath, zorba_root_dir, zstring(ZORBA_NONCORE_URI_DIR));
164         append_to_path(lRootLibPath, zorba_root_dir, zstring(ZORBA_CORE_LIB_DIR));
165         append_to_path(lRootLibPath, zorba_root_dir, zstring(ZORBA_NONCORE_LIB_DIR));
166       }
167     }
168   }
169 #endif
170   const char ** lURIPathIter = get_builtin_uri_path();
171   for (; *lURIPathIter != 0; ++lURIPathIter)
172   {
173     lRootURIPath.push_back(*lURIPathIter);
174   }
175   set_uri_path(lRootURIPath);
176 
177   const char ** lLibPathIter = get_builtin_lib_path();
178   for (; *lLibPathIter != 0; ++lLibPathIter)
179   {
180     lRootLibPath.push_back(*lLibPathIter);
181   }
182   set_lib_path(lRootLibPath);
183 
184   // by default enabled features
185   set_feature( feature::ddl );
186   set_feature( feature::scripting );
187   set_feature( feature::trace );
188 }
189 
190 
~root_static_context()191 root_static_context::~root_static_context()
192 {
193 }
194 
195 }
196 /* vim:set et sw=2 ts=2: */
197