xsconstraints.java
来自「JAVA 所有包」· Java 代码 · 共 1,445 行 · 第 1/4 页
JAVA
1,445 行
/* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed 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 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 * * @xerces.internal * * @author Sandy Gao, IBM * * @version $Id: XSConstraints.java,v 1.2.6.1 2005/09/09 07:30:55 sunithareddy 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; } try { // validate the original lexical rep, and set the actual value actualValue = dv.validate(value, context, vinfo); // validate the canonical lexical rep if (vinfo != null) actualValue = dv.validate(vinfo.stringValue(), context, vinfo); } catch (InvalidDatatypeValueException ide) { return null; } return actualValue; } static void reportSchemaError(XMLErrorReporter errorReporter, SimpleLocator loc, String key, Object[] args) { if (loc != null) { errorReporter.reportError(loc, XSMessageFormatter.SCHEMA_DOMAIN, key, args, XMLErrorReporter.SEVERITY_ERROR); } else { errorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN, key, args, XMLErrorReporter.SEVERITY_ERROR); } } /** * used to check the 3 constraints against each complex type * (should be each model group): * Unique Particle Attribution, Particle Derivation (Restriction), * Element Declrations Consistent. */ public static void fullSchemaChecking(XSGrammarBucket grammarBucket, SubstitutionGroupHandler SGHandler, CMBuilder cmBuilder, XMLErrorReporter errorReporter) { // get all grammars, and put all substitution group information // in the substitution group handler SchemaGrammar[] grammars = grammarBucket.getGrammars(); for (int i = grammars.length-1; i >= 0; i--) { SGHandler.addSubstitutionGroup(grammars[i].getSubstitutionGroups()); } XSParticleDecl fakeDerived = new XSParticleDecl(); XSParticleDecl fakeBase = new XSParticleDecl(); fakeDerived.fType = XSParticleDecl.PARTICLE_MODELGROUP; fakeBase.fType = XSParticleDecl.PARTICLE_MODELGROUP; // before worrying about complexTypes, let's get // groups redefined by restriction out of the way. for (int g = grammars.length-1; g >= 0; g--) { XSGroupDecl [] redefinedGroups = grammars[g].getRedefinedGroupDecls(); SimpleLocator [] rgLocators = grammars[g].getRGLocators(); for(int i=0; i<redefinedGroups.length; ) { XSGroupDecl derivedGrp = redefinedGroups[i++]; XSModelGroupImpl derivedMG = derivedGrp.fModelGroup; XSGroupDecl baseGrp = redefinedGroups[i++]; XSModelGroupImpl baseMG = baseGrp.fModelGroup; if(baseMG == null) { if(derivedMG != null) { // can't be a restriction! reportSchemaError(errorReporter, rgLocators[i/2-1], "src-redefine.6.2.2", new Object[]{derivedGrp.fName, "rcase-Recurse.2"}); } } else { fakeDerived.fValue = derivedMG; fakeBase.fValue = baseMG; try { particleValidRestriction(fakeDerived, SGHandler, fakeBase, SGHandler); } catch (XMLSchemaException e) { String key = e.getKey(); reportSchemaError(errorReporter, rgLocators[i/2-1], key, e.getArgs()); reportSchemaError(errorReporter, rgLocators[i/2-1], "src-redefine.6.2.2", new Object[]{derivedGrp.fName, key}); } } } } // for each complex type, check the 3 constraints. // types need to be checked XSComplexTypeDecl[] types; SimpleLocator [] ctLocators; // to hold the errors // REVISIT: do we want to report all errors? or just one? //XMLSchemaError1D errors = new XMLSchemaError1D(); // whether need to check this type again; // whether only do UPA checking boolean further, fullChecked; // if do all checkings, how many need to be checked again. int keepType; // i: grammar; j: type; k: error // for all grammars SymbolHash elemTable = new SymbolHash(); for (int i = grammars.length-1, j, k; i >= 0; i--) { // get whether to skip EDC, and types need to be checked keepType = 0; fullChecked = grammars[i].fFullChecked; types = grammars[i].getUncheckedComplexTypeDecls(); ctLocators = grammars[i].getUncheckedCTLocators(); // for each type for (j = 0; j < types.length; j++) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?