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

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

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

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

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