aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.xjc.reader.xmlschema.ct; aoqi@0: aoqi@0: import java.util.HashMap; aoqi@0: import java.util.Iterator; aoqi@0: import java.util.Map; aoqi@0: aoqi@0: import com.sun.tools.internal.xjc.reader.xmlschema.WildcardNameClassBuilder; aoqi@0: import com.sun.xml.internal.xsom.XSAttributeUse; aoqi@0: import com.sun.xml.internal.xsom.XSComplexType; aoqi@0: import com.sun.xml.internal.xsom.XSContentType; aoqi@0: import com.sun.xml.internal.xsom.XSDeclaration; aoqi@0: import com.sun.xml.internal.xsom.XSElementDecl; aoqi@0: import com.sun.xml.internal.xsom.XSModelGroup; aoqi@0: import com.sun.xml.internal.xsom.XSModelGroupDecl; aoqi@0: import com.sun.xml.internal.xsom.XSParticle; aoqi@0: import com.sun.xml.internal.xsom.XSType; aoqi@0: import com.sun.xml.internal.xsom.XSWildcard; aoqi@0: import com.sun.xml.internal.xsom.visitor.XSTermFunction; aoqi@0: import javax.xml.namespace.QName; aoqi@0: aoqi@0: import com.sun.xml.internal.rngom.nc.ChoiceNameClass; aoqi@0: import com.sun.xml.internal.rngom.nc.NameClass; aoqi@0: import com.sun.xml.internal.rngom.nc.SimpleNameClass; aoqi@0: aoqi@0: /** aoqi@0: * Binds a complex type derived from another complex type by extension. aoqi@0: * aoqi@0: * @author aoqi@0: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) aoqi@0: */ aoqi@0: abstract class AbstractExtendedComplexTypeBuilder extends CTBuilder { aoqi@0: aoqi@0: /** aoqi@0: * Map from {@link XSComplexType} to {@link NameClass}[2] that aoqi@0: * represents the names used in its child elements [0] and aoqi@0: * attributes [1]. aoqi@0: */ aoqi@0: protected final Map characteristicNameClasses = new HashMap(); aoqi@0: aoqi@0: /** aoqi@0: * Computes a name class that represents everything in a given content model. aoqi@0: */ aoqi@0: protected final XSTermFunction contentModelNameClassBuilder = new XSTermFunction() { aoqi@0: @Override aoqi@0: public NameClass wildcard(XSWildcard wc) { aoqi@0: return WildcardNameClassBuilder.build(wc); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public NameClass modelGroupDecl(XSModelGroupDecl decl) { aoqi@0: return modelGroup(decl.getModelGroup()); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public NameClass modelGroup(XSModelGroup group) { aoqi@0: NameClass nc = NameClass.NULL; aoqi@0: for( int i=0; i t_2 > ... > t aoqi@0: * and find t_i such that t_i derives by restriction but aoqi@0: * for every j>i, t_j derives by extension. aoqi@0: * aoqi@0: * @return null aoqi@0: * If there's no such t_i or if t_i is any type. aoqi@0: */ aoqi@0: protected XSComplexType getLastRestrictedType(XSComplexType t) { aoqi@0: if (t.getBaseType() == schemas.getAnyType()) { aoqi@0: return null; // we don't count the restriction from anyType aoqi@0: } aoqi@0: if (t.getDerivationMethod() == XSType.RESTRICTION) { aoqi@0: return t; aoqi@0: } aoqi@0: aoqi@0: XSComplexType baseType = t.getBaseType().asComplexType(); aoqi@0: if (baseType != null) { aoqi@0: return getLastRestrictedType(baseType); aoqi@0: } else { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Checks if this new extension is safe. aoqi@0: * aoqi@0: * UGLY. aoqi@0: *

aoqi@0: * If you have ctA extending ctB and ctB restricting ctC, our aoqi@0: * Java classes will look like CtAImpl extending CtBImpl aoqi@0: * extending CtCImpl. aoqi@0: * aoqi@0: *

aoqi@0: * Since a derived class unmarshaller uses the base class unmarshaller, aoqi@0: * this could potentially result in incorrect unmarshalling. aoqi@0: * We used to just reject such a case, but then we found that aoqi@0: * there are schemas that are using it. aoqi@0: * aoqi@0: *

aoqi@0: * One generalized observation that we reached is that if the extension aoqi@0: * is only adding new elements/attributes which has never been used aoqi@0: * in any of its base class (IOW, if none of the particle / attribute use / aoqi@0: * attribute wildcard can match the name of newly added elements/attributes) aoqi@0: * then it is safe to add them. aoqi@0: * aoqi@0: *

aoqi@0: * This function checks if the derivation chain to this type is aoqi@0: * not using restriction, and if it is, then checks if it is safe aoqi@0: * according to the above condition. aoqi@0: * aoqi@0: * @return false aoqi@0: * If this complex type needs to be rejected. aoqi@0: */ aoqi@0: protected boolean checkIfExtensionSafe(XSComplexType baseType, XSComplexType thisType) { aoqi@0: XSComplexType lastType = getLastRestrictedType(baseType); aoqi@0: aoqi@0: if (lastType == null) { aoqi@0: return true; // no restriction in derivation chain aoqi@0: } aoqi@0: NameClass anc = NameClass.NULL; aoqi@0: // build name class for attributes in new complex type aoqi@0: Iterator itr = thisType.iterateDeclaredAttributeUses(); aoqi@0: while (itr.hasNext()) { aoqi@0: anc = new ChoiceNameClass(anc, getNameClass(((XSAttributeUse) itr.next()).getDecl())); aoqi@0: } aoqi@0: // TODO: attribute wildcard aoqi@0: aoqi@0: NameClass enc = getNameClass(thisType.getExplicitContent()); aoqi@0: aoqi@0: // check against every base type ... except the root anyType aoqi@0: while (lastType != lastType.getBaseType()) { aoqi@0: if (checkCollision(anc, enc, lastType)) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: if (lastType.getBaseType().isSimpleType()) // if the base type is a simple type, there won't be aoqi@0: // any further name collision. aoqi@0: { aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: lastType = lastType.getBaseType().asComplexType(); aoqi@0: } aoqi@0: aoqi@0: return true; // OK aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets a {@link NameClass} that represents all the terms in the given content type. aoqi@0: * If t is not a particle, just return an empty name class. aoqi@0: */ aoqi@0: private NameClass getNameClass(XSContentType t) { aoqi@0: if(t==null) return NameClass.NULL; aoqi@0: XSParticle p = t.asParticle(); aoqi@0: if(p==null) return NameClass.NULL; aoqi@0: else return p.getTerm().apply(contentModelNameClassBuilder); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets a {@link SimpleNameClass} from the name of a {@link XSDeclaration}. aoqi@0: */ aoqi@0: private NameClass getNameClass(XSDeclaration decl) { aoqi@0: return new SimpleNameClass(new QName(decl.getTargetNamespace(), decl.getName())); aoqi@0: } aoqi@0: aoqi@0: }