📄 xsdsimpletypetraverser.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 java.util.Vector;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.XSSimpleType;import com.sun.org.apache.xerces.internal.impl.dv.xs.SchemaDVFactoryImpl;import com.sun.org.apache.xerces.internal.impl.dv.xs.XSSimpleTypeDecl;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.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.XSConstants;import com.sun.org.apache.xerces.internal.xs.XSObjectList;import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;import org.w3c.dom.Element;/** * The simple type definition schema component traverser. * * <simpleType * final = (#all | (list | union | restriction)) * id = ID * name = NCName * {any attributes with non-schema namespace . . .}> * Content: (annotation?, (restriction | list | union)) * </simpleType> * * <restriction * base = QName * id = ID * {any attributes with non-schema namespace . . .}> * Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)) * </restriction> * * <list * id = ID * itemType = QName * {any attributes with non-schema namespace . . .}> * Content: (annotation?, (simpleType?)) * </list> * * <union * id = ID * memberTypes = List of QName * {any attributes with non-schema namespace . . .}> * Content: (annotation?, (simpleType*)) * </union> * * @xerces.internal * * @author Elena Litani, IBM * @author Neeraj Bajaj, Sun Microsystems, Inc. * @author Sandy Gao, IBM * * @version $Id: XSDSimpleTypeTraverser.java,v 1.2.6.1 2005/09/09 07:25:08 sunithareddy Exp $ */class XSDSimpleTypeTraverser extends XSDAbstractTraverser { // the factory used to query/create simple types private final SchemaDVFactory schemaFactory = SchemaDVFactory.getInstance(); // whether the type being parsed is a S4S built-in type. private boolean fIsBuiltIn = false; XSDSimpleTypeTraverser (XSDHandler handler, XSAttributeChecker gAttrCheck) { super(handler, gAttrCheck); if (schemaFactory instanceof SchemaDVFactoryImpl) { ((SchemaDVFactoryImpl)schemaFactory).setDeclPool(handler.fDeclPool); } } //return qualified name of simpleType or empty string if error occured XSSimpleType traverseGlobal(Element elmNode, XSDocumentInfo schemaDoc, SchemaGrammar grammar) { // General Attribute Checking Object[] attrValues = fAttrChecker.checkAttributes(elmNode, true, schemaDoc); String nameAtt = (String)attrValues[XSAttributeChecker.ATTIDX_NAME]; XSSimpleType type = traverseSimpleTypeDecl(elmNode, attrValues, schemaDoc, grammar); fAttrChecker.returnAttrArray(attrValues, schemaDoc); // if it's a global type without a name, return null if (nameAtt == null) { reportSchemaError("s4s-att-must-appear", new Object[]{SchemaSymbols.ELT_SIMPLETYPE, SchemaSymbols.ATT_NAME}, elmNode); type = null; } // don't add global components without name to the grammar if (type != null) { grammar.addGlobalTypeDecl(type); } return type; } XSSimpleType traverseLocal(Element elmNode, XSDocumentInfo schemaDoc, SchemaGrammar grammar) { // General Attribute Checking Object[] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc); String name = genAnonTypeName(elmNode); XSSimpleType type = getSimpleType (name, elmNode, attrValues, schemaDoc, grammar); if (type instanceof XSSimpleTypeDecl) { ((XSSimpleTypeDecl)type).setAnonymous(true); } fAttrChecker.returnAttrArray(attrValues, schemaDoc); return type; } private XSSimpleType traverseSimpleTypeDecl(Element simpleTypeDecl, Object[] attrValues, XSDocumentInfo schemaDoc, SchemaGrammar grammar) { // get name and final values String name = (String)attrValues[XSAttributeChecker.ATTIDX_NAME]; return getSimpleType(name, simpleTypeDecl, attrValues, schemaDoc, grammar); } /* * Generate a name for an anonymous type */ private String genAnonTypeName(Element simpleTypeDecl) { // Generate a unique name for the anonymous type by concatenating together the // names of parent nodes // The name is quite good for debugging/error purposes, but we may want to // revisit how this is done for performance reasons (LM). StringBuffer typeName = new StringBuffer("#AnonType_"); Element node = DOMUtil.getParent(simpleTypeDecl); while (node != null && (node != DOMUtil.getRoot(DOMUtil.getDocument(node)))) { typeName.append(node.getAttribute(SchemaSymbols.ATT_NAME)); node = DOMUtil.getParent(node); } return typeName.toString(); } /** * @param name * @param simpleTypeDecl * @param attrValues * @param schemaDoc * @param grammar * @return */ private XSSimpleType getSimpleType(String name, Element simpleTypeDecl, Object[] attrValues, XSDocumentInfo schemaDoc, SchemaGrammar grammar) { XInt finalAttr = (XInt)attrValues[XSAttributeChecker.ATTIDX_FINAL]; int finalProperty = finalAttr == null ? schemaDoc.fFinalDefault : finalAttr.intValue(); // annotation?,(list|restriction|union) Element child = DOMUtil.getFirstChildElement(simpleTypeDecl); XSAnnotationImpl [] annotations = null; if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) { XSAnnotationImpl annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc); if (annotation != null) annotations = new XSAnnotationImpl [] {annotation}; child = DOMUtil.getNextSiblingElement(child); } else { String text = DOMUtil.getSyntheticAnnotation(simpleTypeDecl); if (text != null) { XSAnnotationImpl annotation = traverseSyntheticAnnotation(simpleTypeDecl, text, attrValues, false, schemaDoc); annotations = new XSAnnotationImpl[] {annotation}; } } // (list|restriction|union) if (child == null) { reportSchemaError("s4s-elt-must-match.2", new Object[]{SchemaSymbols.ELT_SIMPLETYPE, "(annotation?, (restriction | list | union))"}, simpleTypeDecl); return errorType(name, schemaDoc.fTargetNamespace, XSConstants.DERIVATION_RESTRICTION); } // derivation type: restriction/list/union String varietyProperty = DOMUtil.getLocalName(child); short refType = XSConstants.DERIVATION_RESTRICTION; boolean restriction = false, list = false, union = false; if (varietyProperty.equals(SchemaSymbols.ELT_RESTRICTION)) { refType = XSConstants.DERIVATION_RESTRICTION; restriction = true; } else if (varietyProperty.equals(SchemaSymbols.ELT_LIST)) { refType = XSConstants.DERIVATION_LIST; list = true; } else if (varietyProperty.equals(SchemaSymbols.ELT_UNION)) { refType = XSConstants.DERIVATION_UNION; union = true; } else { reportSchemaError("s4s-elt-must-match.1", new Object[]{SchemaSymbols.ELT_SIMPLETYPE, "(annotation?, (restriction | list | union))", varietyProperty}, simpleTypeDecl); return errorType(name, schemaDoc.fTargetNamespace, XSConstants.DERIVATION_RESTRICTION); } // nothing should follow this element Element nextChild = DOMUtil.getNextSiblingElement(child); if (nextChild != null) { reportSchemaError("s4s-elt-must-match.1", new Object[]{SchemaSymbols.ELT_SIMPLETYPE, "(annotation?, (restriction | list | union))", DOMUtil.getLocalName(nextChild)}, nextChild); } // General Attribute Checking: get base/item/member types Object[] contentAttrs = fAttrChecker.checkAttributes(child, false, schemaDoc); QName baseTypeName = (QName)contentAttrs[restriction ? XSAttributeChecker.ATTIDX_BASE : XSAttributeChecker.ATTIDX_ITEMTYPE]; Vector memberTypes = (Vector)contentAttrs[XSAttributeChecker.ATTIDX_MEMBERTYPES]; //content = {annotation?,simpleType?...} Element content = DOMUtil.getFirstChildElement(child); //check content (annotation?, ...) if (content != null && DOMUtil.getLocalName(content).equals(SchemaSymbols.ELT_ANNOTATION)) { XSAnnotationImpl annotation = traverseAnnotationDecl(content, contentAttrs, false, schemaDoc); if (annotation != null ) { if(annotations == null) { annotations = new XSAnnotationImpl [] {annotation}; } else { XSAnnotationImpl [] tempArray = new XSAnnotationImpl[2]; tempArray[0] = annotations[0]; annotations = tempArray; annotations[1] = annotation; } } content = DOMUtil.getNextSiblingElement(content); } else { String text = DOMUtil.getSyntheticAnnotation(child); if (text != null) { XSAnnotationImpl annotation = traverseSyntheticAnnotation(child, text, contentAttrs, false, schemaDoc); if (annotations == null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -