source: icXML/icXML-devel/src/xercesc/validators/schema/identity/IdentityConstraintHandler.cpp @ 2722

Last change on this file since 2722 was 2722, checked in by cameron, 6 years ago

Original Xerces files with import mods for icxercesc

File size: 7.2 KB
Line 
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: IdentityConstraintHandler.cpp 803869 2009-08-13 12:56:21Z amassari $
20 */
21
22// ---------------------------------------------------------------------------
23//  Includes
24// ---------------------------------------------------------------------------
25#include "IdentityConstraintHandler.hpp"
26
27#include <xercesc/validators/schema/SchemaElementDecl.hpp>
28
29#include <xercesc/validators/schema/identity/FieldActivator.hpp>
30#include <xercesc/validators/schema/identity/ValueStore.hpp>
31#include <xercesc/validators/schema/identity/IC_Selector.hpp>
32
33#include <xercesc/util/OutOfMemoryException.hpp>
34
35XERCES_CPP_NAMESPACE_BEGIN
36
37typedef JanitorMemFunCall<IdentityConstraintHandler>    CleanupType;
38
39// ---------------------------------------------------------------------------
40//  IdentityConstraintHandler: Constructors and Destructor
41// ---------------------------------------------------------------------------
42IdentityConstraintHandler::IdentityConstraintHandler(XMLScanner*    const scanner
43                     , MemoryManager* const manager)
44: fScanner(scanner)
45, fMemoryManager(manager)
46, fMatcherStack(0)
47, fValueStoreCache(0)
48, fFieldActivator(0)
49{
50    CleanupType cleanup(this, &IdentityConstraintHandler::cleanUp);
51
52    try {
53
54        fMatcherStack    = new (fMemoryManager) XPathMatcherStack(fMemoryManager);
55        fValueStoreCache = new (fMemoryManager) ValueStoreCache(fMemoryManager);
56        fFieldActivator  = new (fMemoryManager) FieldActivator(fValueStoreCache, fMatcherStack, fMemoryManager);
57
58        fValueStoreCache->setScanner(scanner);
59    }
60    catch(const OutOfMemoryException&)
61    {
62        cleanup.release();
63
64        throw;
65    }
66
67    cleanup.release();
68}
69
70IdentityConstraintHandler::~IdentityConstraintHandler()
71{
72    cleanUp();
73}
74
75// ---------------------------------------------------------------------------
76//  IdentityConstraintHandler:  methods
77// ---------------------------------------------------------------------------
78void IdentityConstraintHandler::deactivateContext(      SchemaElementDecl* const elem
79                                                , const XMLCh*             const content
80                                                , ValidationContext*       validationContext /*=0*/
81                                                , DatatypeValidator*       actualValidator /*=0*/)
82{
83
84    XMLSize_t oldCount = fMatcherStack->getMatcherCount();
85
86    if (oldCount || elem->getIdentityConstraintCount()) 
87    {
88
89        for (XMLSize_t i = oldCount; i > 0; i--) 
90        {
91            XPathMatcher* matcher = fMatcherStack->getMatcherAt(i-1);
92            matcher->endElement(*(elem), content, validationContext, actualValidator);
93        }
94
95        if (fMatcherStack->size() > 0) 
96        {
97            fMatcherStack->popContext();
98        }
99
100        // handle everything *but* keyref's.
101        XMLSize_t newCount = fMatcherStack->getMatcherCount();
102
103        for (XMLSize_t j = oldCount; j > newCount; j--) 
104        {
105            XPathMatcher* matcher = fMatcherStack->getMatcherAt(j-1);
106            IdentityConstraint* ic = matcher->getIdentityConstraint();
107
108            if (ic  && (ic->getType() != IdentityConstraint::ICType_KEYREF))
109                fValueStoreCache->transplant(ic, matcher->getInitialDepth());
110        }
111
112        // now handle keyref's...
113        for (XMLSize_t k = oldCount; k > newCount; k--)
114        {
115            XPathMatcher* matcher = fMatcherStack->getMatcherAt(k-1);
116            IdentityConstraint* ic = matcher->getIdentityConstraint();
117
118            if (ic && (ic->getType() == IdentityConstraint::ICType_KEYREF)) 
119            {
120                ValueStore* values = fValueStoreCache->getValueStoreFor(ic, matcher->getInitialDepth());
121
122                if (values) { // nothing to do if nothing matched!
123                    values->endDocumentFragment(fValueStoreCache);
124                }
125            }
126        }
127
128        fValueStoreCache->endElement();
129
130    }
131}
132
133void IdentityConstraintHandler::activateIdentityConstraint
134                     (     
135                             SchemaElementDecl* const     elem
136                     ,       int                          elemDepth
137                     , const unsigned int                 uriId
138                     , const XMLCh*                 const elemPrefix
139                     , const RefVectorOf<XMLAttr>&        attrList
140                     , const XMLSize_t                    attrCount
141                     , ValidationContext*                 validationContext /*=0*/)
142{
143
144    XMLSize_t count = elem->getIdentityConstraintCount();
145
146    if (count || fMatcherStack->getMatcherCount()) 
147    {
148
149        fValueStoreCache->startElement();
150        fMatcherStack->pushContext();
151        fValueStoreCache->initValueStoresFor( elem, elemDepth);
152
153        for (XMLSize_t i = 0; i < count; i++) 
154        {
155            activateSelectorFor(elem->getIdentityConstraintAt(i), elemDepth);
156        }
157
158        // call all active identity constraints
159        count = fMatcherStack->getMatcherCount();
160
161        for (XMLSize_t j = 0; j < count; j++) 
162        {
163            XPathMatcher* matcher = fMatcherStack->getMatcherAt(j);
164            matcher->startElement(*elem, uriId, elemPrefix, attrList, attrCount, validationContext);
165        }
166    }
167}
168
169void IdentityConstraintHandler::activateSelectorFor(      IdentityConstraint* const ic
170                                   , const int                       initialDepth) 
171{
172
173    IC_Selector* selector = ic->getSelector();
174
175    if (!selector)
176        return;
177
178    XPathMatcher* matcher = selector->createMatcher(fFieldActivator, initialDepth, fMemoryManager);
179
180    fMatcherStack->addMatcher(matcher);
181    matcher->startDocumentFragment();
182}
183
184// ---------------------------------------------------------------------------
185//  IdentityConstraintHandler:  Getter methods
186// ---------------------------------------------------------------------------
187
188// ---------------------------------------------------------------------------
189//  IdentityConstraintHandler: cleanUp methods
190// ---------------------------------------------------------------------------
191void IdentityConstraintHandler::cleanUp() 
192{
193    if (fMatcherStack)
194        delete fMatcherStack;
195
196    if (fValueStoreCache)
197        delete fValueStoreCache;
198
199    if (fFieldActivator)
200        delete fFieldActivator;
201
202}
203
204void IdentityConstraintHandler::reset()
205{
206    fValueStoreCache->startDocument();
207    fMatcherStack->clear();
208}
209
210XERCES_CPP_NAMESPACE_END
211
212/**
213  * End of file IdentityConstraintHandler.cpp
214  */
215
Note: See TracBrowser for help on using the repository browser.