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_TYPES_SCHEMA_REVALIDATION
18 #define ZORBA_TYPES_SCHEMA_REVALIDATION
19 
20 #include <set>
21 
22 #include "common/shared_types.h"
23 
24 #include "store/api/validator.h"
25 #include "types/schema/EventSchemaValidator.h"
26 #include "types/schema/StrX.h"
27 
28 namespace zorba
29 {
30 //bool typeHasValue(xqtref_t t);
31 //bool typeHasTypedValue(xqtref_t t);
32 //bool typeHasEmptyValue(xqtref_t t);
33 
34 
35 class QueryLoc;
36 class static_context;
37 
38 
39 /***************************************************************************//**
40   An implementation of the abstract store::Validator class, which provides a
41   callback method for the store to call in order to preform revalidation after
42   applying a PUL.
43 ********************************************************************************/
44 class SchemaValidatorImpl : public store::SchemaValidator
45 {
46 protected:
47   const QueryLoc & theLoc;
48   static_context * theSctx;
49 
50 public:
SchemaValidatorImpl(const QueryLoc & loc,static_context * sctx)51   SchemaValidatorImpl(const QueryLoc& loc, static_context* sctx)
52     :
53     theLoc(loc),
54     theSctx(sctx)
55   {
56   }
57 
58 #ifndef ZORBA_NO_XMLSCHEMA
59   /**
60    * Validates entire documents. nodes is a set of root documents or elements.
61    * An error is thrown in case of invalid content.
62    */
63   void validate(
64       const std::set<store::Item*>& nodes,
65       store::PUL& pul);
66 
67   bool isPossibleSimpleContentRevalidation(store::Item *typeQName);
68 
69   void validateSimpleContent(
70       store::Item *typeQName,
71       zstring newValue,
72       std::vector<store::Item_t>& resultList);
73 
74 #else
validate(const std::set<store::Item * > & nodes,store::PUL & pul)75   void validate(
76       const std::set<store::Item*>& nodes,
77       store::PUL& pul)
78   {
79   }
80 
isPossibleSimpleContentRevalidation(store::Item * typeQName)81   bool isPossibleSimpleContentRevalidation(store::Item *typeQName)
82   {
83     return false;
84   }
85 
validateSimpleContent(store::Item * typeQName,zstring newValue,std::vector<store::Item_t> & resultList)86   void validateSimpleContent(
87       store::Item *typeQName,
88       zstring newValue,
89       std::vector<store::Item_t>& resultList)
90   {
91   }
92 #endif //ZORBA_NO_XMLSCHEMA
93 
94 private:
95 #ifndef ZORBA_NO_XMLSCHEMA
96   void validateAfterUpdate(
97       store::Item* item,
98       zorba::store::PUL* pul,
99       const QueryLoc& loc);
100 
101   void processElement(
102       store::PUL* pul,
103       TypeManager* typeManager,
104       EventSchemaValidator& schemaValidator,
105       store::Item_t element,
106       const QueryLoc& loc);
107 
108   void validateAttributes(
109       EventSchemaValidator& schemaValidator,
110       store::Iterator_t attributes);
111 
112   void processAttributes(
113       store::PUL* pul,
114       namespace_context& nsCtx,
115       TypeManager* typeManager,
116       EventSchemaValidator& schemaValidator,
117       store::Item* parent,
118       store::Iterator_t attributes,
119       const QueryLoc& loc);
120 
121   int processChildren(
122       store::PUL* pul,
123       namespace_context& nsCtx,
124       TypeManager* typeManager,
125       EventSchemaValidator& schemaValidator,
126       store::Iterator_t children,
127       std::vector<store::Item_t>& typedValues,
128       const QueryLoc& loc);
129 
130   void processNamespaces(
131       EventSchemaValidator& schemaValidator,
132       const store::Item *item);
133 
134   void processTextValue(
135       store::PUL* pul,
136       TypeManager* typeManager,
137       namespace_context &nsCtx,
138       store::Item_t typeQName,
139       zstring& textValue,
140       store::Item_t& originalChild,
141       std::vector<store::Item_t>& resultList,
142       const QueryLoc& loc);
143 
144   store::Item_t findAttributeItem(
145       const store::Item *parent,
146       store::Item_t &attQName);
147 
148   bool isPossibleSimpleContentRevalImpl(xqtref_t schemaType);
149 
150 #endif //ifndef ZORBA_NO_XMLSCHEMA
151 
152 };
153 
154 
155 }
156 
157 #endif //ifndef ZORBA_TYPES_SCHEMA_REVALIDATION
158 
159 /*
160  * Local variables:
161  * mode: c++
162  * End:
163  */
164 /* vim:set et sw=2 ts=2: */
165