Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, 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; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.util.ArrayList; |
aoqi@0 | 29 | import java.util.HashMap; |
aoqi@0 | 30 | import java.util.List; |
aoqi@0 | 31 | import java.util.Map; |
aoqi@0 | 32 | import java.util.Set; |
aoqi@0 | 33 | |
aoqi@0 | 34 | import javax.xml.namespace.QName; |
aoqi@0 | 35 | import javax.xml.transform.Transformer; |
aoqi@0 | 36 | import javax.xml.transform.TransformerConfigurationException; |
aoqi@0 | 37 | import javax.xml.transform.TransformerFactory; |
aoqi@0 | 38 | |
aoqi@0 | 39 | import com.sun.codemodel.internal.JCodeModel; |
aoqi@0 | 40 | import com.sun.codemodel.internal.fmt.JTextFile; |
aoqi@0 | 41 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 42 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 43 | import com.sun.tools.internal.xjc.ErrorReceiver; |
aoqi@0 | 44 | import com.sun.tools.internal.xjc.Options; |
aoqi@0 | 45 | import com.sun.tools.internal.xjc.Plugin; |
aoqi@0 | 46 | import com.sun.tools.internal.xjc.generator.bean.field.FieldRendererFactory; |
aoqi@0 | 47 | import com.sun.tools.internal.xjc.model.CClassInfoParent; |
aoqi@0 | 48 | import com.sun.tools.internal.xjc.model.Model; |
aoqi@0 | 49 | import com.sun.tools.internal.xjc.reader.ModelChecker; |
aoqi@0 | 50 | import com.sun.tools.internal.xjc.reader.Ring; |
aoqi@0 | 51 | import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIDeclaration; |
aoqi@0 | 52 | import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIDom; |
aoqi@0 | 53 | import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIGlobalBinding; |
aoqi@0 | 54 | import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BISchemaBinding; |
aoqi@0 | 55 | import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BISerializable; |
aoqi@0 | 56 | import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BindInfo; |
aoqi@0 | 57 | import com.sun.tools.internal.xjc.util.CodeModelClassFactory; |
aoqi@0 | 58 | import com.sun.tools.internal.xjc.util.ErrorReceiverFilter; |
aoqi@0 | 59 | import com.sun.xml.internal.bind.api.impl.NameConverter; |
aoqi@0 | 60 | import com.sun.xml.internal.bind.v2.util.XmlFactory; |
aoqi@0 | 61 | import com.sun.xml.internal.xsom.XSAnnotation; |
aoqi@0 | 62 | import com.sun.xml.internal.xsom.XSAttributeUse; |
aoqi@0 | 63 | import com.sun.xml.internal.xsom.XSComponent; |
aoqi@0 | 64 | import com.sun.xml.internal.xsom.XSDeclaration; |
aoqi@0 | 65 | import com.sun.xml.internal.xsom.XSParticle; |
aoqi@0 | 66 | import com.sun.xml.internal.xsom.XSSchema; |
aoqi@0 | 67 | import com.sun.xml.internal.xsom.XSSchemaSet; |
aoqi@0 | 68 | import com.sun.xml.internal.xsom.XSSimpleType; |
aoqi@0 | 69 | import com.sun.xml.internal.xsom.XSTerm; |
aoqi@0 | 70 | import com.sun.xml.internal.xsom.XSType; |
aoqi@0 | 71 | import com.sun.xml.internal.xsom.XSWildcard; |
aoqi@0 | 72 | import com.sun.xml.internal.xsom.util.XSFinder; |
aoqi@0 | 73 | |
aoqi@0 | 74 | import org.xml.sax.Locator; |
aoqi@0 | 75 | |
aoqi@0 | 76 | /** |
aoqi@0 | 77 | * Root of the XML Schema binder. |
aoqi@0 | 78 | * |
aoqi@0 | 79 | * <div><img src="doc-files/binding_chart.png"/></div> |
aoqi@0 | 80 | * |
aoqi@0 | 81 | * @author Kohsuke Kawaguchi |
aoqi@0 | 82 | */ |
aoqi@0 | 83 | public class BGMBuilder extends BindingComponent { |
aoqi@0 | 84 | |
aoqi@0 | 85 | /** |
aoqi@0 | 86 | * Entry point. |
aoqi@0 | 87 | */ |
aoqi@0 | 88 | public static Model build( XSSchemaSet _schemas, JCodeModel codeModel, |
aoqi@0 | 89 | ErrorReceiver _errorReceiver, Options opts ) { |
aoqi@0 | 90 | // set up a ring |
aoqi@0 | 91 | final Ring old = Ring.begin(); |
aoqi@0 | 92 | try { |
aoqi@0 | 93 | ErrorReceiverFilter ef = new ErrorReceiverFilter(_errorReceiver); |
aoqi@0 | 94 | |
aoqi@0 | 95 | Ring.add(XSSchemaSet.class,_schemas); |
aoqi@0 | 96 | Ring.add(codeModel); |
aoqi@0 | 97 | Model model = new Model(opts, codeModel, null/*set later*/, opts.classNameAllocator, _schemas); |
aoqi@0 | 98 | Ring.add(model); |
aoqi@0 | 99 | Ring.add(ErrorReceiver.class,ef); |
aoqi@0 | 100 | Ring.add(CodeModelClassFactory.class,new CodeModelClassFactory(ef)); |
aoqi@0 | 101 | |
aoqi@0 | 102 | BGMBuilder builder = new BGMBuilder(opts.defaultPackage,opts.defaultPackage2, |
aoqi@0 | 103 | opts.isExtensionMode(),opts.getFieldRendererFactory(), opts.activePlugins); |
aoqi@0 | 104 | builder._build(); |
aoqi@0 | 105 | |
aoqi@0 | 106 | if(ef.hadError()) return null; |
aoqi@0 | 107 | else return model; |
aoqi@0 | 108 | } finally { |
aoqi@0 | 109 | Ring.end(old); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | } |
aoqi@0 | 112 | |
aoqi@0 | 113 | |
aoqi@0 | 114 | /** |
aoqi@0 | 115 | * True if the compiler is running in the extension mode |
aoqi@0 | 116 | * (as opposed to the strict conformance mode.) |
aoqi@0 | 117 | */ |
aoqi@0 | 118 | public final boolean inExtensionMode; |
aoqi@0 | 119 | |
aoqi@0 | 120 | /** |
aoqi@0 | 121 | * If this is non-null, this package name takes over |
aoqi@0 | 122 | * all the schema customizations. |
aoqi@0 | 123 | */ |
aoqi@0 | 124 | public final String defaultPackage1; |
aoqi@0 | 125 | |
aoqi@0 | 126 | /** |
aoqi@0 | 127 | * If this is non-null, this package name will be |
aoqi@0 | 128 | * used when no customization is specified. |
aoqi@0 | 129 | */ |
aoqi@0 | 130 | public final String defaultPackage2; |
aoqi@0 | 131 | |
aoqi@0 | 132 | private final BindGreen green = Ring.get(BindGreen.class); |
aoqi@0 | 133 | private final BindPurple purple = Ring.get(BindPurple.class); |
aoqi@0 | 134 | |
aoqi@0 | 135 | public final Model model = Ring.get(Model.class); |
aoqi@0 | 136 | |
aoqi@0 | 137 | public final FieldRendererFactory fieldRendererFactory; |
aoqi@0 | 138 | |
aoqi@0 | 139 | /** |
aoqi@0 | 140 | * Lazily computed {@link RefererFinder}. |
aoqi@0 | 141 | * |
aoqi@0 | 142 | * @see #getReferer |
aoqi@0 | 143 | */ |
aoqi@0 | 144 | private RefererFinder refFinder; |
aoqi@0 | 145 | |
aoqi@0 | 146 | private List<Plugin> activePlugins; |
aoqi@0 | 147 | |
aoqi@0 | 148 | protected BGMBuilder(String defaultPackage1, String defaultPackage2, |
aoqi@0 | 149 | boolean _inExtensionMode, FieldRendererFactory fieldRendererFactory, |
aoqi@0 | 150 | List<Plugin> activePlugins) { |
aoqi@0 | 151 | this.inExtensionMode = _inExtensionMode; |
aoqi@0 | 152 | this.defaultPackage1 = defaultPackage1; |
aoqi@0 | 153 | this.defaultPackage2 = defaultPackage2; |
aoqi@0 | 154 | this.fieldRendererFactory = fieldRendererFactory; |
aoqi@0 | 155 | this.activePlugins = activePlugins; |
aoqi@0 | 156 | promoteGlobalBindings(); |
aoqi@0 | 157 | } |
aoqi@0 | 158 | |
aoqi@0 | 159 | private void _build() { |
aoqi@0 | 160 | // do the binding |
aoqi@0 | 161 | buildContents(); |
aoqi@0 | 162 | getClassSelector().executeTasks(); |
aoqi@0 | 163 | |
aoqi@0 | 164 | // additional error check |
aoqi@0 | 165 | // Reports unused customizations to the user as errors. |
aoqi@0 | 166 | Ring.get(UnusedCustomizationChecker.class).run(); |
aoqi@0 | 167 | |
aoqi@0 | 168 | Ring.get(ModelChecker.class).check(); |
aoqi@0 | 169 | |
aoqi@0 | 170 | for( Plugin ma : activePlugins ) |
aoqi@0 | 171 | ma.postProcessModel(model, Ring.get(ErrorReceiver.class)); |
aoqi@0 | 172 | |
aoqi@0 | 173 | } |
aoqi@0 | 174 | |
aoqi@0 | 175 | |
aoqi@0 | 176 | /** List up all the global bindings. */ |
aoqi@0 | 177 | private void promoteGlobalBindings() { |
aoqi@0 | 178 | // promote any global bindings in the schema |
aoqi@0 | 179 | XSSchemaSet schemas = Ring.get(XSSchemaSet.class); |
aoqi@0 | 180 | |
aoqi@0 | 181 | for( XSSchema s : schemas.getSchemas() ) { |
aoqi@0 | 182 | BindInfo bi = getBindInfo(s); |
aoqi@0 | 183 | |
aoqi@0 | 184 | // collect all global customizations |
aoqi@0 | 185 | model.getCustomizations().addAll(bi.toCustomizationList()); |
aoqi@0 | 186 | |
aoqi@0 | 187 | BIGlobalBinding gb = bi.get(BIGlobalBinding.class); |
aoqi@0 | 188 | if(gb==null) |
aoqi@0 | 189 | continue; |
aoqi@0 | 190 | |
aoqi@0 | 191 | gb.markAsAcknowledged(); |
aoqi@0 | 192 | |
aoqi@0 | 193 | if(globalBinding==null) { |
aoqi@0 | 194 | globalBinding = gb; |
aoqi@0 | 195 | } else { |
aoqi@0 | 196 | if (!globalBinding.isEqual(gb)) { // see Issue 687 - this may happen with syntactically imported documents |
aoqi@0 | 197 | // acknowledge this customization and report an error |
aoqi@0 | 198 | // otherwise the user will see "customization is attached to a wrong place" error, |
aoqi@0 | 199 | // which is incorrect |
aoqi@0 | 200 | getErrorReporter().error( gb.getLocation(), |
aoqi@0 | 201 | Messages.ERR_MULTIPLE_GLOBAL_BINDINGS); |
aoqi@0 | 202 | getErrorReporter().error( globalBinding.getLocation(), |
aoqi@0 | 203 | Messages.ERR_MULTIPLE_GLOBAL_BINDINGS_OTHER); |
aoqi@0 | 204 | } |
aoqi@0 | 205 | } |
aoqi@0 | 206 | } |
aoqi@0 | 207 | |
aoqi@0 | 208 | if( globalBinding==null ) { |
aoqi@0 | 209 | // no global customization is present. |
aoqi@0 | 210 | // use the default one |
aoqi@0 | 211 | globalBinding = new BIGlobalBinding(); |
aoqi@0 | 212 | BindInfo big = new BindInfo(); |
aoqi@0 | 213 | big.addDecl(globalBinding); |
aoqi@0 | 214 | big.setOwner(this,null); |
aoqi@0 | 215 | } |
aoqi@0 | 216 | |
aoqi@0 | 217 | // code generation mode |
aoqi@0 | 218 | model.strategy = globalBinding.getCodeGenerationStrategy(); |
aoqi@0 | 219 | model.rootClass = globalBinding.getSuperClass(); |
aoqi@0 | 220 | model.rootInterface = globalBinding.getSuperInterface(); |
aoqi@0 | 221 | |
aoqi@0 | 222 | particleBinder = globalBinding.isSimpleMode() ? new ExpressionParticleBinder() : new DefaultParticleBinder(); |
aoqi@0 | 223 | |
aoqi@0 | 224 | // check XJC extensions and realize them |
aoqi@0 | 225 | BISerializable serial = globalBinding.getSerializable(); |
aoqi@0 | 226 | if(serial!=null) { |
aoqi@0 | 227 | model.serializable = true; |
aoqi@0 | 228 | model.serialVersionUID = serial.uid; |
aoqi@0 | 229 | } |
aoqi@0 | 230 | |
aoqi@0 | 231 | // obtain the name conversion mode |
aoqi@0 | 232 | if (globalBinding.nameConverter!=null) |
aoqi@0 | 233 | model.setNameConverter(globalBinding.nameConverter); |
aoqi@0 | 234 | |
aoqi@0 | 235 | // attach global conversions to the appropriate simple types |
aoqi@0 | 236 | globalBinding.dispatchGlobalConversions(schemas); |
aoqi@0 | 237 | |
aoqi@0 | 238 | globalBinding.errorCheck(); |
aoqi@0 | 239 | } |
aoqi@0 | 240 | |
aoqi@0 | 241 | /** |
aoqi@0 | 242 | * Global bindings. |
aoqi@0 | 243 | * |
aoqi@0 | 244 | * The empty global binding is set as the default, so that |
aoqi@0 | 245 | * there will be no need to test if the value is null. |
aoqi@0 | 246 | */ |
aoqi@0 | 247 | private BIGlobalBinding globalBinding; |
aoqi@0 | 248 | |
aoqi@0 | 249 | /** |
aoqi@0 | 250 | * Gets the global bindings. |
aoqi@0 | 251 | */ |
aoqi@0 | 252 | public @NotNull BIGlobalBinding getGlobalBinding() { return globalBinding; } |
aoqi@0 | 253 | |
aoqi@0 | 254 | |
aoqi@0 | 255 | private ParticleBinder particleBinder; |
aoqi@0 | 256 | |
aoqi@0 | 257 | /** |
aoqi@0 | 258 | * Gets the particle binder for this binding. |
aoqi@0 | 259 | */ |
aoqi@0 | 260 | public @NotNull ParticleBinder getParticleBinder() { return particleBinder; } |
aoqi@0 | 261 | |
aoqi@0 | 262 | |
aoqi@0 | 263 | /** |
aoqi@0 | 264 | * Name converter that implements "XML->Java name conversion" |
aoqi@0 | 265 | * as specified in the spec. |
aoqi@0 | 266 | * |
aoqi@0 | 267 | * This object abstracts the detail that we use different name |
aoqi@0 | 268 | * conversion depending on the customization. |
aoqi@0 | 269 | * |
aoqi@0 | 270 | * <p> |
aoqi@0 | 271 | * This object should be used to perform any name conversion |
aoqi@0 | 272 | * needs, instead of the JJavaName class in CodeModel. |
aoqi@0 | 273 | */ |
aoqi@0 | 274 | public NameConverter getNameConverter() { return model.getNameConverter(); } |
aoqi@0 | 275 | |
aoqi@0 | 276 | /** Fill-in the contents of each classes. */ |
aoqi@0 | 277 | private void buildContents() { |
aoqi@0 | 278 | ClassSelector cs = getClassSelector(); |
aoqi@0 | 279 | SimpleTypeBuilder stb = Ring.get(SimpleTypeBuilder.class); |
aoqi@0 | 280 | |
aoqi@0 | 281 | for( XSSchema s : Ring.get(XSSchemaSet.class).getSchemas() ) { |
aoqi@0 | 282 | BISchemaBinding sb = getBindInfo(s).get(BISchemaBinding.class); |
aoqi@0 | 283 | |
aoqi@0 | 284 | if(sb!=null && !sb.map) { |
aoqi@0 | 285 | sb.markAsAcknowledged(); |
aoqi@0 | 286 | continue; // no mapping for this package |
aoqi@0 | 287 | } |
aoqi@0 | 288 | |
aoqi@0 | 289 | getClassSelector().pushClassScope( new CClassInfoParent.Package( |
aoqi@0 | 290 | getClassSelector().getPackage(s.getTargetNamespace())) ); |
aoqi@0 | 291 | |
aoqi@0 | 292 | checkMultipleSchemaBindings(s); |
aoqi@0 | 293 | processPackageJavadoc(s); |
aoqi@0 | 294 | populate(s.getAttGroupDecls(),s); |
aoqi@0 | 295 | populate(s.getAttributeDecls(),s); |
aoqi@0 | 296 | populate(s.getElementDecls(),s); |
aoqi@0 | 297 | populate(s.getModelGroupDecls(),s); |
aoqi@0 | 298 | |
aoqi@0 | 299 | // fill in typeUses |
aoqi@0 | 300 | for (XSType t : s.getTypes().values()) { |
aoqi@0 | 301 | stb.refererStack.push(t); |
aoqi@0 | 302 | model.typeUses().put( getName(t), cs.bindToType(t,s) ); |
aoqi@0 | 303 | stb.refererStack.pop(); |
aoqi@0 | 304 | } |
aoqi@0 | 305 | |
aoqi@0 | 306 | getClassSelector().popClassScope(); |
aoqi@0 | 307 | } |
aoqi@0 | 308 | } |
aoqi@0 | 309 | |
aoqi@0 | 310 | /** Reports an error if there are more than one jaxb:schemaBindings customization. */ |
aoqi@0 | 311 | private void checkMultipleSchemaBindings( XSSchema schema ) { |
aoqi@0 | 312 | ArrayList<Locator> locations = new ArrayList<Locator>(); |
aoqi@0 | 313 | |
aoqi@0 | 314 | BindInfo bi = getBindInfo(schema); |
aoqi@0 | 315 | for( BIDeclaration bid : bi ) { |
aoqi@0 | 316 | if( bid.getName()==BISchemaBinding.NAME ) |
aoqi@0 | 317 | locations.add( bid.getLocation() ); |
aoqi@0 | 318 | } |
aoqi@0 | 319 | if(locations.size()<=1) return; // OK |
aoqi@0 | 320 | |
aoqi@0 | 321 | // error |
aoqi@0 | 322 | getErrorReporter().error( locations.get(0), |
aoqi@0 | 323 | Messages.ERR_MULTIPLE_SCHEMA_BINDINGS, |
aoqi@0 | 324 | schema.getTargetNamespace() ); |
aoqi@0 | 325 | for( int i=1; i<locations.size(); i++ ) |
aoqi@0 | 326 | getErrorReporter().error( (Locator)locations.get(i), |
aoqi@0 | 327 | Messages.ERR_MULTIPLE_SCHEMA_BINDINGS_LOCATION); |
aoqi@0 | 328 | } |
aoqi@0 | 329 | |
aoqi@0 | 330 | /** |
aoqi@0 | 331 | * Calls {@link ClassSelector} for each item in the iterator |
aoqi@0 | 332 | * to populate class items if there is any. |
aoqi@0 | 333 | */ |
aoqi@0 | 334 | private void populate( Map<String,? extends XSComponent> col, XSSchema schema ) { |
aoqi@0 | 335 | ClassSelector cs = getClassSelector(); |
aoqi@0 | 336 | for( XSComponent sc : col.values() ) |
aoqi@0 | 337 | cs.bindToType(sc,schema); |
aoqi@0 | 338 | } |
aoqi@0 | 339 | |
aoqi@0 | 340 | /** |
aoqi@0 | 341 | * Generates <code>package.html</code> if the customization |
aoqi@0 | 342 | * says so. |
aoqi@0 | 343 | */ |
aoqi@0 | 344 | private void processPackageJavadoc( XSSchema s ) { |
aoqi@0 | 345 | // look for the schema-wide customization |
aoqi@0 | 346 | BISchemaBinding cust = getBindInfo(s).get(BISchemaBinding.class); |
aoqi@0 | 347 | if(cust==null) return; // not present |
aoqi@0 | 348 | |
aoqi@0 | 349 | cust.markAsAcknowledged(); |
aoqi@0 | 350 | if( cust.getJavadoc()==null ) return; // no javadoc customization |
aoqi@0 | 351 | |
aoqi@0 | 352 | // produce a HTML file |
aoqi@0 | 353 | JTextFile html = new JTextFile("package.html"); |
aoqi@0 | 354 | html.setContents(cust.getJavadoc()); |
aoqi@0 | 355 | getClassSelector().getPackage(s.getTargetNamespace()).addResourceFile(html); |
aoqi@0 | 356 | } |
aoqi@0 | 357 | |
aoqi@0 | 358 | |
aoqi@0 | 359 | |
aoqi@0 | 360 | |
aoqi@0 | 361 | |
aoqi@0 | 362 | |
aoqi@0 | 363 | /** |
aoqi@0 | 364 | * Gets or creates the BindInfo object associated to a schema component. |
aoqi@0 | 365 | * |
aoqi@0 | 366 | * @return |
aoqi@0 | 367 | * Always return a non-null valid BindInfo object. |
aoqi@0 | 368 | * Even if no declaration was specified, this method creates |
aoqi@0 | 369 | * a new BindInfo so that new decls can be added. |
aoqi@0 | 370 | */ |
aoqi@0 | 371 | public BindInfo getOrCreateBindInfo( XSComponent schemaComponent ) { |
aoqi@0 | 372 | |
aoqi@0 | 373 | BindInfo bi = _getBindInfoReadOnly(schemaComponent); |
aoqi@0 | 374 | if(bi!=null) return bi; |
aoqi@0 | 375 | |
aoqi@0 | 376 | // XSOM is read-only, so we cannot add new annotations. |
aoqi@0 | 377 | // for components that didn't have annotations, |
aoqi@0 | 378 | // we maintain an external map. |
aoqi@0 | 379 | bi = new BindInfo(); |
aoqi@0 | 380 | bi.setOwner(this,schemaComponent); |
aoqi@0 | 381 | externalBindInfos.put(schemaComponent,bi); |
aoqi@0 | 382 | return bi; |
aoqi@0 | 383 | } |
aoqi@0 | 384 | |
aoqi@0 | 385 | |
aoqi@0 | 386 | /** |
aoqi@0 | 387 | * Used as a constant instance to represent the empty {@link BindInfo}. |
aoqi@0 | 388 | */ |
aoqi@0 | 389 | private final BindInfo emptyBindInfo = new BindInfo(); |
aoqi@0 | 390 | |
aoqi@0 | 391 | /** |
aoqi@0 | 392 | * Gets the BindInfo object associated to a schema component. |
aoqi@0 | 393 | * |
aoqi@0 | 394 | * @return |
aoqi@0 | 395 | * always return a valid {@link BindInfo} object. If none |
aoqi@0 | 396 | * is specified for the given component, a dummy empty BindInfo |
aoqi@0 | 397 | * will be returned. |
aoqi@0 | 398 | */ |
aoqi@0 | 399 | public BindInfo getBindInfo( XSComponent schemaComponent ) { |
aoqi@0 | 400 | BindInfo bi = _getBindInfoReadOnly(schemaComponent); |
aoqi@0 | 401 | if(bi!=null) return bi; |
aoqi@0 | 402 | else return emptyBindInfo; |
aoqi@0 | 403 | } |
aoqi@0 | 404 | |
aoqi@0 | 405 | /** |
aoqi@0 | 406 | * Gets the BindInfo object associated to a schema component. |
aoqi@0 | 407 | * |
aoqi@0 | 408 | * @return |
aoqi@0 | 409 | * null if no bind info is associated to this schema component. |
aoqi@0 | 410 | */ |
aoqi@0 | 411 | private BindInfo _getBindInfoReadOnly( XSComponent schemaComponent ) { |
aoqi@0 | 412 | |
aoqi@0 | 413 | BindInfo bi = externalBindInfos.get(schemaComponent); |
aoqi@0 | 414 | if(bi!=null) return bi; |
aoqi@0 | 415 | |
aoqi@0 | 416 | XSAnnotation annon = schemaComponent.getAnnotation(); |
aoqi@0 | 417 | if(annon!=null) { |
aoqi@0 | 418 | bi = (BindInfo)annon.getAnnotation(); |
aoqi@0 | 419 | if(bi!=null) { |
aoqi@0 | 420 | if(bi.getOwner()==null) |
aoqi@0 | 421 | bi.setOwner(this,schemaComponent); |
aoqi@0 | 422 | return bi; |
aoqi@0 | 423 | } |
aoqi@0 | 424 | } |
aoqi@0 | 425 | |
aoqi@0 | 426 | return null; |
aoqi@0 | 427 | } |
aoqi@0 | 428 | |
aoqi@0 | 429 | /** |
aoqi@0 | 430 | * A map that stores binding declarations augmented by XJC. |
aoqi@0 | 431 | */ |
aoqi@0 | 432 | private final Map<XSComponent,BindInfo> externalBindInfos = new HashMap<XSComponent,BindInfo>(); |
aoqi@0 | 433 | |
aoqi@0 | 434 | /** |
aoqi@0 | 435 | * Gets the {@link BIDom} object that applies to the given particle. |
aoqi@0 | 436 | */ |
aoqi@0 | 437 | protected final BIDom getLocalDomCustomization( XSParticle p ) { |
aoqi@0 | 438 | if (p == null) { |
aoqi@0 | 439 | return null; |
aoqi@0 | 440 | } |
aoqi@0 | 441 | BIDom dom = getBindInfo(p).get(BIDom.class); |
aoqi@0 | 442 | if(dom!=null) return dom; |
aoqi@0 | 443 | |
aoqi@0 | 444 | // if not, the term might have one. |
aoqi@0 | 445 | dom = getBindInfo(p.getTerm()).get(BIDom.class); |
aoqi@0 | 446 | if(dom!=null) return dom; |
aoqi@0 | 447 | |
aoqi@0 | 448 | XSTerm t = p.getTerm(); |
aoqi@0 | 449 | // type could also have one, in case of the dom customization |
aoqi@0 | 450 | if(t.isElementDecl()) |
aoqi@0 | 451 | return getBindInfo(t.asElementDecl().getType()).get(BIDom.class); |
aoqi@0 | 452 | // similarly the model group in a model group definition may have one. |
aoqi@0 | 453 | if(t.isModelGroupDecl()) |
aoqi@0 | 454 | return getBindInfo(t.asModelGroupDecl().getModelGroup()).get(BIDom.class); |
aoqi@0 | 455 | |
aoqi@0 | 456 | return null; |
aoqi@0 | 457 | } |
aoqi@0 | 458 | |
aoqi@0 | 459 | /** |
aoqi@0 | 460 | * Returns true if the component should be processed by purple. |
aoqi@0 | 461 | */ |
aoqi@0 | 462 | private final XSFinder toPurple = new XSFinder() { |
aoqi@0 | 463 | @Override |
aoqi@0 | 464 | public Boolean attributeUse(XSAttributeUse use) { |
aoqi@0 | 465 | // attribute use always maps to a property |
aoqi@0 | 466 | return true; |
aoqi@0 | 467 | } |
aoqi@0 | 468 | |
aoqi@0 | 469 | @Override |
aoqi@0 | 470 | public Boolean simpleType(XSSimpleType xsSimpleType) { |
aoqi@0 | 471 | // simple type always maps to a type, hence we should take purple |
aoqi@0 | 472 | return true; |
aoqi@0 | 473 | } |
aoqi@0 | 474 | |
aoqi@0 | 475 | @Override |
aoqi@0 | 476 | public Boolean wildcard(XSWildcard xsWildcard) { |
aoqi@0 | 477 | // attribute wildcards always maps to a property. |
aoqi@0 | 478 | // element wildcards should have been processed with particle binders |
aoqi@0 | 479 | return true; |
aoqi@0 | 480 | } |
aoqi@0 | 481 | }; |
aoqi@0 | 482 | /** |
aoqi@0 | 483 | * If the component maps to a property, forwards to purple, otherwise to green. |
aoqi@0 | 484 | * |
aoqi@0 | 485 | * If the component is mapped to a type, this method needs to return true. |
aoqi@0 | 486 | * See the chart at the class javadoc. |
aoqi@0 | 487 | */ |
aoqi@0 | 488 | public void ying( XSComponent sc, @Nullable XSComponent referer ) { |
aoqi@0 | 489 | if(sc.apply(toPurple)==true || getClassSelector().bindToType(sc,referer)!=null) |
aoqi@0 | 490 | sc.visit(purple); |
aoqi@0 | 491 | else |
aoqi@0 | 492 | sc.visit(green); |
aoqi@0 | 493 | } |
aoqi@0 | 494 | |
aoqi@0 | 495 | private Transformer identityTransformer; |
aoqi@0 | 496 | |
aoqi@0 | 497 | /** |
aoqi@0 | 498 | * Gets the shared instance of the identity transformer. |
aoqi@0 | 499 | */ |
aoqi@0 | 500 | public Transformer getIdentityTransformer() { |
aoqi@0 | 501 | try { |
aoqi@0 | 502 | if(identityTransformer==null) { |
aoqi@0 | 503 | TransformerFactory tf = XmlFactory.createTransformerFactory(model.options.disableXmlSecurity); |
aoqi@0 | 504 | identityTransformer = tf.newTransformer(); |
aoqi@0 | 505 | } |
aoqi@0 | 506 | return identityTransformer; |
aoqi@0 | 507 | } catch (TransformerConfigurationException e) { |
aoqi@0 | 508 | throw new Error(e); // impossible |
aoqi@0 | 509 | } |
aoqi@0 | 510 | } |
aoqi@0 | 511 | |
aoqi@0 | 512 | /** |
aoqi@0 | 513 | * Find all types that refer to the given complex type. |
aoqi@0 | 514 | */ |
aoqi@0 | 515 | public Set<XSComponent> getReferer(XSType c) { |
aoqi@0 | 516 | if(refFinder==null) { |
aoqi@0 | 517 | refFinder = new RefererFinder(); |
aoqi@0 | 518 | refFinder.schemaSet(Ring.get(XSSchemaSet.class)); |
aoqi@0 | 519 | } |
aoqi@0 | 520 | return refFinder.getReferer(c); |
aoqi@0 | 521 | } |
aoqi@0 | 522 | |
aoqi@0 | 523 | /** |
aoqi@0 | 524 | * Returns the QName of the declaration. |
aoqi@0 | 525 | * @return null |
aoqi@0 | 526 | * if the declaration is anonymous. |
aoqi@0 | 527 | */ |
aoqi@0 | 528 | public static QName getName(XSDeclaration decl) { |
aoqi@0 | 529 | String local = decl.getName(); |
aoqi@0 | 530 | if(local==null) return null; |
aoqi@0 | 531 | return new QName(decl.getTargetNamespace(),local); |
aoqi@0 | 532 | } |
aoqi@0 | 533 | |
aoqi@0 | 534 | /** |
aoqi@0 | 535 | * Derives a name from a schema component. |
aoqi@0 | 536 | * |
aoqi@0 | 537 | * This method handles prefix/suffix modification and |
aoqi@0 | 538 | * XML-to-Java name conversion. |
aoqi@0 | 539 | * |
aoqi@0 | 540 | * @param name |
aoqi@0 | 541 | * The base name. This should be things like element names |
aoqi@0 | 542 | * or type names. |
aoqi@0 | 543 | * @param comp |
aoqi@0 | 544 | * The component from which the base name was taken. |
aoqi@0 | 545 | * Used to determine how names are modified. |
aoqi@0 | 546 | */ |
aoqi@0 | 547 | public String deriveName( String name, XSComponent comp ) { |
aoqi@0 | 548 | XSSchema owner = comp.getOwnerSchema(); |
aoqi@0 | 549 | |
aoqi@0 | 550 | name = getNameConverter().toClassName(name); |
aoqi@0 | 551 | |
aoqi@0 | 552 | if( owner!=null ) { |
aoqi@0 | 553 | BISchemaBinding sb = getBindInfo(owner).get(BISchemaBinding.class); |
aoqi@0 | 554 | |
aoqi@0 | 555 | if(sb!=null) name = sb.mangleClassName(name,comp); |
aoqi@0 | 556 | } |
aoqi@0 | 557 | |
aoqi@0 | 558 | return name; |
aoqi@0 | 559 | } |
aoqi@0 | 560 | |
aoqi@0 | 561 | public boolean isGenerateMixedExtensions() { |
aoqi@0 | 562 | if (globalBinding != null) { |
aoqi@0 | 563 | return globalBinding.isGenerateMixedExtensions(); |
aoqi@0 | 564 | } |
aoqi@0 | 565 | return false; |
aoqi@0 | 566 | } |
aoqi@0 | 567 | |
aoqi@0 | 568 | } |