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

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 637
9c07ef4934dd
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

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

mercurial