xsconstraints.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,481 行 · 第 1/5 页

JAVA
1,481
字号
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999-2004 The Apache Software Foundation.  All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment: *       "This product includes software developed by the *        Apache Software Foundation (http://www.apache.org/)." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xerces" and "Apache Software Foundation" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written *    permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", *    nor may "Apache" appear in their name, without prior written *    permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, International * Business Machines, Inc., http://www.apache.org.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package com.sun.org.apache.xerces.internal.impl.xs;import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;import com.sun.org.apache.xerces.internal.impl.xs.models.CMBuilder;import com.sun.org.apache.xerces.internal.impl.xs.models.XSCMValidator;import com.sun.org.apache.xerces.internal.impl.xs.util.SimpleLocator;import com.sun.org.apache.xerces.internal.xs.XSConstants;import com.sun.org.apache.xerces.internal.xs.XSObjectList;import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;import com.sun.org.apache.xerces.internal.util.SymbolHash;import java.util.Vector;/** * Constaints shared by traversers and validator * * @author Sandy Gao, IBM * * @version $Id: XSConstraints.java,v 1.37 2004/02/03 17:27:45 sandygao Exp $ */public class XSConstraints {    static final int OCCURRENCE_UNKNOWN = SchemaSymbols.OCCURRENCE_UNBOUNDED-1;    static final XSSimpleType STRING_TYPE = (XSSimpleType)SchemaGrammar.SG_SchemaNS.getGlobalTypeDecl(SchemaSymbols.ATTVAL_STRING);    /**     * check whether derived is valid derived from base, given a subset     * of {restriction, extension}.B     */    public static boolean checkTypeDerivationOk(XSTypeDefinition derived, XSTypeDefinition base, short block) {        // if derived is anyType, then it's valid only if base is anyType too        if (derived == SchemaGrammar.fAnyType)            return derived == base;        // if derived is anySimpleType, then it's valid only if the base        // is ur-type        if (derived == SchemaGrammar.fAnySimpleType) {            return (base == SchemaGrammar.fAnyType ||                    base == SchemaGrammar.fAnySimpleType);        }        // if derived is simple type        if (derived.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) {            // if base is complex type            if (base.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) {                // if base is anyType, change base to anySimpleType,                // otherwise, not valid                if (base == SchemaGrammar.fAnyType)                    base = SchemaGrammar.fAnySimpleType;                else                    return false;            }            return checkSimpleDerivation((XSSimpleType)derived,                                         (XSSimpleType)base, block);        } else {            return checkComplexDerivation((XSComplexTypeDecl)derived, base, block);        }    }    /**     * check whether simple type derived is valid derived from base,     * given a subset of {restriction, extension}.     */    public static boolean checkSimpleDerivationOk(XSSimpleType derived, XSTypeDefinition base, short block) {        // if derived is anySimpleType, then it's valid only if the base        // is ur-type        if (derived == SchemaGrammar.fAnySimpleType) {            return (base == SchemaGrammar.fAnyType ||                    base == SchemaGrammar.fAnySimpleType);        }        // if base is complex type        if (base.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) {            // if base is anyType, change base to anySimpleType,            // otherwise, not valid            if (base == SchemaGrammar.fAnyType)                base = SchemaGrammar.fAnySimpleType;            else                return false;        }        return checkSimpleDerivation((XSSimpleType)derived,                                     (XSSimpleType)base, block);    }    /**     * check whether complex type derived is valid derived from base,     * given a subset of {restriction, extension}.     */    public static boolean checkComplexDerivationOk(XSComplexTypeDecl derived, XSTypeDefinition base, short block) {        // if derived is anyType, then it's valid only if base is anyType too        if (derived == SchemaGrammar.fAnyType)            return derived == base;        return checkComplexDerivation((XSComplexTypeDecl)derived, base, block);    }    /**     * Note: this will be a private method, and it assumes that derived is not     *       anySimpleType, and base is not anyType. Another method will be     *       introduced for public use, which will call this method.     */    private static boolean checkSimpleDerivation(XSSimpleType derived, XSSimpleType base, short block) {        // 1 They are the same type definition.        if (derived == base)            return true;        // 2 All of the following must be true:        // 2.1 restriction is not in the subset, or in the {final} of its own {base type definition};        if ((block & XSConstants.DERIVATION_RESTRICTION) != 0 ||            (derived.getBaseType().getFinal() & XSConstants.DERIVATION_RESTRICTION) != 0) {            return false;        }        // 2.2 One of the following must be true:        // 2.2.1 D's base type definition is B.        XSSimpleType directBase = (XSSimpleType)derived.getBaseType();        if (directBase == base)            return true;        // 2.2.2 D's base type definition is not the simple ur-type definition and is validly derived from B given the subset, as defined by this constraint.        if (directBase != SchemaGrammar.fAnySimpleType &&            checkSimpleDerivation(directBase, base, block)) {            return true;        }        // 2.2.3 D's {variety} is list or union and B is the simple ur-type definition.        if ((derived.getVariety() == XSSimpleType.VARIETY_LIST ||             derived.getVariety() == XSSimpleType.VARIETY_UNION) &&            base == SchemaGrammar.fAnySimpleType) {            return true;        }        // 2.2.4 B's {variety} is union and D is validly derived from a type definition in B's {member type definitions} given the subset, as defined by this constraint.        if (base.getVariety() == XSSimpleType.VARIETY_UNION) {            XSObjectList subUnionMemberDV = base.getMemberTypes();            int subUnionSize = subUnionMemberDV.getLength();            for (int i=0; i<subUnionSize; i++) {                base = (XSSimpleType)subUnionMemberDV.item(i);                if (checkSimpleDerivation(derived, base, block))                    return true;            }        }        return false;    }    /**     * Note: this will be a private method, and it assumes that derived is not     *       anyType. Another method will be introduced for public use,     *       which will call this method.     */    private static boolean checkComplexDerivation(XSComplexTypeDecl derived, XSTypeDefinition base, short block) {        // 2.1 B and D must be the same type definition.        if (derived == base)            return true;        // 1 If B and D are not the same type definition, then the {derivation method} of D must not be in the subset.        if ((derived.fDerivedBy & block) != 0)            return false;        // 2 One of the following must be true:        XSTypeDefinition directBase = derived.fBaseType;        // 2.2 B must be D's {base type definition}.        if (directBase == base)            return true;        // 2.3 All of the following must be true:        // 2.3.1 D's {base type definition} must not be the ur-type definition.        if (directBase == SchemaGrammar.fAnyType ||            directBase == SchemaGrammar.fAnySimpleType) {            return false;        }        // 2.3.2 The appropriate case among the following must be true:        // 2.3.2.1 If D's {base type definition} is complex, then it must be validly derived from B given the subset as defined by this constraint.        if (directBase.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE)            return checkComplexDerivation((XSComplexTypeDecl)directBase, base, block);        // 2.3.2.2 If D's {base type definition} is simple, then it must be validly derived from B given the subset as defined in Type Derivation OK (Simple) (3.14.6).        if (directBase.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) {            // if base is complex type            if (base.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) {                // if base is anyType, change base to anySimpleType,                // otherwise, not valid                if (base == SchemaGrammar.fAnyType)                    base = SchemaGrammar.fAnySimpleType;                else                    return false;            }            return checkSimpleDerivation((XSSimpleType)directBase,                                         (XSSimpleType)base, block);        }        return false;    }    /**     * check whether a value is a valid default for some type     * returns the compiled form of the value     * The parameter value could be either a String or a ValidatedInfo object     */    public static Object ElementDefaultValidImmediate(XSTypeDefinition type, String value, ValidationContext context, ValidatedInfo vinfo) {        XSSimpleType dv = null;        // e-props-correct        // For a string to be a valid default with respect to a type definition the appropriate case among the following must be true:        // 1 If the type definition is a simple type definition, then the string must be valid with respect to that definition as defined by String Valid (3.14.4).        if (type.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) {            dv = (XSSimpleType)type;        }        // 2 If the type definition is a complex type definition, then all of the following must be true:        else {            // 2.1 its {content type} must be a simple type definition or mixed.            XSComplexTypeDecl ctype = (XSComplexTypeDecl)type;            // 2.2 The appropriate case among the following must be true:            // 2.2.1 If the {content type} is a simple type definition, then the string must be valid with respect to that simple type definition as defined by String Valid (3.14.4).            if (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_SIMPLE) {                dv = ctype.fXSSimpleType;            }            // 2.2.2 If the {content type} is mixed, then the {content type}'s particle must be emptiable as defined by Particle Emptiable (3.9.6).            else if (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_MIXED) {                if (!((XSParticleDecl)ctype.getParticle()).emptiable())                    return null;            }            else {                return null;            }        }        // get the simple type declaration, and validate        Object actualValue = null;        if (dv == null) {            // complex type with mixed. to make sure that we store correct            // information in vinfo and return the correct value, we use            // "string" type for validation            dv = STRING_TYPE;        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?