1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /*
19  * $Id$
20  */
21 
22 // ---------------------------------------------------------------------------
23 //  Includes
24 // ---------------------------------------------------------------------------
25 #include <xercesc/framework/XMLAttr.hpp>
26 #include <xercesc/validators/schema/identity/IC_Selector.hpp>
27 #include <xercesc/validators/schema/identity/XercesXPath.hpp>
28 #include <xercesc/validators/schema/identity/IdentityConstraint.hpp>
29 #include <xercesc/validators/schema/identity/FieldActivator.hpp>
30 
31 XERCES_CPP_NAMESPACE_BEGIN
32 
33 // ---------------------------------------------------------------------------
34 //  SelectorMatcher: Constructors and Destructor
35 // ---------------------------------------------------------------------------
SelectorMatcher(XercesXPath * const xpath,IC_Selector * const selector,FieldActivator * const fieldActivator,const int initialDepth,MemoryManager * const manager)36 SelectorMatcher::SelectorMatcher(XercesXPath* const xpath,
37                                  IC_Selector* const selector,
38                                  FieldActivator* const fieldActivator,
39                                  const int initialDepth,
40                                  MemoryManager* const manager)
41     : XPathMatcher(xpath, selector->getIdentityConstraint(), manager)
42     , fInitialDepth(initialDepth)
43     , fElementDepth(0)
44     , fMatchedDepth(0)
45     , fSelector(selector)
46     , fFieldActivator(fieldActivator)
47 {
48     fMatchedDepth = (int*) fMemoryManager->allocate
49     (
50         fLocationPathSize * sizeof(int)
51     );//new int[fLocationPathSize];
52     for(XMLSize_t k = 0;k<fLocationPathSize;k++)
53         fMatchedDepth[k] = -1;
54 }
55 
~SelectorMatcher()56 SelectorMatcher::~SelectorMatcher()
57 {
58     fMemoryManager->deallocate(fMatchedDepth);//delete [] fMatchedDepth;
59 }
60 
61 // ---------------------------------------------------------------------------
62 //  FieldMatcher: XMLDocumentHandler methods
63 // ---------------------------------------------------------------------------
startDocumentFragment()64 void SelectorMatcher::startDocumentFragment() {
65 
66     XPathMatcher::startDocumentFragment();
67     fElementDepth = 0;
68     for(XMLSize_t k = 0;k<fLocationPathSize;k++)
69         fMatchedDepth[k] = -1;
70 }
71 
startElement(const XMLElementDecl & elemDecl,const unsigned int urlId,const XMLCh * const elemPrefix,const RefVectorOf<XMLAttr> & attrList,const XMLSize_t attrCount,ValidationContext * validationContext)72 void SelectorMatcher::startElement(const XMLElementDecl& elemDecl,
73                                    const unsigned int urlId,
74                                    const XMLCh* const elemPrefix,
75                                    const RefVectorOf<XMLAttr>& attrList,
76                                    const XMLSize_t attrCount,
77                                    ValidationContext* validationContext /*=0*/)
78 {
79 
80     XPathMatcher::startElement(elemDecl, urlId, elemPrefix, attrList, attrCount, validationContext);
81     fElementDepth++;
82 
83     for(XMLSize_t k = 0;k<fLocationPathSize;k++)
84     {
85         // use the match flag of each member of the union
86         unsigned char matched = 0;
87         if (((fMatched[k] & XP_MATCHED) == XP_MATCHED)
88             && ((fMatched[k] & XP_MATCHED_DP) != XP_MATCHED_DP))
89             matched = fMatched[k];
90         if ((fMatchedDepth[k] == -1 && ((matched & XP_MATCHED) == XP_MATCHED))
91             || ((matched & XP_MATCHED_D) == XP_MATCHED_D)) {
92 
93             IdentityConstraint* ic = fSelector->getIdentityConstraint();
94             XMLSize_t count = ic->getFieldCount();
95 
96             fMatchedDepth[k] = fElementDepth;
97             fFieldActivator->startValueScopeFor(ic, fInitialDepth);
98 
99             for (XMLSize_t i = 0; i < count; i++) {
100 
101                 XPathMatcher* matcher = fFieldActivator->activateField(ic->getFieldAt(i), fInitialDepth);
102                 matcher->startElement(elemDecl, urlId, elemPrefix, attrList, attrCount, validationContext);
103             }
104             break;
105         }
106     }
107 }
108 
endElement(const XMLElementDecl & elemDecl,const XMLCh * const elemContent,ValidationContext * validationContext,DatatypeValidator * actualValidator)109 void SelectorMatcher::endElement(const XMLElementDecl& elemDecl,
110                                  const XMLCh* const elemContent,
111                                  ValidationContext* validationContext /*=0*/,
112                                  DatatypeValidator* actualValidator /*=0*/)
113 {
114 
115     XPathMatcher::endElement(elemDecl, elemContent, validationContext, actualValidator);
116 
117     for(XMLSize_t k = 0;k<fLocationPathSize;k++)
118     {
119         if (fElementDepth == fMatchedDepth[k]) {
120 
121             fMatchedDepth[k] = -1;
122             fFieldActivator->endValueScopeFor(fSelector->getIdentityConstraint(), fInitialDepth);
123             break;
124         }
125     }
126     --fElementDepth;
127 }
128 
129 // ---------------------------------------------------------------------------
130 //  IC_Selector: Constructors and Destructor
131 // ---------------------------------------------------------------------------
IC_Selector(XercesXPath * const xpath,IdentityConstraint * const identityConstraint)132 IC_Selector::IC_Selector(XercesXPath* const xpath,
133                          IdentityConstraint* const identityConstraint)
134     : fXPath(xpath)
135     , fIdentityConstraint(identityConstraint)
136 {
137 }
138 
139 
~IC_Selector()140 IC_Selector::~IC_Selector()
141 {
142     delete fXPath;
143 }
144 
145 // ---------------------------------------------------------------------------
146 //  IC_Selector: operators
147 // ---------------------------------------------------------------------------
operator ==(const IC_Selector & other) const148 bool IC_Selector::operator ==(const IC_Selector& other) const {
149 
150     return (*fXPath == *(other.fXPath));
151 }
152 
operator !=(const IC_Selector & other) const153 bool IC_Selector::operator !=(const IC_Selector& other) const {
154 
155     return !operator==(other);
156 }
157 
158 // ---------------------------------------------------------------------------
159 //  IC_Selector: Factory methods
160 // ---------------------------------------------------------------------------
createMatcher(FieldActivator * const fieldActivator,const int initialDepth,MemoryManager * const manager)161 XPathMatcher* IC_Selector::createMatcher(FieldActivator* const fieldActivator,
162                                          const int initialDepth,
163                                          MemoryManager* const manager) {
164 
165     return new (manager) SelectorMatcher(fXPath, this, fieldActivator, initialDepth, manager);
166 }
167 
168 /***
169  * Support for Serialization/De-serialization
170  ***/
171 
IMPL_XSERIALIZABLE_TOCREATE(IC_Selector)172 IMPL_XSERIALIZABLE_TOCREATE(IC_Selector)
173 
174 void IC_Selector::serialize(XSerializeEngine& serEng)
175 {
176     if (serEng.isStoring())
177     {
178         serEng<<fXPath;
179 
180         IdentityConstraint::storeIC(serEng, fIdentityConstraint);
181     }
182     else
183     {
184         serEng>>fXPath;
185 
186         fIdentityConstraint = IdentityConstraint::loadIC(serEng);
187     }
188 
189 }
190 
IC_Selector(MemoryManager * const)191 IC_Selector::IC_Selector(MemoryManager* const )
192 :fXPath(0)
193 ,fIdentityConstraint(0)
194 {
195 }
196 
197 XERCES_CPP_NAMESPACE_END
198 
199 /**
200   * End of file IC_Selector.cpp
201   */
202 
203