GME  13
IC_Selector.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: IC_Selector.cpp 803869 2009-08-13 12:56:21Z amassari $
00020  */
00021 
00022 // ---------------------------------------------------------------------------
00023 //  Includes
00024 // ---------------------------------------------------------------------------
00025 #include <xercesc/framework/XMLAttr.hpp>
00026 #include <xercesc/validators/schema/identity/IC_Selector.hpp>
00027 #include <xercesc/validators/schema/identity/XercesXPath.hpp>
00028 #include <xercesc/validators/schema/identity/IdentityConstraint.hpp>
00029 #include <xercesc/validators/schema/identity/FieldActivator.hpp>
00030 
00031 XERCES_CPP_NAMESPACE_BEGIN
00032 
00033 // ---------------------------------------------------------------------------
00034 //  SelectorMatcher: Constructors and Destructor
00035 // ---------------------------------------------------------------------------
00036 SelectorMatcher::SelectorMatcher(XercesXPath* const xpath,
00037                                  IC_Selector* const selector,
00038                                  FieldActivator* const fieldActivator,
00039                                  const int initialDepth,
00040                                  MemoryManager* const manager)
00041     : XPathMatcher(xpath, selector->getIdentityConstraint(), manager)
00042     , fInitialDepth(initialDepth)
00043     , fElementDepth(0)
00044     , fMatchedDepth(-1)
00045     , fSelector(selector)
00046     , fFieldActivator(fieldActivator)
00047 {
00048 }
00049 
00050 // ---------------------------------------------------------------------------
00051 //  FieldMatcher: XMLDocumentHandler methods
00052 // ---------------------------------------------------------------------------
00053 void SelectorMatcher::startDocumentFragment() {
00054 
00055     XPathMatcher::startDocumentFragment();
00056     fElementDepth = 0;
00057     fMatchedDepth = -1;
00058 }
00059 
00060 void SelectorMatcher::startElement(const XMLElementDecl& elemDecl,
00061                                    const unsigned int urlId,
00062                                    const XMLCh* const elemPrefix,
00063                                    const RefVectorOf<XMLAttr>& attrList,
00064                                    const XMLSize_t attrCount,
00065                                    ValidationContext* validationContext /*=0*/) 
00066 {
00067 
00068     XPathMatcher::startElement(elemDecl, urlId, elemPrefix, attrList, attrCount, validationContext);
00069     fElementDepth++;
00070 
00071     // activate the fields, if selector is matched
00072     unsigned char matched = isMatched();
00073     if ((fMatchedDepth == -1 && ((matched & XP_MATCHED) == XP_MATCHED))
00074         || ((matched & XP_MATCHED_D) == XP_MATCHED_D)) {
00075 
00076         IdentityConstraint* ic = fSelector->getIdentityConstraint();
00077         XMLSize_t count = ic->getFieldCount();
00078 
00079         fMatchedDepth = fElementDepth;
00080         fFieldActivator->startValueScopeFor(ic, fInitialDepth);
00081 
00082         for (XMLSize_t i = 0; i < count; i++) {
00083 
00084             XPathMatcher* matcher = fFieldActivator->activateField(ic->getFieldAt(i), fInitialDepth);
00085             matcher->startElement(elemDecl, urlId, elemPrefix, attrList, attrCount, validationContext);
00086         }
00087     }
00088 }
00089 
00090 void SelectorMatcher::endElement(const XMLElementDecl& elemDecl,
00091                                  const XMLCh* const elemContent, 
00092                                  ValidationContext* validationContext /*=0*/,
00093                                  DatatypeValidator* actualValidator /*=0*/) 
00094 {
00095 
00096     XPathMatcher::endElement(elemDecl, elemContent, validationContext, actualValidator);
00097 
00098     if (fElementDepth-- == fMatchedDepth) {
00099 
00100         fMatchedDepth = -1;
00101         fFieldActivator->endValueScopeFor(fSelector->getIdentityConstraint(), fInitialDepth);
00102     }
00103 }
00104 
00105 // ---------------------------------------------------------------------------
00106 //  IC_Selector: Constructors and Destructor
00107 // ---------------------------------------------------------------------------
00108 IC_Selector::IC_Selector(XercesXPath* const xpath,
00109                          IdentityConstraint* const identityConstraint)
00110     : fXPath(xpath)
00111     , fIdentityConstraint(identityConstraint)
00112 {
00113 }
00114 
00115 
00116 IC_Selector::~IC_Selector()
00117 {
00118     delete fXPath;
00119 }
00120 
00121 // ---------------------------------------------------------------------------
00122 //  IC_Selector: operators
00123 // ---------------------------------------------------------------------------
00124 bool IC_Selector::operator ==(const IC_Selector& other) const {
00125 
00126     return (*fXPath == *(other.fXPath));
00127 }
00128 
00129 bool IC_Selector::operator !=(const IC_Selector& other) const {
00130 
00131     return !operator==(other);
00132 }
00133 
00134 // ---------------------------------------------------------------------------
00135 //  IC_Selector: Factory methods
00136 // ---------------------------------------------------------------------------
00137 XPathMatcher* IC_Selector::createMatcher(FieldActivator* const fieldActivator,
00138                                          const int initialDepth,
00139                                          MemoryManager* const manager) {
00140 
00141     return new (manager) SelectorMatcher(fXPath, this, fieldActivator, initialDepth, manager);
00142 }
00143 
00144 /***
00145  * Support for Serialization/De-serialization
00146  ***/
00147 
00148 IMPL_XSERIALIZABLE_TOCREATE(IC_Selector)
00149 
00150 void IC_Selector::serialize(XSerializeEngine& serEng)
00151 {
00152     if (serEng.isStoring())
00153     {
00154         serEng<<fXPath;
00155         
00156         IdentityConstraint::storeIC(serEng, fIdentityConstraint);
00157     }
00158     else
00159     {
00160         serEng>>fXPath;
00161 
00162         fIdentityConstraint = IdentityConstraint::loadIC(serEng);
00163     }
00164 
00165 }
00166 
00167 IC_Selector::IC_Selector(MemoryManager* const )
00168 :fXPath(0)
00169 ,fIdentityConstraint(0)
00170 {
00171 }
00172 
00173 XERCES_CPP_NAMESPACE_END
00174