1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/RawTypeSetBuilder.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,379 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.xjc.reader.xmlschema; 1.30 + 1.31 +import java.util.LinkedHashSet; 1.32 +import java.util.Set; 1.33 + 1.34 +import javax.activation.MimeType; 1.35 +import javax.xml.namespace.QName; 1.36 + 1.37 +import com.sun.tools.internal.xjc.model.CAdapter; 1.38 +import com.sun.tools.internal.xjc.model.CClass; 1.39 +import com.sun.tools.internal.xjc.model.CClassInfo; 1.40 +import com.sun.tools.internal.xjc.model.CCustomizations; 1.41 +import com.sun.tools.internal.xjc.model.CElement; 1.42 +import com.sun.tools.internal.xjc.model.CElementInfo; 1.43 +import com.sun.tools.internal.xjc.model.CElementPropertyInfo; 1.44 +import com.sun.tools.internal.xjc.model.CReferencePropertyInfo; 1.45 +import com.sun.tools.internal.xjc.model.CTypeRef; 1.46 +import com.sun.tools.internal.xjc.model.Model; 1.47 +import com.sun.tools.internal.xjc.model.Multiplicity; 1.48 +import com.sun.tools.internal.xjc.model.TypeUse; 1.49 +import com.sun.tools.internal.xjc.reader.RawTypeSet; 1.50 +import com.sun.tools.internal.xjc.reader.Ring; 1.51 +import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIDom; 1.52 +import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIGlobalBinding; 1.53 +import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIXSubstitutable; 1.54 +import com.sun.xml.internal.bind.v2.model.core.ID; 1.55 +import com.sun.xml.internal.bind.v2.model.core.WildcardMode; 1.56 +import com.sun.xml.internal.xsom.XSElementDecl; 1.57 +import com.sun.xml.internal.xsom.XSModelGroup; 1.58 +import com.sun.xml.internal.xsom.XSModelGroupDecl; 1.59 +import com.sun.xml.internal.xsom.XSParticle; 1.60 +import com.sun.xml.internal.xsom.XSWildcard; 1.61 +import com.sun.xml.internal.xsom.visitor.XSTermVisitor; 1.62 + 1.63 +/** 1.64 + * Builds {@link RawTypeSet} for XML Schema. 1.65 + * 1.66 + * @author Kohsuke Kawaguchi 1.67 + */ 1.68 +public class RawTypeSetBuilder implements XSTermVisitor { 1.69 + /** 1.70 + * @param optional 1.71 + * if this whole property is optional due to the 1.72 + * occurrence constraints on ancestors, set this to true. 1.73 + * this will prevent the primitive types to be generated. 1.74 + */ 1.75 + public static RawTypeSet build( XSParticle p, boolean optional ) { 1.76 + RawTypeSetBuilder rtsb = new RawTypeSetBuilder(); 1.77 + rtsb.particle(p); 1.78 + Multiplicity mul = MultiplicityCounter.theInstance.particle(p); 1.79 + 1.80 + if(optional) 1.81 + mul = mul.makeOptional(); 1.82 + 1.83 + return new RawTypeSet(rtsb.refs,mul); 1.84 + } 1.85 + 1.86 + /** 1.87 + * To avoid declaring the same element twice for a content model like 1.88 + * (A,A), we keep track of element names here while we are building up 1.89 + * this instance. 1.90 + */ 1.91 + private final Set<QName> elementNames = new LinkedHashSet<QName>(); 1.92 + 1.93 + private final Set<RawTypeSet.Ref> refs = new LinkedHashSet<RawTypeSet.Ref>(); 1.94 + 1.95 + protected final BGMBuilder builder = Ring.get(BGMBuilder.class); 1.96 + 1.97 + public RawTypeSetBuilder() {} 1.98 + 1.99 + 1.100 + /** 1.101 + * Gets the {@link RawTypeSet.Ref}s that were built. 1.102 + */ 1.103 + public Set<RawTypeSet.Ref> getRefs() { 1.104 + return refs; 1.105 + } 1.106 + 1.107 + /** 1.108 + * Build up {@link #refs} and compute the total multiplicity of this {@link RawTypeSet.Ref} set. 1.109 + */ 1.110 + private void particle( XSParticle p ) { 1.111 + // if the DOM customization is present, bind it like a wildcard 1.112 + BIDom dom = builder.getLocalDomCustomization(p); 1.113 + if(dom!=null) { 1.114 + dom.markAsAcknowledged(); 1.115 + refs.add(new WildcardRef(WildcardMode.SKIP)); 1.116 + } else { 1.117 + p.getTerm().visit(this); 1.118 + } 1.119 + } 1.120 + 1.121 + public void wildcard(XSWildcard wc) { 1.122 + refs.add(new WildcardRef(wc)); 1.123 + } 1.124 + 1.125 + public void modelGroupDecl(XSModelGroupDecl decl) { 1.126 + modelGroup(decl.getModelGroup()); 1.127 + } 1.128 + 1.129 + public void modelGroup(XSModelGroup group) { 1.130 + for( XSParticle p : group.getChildren()) 1.131 + particle(p); 1.132 + } 1.133 + 1.134 + public void elementDecl(XSElementDecl decl) { 1.135 + 1.136 + QName n = BGMBuilder.getName(decl); 1.137 + if(elementNames.add(n)) { 1.138 + CElement elementBean = Ring.get(ClassSelector.class).bindToType(decl,null); 1.139 + if(elementBean==null) 1.140 + refs.add(new XmlTypeRef(decl)); 1.141 + else { 1.142 + // yikes! 1.143 + if(elementBean instanceof CClass) 1.144 + refs.add(new CClassRef(decl,(CClass)elementBean)); 1.145 + else 1.146 + refs.add(new CElementInfoRef(decl,(CElementInfo)elementBean)); 1.147 + } 1.148 + } 1.149 + } 1.150 + 1.151 + /** 1.152 + * Reference to a wildcard. 1.153 + */ 1.154 + public static final class WildcardRef extends RawTypeSet.Ref { 1.155 + private final WildcardMode mode; 1.156 + 1.157 + WildcardRef(XSWildcard wildcard) { 1.158 + this.mode = getMode(wildcard); 1.159 + } 1.160 + WildcardRef(WildcardMode mode) { 1.161 + this.mode = mode; 1.162 + } 1.163 + 1.164 + private static WildcardMode getMode(XSWildcard wildcard) { 1.165 + switch(wildcard.getMode()) { 1.166 + case XSWildcard.LAX: 1.167 + return WildcardMode.LAX; 1.168 + case XSWildcard.STRTICT: 1.169 + return WildcardMode.STRICT; 1.170 + case XSWildcard.SKIP: 1.171 + return WildcardMode.SKIP; 1.172 + default: 1.173 + throw new IllegalStateException(); 1.174 + } 1.175 + } 1.176 + 1.177 + protected CTypeRef toTypeRef(CElementPropertyInfo ep) { 1.178 + // we don't allow a mapping to typeRef if the wildcard is present 1.179 + throw new IllegalStateException(); 1.180 + } 1.181 + 1.182 + protected void toElementRef(CReferencePropertyInfo prop) { 1.183 + prop.setWildcard(mode); 1.184 + } 1.185 + 1.186 + protected RawTypeSet.Mode canBeType(RawTypeSet parent) { 1.187 + return RawTypeSet.Mode.MUST_BE_REFERENCE; 1.188 + } 1.189 + 1.190 + protected boolean isListOfValues() { 1.191 + return false; 1.192 + } 1.193 + 1.194 + protected ID id() { 1.195 + return ID.NONE; 1.196 + } 1.197 + } 1.198 + 1.199 + /** 1.200 + * Reference to a class that maps from an element. 1.201 + */ 1.202 + public static final class CClassRef extends RawTypeSet.Ref { 1.203 + public final CClass target; 1.204 + public final XSElementDecl decl; 1.205 + 1.206 + CClassRef(XSElementDecl decl, CClass target) { 1.207 + this.decl = decl; 1.208 + this.target = target; 1.209 + } 1.210 + 1.211 + protected CTypeRef toTypeRef(CElementPropertyInfo ep) { 1.212 + return new CTypeRef(target,decl); 1.213 + } 1.214 + 1.215 + protected void toElementRef(CReferencePropertyInfo prop) { 1.216 + prop.getElements().add(target); 1.217 + } 1.218 + 1.219 + protected RawTypeSet.Mode canBeType(RawTypeSet parent) { 1.220 + // if element substitution can occur, no way it can be mapped to a list of types 1.221 + if(decl.getSubstitutables().size()>1) 1.222 + return RawTypeSet.Mode.MUST_BE_REFERENCE; 1.223 + 1.224 + return RawTypeSet.Mode.SHOULD_BE_TYPEREF; 1.225 + } 1.226 + 1.227 + protected boolean isListOfValues() { 1.228 + return false; 1.229 + } 1.230 + 1.231 + protected ID id() { 1.232 + return ID.NONE; 1.233 + } 1.234 + } 1.235 + 1.236 + /** 1.237 + * Reference to a class that maps from an element. 1.238 + */ 1.239 + public final class CElementInfoRef extends RawTypeSet.Ref { 1.240 + public final CElementInfo target; 1.241 + public final XSElementDecl decl; 1.242 + 1.243 + CElementInfoRef(XSElementDecl decl, CElementInfo target) { 1.244 + this.decl = decl; 1.245 + this.target = target; 1.246 + } 1.247 + 1.248 + protected CTypeRef toTypeRef(CElementPropertyInfo ep) { 1.249 + assert !target.isCollection(); 1.250 + CAdapter a = target.getProperty().getAdapter(); 1.251 + if(a!=null && ep!=null) ep.setAdapter(a); 1.252 + 1.253 + return new CTypeRef(target.getContentType(),decl); 1.254 + } 1.255 + 1.256 + protected void toElementRef(CReferencePropertyInfo prop) { 1.257 + prop.getElements().add(target); 1.258 + } 1.259 + 1.260 + protected RawTypeSet.Mode canBeType(RawTypeSet parent) { 1.261 + // if element substitution can occur, no way it can be mapped to a list of types 1.262 + if(decl.getSubstitutables().size()>1) 1.263 + return RawTypeSet.Mode.MUST_BE_REFERENCE; 1.264 + // BIXSubstitutable also simulates this effect. Useful for separate compilation 1.265 + BIXSubstitutable subst = builder.getBindInfo(decl).get(BIXSubstitutable.class); 1.266 + if(subst!=null) { 1.267 + subst.markAsAcknowledged(); 1.268 + return RawTypeSet.Mode.MUST_BE_REFERENCE; 1.269 + } 1.270 + 1.271 + // we have no place to put an adater if this thing maps to a type 1.272 + CElementPropertyInfo p = target.getProperty(); 1.273 + // if we have an adapter or IDness, which requires special 1.274 + // annotation, and there's more than one element, 1.275 + // we have no place to put the special annotation, so we need JAXBElement. 1.276 + if((parent.refs.size()>1 || !parent.mul.isAtMostOnce()) && p.id()!=ID.NONE) 1.277 + return RawTypeSet.Mode.MUST_BE_REFERENCE; 1.278 + if(parent.refs.size() > 1 && p.getAdapter() != null) 1.279 + return RawTypeSet.Mode.MUST_BE_REFERENCE; 1.280 + 1.281 + if(target.hasClass()) 1.282 + // if the CElementInfo was explicitly bound to a class (which happen if and only if 1.283 + // the user requested so, then map that to reference property so that the user sees a class 1.284 + return RawTypeSet.Mode.CAN_BE_TYPEREF; 1.285 + else 1.286 + return RawTypeSet.Mode.SHOULD_BE_TYPEREF; 1.287 + } 1.288 + 1.289 + protected boolean isListOfValues() { 1.290 + return target.getProperty().isValueList(); 1.291 + } 1.292 + 1.293 + protected ID id() { 1.294 + return target.getProperty().id(); 1.295 + } 1.296 + 1.297 + @Override 1.298 + protected MimeType getExpectedMimeType() { 1.299 + return target.getProperty().getExpectedMimeType(); 1.300 + } 1.301 + } 1.302 + 1.303 + /** 1.304 + * References to a type. Could be global or local. 1.305 + */ 1.306 + public static final class XmlTypeRef extends RawTypeSet.Ref { 1.307 + private final XSElementDecl decl; 1.308 + private final TypeUse target; 1.309 + 1.310 + public XmlTypeRef(XSElementDecl decl) { 1.311 + this.decl = decl; 1.312 + SimpleTypeBuilder stb = Ring.get(SimpleTypeBuilder.class); 1.313 + stb.refererStack.push(decl); 1.314 + TypeUse r = Ring.get(ClassSelector.class).bindToType(decl.getType(),decl); 1.315 + stb.refererStack.pop(); 1.316 + target = r; 1.317 + } 1.318 + 1.319 + protected CTypeRef toTypeRef(CElementPropertyInfo ep) { 1.320 + if(ep!=null && target.getAdapterUse()!=null) 1.321 + ep.setAdapter(target.getAdapterUse()); 1.322 + return new CTypeRef(target.getInfo(),decl); 1.323 + } 1.324 + 1.325 + /** 1.326 + * The whole type set can be later bound to a reference property, 1.327 + * in which case we need to generate additional code to wrap this 1.328 + * type reference into an element class. 1.329 + * 1.330 + * This method generates such an element class and returns it. 1.331 + */ 1.332 + protected void toElementRef(CReferencePropertyInfo prop) { 1.333 + CClassInfo scope = Ring.get(ClassSelector.class).getCurrentBean(); 1.334 + Model model = Ring.get(Model.class); 1.335 + 1.336 + CCustomizations custs = Ring.get(BGMBuilder.class).getBindInfo(decl).toCustomizationList(); 1.337 + 1.338 + if(target instanceof CClassInfo && Ring.get(BIGlobalBinding.class).isSimpleMode()) { 1.339 + CClassInfo bean = new CClassInfo(model,scope, 1.340 + model.getNameConverter().toClassName(decl.getName()), 1.341 + decl.getLocator(), null, BGMBuilder.getName(decl), decl, 1.342 + custs); 1.343 + bean.setBaseClass((CClassInfo)target); 1.344 + prop.getElements().add(bean); 1.345 + } else { 1.346 + CElementInfo e = new CElementInfo(model,BGMBuilder.getName(decl),scope,target, 1.347 + decl.getDefaultValue(), decl, custs, decl.getLocator()); 1.348 + prop.getElements().add(e); 1.349 + } 1.350 + } 1.351 + 1.352 + protected RawTypeSet.Mode canBeType(RawTypeSet parent) { 1.353 + // if we have an adapter or IDness, which requires special 1.354 + // annotation, and there's more than one element, 1.355 + // we have no place to put the special annotation, so we need JAXBElement. 1.356 + if((parent.refs.size()>1 || !parent.mul.isAtMostOnce()) && target.idUse()!=ID.NONE) 1.357 + return RawTypeSet.Mode.MUST_BE_REFERENCE; 1.358 + if(parent.refs.size() > 1 && target.getAdapterUse() != null) 1.359 + return RawTypeSet.Mode.MUST_BE_REFERENCE; 1.360 + 1.361 + // nillable and optional at the same time. needs an element wrapper to distinguish those 1.362 + // two states. But this is not a hard requirement. 1.363 + if(decl.isNillable() && parent.mul.isOptional()) 1.364 + return RawTypeSet.Mode.CAN_BE_TYPEREF; 1.365 + 1.366 + return RawTypeSet.Mode.SHOULD_BE_TYPEREF; 1.367 + } 1.368 + 1.369 + protected boolean isListOfValues() { 1.370 + return target.isCollection(); 1.371 + } 1.372 + 1.373 + protected ID id() { 1.374 + return target.idUse(); 1.375 + } 1.376 + 1.377 + @Override 1.378 + protected MimeType getExpectedMimeType() { 1.379 + return target.getExpectedMimeType(); 1.380 + } 1.381 + } 1.382 +}