All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.xerces.impl.xs.XS11Constraints Maven / Gradle / Ivy

Go to download

A processor for parsing, validating, serializing and manipulating XML, written in Java

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.xerces.impl.xs;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Stack;

import org.apache.xerces.impl.Constants;
import org.apache.xerces.impl.XMLErrorReporter;
import org.apache.xerces.impl.dv.XSSimpleType;
import org.apache.xerces.impl.xs.alternative.XSTypeAlternativeImpl;
import org.apache.xerces.impl.xs.models.CMBuilder;
import org.apache.xerces.impl.xs.models.XS11CMRestriction;
import org.apache.xerces.impl.xs.models.XSCMValidator;
import org.apache.xerces.impl.xs.util.SimpleLocator;
import org.apache.xerces.impl.xs.util.XS11TypeHelper;
import org.apache.xerces.impl.xs.util.XSObjectListImpl;
import org.apache.xerces.util.NamespaceSupport;
import org.apache.xerces.util.SymbolHash;
import org.apache.xerces.xni.QName;
import org.apache.xerces.xs.XSConstants;
import org.apache.xerces.xs.XSTypeDefinition;

/**
 * XML Schema 1.1 constraints
 * 
 * @xerces.internal
 *
 * @author Khaled Noaman, IBM
 *
 * @version $Id$
 *
 */
class XS11Constraints extends XSConstraints {

    public XS11Constraints() {
        super(SchemaGrammar.getXSAnyType(Constants.SCHEMA_VERSION_1_1), Constants.SCHEMA_VERSION_1_1);
    }

    public boolean overlapUPA(XSElementDecl element,
            XSWildcardDecl wildcard,
            SubstitutionGroupHandler sgHandler) {
        return false;
    }

    /**
     * Check that a given particle is a valid restriction of a base particle.
     */
    final protected void checkElementDeclsConsistent(XSComplexTypeDecl type,
            XSParticleDecl particle,
            SymbolHash elemDeclHash,
            SubstitutionGroupHandler sgHandler,
            XSGrammarBucket grammarBucket,
            ArrayList wcList,
            Stack elemDeclStack) throws XMLSchemaException {

        // check for elements in the tree with the same name and namespace
        if (particle.fType == XSParticleDecl.PARTICLE_WILDCARD) {
            return;
        }

        // clear wildcard decl list and element decl stack
        if (wcList.size() > 0) {
            wcList.clear();
        }
        
        if (elemDeclStack.size() > 0) {
            elemDeclStack.clear();
        }

        // check for open content
        if (type.fOpenContent != null) {
            final XSWildcardDecl wc = type.fOpenContent.fWildcard;
            if (wc != null && wc.fProcessContents != XSWildcardDecl.PC_SKIP) {
                wcList.add(wc);
            }
        }

        if (particle.fType == XSParticleDecl.PARTICLE_ELEMENT) {
            elemDeclStack.push((XSElementDecl)(particle.fValue));
        }
        else  {
            preprocessModelGroupParticle((XSModelGroupImpl)particle.fValue, wcList, elemDeclStack);
        }

        // process element declarations
        while (!elemDeclStack.empty()) {
            final XSElementDecl elem = (XSElementDecl)elemDeclStack.pop();

            findElemInTable(type, elem, elemDeclHash);

            if (elem.fScope == XSConstants.SCOPE_GLOBAL) {
                // Check for substitution groups.
                XSElementDecl[] subGroup = sgHandler.getSubstitutionGroup(elem, fSchemaVersion);
                for (int i = 0; i < subGroup.length; i++) {
                    findElemInTable(type, subGroup[i], elemDeclHash);
                }
            }
            else {
                checkExtraEDCRules(type, elem, grammarBucket, wcList);
            }
        }
    }

    final public void findElemInTable(XSComplexTypeDecl type,
            XSElementDecl elem,
            SymbolHash elemDeclHash)
        throws XMLSchemaException {

        final XSElementDecl existingElem = findExistingElement(elem, elemDeclHash);

        // First time or is same element
        if (existingElem == null || existingElem == elem) {
            return;
        }

        if (elem.fType != existingElem.fType) {
            // Types are not the same
            throw new XMLSchemaException("cos-element-consistent", new Object[] {type.fName, elem.fName});
        }
        
        if (XS11TypeHelper.isTypeTablesComparable(elem.getTypeAlternatives(), existingElem.getTypeAlternatives()) && !isTypeTablesEquivalent(elem, existingElem)) {
            // Type tables are not equivalent
            throw new XMLSchemaException("cos-element-consistent.4.b", new Object[] {type.fName, elem.fName});  
        }
    }

    protected void preprocessModelGroupParticle(XSModelGroupImpl group, ArrayList wcList, Stack elemDeclStack) {
        for (int i = group.fParticleCount - 1; i >=0 ; i--) {
            final XSParticleDecl particle = group.fParticles[i];
            final int pType = particle.fType;
            if (pType == XSParticleDecl.PARTICLE_WILDCARD) {
                final XSWildcardDecl wc = (XSWildcardDecl) particle.fValue;
                if (wc.fProcessContents != XSWildcardDecl.PC_SKIP) {
                    wcList.add(wc);
                }
            }
            else if (pType == XSParticleDecl.PARTICLE_ELEMENT) {
                elemDeclStack.push((XSElementDecl)particle.fValue);
            }
            else {
                preprocessModelGroupParticle((XSModelGroupImpl)particle.fValue, wcList, elemDeclStack);
            }
        }
    }

    private void checkExtraEDCRules(XSComplexTypeDecl type,
            XSElementDecl elem,
            XSGrammarBucket grammarBucket,
            ArrayList wcList) throws XMLSchemaException {
        
        // If all of the following are true:
        //  1 The {particles} property contains (either directly, indirectly,
        //    or implicitly) one or more element declarations with the same
        //    expanded name Q; call these element declarations EDS.
        //  2 At least one of the following is true
        //    2.1 The {particles} property contains one or more strict or lax
        //        wildcard particles which match Q.
        //    2.2 The Model Group is the {term} of the content model of some
        //        Complex Type Definition CTD and CTD.{content type} has an
        //        {open content} with a strict or lax Wildcard which matches Q.
        //  3 There exists a top-level element declaration G with the expanded
        //    name Q.
        //  then the {type table}s of EDS and the {type table} of G must either
        //  all be absent or else all be present and equivalent.
        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy