📄 xsdcomplextypetraverser.java
字号:
/* * Copyright 2001-2005 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.traversers;import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeFacetException;import com.sun.org.apache.xerces.internal.impl.dv.SchemaDVFactory;import com.sun.org.apache.xerces.internal.impl.dv.XSFacets;import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl;import com.sun.org.apache.xerces.internal.impl.xs.XSAttributeGroupDecl;import com.sun.org.apache.xerces.internal.impl.xs.XSAttributeUseImpl;import com.sun.org.apache.xerces.internal.impl.xs.XSComplexTypeDecl;import com.sun.org.apache.xerces.internal.impl.xs.XSConstraints;import com.sun.org.apache.xerces.internal.impl.xs.XSModelGroupImpl;import com.sun.org.apache.xerces.internal.impl.xs.XSParticleDecl;import com.sun.org.apache.xerces.internal.impl.xs.XSWildcardDecl;import com.sun.org.apache.xerces.internal.impl.xs.util.XInt;import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;import com.sun.org.apache.xerces.internal.util.DOMUtil;import com.sun.org.apache.xerces.internal.xni.QName;import com.sun.org.apache.xerces.internal.xs.XSAttributeUse;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 org.w3c.dom.Element;/** * A complex type definition schema component traverser. * * <complexType * abstract = boolean : false * block = (#all | List of (extension | restriction)) * final = (#all | List of (extension | restriction)) * id = ID * mixed = boolean : false * name = NCName * {any attributes with non-schema namespace . . .}> * Content: (annotation?, (simpleContent | complexContent | * ((group | all | choice | sequence)?, * ((attribute | attributeGroup)*, anyAttribute?)))) * </complexType> * * @xerces.internal * * @version $Id: XSDComplexTypeTraverser.java,v 1.2.6.1 2005/09/08 14:22:48 sunithareddy Exp $ */class XSDComplexTypeTraverser extends XSDAbstractParticleTraverser { // size of stack to hold globals: private final static int GLOBAL_NUM = 11; // globals for building XSComplexTypeDecls private String fName = null; private String fTargetNamespace = null; private short fDerivedBy = XSConstants.DERIVATION_RESTRICTION; private short fFinal = XSConstants.DERIVATION_NONE; private short fBlock = XSConstants.DERIVATION_NONE; private short fContentType = XSComplexTypeDecl.CONTENTTYPE_EMPTY; private XSTypeDefinition fBaseType = null; private XSAttributeGroupDecl fAttrGrp = null; private XSSimpleType fXSSimpleType = null; private XSParticleDecl fParticle = null; private boolean fIsAbstract = false; private XSComplexTypeDecl fComplexTypeDecl = null; private XSAnnotationImpl [] fAnnotations = null; private XSParticleDecl fEmptyParticle = null; // our own little stack to retain state when getGlobalDecls is called: private Object [] fGlobalStore = null; private int fGlobalStorePos = 0; XSDComplexTypeTraverser (XSDHandler handler, XSAttributeChecker gAttrCheck) { super(handler, gAttrCheck); } private static final boolean DEBUG=false; private SchemaDVFactory schemaFactory = SchemaDVFactory.getInstance(); private class ComplexTypeRecoverableError extends Exception { private static final long serialVersionUID = 3762247556666831417L; Object[] errorSubstText=null; Element errorElem = null; ComplexTypeRecoverableError() { super(); } ComplexTypeRecoverableError(String msgKey, Object[] args, Element e) { super(msgKey); errorSubstText=args; errorElem = e; } } /** * Traverse local complexType declarations * * @param Element * @param XSDocumentInfo * @param SchemaGrammar * @return XSComplexTypeDecl */ XSComplexTypeDecl traverseLocal(Element complexTypeNode, XSDocumentInfo schemaDoc, SchemaGrammar grammar) { Object[] attrValues = fAttrChecker.checkAttributes(complexTypeNode, false, schemaDoc); String complexTypeName = genAnonTypeName(complexTypeNode); contentBackup(); XSComplexTypeDecl type = traverseComplexTypeDecl (complexTypeNode, complexTypeName, attrValues, schemaDoc, grammar); contentRestore(); // need to add the type to the grammar for later constraint checking grammar.addComplexTypeDecl(type, fSchemaHandler.element2Locator(complexTypeNode)); type.setIsAnonymous(); fAttrChecker.returnAttrArray(attrValues, schemaDoc); return type; } /** * Traverse global complexType declarations * * @param Element * @param XSDocumentInfo * @param SchemaGrammar * @return XSComplexTypeDecXSComplexTypeDecl */ XSComplexTypeDecl traverseGlobal (Element complexTypeNode, XSDocumentInfo schemaDoc, SchemaGrammar grammar) { Object[] attrValues = fAttrChecker.checkAttributes(complexTypeNode, true, schemaDoc); String complexTypeName = (String) attrValues[XSAttributeChecker.ATTIDX_NAME]; contentBackup(); XSComplexTypeDecl type = traverseComplexTypeDecl (complexTypeNode, complexTypeName, attrValues, schemaDoc, grammar); contentRestore(); if (complexTypeName == null) { reportSchemaError("s4s-att-must-appear", new Object[]{SchemaSymbols.ELT_COMPLEXTYPE, SchemaSymbols.ATT_NAME}, complexTypeNode); } else { grammar.addGlobalTypeDecl(type); } // need to add the type to the grammar for later constraint checking grammar.addComplexTypeDecl(type, fSchemaHandler.element2Locator(complexTypeNode)); fAttrChecker.returnAttrArray(attrValues, schemaDoc); return type; } private XSComplexTypeDecl traverseComplexTypeDecl(Element complexTypeDecl, String complexTypeName, Object[] attrValues, XSDocumentInfo schemaDoc, SchemaGrammar grammar) { fComplexTypeDecl = new XSComplexTypeDecl(); fAttrGrp = new XSAttributeGroupDecl(); Boolean abstractAtt = (Boolean) attrValues[XSAttributeChecker.ATTIDX_ABSTRACT]; XInt blockAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_BLOCK]; Boolean mixedAtt = (Boolean) attrValues[XSAttributeChecker.ATTIDX_MIXED]; XInt finalAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_FINAL]; fName = complexTypeName; fComplexTypeDecl.setName(fName); fTargetNamespace = schemaDoc.fTargetNamespace; fBlock = blockAtt == null ? schemaDoc.fBlockDefault : blockAtt.shortValue(); fFinal = finalAtt == null ? schemaDoc.fFinalDefault : finalAtt.shortValue(); //discard valid Block/Final 'Default' values that are invalid for Block/Final fBlock &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION); fFinal &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION); fIsAbstract = (abstractAtt != null && abstractAtt.booleanValue()); Element child = null; try { // --------------------------------------------------------------- // First, handle any ANNOTATION declaration and get next child // --------------------------------------------------------------- child = DOMUtil.getFirstChildElement(complexTypeDecl); if(child != null) { if (DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) { addAnnotation(traverseAnnotationDecl(child, attrValues, false, schemaDoc)); child = DOMUtil.getNextSiblingElement(child); } else { String text = DOMUtil.getSyntheticAnnotation(complexTypeDecl); if (text != null) { addAnnotation(traverseSyntheticAnnotation(complexTypeDecl, text, attrValues, false, schemaDoc)); } } if (child !=null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) { throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1", new Object[]{fName,SchemaSymbols.ELT_ANNOTATION}, child); } } else { String text = DOMUtil.getSyntheticAnnotation(complexTypeDecl); if (text != null) { addAnnotation(traverseSyntheticAnnotation(complexTypeDecl, text, attrValues, false, schemaDoc)); } } // --------------------------------------------------------------- // Process the content of the complex type definition // --------------------------------------------------------------- if (child==null) { // // EMPTY complexType with complexContent // // set the base to the anyType fBaseType = SchemaGrammar.fAnyType; processComplexContent(child, mixedAtt.booleanValue(), false, schemaDoc, grammar); } else if (DOMUtil.getLocalName(child).equals (SchemaSymbols.ELT_SIMPLECONTENT)) { // // SIMPLE CONTENT // traverseSimpleContent(child, schemaDoc, grammar); Element elemTmp = DOMUtil.getNextSiblingElement(child); if (elemTmp != null) { String siblingName = DOMUtil.getLocalName(elemTmp); throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1", new Object[]{fName,siblingName}, elemTmp); } } else if (DOMUtil.getLocalName(child).equals (SchemaSymbols.ELT_COMPLEXCONTENT)) { traverseComplexContent(child, mixedAtt.booleanValue(), schemaDoc, grammar); Element elemTmp = DOMUtil.getNextSiblingElement(child); if (elemTmp != null) { String siblingName = DOMUtil.getLocalName(elemTmp); throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1", new Object[]{fName,siblingName}, elemTmp); } } else { // // We must have .... // GROUP, ALL, SEQUENCE or CHOICE, followed by optional attributes // Note that it's possible that only attributes are specified. // // set the base to the anyType fBaseType = SchemaGrammar.fAnyType; processComplexContent(child, mixedAtt.booleanValue(), false, schemaDoc, grammar); } } catch (ComplexTypeRecoverableError e) { handleComplexTypeError(e.getMessage(), e.errorSubstText, e.errorElem); } if (DEBUG) { System.out.println(fName); } fComplexTypeDecl.setValues(fName, fTargetNamespace, fBaseType, fDerivedBy, fFinal, fBlock, fContentType, fIsAbstract, fAttrGrp, fXSSimpleType, fParticle, new XSObjectListImpl(fAnnotations, fAnnotations == null? 0 : fAnnotations.length)); return fComplexTypeDecl; } private void traverseSimpleContent(Element simpleContentElement, XSDocumentInfo schemaDoc, SchemaGrammar grammar) throws ComplexTypeRecoverableError {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -