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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 408
b0610cd08440
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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;
ohair@286 44
ohair@286 45 import com.sun.xml.internal.rngom.nc.ChoiceNameClass;
ohair@286 46 import com.sun.xml.internal.rngom.nc.NameClass;
ohair@286 47 import com.sun.xml.internal.rngom.nc.SimpleNameClass;
ohair@286 48
ohair@286 49 /**
ohair@286 50 * Binds a complex type derived from another complex type by extension.
ohair@286 51 *
ohair@286 52 * @author
ohair@286 53 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
ohair@286 54 */
ohair@286 55 abstract class AbstractExtendedComplexTypeBuilder extends CTBuilder {
ohair@286 56
ohair@286 57 /**
ohair@286 58 * Map from {@link XSComplexType} to {@link NameClass}[2] that
ohair@286 59 * represents the names used in its child elements [0] and
ohair@286 60 * attributes [1].
ohair@286 61 */
ohair@286 62 protected final Map<XSComplexType, NameClass[]> characteristicNameClasses = new HashMap<XSComplexType, NameClass[]>();
ohair@286 63
ohair@286 64 /**
ohair@286 65 * Computes a name class that represents everything in a given content model.
ohair@286 66 */
ohair@286 67 protected final XSTermFunction<NameClass> contentModelNameClassBuilder = new XSTermFunction<NameClass>() {
ohair@286 68 public NameClass wildcard(XSWildcard wc) {
ohair@286 69 return WildcardNameClassBuilder.build(wc);
ohair@286 70 }
ohair@286 71
ohair@286 72 public NameClass modelGroupDecl(XSModelGroupDecl decl) {
ohair@286 73 return modelGroup(decl.getModelGroup());
ohair@286 74 }
ohair@286 75
ohair@286 76 public NameClass modelGroup(XSModelGroup group) {
ohair@286 77 NameClass nc = NameClass.NULL;
ohair@286 78 for( int i=0; i<group.getSize(); i++ )
ohair@286 79 nc = new ChoiceNameClass(nc, group.getChild(i).getTerm().apply(this));
ohair@286 80 return nc;
ohair@286 81 }
ohair@286 82
ohair@286 83 public NameClass elementDecl(XSElementDecl decl) {
ohair@286 84 return getNameClass(decl);
ohair@286 85 }
ohair@286 86 };
ohair@286 87
ohair@286 88 /**
ohair@286 89 * Checks if the particles/attributes defined in the type parameter
ohair@286 90 * collides with the name classes of anc/enc.
ohair@286 91 *
ohair@286 92 * @return true if there's a collision.
ohair@286 93 */
ohair@286 94 protected boolean checkCollision(NameClass anc, NameClass enc, XSComplexType type) {
ohair@286 95 NameClass[] chnc = characteristicNameClasses.get(type);
ohair@286 96 if (chnc == null) {
ohair@286 97 chnc = new NameClass[2];
ohair@286 98 chnc[0] = getNameClass(type.getContentType());
ohair@286 99
ohair@286 100 // build attribute name classes
ohair@286 101 NameClass nc = NameClass.NULL;
ohair@286 102 Iterator itr = type.iterateAttributeUses();
ohair@286 103 while( itr.hasNext() )
ohair@286 104 anc = new ChoiceNameClass(anc, getNameClass(((XSAttributeUse) itr.next()).getDecl()));
ohair@286 105 XSWildcard wc = type.getAttributeWildcard();
ohair@286 106 if(wc!=null)
ohair@286 107 nc = new ChoiceNameClass(nc, WildcardNameClassBuilder.build(wc));
ohair@286 108 chnc[1] = nc;
ohair@286 109
ohair@286 110 characteristicNameClasses.put(type, chnc);
ohair@286 111 }
ohair@286 112
ohair@286 113 return chnc[0].hasOverlapWith(enc) || chnc[1].hasOverlapWith(anc);
ohair@286 114 }
ohair@286 115
ohair@286 116 /**
ohair@286 117 * Looks for the derivation chain t_1 > t_2 > ... > t
ohair@286 118 * and find t_i such that t_i derives by restriction but
ohair@286 119 * for every j>i, t_j derives by extension.
ohair@286 120 *
ohair@286 121 * @return null
ohair@286 122 * If there's no such t_i or if t_i is any type.
ohair@286 123 */
ohair@286 124 protected XSComplexType getLastRestrictedType(XSComplexType t) {
ohair@286 125 if (t.getBaseType() == schemas.getAnyType()) {
ohair@286 126 return null; // we don't count the restriction from anyType
ohair@286 127 }
ohair@286 128 if (t.getDerivationMethod() == XSType.RESTRICTION) {
ohair@286 129 return t;
ohair@286 130 }
ohair@286 131
ohair@286 132 XSComplexType baseType = t.getBaseType().asComplexType();
ohair@286 133 if (baseType != null) {
ohair@286 134 return getLastRestrictedType(baseType);
ohair@286 135 } else {
ohair@286 136 return null;
ohair@286 137 }
ohair@286 138 }
ohair@286 139
ohair@286 140 /**
ohair@286 141 * Checks if this new extension is safe.
ohair@286 142 *
ohair@286 143 * UGLY.
ohair@286 144 * <p>
ohair@286 145 * If you have ctA extending ctB and ctB restricting ctC, our
ohair@286 146 * Java classes will look like CtAImpl extending CtBImpl
ohair@286 147 * extending CtCImpl.
ohair@286 148 *
ohair@286 149 * <p>
ohair@286 150 * Since a derived class unmarshaller uses the base class unmarshaller,
ohair@286 151 * this could potentially result in incorrect unmarshalling.
ohair@286 152 * We used to just reject such a case, but then we found that
ohair@286 153 * there are schemas that are using it.
ohair@286 154 *
ohair@286 155 * <p>
ohair@286 156 * One generalized observation that we reached is that if the extension
ohair@286 157 * is only adding new elements/attributes which has never been used
ohair@286 158 * in any of its base class (IOW, if none of the particle / attribute use /
ohair@286 159 * attribute wildcard can match the name of newly added elements/attributes)
ohair@286 160 * then it is safe to add them.
ohair@286 161 *
ohair@286 162 * <p>
ohair@286 163 * This function checks if the derivation chain to this type is
ohair@286 164 * not using restriction, and if it is, then checks if it is safe
ohair@286 165 * according to the above condition.
ohair@286 166 *
ohair@286 167 * @return false
ohair@286 168 * If this complex type needs to be rejected.
ohair@286 169 */
ohair@286 170 protected boolean checkIfExtensionSafe(XSComplexType baseType, XSComplexType thisType) {
ohair@286 171 XSComplexType lastType = getLastRestrictedType(baseType);
ohair@286 172
ohair@286 173 if (lastType == null) {
ohair@286 174 return true; // no restriction in derivation chain
ohair@286 175 }
ohair@286 176 NameClass anc = NameClass.NULL;
ohair@286 177 // build name class for attributes in new complex type
ohair@286 178 Iterator itr = thisType.iterateDeclaredAttributeUses();
ohair@286 179 while (itr.hasNext()) {
ohair@286 180 anc = new ChoiceNameClass(anc, getNameClass(((XSAttributeUse) itr.next()).getDecl()));
ohair@286 181 }
ohair@286 182 // TODO: attribute wildcard
ohair@286 183
ohair@286 184 NameClass enc = getNameClass(thisType.getExplicitContent());
ohair@286 185
ohair@286 186 // check against every base type ... except the root anyType
ohair@286 187 while (lastType != lastType.getBaseType()) {
ohair@286 188 if (checkCollision(anc, enc, lastType)) {
ohair@286 189 return false;
ohair@286 190 }
ohair@286 191
ohair@286 192 if (lastType.getBaseType().isSimpleType()) // if the base type is a simple type, there won't be
ohair@286 193 // any further name collision.
ohair@286 194 {
ohair@286 195 return true;
ohair@286 196 }
ohair@286 197
ohair@286 198 lastType = lastType.getBaseType().asComplexType();
ohair@286 199 }
ohair@286 200
ohair@286 201 return true; // OK
ohair@286 202 }
ohair@286 203
ohair@286 204 /**
ohair@286 205 * Gets a {@link NameClass} that represents all the terms in the given content type.
ohair@286 206 * If t is not a particle, just return an empty name class.
ohair@286 207 */
ohair@286 208 private NameClass getNameClass(XSContentType t) {
ohair@286 209 if(t==null) return NameClass.NULL;
ohair@286 210 XSParticle p = t.asParticle();
ohair@286 211 if(p==null) return NameClass.NULL;
ohair@286 212 else return p.getTerm().apply(contentModelNameClassBuilder);
ohair@286 213 }
ohair@286 214
ohair@286 215 /**
ohair@286 216 * Gets a {@link SimpleNameClass} from the name of a {@link XSDeclaration}.
ohair@286 217 */
ohair@286 218 private NameClass getNameClass(XSDeclaration decl) {
ohair@286 219 return new SimpleNameClass(decl.getTargetNamespace(), decl.getName());
ohair@286 220 }
ohair@286 221
ohair@286 222 }

mercurial