src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/ct/AbstractExtendedComplexTypeBuilder.java

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.tools.internal.xjc.reader.xmlschema.ct;
ohair@286 27
ohair@286 28 import java.util.HashMap;
ohair@286 29 import java.util.Iterator;
ohair@286 30 import java.util.Map;
ohair@286 31
ohair@286 32 import com.sun.tools.internal.xjc.reader.xmlschema.WildcardNameClassBuilder;
ohair@286 33 import com.sun.xml.internal.xsom.XSAttributeUse;
ohair@286 34 import com.sun.xml.internal.xsom.XSComplexType;
ohair@286 35 import com.sun.xml.internal.xsom.XSContentType;
ohair@286 36 import com.sun.xml.internal.xsom.XSDeclaration;
ohair@286 37 import com.sun.xml.internal.xsom.XSElementDecl;
ohair@286 38 import com.sun.xml.internal.xsom.XSModelGroup;
ohair@286 39 import com.sun.xml.internal.xsom.XSModelGroupDecl;
ohair@286 40 import com.sun.xml.internal.xsom.XSParticle;
ohair@286 41 import com.sun.xml.internal.xsom.XSType;
ohair@286 42 import com.sun.xml.internal.xsom.XSWildcard;
ohair@286 43 import com.sun.xml.internal.xsom.visitor.XSTermFunction;
mkos@408 44 import javax.xml.namespace.QName;
ohair@286 45
ohair@286 46 import com.sun.xml.internal.rngom.nc.ChoiceNameClass;
ohair@286 47 import com.sun.xml.internal.rngom.nc.NameClass;
ohair@286 48 import com.sun.xml.internal.rngom.nc.SimpleNameClass;
ohair@286 49
ohair@286 50 /**
ohair@286 51 * Binds a complex type derived from another complex type by extension.
ohair@286 52 *
ohair@286 53 * @author
ohair@286 54 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
ohair@286 55 */
ohair@286 56 abstract class AbstractExtendedComplexTypeBuilder extends CTBuilder {
ohair@286 57
ohair@286 58 /**
ohair@286 59 * Map from {@link XSComplexType} to {@link NameClass}[2] that
ohair@286 60 * represents the names used in its child elements [0] and
ohair@286 61 * attributes [1].
ohair@286 62 */
ohair@286 63 protected final Map<XSComplexType, NameClass[]> characteristicNameClasses = new HashMap<XSComplexType, NameClass[]>();
ohair@286 64
ohair@286 65 /**
ohair@286 66 * Computes a name class that represents everything in a given content model.
ohair@286 67 */
ohair@286 68 protected final XSTermFunction<NameClass> contentModelNameClassBuilder = new XSTermFunction<NameClass>() {
mkos@408 69 @Override
ohair@286 70 public NameClass wildcard(XSWildcard wc) {
ohair@286 71 return WildcardNameClassBuilder.build(wc);
ohair@286 72 }
ohair@286 73
mkos@408 74 @Override
ohair@286 75 public NameClass modelGroupDecl(XSModelGroupDecl decl) {
ohair@286 76 return modelGroup(decl.getModelGroup());
ohair@286 77 }
ohair@286 78
mkos@408 79 @Override
ohair@286 80 public NameClass modelGroup(XSModelGroup group) {
ohair@286 81 NameClass nc = NameClass.NULL;
ohair@286 82 for( int i=0; i<group.getSize(); i++ )
ohair@286 83 nc = new ChoiceNameClass(nc, group.getChild(i).getTerm().apply(this));
ohair@286 84 return nc;
ohair@286 85 }
ohair@286 86
ohair@286 87 public NameClass elementDecl(XSElementDecl decl) {
ohair@286 88 return getNameClass(decl);
ohair@286 89 }
ohair@286 90 };
ohair@286 91
ohair@286 92 /**
ohair@286 93 * Checks if the particles/attributes defined in the type parameter
ohair@286 94 * collides with the name classes of anc/enc.
ohair@286 95 *
ohair@286 96 * @return true if there's a collision.
ohair@286 97 */
ohair@286 98 protected boolean checkCollision(NameClass anc, NameClass enc, XSComplexType type) {
ohair@286 99 NameClass[] chnc = characteristicNameClasses.get(type);
ohair@286 100 if (chnc == null) {
ohair@286 101 chnc = new NameClass[2];
ohair@286 102 chnc[0] = getNameClass(type.getContentType());
ohair@286 103
ohair@286 104 // build attribute name classes
ohair@286 105 NameClass nc = NameClass.NULL;
ohair@286 106 Iterator itr = type.iterateAttributeUses();
ohair@286 107 while( itr.hasNext() )
ohair@286 108 anc = new ChoiceNameClass(anc, getNameClass(((XSAttributeUse) itr.next()).getDecl()));
ohair@286 109 XSWildcard wc = type.getAttributeWildcard();
ohair@286 110 if(wc!=null)
ohair@286 111 nc = new ChoiceNameClass(nc, WildcardNameClassBuilder.build(wc));
ohair@286 112 chnc[1] = nc;
ohair@286 113
ohair@286 114 characteristicNameClasses.put(type, chnc);
ohair@286 115 }
ohair@286 116
ohair@286 117 return chnc[0].hasOverlapWith(enc) || chnc[1].hasOverlapWith(anc);
ohair@286 118 }
ohair@286 119
ohair@286 120 /**
ohair@286 121 * Looks for the derivation chain t_1 > t_2 > ... > t
ohair@286 122 * and find t_i such that t_i derives by restriction but
ohair@286 123 * for every j>i, t_j derives by extension.
ohair@286 124 *
ohair@286 125 * @return null
ohair@286 126 * If there's no such t_i or if t_i is any type.
ohair@286 127 */
ohair@286 128 protected XSComplexType getLastRestrictedType(XSComplexType t) {
ohair@286 129 if (t.getBaseType() == schemas.getAnyType()) {
ohair@286 130 return null; // we don't count the restriction from anyType
ohair@286 131 }
ohair@286 132 if (t.getDerivationMethod() == XSType.RESTRICTION) {
ohair@286 133 return t;
ohair@286 134 }
ohair@286 135
ohair@286 136 XSComplexType baseType = t.getBaseType().asComplexType();
ohair@286 137 if (baseType != null) {
ohair@286 138 return getLastRestrictedType(baseType);
ohair@286 139 } else {
ohair@286 140 return null;
ohair@286 141 }
ohair@286 142 }
ohair@286 143
ohair@286 144 /**
ohair@286 145 * Checks if this new extension is safe.
ohair@286 146 *
ohair@286 147 * UGLY.
ohair@286 148 * <p>
ohair@286 149 * If you have ctA extending ctB and ctB restricting ctC, our
ohair@286 150 * Java classes will look like CtAImpl extending CtBImpl
ohair@286 151 * extending CtCImpl.
ohair@286 152 *
ohair@286 153 * <p>
ohair@286 154 * Since a derived class unmarshaller uses the base class unmarshaller,
ohair@286 155 * this could potentially result in incorrect unmarshalling.
ohair@286 156 * We used to just reject such a case, but then we found that
ohair@286 157 * there are schemas that are using it.
ohair@286 158 *
ohair@286 159 * <p>
ohair@286 160 * One generalized observation that we reached is that if the extension
ohair@286 161 * is only adding new elements/attributes which has never been used
ohair@286 162 * in any of its base class (IOW, if none of the particle / attribute use /
ohair@286 163 * attribute wildcard can match the name of newly added elements/attributes)
ohair@286 164 * then it is safe to add them.
ohair@286 165 *
ohair@286 166 * <p>
ohair@286 167 * This function checks if the derivation chain to this type is
ohair@286 168 * not using restriction, and if it is, then checks if it is safe
ohair@286 169 * according to the above condition.
ohair@286 170 *
ohair@286 171 * @return false
ohair@286 172 * If this complex type needs to be rejected.
ohair@286 173 */
ohair@286 174 protected boolean checkIfExtensionSafe(XSComplexType baseType, XSComplexType thisType) {
ohair@286 175 XSComplexType lastType = getLastRestrictedType(baseType);
ohair@286 176
ohair@286 177 if (lastType == null) {
ohair@286 178 return true; // no restriction in derivation chain
ohair@286 179 }
ohair@286 180 NameClass anc = NameClass.NULL;
ohair@286 181 // build name class for attributes in new complex type
ohair@286 182 Iterator itr = thisType.iterateDeclaredAttributeUses();
ohair@286 183 while (itr.hasNext()) {
ohair@286 184 anc = new ChoiceNameClass(anc, getNameClass(((XSAttributeUse) itr.next()).getDecl()));
ohair@286 185 }
ohair@286 186 // TODO: attribute wildcard
ohair@286 187
ohair@286 188 NameClass enc = getNameClass(thisType.getExplicitContent());
ohair@286 189
ohair@286 190 // check against every base type ... except the root anyType
ohair@286 191 while (lastType != lastType.getBaseType()) {
ohair@286 192 if (checkCollision(anc, enc, lastType)) {
ohair@286 193 return false;
ohair@286 194 }
ohair@286 195
ohair@286 196 if (lastType.getBaseType().isSimpleType()) // if the base type is a simple type, there won't be
ohair@286 197 // any further name collision.
ohair@286 198 {
ohair@286 199 return true;
ohair@286 200 }
ohair@286 201
ohair@286 202 lastType = lastType.getBaseType().asComplexType();
ohair@286 203 }
ohair@286 204
ohair@286 205 return true; // OK
ohair@286 206 }
ohair@286 207
ohair@286 208 /**
ohair@286 209 * Gets a {@link NameClass} that represents all the terms in the given content type.
ohair@286 210 * If t is not a particle, just return an empty name class.
ohair@286 211 */
ohair@286 212 private NameClass getNameClass(XSContentType t) {
ohair@286 213 if(t==null) return NameClass.NULL;
ohair@286 214 XSParticle p = t.asParticle();
ohair@286 215 if(p==null) return NameClass.NULL;
ohair@286 216 else return p.getTerm().apply(contentModelNameClassBuilder);
ohair@286 217 }
ohair@286 218
ohair@286 219 /**
ohair@286 220 * Gets a {@link SimpleNameClass} from the name of a {@link XSDeclaration}.
ohair@286 221 */
ohair@286 222 private NameClass getNameClass(XSDeclaration decl) {
mkos@408 223 return new SimpleNameClass(new QName(decl.getTargetNamespace(), decl.getName()));
ohair@286 224 }
ohair@286 225
ohair@286 226 }

mercurial