src/share/jaxws_classes/com/sun/xml/internal/bind/v2/schemagen/XmlSchemaGenerator.java

Tue, 29 Mar 2016 13:16:00 +0300

author
aefimov
date
Tue, 29 Mar 2016 13:16:00 +0300
changeset 1101
bd88174c3095
parent 384
8f2986ff0235
child 1288
f4ace6971570
permissions
-rw-r--r--

8073872: Schemagen fails with StackOverflowError if element references containing class
Reviewed-by: lancea

ohair@286 1 /*
aefimov@1101 2 * Copyright (c) 1997, 2016, 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.xml.internal.bind.v2.schemagen;
ohair@286 27
ohair@286 28 import java.io.IOException;
ohair@286 29 import java.io.OutputStream;
ohair@286 30 import java.io.Writer;
ohair@286 31 import java.io.File;
ohair@286 32 import java.net.URI;
ohair@286 33 import java.net.URISyntaxException;
ohair@286 34 import java.util.Comparator;
ohair@286 35 import java.util.HashMap;
ohair@286 36 import java.util.LinkedHashSet;
ohair@286 37 import java.util.Map;
ohair@286 38 import java.util.Set;
ohair@286 39 import java.util.TreeMap;
ohair@286 40 import java.util.ArrayList;
ohair@286 41 import java.util.logging.Level;
ohair@286 42 import java.util.logging.Logger;
ohair@286 43
ohair@286 44 import javax.activation.MimeType;
ohair@286 45 import javax.xml.bind.SchemaOutputResolver;
ohair@286 46 import javax.xml.bind.annotation.XmlElement;
ohair@286 47 import javax.xml.namespace.QName;
ohair@286 48 import javax.xml.transform.Result;
ohair@286 49 import javax.xml.transform.stream.StreamResult;
ohair@286 50
ohair@286 51 import com.sun.istack.internal.Nullable;
ohair@286 52 import com.sun.istack.internal.NotNull;
ohair@286 53 import com.sun.xml.internal.bind.Util;
ohair@286 54 import com.sun.xml.internal.bind.api.CompositeStructure;
ohair@286 55 import com.sun.xml.internal.bind.api.ErrorListener;
ohair@286 56 import com.sun.xml.internal.bind.v2.TODO;
ohair@286 57 import com.sun.xml.internal.bind.v2.WellKnownNamespace;
ohair@286 58 import com.sun.xml.internal.bind.v2.util.CollisionCheckStack;
ohair@286 59 import com.sun.xml.internal.bind.v2.util.StackRecorder;
ohair@286 60 import static com.sun.xml.internal.bind.v2.WellKnownNamespace.XML_SCHEMA;
ohair@286 61 import com.sun.xml.internal.bind.v2.model.core.Adapter;
ohair@286 62 import com.sun.xml.internal.bind.v2.model.core.ArrayInfo;
ohair@286 63 import com.sun.xml.internal.bind.v2.model.core.AttributePropertyInfo;
ohair@286 64 import com.sun.xml.internal.bind.v2.model.core.ClassInfo;
ohair@286 65 import com.sun.xml.internal.bind.v2.model.core.Element;
ohair@286 66 import com.sun.xml.internal.bind.v2.model.core.ElementInfo;
ohair@286 67 import com.sun.xml.internal.bind.v2.model.core.ElementPropertyInfo;
ohair@286 68 import com.sun.xml.internal.bind.v2.model.core.EnumConstant;
ohair@286 69 import com.sun.xml.internal.bind.v2.model.core.EnumLeafInfo;
ohair@286 70 import com.sun.xml.internal.bind.v2.model.core.MapPropertyInfo;
ohair@286 71 import com.sun.xml.internal.bind.v2.model.core.MaybeElement;
ohair@286 72 import com.sun.xml.internal.bind.v2.model.core.NonElement;
ohair@286 73 import com.sun.xml.internal.bind.v2.model.core.NonElementRef;
ohair@286 74 import com.sun.xml.internal.bind.v2.model.core.PropertyInfo;
ohair@286 75 import com.sun.xml.internal.bind.v2.model.core.ReferencePropertyInfo;
ohair@286 76 import com.sun.xml.internal.bind.v2.model.core.TypeInfo;
ohair@286 77 import com.sun.xml.internal.bind.v2.model.core.TypeInfoSet;
ohair@286 78 import com.sun.xml.internal.bind.v2.model.core.TypeRef;
ohair@286 79 import com.sun.xml.internal.bind.v2.model.core.ValuePropertyInfo;
ohair@286 80 import com.sun.xml.internal.bind.v2.model.core.WildcardMode;
ohair@286 81 import com.sun.xml.internal.bind.v2.model.impl.ClassInfoImpl;
ohair@286 82 import com.sun.xml.internal.bind.v2.model.nav.Navigator;
ohair@286 83 import com.sun.xml.internal.bind.v2.runtime.SwaRefAdapter;
ohair@286 84 import static com.sun.xml.internal.bind.v2.schemagen.Util.*;
ohair@286 85 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Any;
ohair@286 86 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.AttrDecls;
ohair@286 87 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.ComplexExtension;
ohair@286 88 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.ComplexType;
ohair@286 89 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.ComplexTypeHost;
ohair@286 90 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.ExplicitGroup;
ohair@286 91 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Import;
ohair@286 92 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
ohair@286 93 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.LocalAttribute;
ohair@286 94 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.LocalElement;
ohair@286 95 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Schema;
ohair@286 96 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.SimpleExtension;
ohair@286 97 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.SimpleRestrictionModel;
ohair@286 98 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.SimpleType;
ohair@286 99 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.SimpleTypeHost;
ohair@286 100 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.TopLevelAttribute;
ohair@286 101 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.TopLevelElement;
ohair@286 102 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.TypeHost;
ohair@286 103 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.ContentModelContainer;
ohair@286 104 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.TypeDefParticle;
ohair@286 105 import com.sun.xml.internal.bind.v2.schemagen.xmlschema.AttributeType;
ohair@286 106 import com.sun.xml.internal.bind.v2.schemagen.episode.Bindings;
ohair@286 107 import com.sun.xml.internal.txw2.TXW;
ohair@286 108 import com.sun.xml.internal.txw2.TxwException;
ohair@286 109 import com.sun.xml.internal.txw2.TypedXmlWriter;
ohair@286 110 import com.sun.xml.internal.txw2.output.ResultFactory;
ohair@286 111 import com.sun.xml.internal.txw2.output.XmlSerializer;
ohair@286 112 import java.util.Collection;
aefimov@1101 113 import java.util.HashSet;
ohair@286 114 import org.xml.sax.SAXParseException;
ohair@286 115
ohair@286 116 /**
ohair@286 117 * Generates a set of W3C XML Schema documents from a set of Java classes.
ohair@286 118 *
ohair@286 119 * <p>
ohair@286 120 * A client must invoke methods in the following order:
ohair@286 121 * <ol>
ohair@286 122 * <li>Create a new {@link XmlSchemaGenerator}
ohair@286 123 * <li>Invoke {@link #add} methods, multiple times if necessary.
ohair@286 124 * <li>Invoke {@link #write}
ohair@286 125 * <li>Discard the {@link XmlSchemaGenerator}.
ohair@286 126 * </ol>
ohair@286 127 *
ohair@286 128 * @author Ryan Shoemaker
ohair@286 129 * @author Kohsuke Kawaguchi (kk@kohsuke.org)
ohair@286 130 */
ohair@286 131 public final class XmlSchemaGenerator<T,C,F,M> {
ohair@286 132
ohair@286 133 private static final Logger logger = Util.getClassLogger();
ohair@286 134
ohair@286 135 /**
ohair@286 136 * Java classes to be written, organized by their namespace.
ohair@286 137 *
ohair@286 138 * <p>
ohair@286 139 * We use a {@link TreeMap} here so that the suggested names will
ohair@286 140 * be consistent across JVMs.
ohair@286 141 *
ohair@286 142 * @see SchemaOutputResolver#createOutput(String, String)
ohair@286 143 */
ohair@286 144 private final Map<String,Namespace> namespaces = new TreeMap<String,Namespace>(NAMESPACE_COMPARATOR);
ohair@286 145
ohair@286 146 /**
ohair@286 147 * {@link ErrorListener} to send errors to.
ohair@286 148 */
ohair@286 149 private ErrorListener errorListener;
ohair@286 150
ohair@286 151 /** model navigator **/
ohair@286 152 private Navigator<T,C,F,M> navigator;
ohair@286 153
ohair@286 154 private final TypeInfoSet<T,C,F,M> types;
ohair@286 155
ohair@286 156 /**
ohair@286 157 * Representation for xs:string.
ohair@286 158 */
ohair@286 159 private final NonElement<T,C> stringType;
ohair@286 160
ohair@286 161 /**
ohair@286 162 * Represents xs:anyType.
ohair@286 163 */
ohair@286 164 private final NonElement<T,C> anyType;
ohair@286 165
ohair@286 166 /**
ohair@286 167 * Used to detect cycles in anonymous types.
ohair@286 168 */
ohair@286 169 private final CollisionCheckStack<ClassInfo<T,C>> collisionChecker = new CollisionCheckStack<ClassInfo<T,C>>();
ohair@286 170
ohair@286 171 public XmlSchemaGenerator( Navigator<T,C,F,M> navigator, TypeInfoSet<T,C,F,M> types ) {
ohair@286 172 this.navigator = navigator;
ohair@286 173 this.types = types;
ohair@286 174
ohair@286 175 this.stringType = types.getTypeInfo(navigator.ref(String.class));
ohair@286 176 this.anyType = types.getAnyTypeInfo();
ohair@286 177
ohair@286 178 // populate the object
ohair@286 179 for( ClassInfo<T,C> ci : types.beans().values() )
ohair@286 180 add(ci);
ohair@286 181 for( ElementInfo<T,C> ei1 : types.getElementMappings(null).values() )
ohair@286 182 add(ei1);
ohair@286 183 for( EnumLeafInfo<T,C> ei : types.enums().values() )
ohair@286 184 add(ei);
ohair@286 185 for( ArrayInfo<T,C> a : types.arrays().values())
ohair@286 186 add(a);
ohair@286 187 }
ohair@286 188
ohair@286 189 private Namespace getNamespace(String uri) {
ohair@286 190 Namespace n = namespaces.get(uri);
ohair@286 191 if(n==null)
ohair@286 192 namespaces.put(uri,n=new Namespace(uri));
ohair@286 193 return n;
ohair@286 194 }
ohair@286 195
ohair@286 196 /**
ohair@286 197 * Adds a new class to the list of classes to be written.
ohair@286 198 *
ohair@286 199 * <p>
ohair@286 200 * A {@link ClassInfo} may have two namespaces --- one for the element name
ohair@286 201 * and the other for the type name. If they are different, we put the same
ohair@286 202 * {@link ClassInfo} to two {@link Namespace}s.
ohair@286 203 */
ohair@286 204 public void add( ClassInfo<T,C> clazz ) {
ohair@286 205 assert clazz!=null;
ohair@286 206
ohair@286 207 String nsUri = null;
ohair@286 208
ohair@286 209 if(clazz.getClazz()==navigator.asDecl(CompositeStructure.class))
ohair@286 210 return; // this is a special class we introduced for JAX-WS that we *don't* want in the schema
ohair@286 211
ohair@286 212 if(clazz.isElement()) {
ohair@286 213 // put element -> type reference
ohair@286 214 nsUri = clazz.getElementName().getNamespaceURI();
ohair@286 215 Namespace ns = getNamespace(nsUri);
ohair@286 216 ns.classes.add(clazz);
ohair@286 217 ns.addDependencyTo(clazz.getTypeName());
ohair@286 218
ohair@286 219 // schedule writing this global element
ohair@286 220 add(clazz.getElementName(),false,clazz);
ohair@286 221 }
ohair@286 222
ohair@286 223 QName tn = clazz.getTypeName();
ohair@286 224 if(tn!=null) {
ohair@286 225 nsUri = tn.getNamespaceURI();
ohair@286 226 } else {
ohair@286 227 // anonymous type
ohair@286 228 if(nsUri==null)
ohair@286 229 return;
ohair@286 230 }
ohair@286 231
ohair@286 232 Namespace n = getNamespace(nsUri);
ohair@286 233 n.classes.add(clazz);
ohair@286 234
ohair@286 235 // search properties for foreign namespace references
ohair@286 236 for( PropertyInfo<T,C> p : clazz.getProperties()) {
ohair@286 237 n.processForeignNamespaces(p, 1);
ohair@286 238 if (p instanceof AttributePropertyInfo) {
ohair@286 239 AttributePropertyInfo<T,C> ap = (AttributePropertyInfo<T,C>) p;
ohair@286 240 String aUri = ap.getXmlName().getNamespaceURI();
ohair@286 241 if(aUri.length()>0) {
ohair@286 242 // global attribute
ohair@286 243 getNamespace(aUri).addGlobalAttribute(ap);
ohair@286 244 n.addDependencyTo(ap.getXmlName());
ohair@286 245 }
ohair@286 246 }
ohair@286 247 if (p instanceof ElementPropertyInfo) {
ohair@286 248 ElementPropertyInfo<T,C> ep = (ElementPropertyInfo<T,C>) p;
ohair@286 249 for (TypeRef<T,C> tref : ep.getTypes()) {
ohair@286 250 String eUri = tref.getTagName().getNamespaceURI();
ohair@286 251 if(eUri.length()>0 && !eUri.equals(n.uri)) {
ohair@286 252 getNamespace(eUri).addGlobalElement(tref);
ohair@286 253 n.addDependencyTo(tref.getTagName());
ohair@286 254 }
ohair@286 255 }
ohair@286 256 }
ohair@286 257
ohair@286 258 if(generateSwaRefAdapter(p))
ohair@286 259 n.useSwaRef = true;
ohair@286 260
ohair@286 261 MimeType mimeType = p.getExpectedMimeType();
ohair@286 262 if( mimeType != null ) {
ohair@286 263 n.useMimeNs = true;
ohair@286 264 }
ohair@286 265
ohair@286 266 }
ohair@286 267
ohair@286 268 // recurse on baseTypes to make sure that we can refer to them in the schema
ohair@286 269 ClassInfo<T,C> bc = clazz.getBaseClass();
ohair@286 270 if (bc != null) {
ohair@286 271 add(bc);
ohair@286 272 n.addDependencyTo(bc.getTypeName());
ohair@286 273 }
ohair@286 274 }
ohair@286 275
ohair@286 276 /**
ohair@286 277 * Adds a new element to the list of elements to be written.
ohair@286 278 */
ohair@286 279 public void add( ElementInfo<T,C> elem ) {
ohair@286 280 assert elem!=null;
ohair@286 281
mkos@384 282 @SuppressWarnings("UnusedAssignment")
ohair@286 283 boolean nillable = false; // default value
ohair@286 284
ohair@286 285 QName name = elem.getElementName();
ohair@286 286 Namespace n = getNamespace(name.getNamespaceURI());
ohair@286 287 ElementInfo ei;
ohair@286 288
ohair@286 289 if (elem.getScope() != null) { // (probably) never happens
ohair@286 290 ei = this.types.getElementInfo(elem.getScope().getClazz(), name);
ohair@286 291 } else {
ohair@286 292 ei = this.types.getElementInfo(null, name);
ohair@286 293 }
ohair@286 294
ohair@286 295 XmlElement xmlElem = ei.getProperty().readAnnotation(XmlElement.class);
ohair@286 296
ohair@286 297 if (xmlElem == null) {
ohair@286 298 nillable = false;
ohair@286 299 } else {
ohair@286 300 nillable = xmlElem.nillable();
ohair@286 301 }
ohair@286 302
ohair@286 303 n.elementDecls.put(name.getLocalPart(),n.new ElementWithType(nillable, elem.getContentType()));
ohair@286 304
ohair@286 305 // search for foreign namespace references
ohair@286 306 n.processForeignNamespaces(elem.getProperty(), 1);
ohair@286 307 }
ohair@286 308
ohair@286 309 public void add( EnumLeafInfo<T,C> envm ) {
ohair@286 310 assert envm!=null;
ohair@286 311
ohair@286 312 String nsUri = null;
ohair@286 313
ohair@286 314 if(envm.isElement()) {
ohair@286 315 // put element -> type reference
ohair@286 316 nsUri = envm.getElementName().getNamespaceURI();
ohair@286 317 Namespace ns = getNamespace(nsUri);
ohair@286 318 ns.enums.add(envm);
ohair@286 319 ns.addDependencyTo(envm.getTypeName());
ohair@286 320
ohair@286 321 // schedule writing this global element
ohair@286 322 add(envm.getElementName(),false,envm);
ohair@286 323 }
ohair@286 324
ohair@286 325 final QName typeName = envm.getTypeName();
ohair@286 326 if (typeName != null) {
ohair@286 327 nsUri = typeName.getNamespaceURI();
ohair@286 328 } else {
ohair@286 329 if(nsUri==null)
ohair@286 330 return; // anonymous type
ohair@286 331 }
ohair@286 332
ohair@286 333 Namespace n = getNamespace(nsUri);
ohair@286 334 n.enums.add(envm);
ohair@286 335
ohair@286 336 // search for foreign namespace references
ohair@286 337 n.addDependencyTo(envm.getBaseType().getTypeName());
ohair@286 338 }
ohair@286 339
ohair@286 340 public void add( ArrayInfo<T,C> a ) {
ohair@286 341 assert a!=null;
ohair@286 342
ohair@286 343 final String namespaceURI = a.getTypeName().getNamespaceURI();
ohair@286 344 Namespace n = getNamespace(namespaceURI);
ohair@286 345 n.arrays.add(a);
ohair@286 346
ohair@286 347 // search for foreign namespace references
ohair@286 348 n.addDependencyTo(a.getItemType().getTypeName());
ohair@286 349 }
ohair@286 350
ohair@286 351 /**
ohair@286 352 * Adds an additional element declaration.
ohair@286 353 *
ohair@286 354 * @param tagName
ohair@286 355 * The name of the element declaration to be added.
ohair@286 356 * @param type
ohair@286 357 * The type this element refers to.
ohair@286 358 * Can be null, in which case the element refers to an empty anonymous complex type.
ohair@286 359 */
ohair@286 360 public void add( QName tagName, boolean isNillable, NonElement<T,C> type ) {
ohair@286 361
ohair@286 362 if(type!=null && type.getType()==navigator.ref(CompositeStructure.class))
ohair@286 363 return; // this is a special class we introduced for JAX-WS that we *don't* want in the schema
ohair@286 364
ohair@286 365
ohair@286 366 Namespace n = getNamespace(tagName.getNamespaceURI());
ohair@286 367 n.elementDecls.put(tagName.getLocalPart(), n.new ElementWithType(isNillable,type));
ohair@286 368
ohair@286 369 // search for foreign namespace references
ohair@286 370 if(type!=null)
ohair@286 371 n.addDependencyTo(type.getTypeName());
ohair@286 372 }
ohair@286 373
ohair@286 374 /**
ohair@286 375 * Writes out the episode file.
ohair@286 376 */
ohair@286 377 public void writeEpisodeFile(XmlSerializer out) {
ohair@286 378 Bindings root = TXW.create(Bindings.class, out);
ohair@286 379
ohair@286 380 if(namespaces.containsKey("")) // otherwise jaxb binding NS should be the default namespace
ohair@286 381 root._namespace(WellKnownNamespace.JAXB,"jaxb");
ohair@286 382 root.version("2.1");
ohair@286 383 // TODO: don't we want to bake in versions?
ohair@286 384
ohair@286 385 // generate listing per schema
ohair@286 386 for (Map.Entry<String,Namespace> e : namespaces.entrySet()) {
ohair@286 387 Bindings group = root.bindings();
ohair@286 388
ohair@286 389 String prefix;
ohair@286 390 String tns = e.getKey();
ohair@286 391 if(!tns.equals("")) {
ohair@286 392 group._namespace(tns,"tns");
ohair@286 393 prefix = "tns:";
ohair@286 394 } else {
ohair@286 395 prefix = "";
ohair@286 396 }
ohair@286 397
ohair@286 398 group.scd("x-schema::"+(tns.equals("")?"":"tns"));
ohair@286 399 group.schemaBindings().map(false);
ohair@286 400
ohair@286 401 for (ClassInfo<T,C> ci : e.getValue().classes) {
ohair@286 402 if(ci.getTypeName()==null) continue; // local type
ohair@286 403
ohair@286 404 if(ci.getTypeName().getNamespaceURI().equals(tns)) {
ohair@286 405 Bindings child = group.bindings();
ohair@286 406 child.scd('~'+prefix+ci.getTypeName().getLocalPart());
ohair@286 407 child.klass().ref(ci.getName());
ohair@286 408 }
ohair@286 409
ohair@286 410 if(ci.isElement() && ci.getElementName().getNamespaceURI().equals(tns)) {
ohair@286 411 Bindings child = group.bindings();
ohair@286 412 child.scd(prefix+ci.getElementName().getLocalPart());
ohair@286 413 child.klass().ref(ci.getName());
ohair@286 414 }
ohair@286 415 }
ohair@286 416
ohair@286 417 for (EnumLeafInfo<T,C> en : e.getValue().enums) {
ohair@286 418 if(en.getTypeName()==null) continue; // local type
ohair@286 419
ohair@286 420 Bindings child = group.bindings();
ohair@286 421 child.scd('~'+prefix+en.getTypeName().getLocalPart());
ohair@286 422 child.klass().ref(navigator.getClassName(en.getClazz()));
ohair@286 423 }
ohair@286 424
ohair@286 425 group.commit(true);
ohair@286 426 }
ohair@286 427
ohair@286 428 root.commit();
ohair@286 429 }
ohair@286 430
ohair@286 431 /**
ohair@286 432 * Write out the schema documents.
ohair@286 433 */
ohair@286 434 public void write(SchemaOutputResolver resolver, ErrorListener errorListener) throws IOException {
ohair@286 435 if(resolver==null)
ohair@286 436 throw new IllegalArgumentException();
ohair@286 437
ohair@286 438 if(logger.isLoggable(Level.FINE)) {
ohair@286 439 // debug logging to see what's going on.
aefimov@1101 440 logger.log(Level.FINE,"Writing XML Schema for "+toString(),new StackRecorder());
ohair@286 441 }
ohair@286 442
ohair@286 443 // make it fool-proof
ohair@286 444 resolver = new FoolProofResolver(resolver);
ohair@286 445 this.errorListener = errorListener;
ohair@286 446
ohair@286 447 Map<String, String> schemaLocations = types.getSchemaLocations();
ohair@286 448
ohair@286 449 Map<Namespace,Result> out = new HashMap<Namespace,Result>();
ohair@286 450 Map<Namespace,String> systemIds = new HashMap<Namespace,String>();
ohair@286 451
ohair@286 452 // we create a Namespace object for the XML Schema namespace
ohair@286 453 // as a side-effect, but we don't want to generate it.
ohair@286 454 namespaces.remove(WellKnownNamespace.XML_SCHEMA);
ohair@286 455
ohair@286 456 // first create the outputs for all so that we can resolve references among
ohair@286 457 // schema files when we write
ohair@286 458 for( Namespace n : namespaces.values() ) {
ohair@286 459 String schemaLocation = schemaLocations.get(n.uri);
ohair@286 460 if(schemaLocation!=null) {
ohair@286 461 systemIds.put(n,schemaLocation);
ohair@286 462 } else {
ohair@286 463 Result output = resolver.createOutput(n.uri,"schema"+(out.size()+1)+".xsd");
ohair@286 464 if(output!=null) { // null result means no schema for that namespace
ohair@286 465 out.put(n,output);
ohair@286 466 systemIds.put(n,output.getSystemId());
ohair@286 467 }
ohair@286 468 }
aefimov@1101 469 //Clear the namespace specific set with already written classes
aefimov@1101 470 n.resetWritten();
ohair@286 471 }
ohair@286 472
ohair@286 473 // then write'em all
ohair@286 474 for( Map.Entry<Namespace,Result> e : out.entrySet() ) {
ohair@286 475 Result result = e.getValue();
ohair@286 476 e.getKey().writeTo( result, systemIds );
ohair@286 477 if(result instanceof StreamResult) {
ohair@286 478 OutputStream outputStream = ((StreamResult)result).getOutputStream();
ohair@286 479 if(outputStream != null) {
ohair@286 480 outputStream.close(); // fix for bugid: 6291301
ohair@286 481 } else {
ohair@286 482 final Writer writer = ((StreamResult)result).getWriter();
ohair@286 483 if(writer != null) writer.close();
ohair@286 484 }
ohair@286 485 }
ohair@286 486 }
ohair@286 487 }
ohair@286 488
ohair@286 489
ohair@286 490
ohair@286 491 /**
ohair@286 492 * Schema components are organized per namespace.
ohair@286 493 */
ohair@286 494 private class Namespace {
ohair@286 495 final @NotNull String uri;
ohair@286 496
ohair@286 497 /**
ohair@286 498 * Other {@link Namespace}s that this namespace depends on.
ohair@286 499 */
ohair@286 500 private final Set<Namespace> depends = new LinkedHashSet<Namespace>();
ohair@286 501
ohair@286 502 /**
ohair@286 503 * If this schema refers to components from this schema by itself.
ohair@286 504 */
ohair@286 505 private boolean selfReference;
ohair@286 506
ohair@286 507 /**
ohair@286 508 * List of classes in this namespace.
ohair@286 509 */
ohair@286 510 private final Set<ClassInfo<T,C>> classes = new LinkedHashSet<ClassInfo<T,C>>();
ohair@286 511
ohair@286 512 /**
ohair@286 513 * Set of enums in this namespace
ohair@286 514 */
ohair@286 515 private final Set<EnumLeafInfo<T,C>> enums = new LinkedHashSet<EnumLeafInfo<T,C>>();
ohair@286 516
ohair@286 517 /**
ohair@286 518 * Set of arrays in this namespace
ohair@286 519 */
ohair@286 520 private final Set<ArrayInfo<T,C>> arrays = new LinkedHashSet<ArrayInfo<T,C>>();
ohair@286 521
ohair@286 522 /**
ohair@286 523 * Global attribute declarations keyed by their local names.
ohair@286 524 */
ohair@286 525 private final MultiMap<String,AttributePropertyInfo<T,C>> attributeDecls = new MultiMap<String,AttributePropertyInfo<T,C>>(null);
ohair@286 526
ohair@286 527 /**
ohair@286 528 * Global element declarations to be written, keyed by their local names.
ohair@286 529 */
ohair@286 530 private final MultiMap<String,ElementDeclaration> elementDecls =
ohair@286 531 new MultiMap<String,ElementDeclaration>(new ElementWithType(true,anyType));
ohair@286 532
ohair@286 533 private Form attributeFormDefault;
ohair@286 534 private Form elementFormDefault;
ohair@286 535
ohair@286 536 /**
ohair@286 537 * Does schema in this namespace uses swaRef? If so, we need to generate import
ohair@286 538 * statement.
ohair@286 539 */
ohair@286 540 private boolean useSwaRef;
ohair@286 541
ohair@286 542 /**
ohair@286 543 * Import for mime namespace needs to be generated.
ohair@286 544 * See #856
ohair@286 545 */
ohair@286 546 private boolean useMimeNs;
ohair@286 547
aefimov@1101 548 /**
aefimov@1101 549 * Container for already processed classes
aefimov@1101 550 */
aefimov@1101 551 private final Set<ClassInfo> written = new HashSet<ClassInfo>();
aefimov@1101 552
ohair@286 553 public Namespace(String uri) {
ohair@286 554 this.uri = uri;
ohair@286 555 assert !XmlSchemaGenerator.this.namespaces.containsKey(uri);
ohair@286 556 XmlSchemaGenerator.this.namespaces.put(uri,this);
ohair@286 557 }
ohair@286 558
ohair@286 559 /**
aefimov@1101 560 * Clear out the set of already processed classes for this namespace
aefimov@1101 561 */
aefimov@1101 562 void resetWritten() {
aefimov@1101 563 written.clear();
aefimov@1101 564 }
aefimov@1101 565
aefimov@1101 566 /**
ohair@286 567 * Process the given PropertyInfo looking for references to namespaces that
ohair@286 568 * are foreign to the given namespace. Any foreign namespace references
ohair@286 569 * found are added to the given namespaces dependency list and an &lt;import>
ohair@286 570 * is generated for it.
ohair@286 571 *
ohair@286 572 * @param p the PropertyInfo
ohair@286 573 */
ohair@286 574 private void processForeignNamespaces(PropertyInfo<T, C> p, int processingDepth) {
ohair@286 575 for (TypeInfo<T, C> t : p.ref()) {
ohair@286 576 if ((t instanceof ClassInfo) && (processingDepth > 0)) {
ohair@286 577 java.util.List<PropertyInfo> l = ((ClassInfo) t).getProperties();
ohair@286 578 for (PropertyInfo subp : l) {
ohair@286 579 processForeignNamespaces(subp, --processingDepth);
ohair@286 580 }
ohair@286 581 }
ohair@286 582 if (t instanceof Element) {
ohair@286 583 addDependencyTo(((Element) t).getElementName());
ohair@286 584 }
ohair@286 585 if (t instanceof NonElement) {
ohair@286 586 addDependencyTo(((NonElement) t).getTypeName());
ohair@286 587 }
ohair@286 588 }
ohair@286 589 }
ohair@286 590
ohair@286 591 private void addDependencyTo(@Nullable QName qname) {
ohair@286 592 // even though the Element interface says getElementName() returns non-null,
ohair@286 593 // ClassInfo always implements Element (even if an instance of ClassInfo might not be an Element).
ohair@286 594 // so this check is still necessary
ohair@286 595 if (qname==null) {
ohair@286 596 return;
ohair@286 597 }
ohair@286 598
ohair@286 599 String nsUri = qname.getNamespaceURI();
ohair@286 600
ohair@286 601 if (nsUri.equals(XML_SCHEMA)) {
ohair@286 602 // no need to explicitly refer to XSD namespace
ohair@286 603 return;
ohair@286 604 }
ohair@286 605
ohair@286 606 if (nsUri.equals(uri)) {
ohair@286 607 selfReference = true;
ohair@286 608 return;
ohair@286 609 }
ohair@286 610
ohair@286 611 // found a type in a foreign namespace, so make sure we generate an import for it
ohair@286 612 depends.add(getNamespace(nsUri));
ohair@286 613 }
ohair@286 614
ohair@286 615 /**
ohair@286 616 * Writes the schema document to the specified result.
ohair@286 617 *
ohair@286 618 * @param systemIds
ohair@286 619 * System IDs of the other schema documents. "" indicates 'implied'.
ohair@286 620 */
ohair@286 621 private void writeTo(Result result, Map<Namespace,String> systemIds) throws IOException {
ohair@286 622 try {
ohair@286 623 Schema schema = TXW.create(Schema.class,ResultFactory.createSerializer(result));
ohair@286 624
ohair@286 625 // additional namespace declarations to be made.
ohair@286 626 Map<String, String> xmlNs = types.getXmlNs(uri);
ohair@286 627
ohair@286 628 for (Map.Entry<String, String> e : xmlNs.entrySet()) {
ohair@286 629 schema._namespace(e.getValue(),e.getKey());
ohair@286 630 }
ohair@286 631
ohair@286 632 if(useSwaRef)
ohair@286 633 schema._namespace(WellKnownNamespace.SWA_URI,"swaRef");
ohair@286 634
ohair@286 635 if(useMimeNs)
ohair@286 636 schema._namespace(WellKnownNamespace.XML_MIME_URI,"xmime");
ohair@286 637
ohair@286 638 attributeFormDefault = Form.get(types.getAttributeFormDefault(uri));
ohair@286 639 attributeFormDefault.declare("attributeFormDefault",schema);
ohair@286 640
ohair@286 641 elementFormDefault = Form.get(types.getElementFormDefault(uri));
ohair@286 642 // TODO: if elementFormDefault is UNSET, figure out the right default value to use
ohair@286 643 elementFormDefault.declare("elementFormDefault",schema);
ohair@286 644
ohair@286 645
ohair@286 646 // declare XML Schema namespace to be xs, but allow the user to override it.
ohair@286 647 // if 'xs' is used for other things, we'll just let TXW assign a random prefix
ohair@286 648 if(!xmlNs.containsValue(WellKnownNamespace.XML_SCHEMA)
ohair@286 649 && !xmlNs.containsKey("xs"))
ohair@286 650 schema._namespace(WellKnownNamespace.XML_SCHEMA,"xs");
ohair@286 651 schema.version("1.0");
ohair@286 652
ohair@286 653 if(uri.length()!=0)
ohair@286 654 schema.targetNamespace(uri);
ohair@286 655
ohair@286 656 // declare prefixes for them at this level, so that we can avoid redundant
ohair@286 657 // namespace declarations
ohair@286 658 for (Namespace ns : depends) {
ohair@286 659 schema._namespace(ns.uri);
ohair@286 660 }
ohair@286 661
ohair@286 662 if(selfReference && uri.length()!=0) {
ohair@286 663 // use common 'tns' prefix for the own namespace
ohair@286 664 // if self-reference is needed
ohair@286 665 schema._namespace(uri,"tns");
ohair@286 666 }
ohair@286 667
ohair@286 668 schema._pcdata(newline);
ohair@286 669
ohair@286 670 // refer to other schemas
ohair@286 671 for( Namespace n : depends ) {
ohair@286 672 Import imp = schema._import();
ohair@286 673 if(n.uri.length()!=0)
ohair@286 674 imp.namespace(n.uri);
ohair@286 675 String refSystemId = systemIds.get(n);
ohair@286 676 if(refSystemId!=null && !refSystemId.equals("")) {
ohair@286 677 // "" means implied. null if the SchemaOutputResolver said "don't generate!"
ohair@286 678 imp.schemaLocation(relativize(refSystemId,result.getSystemId()));
ohair@286 679 }
ohair@286 680 schema._pcdata(newline);
ohair@286 681 }
ohair@286 682 if(useSwaRef) {
ohair@286 683 schema._import().namespace(WellKnownNamespace.SWA_URI).schemaLocation("http://ws-i.org/profiles/basic/1.1/swaref.xsd");
ohair@286 684 }
ohair@286 685 if(useMimeNs) {
ohair@286 686 schema._import().namespace(WellKnownNamespace.XML_MIME_URI).schemaLocation("http://www.w3.org/2005/05/xmlmime");
ohair@286 687 }
ohair@286 688
ohair@286 689 // then write each component
ohair@286 690 for (Map.Entry<String,ElementDeclaration> e : elementDecls.entrySet()) {
ohair@286 691 e.getValue().writeTo(e.getKey(),schema);
ohair@286 692 schema._pcdata(newline);
ohair@286 693 }
ohair@286 694 for (ClassInfo<T, C> c : classes) {
ohair@286 695 if (c.getTypeName()==null) {
ohair@286 696 // don't generate anything if it's an anonymous type
ohair@286 697 continue;
ohair@286 698 }
ohair@286 699 if(uri.equals(c.getTypeName().getNamespaceURI()))
ohair@286 700 writeClass(c, schema);
ohair@286 701 schema._pcdata(newline);
ohair@286 702 }
ohair@286 703 for (EnumLeafInfo<T, C> e : enums) {
ohair@286 704 if (e.getTypeName()==null) {
ohair@286 705 // don't generate anything if it's an anonymous type
ohair@286 706 continue;
ohair@286 707 }
ohair@286 708 if(uri.equals(e.getTypeName().getNamespaceURI()))
ohair@286 709 writeEnum(e,schema);
ohair@286 710 schema._pcdata(newline);
ohair@286 711 }
ohair@286 712 for (ArrayInfo<T, C> a : arrays) {
ohair@286 713 writeArray(a,schema);
ohair@286 714 schema._pcdata(newline);
ohair@286 715 }
ohair@286 716 for (Map.Entry<String,AttributePropertyInfo<T,C>> e : attributeDecls.entrySet()) {
ohair@286 717 TopLevelAttribute a = schema.attribute();
ohair@286 718 a.name(e.getKey());
ohair@286 719 if(e.getValue()==null)
ohair@286 720 writeTypeRef(a,stringType,"type");
ohair@286 721 else
ohair@286 722 writeAttributeTypeRef(e.getValue(),a);
ohair@286 723 schema._pcdata(newline);
ohair@286 724 }
ohair@286 725
ohair@286 726 // close the schema
ohair@286 727 schema.commit();
ohair@286 728 } catch( TxwException e ) {
ohair@286 729 logger.log(Level.INFO,e.getMessage(),e);
ohair@286 730 throw new IOException(e.getMessage());
ohair@286 731 }
ohair@286 732 }
ohair@286 733
ohair@286 734 /**
ohair@286 735 * Writes a type attribute (if the referenced type is a global type)
ohair@286 736 * or writes out the definition of the anonymous type in place (if the referenced
ohair@286 737 * type is not a global type.)
ohair@286 738 *
ohair@286 739 * Also provides processing for ID/IDREF, MTOM @xmime, and swa:ref
ohair@286 740 *
ohair@286 741 * ComplexTypeHost and SimpleTypeHost don't share an api for creating
ohair@286 742 * and attribute in a type-safe way, so we will compromise for now and
ohair@286 743 * use _attribute().
ohair@286 744 */
ohair@286 745 private void writeTypeRef(TypeHost th, NonElementRef<T, C> typeRef, String refAttName) {
ohair@286 746 // ID / IDREF handling
ohair@286 747 switch(typeRef.getSource().id()) {
ohair@286 748 case ID:
ohair@286 749 th._attribute(refAttName, new QName(WellKnownNamespace.XML_SCHEMA, "ID"));
ohair@286 750 return;
ohair@286 751 case IDREF:
ohair@286 752 th._attribute(refAttName, new QName(WellKnownNamespace.XML_SCHEMA, "IDREF"));
ohair@286 753 return;
ohair@286 754 case NONE:
ohair@286 755 // no ID/IDREF, so continue on and generate the type
ohair@286 756 break;
ohair@286 757 default:
ohair@286 758 throw new IllegalStateException();
ohair@286 759 }
ohair@286 760
ohair@286 761 // MTOM handling
ohair@286 762 MimeType mimeType = typeRef.getSource().getExpectedMimeType();
ohair@286 763 if( mimeType != null ) {
ohair@286 764 th._attribute(new QName(WellKnownNamespace.XML_MIME_URI, "expectedContentTypes", "xmime"), mimeType.toString());
ohair@286 765 }
ohair@286 766
ohair@286 767 // ref:swaRef handling
ohair@286 768 if(generateSwaRefAdapter(typeRef)) {
ohair@286 769 th._attribute(refAttName, new QName(WellKnownNamespace.SWA_URI, "swaRef", "ref"));
ohair@286 770 return;
ohair@286 771 }
ohair@286 772
ohair@286 773 // type name override
ohair@286 774 if(typeRef.getSource().getSchemaType()!=null) {
ohair@286 775 th._attribute(refAttName,typeRef.getSource().getSchemaType());
ohair@286 776 return;
ohair@286 777 }
ohair@286 778
ohair@286 779 // normal type generation
ohair@286 780 writeTypeRef(th, typeRef.getTarget(), refAttName);
ohair@286 781 }
ohair@286 782
ohair@286 783 /**
ohair@286 784 * Writes a type attribute (if the referenced type is a global type)
ohair@286 785 * or writes out the definition of the anonymous type in place (if the referenced
ohair@286 786 * type is not a global type.)
ohair@286 787 *
ohair@286 788 * @param th
ohair@286 789 * the TXW interface to which the attribute will be written.
ohair@286 790 * @param type
ohair@286 791 * type to be referenced.
ohair@286 792 * @param refAttName
ohair@286 793 * The name of the attribute used when referencing a type by QName.
ohair@286 794 */
ohair@286 795 private void writeTypeRef(TypeHost th, NonElement<T,C> type, String refAttName) {
ohair@286 796 Element e = null;
ohair@286 797 if (type instanceof MaybeElement) {
ohair@286 798 MaybeElement me = (MaybeElement)type;
ohair@286 799 boolean isElement = me.isElement();
ohair@286 800 if (isElement) e = me.asElement();
ohair@286 801 }
ohair@286 802 if (type instanceof Element) {
ohair@286 803 e = (Element)type;
ohair@286 804 }
ohair@286 805 if (type.getTypeName()==null) {
ohair@286 806 if ((e != null) && (e.getElementName() != null)) {
ohair@286 807 th.block(); // so that the caller may write other attributes
ohair@286 808 if(type instanceof ClassInfo) {
ohair@286 809 writeClass( (ClassInfo<T,C>)type, th );
ohair@286 810 } else {
ohair@286 811 writeEnum( (EnumLeafInfo<T,C>)type, (SimpleTypeHost)th);
ohair@286 812 }
ohair@286 813 } else {
ohair@286 814 // anonymous
ohair@286 815 th.block(); // so that the caller may write other attributes
ohair@286 816 if(type instanceof ClassInfo) {
ohair@286 817 if(collisionChecker.push((ClassInfo<T,C>)type)) {
ohair@286 818 errorListener.warning(new SAXParseException(
ohair@286 819 Messages.ANONYMOUS_TYPE_CYCLE.format(collisionChecker.getCycleString()),
ohair@286 820 null
ohair@286 821 ));
ohair@286 822 } else {
ohair@286 823 writeClass( (ClassInfo<T,C>)type, th );
ohair@286 824 }
ohair@286 825 collisionChecker.pop();
ohair@286 826 } else {
ohair@286 827 writeEnum( (EnumLeafInfo<T,C>)type, (SimpleTypeHost)th);
ohair@286 828 }
ohair@286 829 }
ohair@286 830 } else {
ohair@286 831 th._attribute(refAttName,type.getTypeName());
ohair@286 832 }
ohair@286 833 }
ohair@286 834
ohair@286 835 /**
ohair@286 836 * writes the schema definition for the given array class
ohair@286 837 */
ohair@286 838 private void writeArray(ArrayInfo<T, C> a, Schema schema) {
ohair@286 839 ComplexType ct = schema.complexType().name(a.getTypeName().getLocalPart());
ohair@286 840 ct._final("#all");
ohair@286 841 LocalElement le = ct.sequence().element().name("item");
ohair@286 842 le.type(a.getItemType().getTypeName());
ohair@286 843 le.minOccurs(0).maxOccurs("unbounded");
ohair@286 844 le.nillable(true);
ohair@286 845 ct.commit();
ohair@286 846 }
ohair@286 847
ohair@286 848 /**
ohair@286 849 * writes the schema definition for the specified type-safe enum in the given TypeHost
ohair@286 850 */
ohair@286 851 private void writeEnum(EnumLeafInfo<T, C> e, SimpleTypeHost th) {
ohair@286 852 SimpleType st = th.simpleType();
ohair@286 853 writeName(e,st);
ohair@286 854
ohair@286 855 SimpleRestrictionModel base = st.restriction();
ohair@286 856 writeTypeRef(base, e.getBaseType(), "base");
ohair@286 857
ohair@286 858 for (EnumConstant c : e.getConstants()) {
ohair@286 859 base.enumeration().value(c.getLexicalValue());
ohair@286 860 }
ohair@286 861 st.commit();
ohair@286 862 }
ohair@286 863
ohair@286 864 /**
ohair@286 865 * Writes the schema definition for the specified class to the schema writer.
ohair@286 866 *
ohair@286 867 * @param c the class info
ohair@286 868 * @param parent the writer of the parent element into which the type will be defined
ohair@286 869 */
ohair@286 870 private void writeClass(ClassInfo<T,C> c, TypeHost parent) {
aefimov@1101 871 if (written.contains(c)) { // to avoid cycles let's check if we haven't already processed the class
aefimov@1101 872 return;
aefimov@1101 873 }
aefimov@1101 874 written.add(c);
ohair@286 875 // special handling for value properties
ohair@286 876 if (containsValueProp(c)) {
ohair@286 877 if (c.getProperties().size() == 1) {
ohair@286 878 // [RESULT 2 - simpleType if the value prop is the only prop]
ohair@286 879 //
ohair@286 880 // <simpleType name="foo">
ohair@286 881 // <xs:restriction base="xs:int"/>
ohair@286 882 // </>
ohair@286 883 ValuePropertyInfo<T,C> vp = (ValuePropertyInfo<T,C>)c.getProperties().get(0);
ohair@286 884 SimpleType st = ((SimpleTypeHost)parent).simpleType();
ohair@286 885 writeName(c, st);
ohair@286 886 if(vp.isCollection()) {
ohair@286 887 writeTypeRef(st.list(),vp.getTarget(),"itemType");
ohair@286 888 } else {
ohair@286 889 writeTypeRef(st.restriction(),vp.getTarget(),"base");
ohair@286 890 }
ohair@286 891 return;
ohair@286 892 } else {
ohair@286 893 // [RESULT 1 - complexType with simpleContent]
ohair@286 894 //
ohair@286 895 // <complexType name="foo">
ohair@286 896 // <simpleContent>
ohair@286 897 // <extension base="xs:int"/>
ohair@286 898 // <attribute name="b" type="xs:boolean"/>
ohair@286 899 // </>
ohair@286 900 // </>
ohair@286 901 // </>
ohair@286 902 // ...
ohair@286 903 // <element name="f" type="foo"/>
ohair@286 904 // ...
ohair@286 905 ComplexType ct = ((ComplexTypeHost)parent).complexType();
ohair@286 906 writeName(c,ct);
ohair@286 907 if(c.isFinal())
ohair@286 908 ct._final("extension restriction");
ohair@286 909
ohair@286 910 SimpleExtension se = ct.simpleContent().extension();
ohair@286 911 se.block(); // because we might have attribute before value
ohair@286 912 for (PropertyInfo<T,C> p : c.getProperties()) {
ohair@286 913 switch (p.kind()) {
ohair@286 914 case ATTRIBUTE:
ohair@286 915 handleAttributeProp((AttributePropertyInfo<T,C>)p,se);
ohair@286 916 break;
ohair@286 917 case VALUE:
ohair@286 918 TODO.checkSpec("what if vp.isCollection() == true?");
ohair@286 919 ValuePropertyInfo vp = (ValuePropertyInfo) p;
ohair@286 920 se.base(vp.getTarget().getTypeName());
ohair@286 921 break;
ohair@286 922 case ELEMENT: // error
ohair@286 923 case REFERENCE: // error
ohair@286 924 default:
ohair@286 925 assert false;
ohair@286 926 throw new IllegalStateException();
ohair@286 927 }
ohair@286 928 }
ohair@286 929 se.commit();
ohair@286 930 }
ohair@286 931 TODO.schemaGenerator("figure out what to do if bc != null");
ohair@286 932 TODO.checkSpec("handle sec 8.9.5.2, bullet #4");
ohair@286 933 // Java types containing value props can only contain properties of type
ohair@286 934 // ValuePropertyinfo and AttributePropertyInfo which have just been handled,
ohair@286 935 // so return.
ohair@286 936 return;
ohair@286 937 }
ohair@286 938
ohair@286 939 // we didn't fall into the special case for value props, so we
ohair@286 940 // need to initialize the ct.
ohair@286 941 // generate the complexType
ohair@286 942 ComplexType ct = ((ComplexTypeHost)parent).complexType();
ohair@286 943 writeName(c,ct);
ohair@286 944 if(c.isFinal())
ohair@286 945 ct._final("extension restriction");
ohair@286 946 if(c.isAbstract())
ohair@286 947 ct._abstract(true);
ohair@286 948
ohair@286 949 // these are where we write content model and attributes
ohair@286 950 AttrDecls contentModel = ct;
ohair@286 951 TypeDefParticle contentModelOwner = ct;
ohair@286 952
ohair@286 953 // if there is a base class, we need to generate an extension in the schema
ohair@286 954 final ClassInfo<T,C> bc = c.getBaseClass();
ohair@286 955 if (bc != null) {
ohair@286 956 if(bc.hasValueProperty()) {
ohair@286 957 // extending complex type with simple content
ohair@286 958 SimpleExtension se = ct.simpleContent().extension();
ohair@286 959 contentModel = se;
ohair@286 960 contentModelOwner = null;
ohair@286 961 se.base(bc.getTypeName());
ohair@286 962 } else {
ohair@286 963 ComplexExtension ce = ct.complexContent().extension();
ohair@286 964 contentModel = ce;
ohair@286 965 contentModelOwner = ce;
ohair@286 966
ohair@286 967 ce.base(bc.getTypeName());
ohair@286 968 // TODO: what if the base type is anonymous?
ohair@286 969 }
ohair@286 970 }
ohair@286 971
ohair@286 972 if(contentModelOwner!=null) {
ohair@286 973 // build the tree that represents the explicit content model from iterate over the properties
ohair@286 974 ArrayList<Tree> children = new ArrayList<Tree>();
ohair@286 975 for (PropertyInfo<T,C> p : c.getProperties()) {
ohair@286 976 // handling for <complexType @mixed='true' ...>
ohair@286 977 if(p instanceof ReferencePropertyInfo && ((ReferencePropertyInfo)p).isMixed()) {
ohair@286 978 ct.mixed(true);
ohair@286 979 }
ohair@286 980 Tree t = buildPropertyContentModel(p);
ohair@286 981 if(t!=null)
ohair@286 982 children.add(t);
ohair@286 983 }
ohair@286 984
ohair@286 985 Tree top = Tree.makeGroup( c.isOrdered() ? GroupKind.SEQUENCE : GroupKind.ALL, children);
ohair@286 986
ohair@286 987 // write the content model
ohair@286 988 top.write(contentModelOwner);
ohair@286 989 }
ohair@286 990
ohair@286 991 // then attributes
ohair@286 992 for (PropertyInfo<T,C> p : c.getProperties()) {
ohair@286 993 if (p instanceof AttributePropertyInfo) {
ohair@286 994 handleAttributeProp((AttributePropertyInfo<T,C>)p, contentModel);
ohair@286 995 }
ohair@286 996 }
ohair@286 997 if( c.hasAttributeWildcard()) {
ohair@286 998 contentModel.anyAttribute().namespace("##other").processContents("skip");
ohair@286 999 }
ohair@286 1000 ct.commit();
ohair@286 1001 }
ohair@286 1002
ohair@286 1003 /**
ohair@286 1004 * Writes the name attribute if it's named.
ohair@286 1005 */
ohair@286 1006 private void writeName(NonElement<T,C> c, TypedXmlWriter xw) {
ohair@286 1007 QName tn = c.getTypeName();
ohair@286 1008 if(tn!=null)
ohair@286 1009 xw._attribute("name",tn.getLocalPart()); // named
ohair@286 1010 }
ohair@286 1011
ohair@286 1012 private boolean containsValueProp(ClassInfo<T, C> c) {
ohair@286 1013 for (PropertyInfo p : c.getProperties()) {
ohair@286 1014 if (p instanceof ValuePropertyInfo) return true;
ohair@286 1015 }
ohair@286 1016 return false;
ohair@286 1017 }
ohair@286 1018
ohair@286 1019 /**
ohair@286 1020 * Builds content model writer for the specified property.
ohair@286 1021 */
ohair@286 1022 private Tree buildPropertyContentModel(PropertyInfo<T,C> p) {
ohair@286 1023 switch(p.kind()) {
ohair@286 1024 case ELEMENT:
ohair@286 1025 return handleElementProp((ElementPropertyInfo<T,C>)p);
ohair@286 1026 case ATTRIBUTE:
ohair@286 1027 // attribuets are handled later
ohair@286 1028 return null;
ohair@286 1029 case REFERENCE:
ohair@286 1030 return handleReferenceProp((ReferencePropertyInfo<T,C>)p);
ohair@286 1031 case MAP:
ohair@286 1032 return handleMapProp((MapPropertyInfo<T,C>)p);
ohair@286 1033 case VALUE:
ohair@286 1034 // value props handled above in writeClass()
ohair@286 1035 assert false;
ohair@286 1036 throw new IllegalStateException();
ohair@286 1037 default:
ohair@286 1038 assert false;
ohair@286 1039 throw new IllegalStateException();
ohair@286 1040 }
ohair@286 1041 }
ohair@286 1042
ohair@286 1043 /**
ohair@286 1044 * Generate the proper schema fragment for the given element property into the
ohair@286 1045 * specified schema compositor.
ohair@286 1046 *
ohair@286 1047 * The element property may or may not represent a collection and it may or may
ohair@286 1048 * not be wrapped.
ohair@286 1049 *
ohair@286 1050 * @param ep the element property
ohair@286 1051 */
ohair@286 1052 private Tree handleElementProp(final ElementPropertyInfo<T,C> ep) {
ohair@286 1053 if (ep.isValueList()) {
ohair@286 1054 return new Tree.Term() {
ohair@286 1055 protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) {
ohair@286 1056 TypeRef<T,C> t = ep.getTypes().get(0);
ohair@286 1057 LocalElement e = parent.element();
ohair@286 1058 e.block(); // we will write occurs later
ohair@286 1059 QName tn = t.getTagName();
ohair@286 1060 e.name(tn.getLocalPart());
ohair@286 1061 List lst = e.simpleType().list();
ohair@286 1062 writeTypeRef(lst,t, "itemType");
ohair@286 1063 elementFormDefault.writeForm(e,tn);
ohair@286 1064 writeOccurs(e,isOptional||!ep.isRequired(),repeated);
ohair@286 1065 }
ohair@286 1066 };
ohair@286 1067 }
ohair@286 1068
ohair@286 1069 ArrayList<Tree> children = new ArrayList<Tree>();
ohair@286 1070 for (final TypeRef<T,C> t : ep.getTypes()) {
ohair@286 1071 children.add(new Tree.Term() {
ohair@286 1072 protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) {
ohair@286 1073 LocalElement e = parent.element();
ohair@286 1074
ohair@286 1075 QName tn = t.getTagName();
ohair@286 1076
ohair@286 1077 PropertyInfo propInfo = t.getSource();
ohair@286 1078 TypeInfo parentInfo = (propInfo == null) ? null : propInfo.parent();
ohair@286 1079
ohair@286 1080 if (canBeDirectElementRef(t, tn, parentInfo)) {
ohair@286 1081 if ((!t.getTarget().isSimpleType()) && (t.getTarget() instanceof ClassInfo) && collisionChecker.findDuplicate((ClassInfo<T, C>) t.getTarget())) {
ohair@286 1082 e.ref(new QName(uri, tn.getLocalPart()));
ohair@286 1083 } else {
ohair@286 1084
ohair@286 1085 QName elemName = null;
ohair@286 1086 if (t.getTarget() instanceof Element) {
ohair@286 1087 Element te = (Element) t.getTarget();
ohair@286 1088 elemName = te.getElementName();
ohair@286 1089 }
ohair@286 1090
alanb@368 1091 Collection<TypeInfo> refs = propInfo.ref();
alanb@368 1092 TypeInfo ti;
alanb@368 1093 if ((refs != null) && (!refs.isEmpty()) && (elemName != null)
alanb@368 1094 && ((ti = refs.iterator().next()) == null || ti instanceof ClassInfoImpl)) {
alanb@368 1095 ClassInfoImpl cImpl = (ClassInfoImpl)ti;
ohair@286 1096 if ((cImpl != null) && (cImpl.getElementName() != null)) {
ohair@286 1097 e.ref(new QName(cImpl.getElementName().getNamespaceURI(), tn.getLocalPart()));
ohair@286 1098 } else {
ohair@286 1099 e.ref(new QName("", tn.getLocalPart()));
ohair@286 1100 }
ohair@286 1101 } else {
ohair@286 1102 e.ref(tn);
ohair@286 1103 }
ohair@286 1104 }
ohair@286 1105 } else {
ohair@286 1106 e.name(tn.getLocalPart());
ohair@286 1107 writeTypeRef(e,t, "type");
ohair@286 1108 elementFormDefault.writeForm(e,tn);
ohair@286 1109 }
ohair@286 1110
ohair@286 1111 if (t.isNillable()) {
ohair@286 1112 e.nillable(true);
ohair@286 1113 }
ohair@286 1114 if(t.getDefaultValue()!=null)
ohair@286 1115 e._default(t.getDefaultValue());
ohair@286 1116 writeOccurs(e,isOptional,repeated);
ohair@286 1117 }
ohair@286 1118 });
ohair@286 1119 }
ohair@286 1120
ohair@286 1121 final Tree choice = Tree.makeGroup(GroupKind.CHOICE, children)
ohair@286 1122 .makeOptional(!ep.isRequired())
ohair@286 1123 .makeRepeated(ep.isCollection()); // see Spec table 8-13
ohair@286 1124
ohair@286 1125
ohair@286 1126 final QName ename = ep.getXmlName();
ohair@286 1127 if (ename != null) { // wrapped collection
ohair@286 1128 return new Tree.Term() {
ohair@286 1129 protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) {
ohair@286 1130 LocalElement e = parent.element();
ohair@286 1131 if(ename.getNamespaceURI().length()>0) {
ohair@286 1132 if (!ename.getNamespaceURI().equals(uri)) {
ohair@286 1133 // TODO: we need to generate the corresponding element declaration for this
ohair@286 1134 // table 8-25: Property/field element wrapper with ref attribute
ohair@286 1135 e.ref(new QName(ename.getNamespaceURI(), ename.getLocalPart()));
ohair@286 1136 return;
ohair@286 1137 }
ohair@286 1138 }
ohair@286 1139 e.name(ename.getLocalPart());
ohair@286 1140 elementFormDefault.writeForm(e,ename);
ohair@286 1141
ohair@286 1142 if(ep.isCollectionNillable()) {
ohair@286 1143 e.nillable(true);
ohair@286 1144 }
ohair@286 1145 writeOccurs(e,!ep.isCollectionRequired(),repeated);
ohair@286 1146
ohair@286 1147 ComplexType p = e.complexType();
ohair@286 1148 choice.write(p);
ohair@286 1149 }
ohair@286 1150 };
ohair@286 1151 } else {// non-wrapped
ohair@286 1152 return choice;
ohair@286 1153 }
ohair@286 1154 }
ohair@286 1155
ohair@286 1156 /**
ohair@286 1157 * Checks if we can collapse
ohair@286 1158 * &lt;element name='foo' type='t' /> to &lt;element ref='foo' />.
ohair@286 1159 *
ohair@286 1160 * This is possible if we already have such declaration to begin with.
ohair@286 1161 */
ohair@286 1162 private boolean canBeDirectElementRef(TypeRef<T, C> t, QName tn, TypeInfo parentInfo) {
ohair@286 1163 Element te = null;
ohair@286 1164 ClassInfo ci = null;
ohair@286 1165 QName targetTagName = null;
ohair@286 1166
ohair@286 1167 if(t.isNillable() || t.getDefaultValue()!=null) {
ohair@286 1168 // can't put those attributes on <element ref>
ohair@286 1169 return false;
ohair@286 1170 }
ohair@286 1171
ohair@286 1172 if (t.getTarget() instanceof Element) {
ohair@286 1173 te = (Element) t.getTarget();
ohair@286 1174 targetTagName = te.getElementName();
ohair@286 1175 if (te instanceof ClassInfo) {
ohair@286 1176 ci = (ClassInfo)te;
ohair@286 1177 }
ohair@286 1178 }
ohair@286 1179
ohair@286 1180 String nsUri = tn.getNamespaceURI();
ohair@286 1181 if ((!nsUri.equals(uri) && nsUri.length()>0) && (!((parentInfo instanceof ClassInfo) && (((ClassInfo)parentInfo).getTypeName() == null)))) {
ohair@286 1182 return true;
ohair@286 1183 }
ohair@286 1184
mkos@384 1185 if ((ci != null) && ((targetTagName != null) && (te.getScope() == null) && (targetTagName.getNamespaceURI() == null))) {
mkos@384 1186 if (targetTagName.equals(tn)) {
ohair@286 1187 return true;
ohair@286 1188 }
ohair@286 1189 }
ohair@286 1190
ohair@286 1191 // we have the precise element defined already
ohair@286 1192 if (te != null) { // it is instanceof Element
ohair@286 1193 return targetTagName!=null && targetTagName.equals(tn);
ohair@286 1194 }
ohair@286 1195
ohair@286 1196 return false;
ohair@286 1197 }
ohair@286 1198
ohair@286 1199
ohair@286 1200 /**
ohair@286 1201 * Generate an attribute for the specified property on the specified complexType
ohair@286 1202 *
ohair@286 1203 * @param ap the attribute
ohair@286 1204 * @param attr the schema definition to which the attribute will be added
ohair@286 1205 */
ohair@286 1206 private void handleAttributeProp(AttributePropertyInfo<T,C> ap, AttrDecls attr) {
ohair@286 1207 // attr is either a top-level ComplexType or a ComplexExtension
ohair@286 1208 //
ohair@286 1209 // [RESULT]
ohair@286 1210 //
ohair@286 1211 // <complexType ...>
ohair@286 1212 // <...>...</>
ohair@286 1213 // <attribute name="foo" type="xs:int"/>
ohair@286 1214 // </>
ohair@286 1215 //
ohair@286 1216 // or
ohair@286 1217 //
ohair@286 1218 // <complexType ...>
ohair@286 1219 // <complexContent>
ohair@286 1220 // <extension ...>
ohair@286 1221 // <...>...</>
ohair@286 1222 // </>
ohair@286 1223 // </>
ohair@286 1224 // <attribute name="foo" type="xs:int"/>
ohair@286 1225 // </>
ohair@286 1226 //
ohair@286 1227 // or it could also be an in-lined type (attr ref)
ohair@286 1228 //
ohair@286 1229 LocalAttribute localAttribute = attr.attribute();
ohair@286 1230
ohair@286 1231 final String attrURI = ap.getXmlName().getNamespaceURI();
ohair@286 1232 if (attrURI.equals("") /*|| attrURI.equals(uri) --- those are generated as global attributes anyway, so use them.*/) {
ohair@286 1233 localAttribute.name(ap.getXmlName().getLocalPart());
ohair@286 1234
ohair@286 1235 writeAttributeTypeRef(ap, localAttribute);
ohair@286 1236
ohair@286 1237 attributeFormDefault.writeForm(localAttribute,ap.getXmlName());
ohair@286 1238 } else { // generate an attr ref
ohair@286 1239 localAttribute.ref(ap.getXmlName());
ohair@286 1240 }
ohair@286 1241
ohair@286 1242 if(ap.isRequired()) {
ohair@286 1243 // TODO: not type safe
ohair@286 1244 localAttribute.use("required");
ohair@286 1245 }
ohair@286 1246 }
ohair@286 1247
ohair@286 1248 private void writeAttributeTypeRef(AttributePropertyInfo<T,C> ap, AttributeType a) {
ohair@286 1249 if( ap.isCollection() )
ohair@286 1250 writeTypeRef(a.simpleType().list(), ap, "itemType");
ohair@286 1251 else
ohair@286 1252 writeTypeRef(a, ap, "type");
ohair@286 1253 }
ohair@286 1254
ohair@286 1255 /**
ohair@286 1256 * Generate the proper schema fragment for the given reference property into the
ohair@286 1257 * specified schema compositor.
ohair@286 1258 *
ohair@286 1259 * The reference property may or may not refer to a collection and it may or may
ohair@286 1260 * not be wrapped.
ohair@286 1261 */
ohair@286 1262 private Tree handleReferenceProp(final ReferencePropertyInfo<T, C> rp) {
ohair@286 1263 // fill in content model
ohair@286 1264 ArrayList<Tree> children = new ArrayList<Tree>();
ohair@286 1265
ohair@286 1266 for (final Element<T,C> e : rp.getElements()) {
ohair@286 1267 children.add(new Tree.Term() {
ohair@286 1268 protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) {
ohair@286 1269 LocalElement eref = parent.element();
ohair@286 1270
ohair@286 1271 boolean local=false;
ohair@286 1272
ohair@286 1273 QName en = e.getElementName();
ohair@286 1274 if(e.getScope()!=null) {
ohair@286 1275 // scoped. needs to be inlined
ohair@286 1276 boolean qualified = en.getNamespaceURI().equals(uri);
ohair@286 1277 boolean unqualified = en.getNamespaceURI().equals("");
ohair@286 1278 if(qualified || unqualified) {
ohair@286 1279 // can be inlined indeed
ohair@286 1280
ohair@286 1281 // write form="..." if necessary
ohair@286 1282 if(unqualified) {
ohair@286 1283 if(elementFormDefault.isEffectivelyQualified)
ohair@286 1284 eref.form("unqualified");
ohair@286 1285 } else {
ohair@286 1286 if(!elementFormDefault.isEffectivelyQualified)
ohair@286 1287 eref.form("qualified");
ohair@286 1288 }
ohair@286 1289
ohair@286 1290 local = true;
ohair@286 1291 eref.name(en.getLocalPart());
ohair@286 1292
ohair@286 1293 // write out type reference
ohair@286 1294 if(e instanceof ClassInfo) {
ohair@286 1295 writeTypeRef(eref,(ClassInfo<T,C>)e,"type");
ohair@286 1296 } else {
ohair@286 1297 writeTypeRef(eref,((ElementInfo<T,C>)e).getContentType(),"type");
ohair@286 1298 }
ohair@286 1299 }
ohair@286 1300 }
ohair@286 1301 if(!local)
ohair@286 1302 eref.ref(en);
ohair@286 1303 writeOccurs(eref,isOptional,repeated);
ohair@286 1304 }
ohair@286 1305 });
ohair@286 1306 }
ohair@286 1307
ohair@286 1308 final WildcardMode wc = rp.getWildcard();
ohair@286 1309 if( wc != null ) {
ohair@286 1310 children.add(new Tree.Term() {
ohair@286 1311 protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) {
ohair@286 1312 Any any = parent.any();
ohair@286 1313 final String pcmode = getProcessContentsModeName(wc);
ohair@286 1314 if( pcmode != null ) any.processContents(pcmode);
ohair@286 1315 any.namespace("##other");
ohair@286 1316 writeOccurs(any,isOptional,repeated);
ohair@286 1317 }
ohair@286 1318 });
ohair@286 1319 }
ohair@286 1320
ohair@286 1321
ohair@286 1322 final Tree choice = Tree.makeGroup(GroupKind.CHOICE, children).makeRepeated(rp.isCollection()).makeOptional(!rp.isRequired());
ohair@286 1323
ohair@286 1324 final QName ename = rp.getXmlName();
ohair@286 1325
ohair@286 1326 if (ename != null) { // wrapped
ohair@286 1327 return new Tree.Term() {
ohair@286 1328 protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) {
ohair@286 1329 LocalElement e = parent.element().name(ename.getLocalPart());
ohair@286 1330 elementFormDefault.writeForm(e,ename);
ohair@286 1331 if(rp.isCollectionNillable())
ohair@286 1332 e.nillable(true);
ohair@286 1333 writeOccurs(e,true,repeated);
ohair@286 1334
ohair@286 1335 ComplexType p = e.complexType();
ohair@286 1336 choice.write(p);
ohair@286 1337 }
ohair@286 1338 };
ohair@286 1339 } else { // unwrapped
ohair@286 1340 return choice;
ohair@286 1341 }
ohair@286 1342 }
ohair@286 1343
ohair@286 1344 /**
ohair@286 1345 * Generate the proper schema fragment for the given map property into the
ohair@286 1346 * specified schema compositor.
ohair@286 1347 *
ohair@286 1348 * @param mp the map property
ohair@286 1349 */
ohair@286 1350 private Tree handleMapProp(final MapPropertyInfo<T,C> mp) {
ohair@286 1351 return new Tree.Term() {
ohair@286 1352 protected void write(ContentModelContainer parent, boolean isOptional, boolean repeated) {
ohair@286 1353 QName ename = mp.getXmlName();
ohair@286 1354
ohair@286 1355 LocalElement e = parent.element();
ohair@286 1356 elementFormDefault.writeForm(e,ename);
ohair@286 1357 if(mp.isCollectionNillable())
ohair@286 1358 e.nillable(true);
ohair@286 1359
ohair@286 1360 e = e.name(ename.getLocalPart());
ohair@286 1361 writeOccurs(e,isOptional,repeated);
ohair@286 1362 ComplexType p = e.complexType();
ohair@286 1363
ohair@286 1364 // TODO: entry, key, and value are always unqualified. that needs to be fixed, too.
ohair@286 1365 // TODO: we need to generate the corresponding element declaration, if they are qualified
ohair@286 1366 e = p.sequence().element();
ohair@286 1367 e.name("entry").minOccurs(0).maxOccurs("unbounded");
ohair@286 1368
ohair@286 1369 ExplicitGroup seq = e.complexType().sequence();
ohair@286 1370 writeKeyOrValue(seq, "key", mp.getKeyType());
ohair@286 1371 writeKeyOrValue(seq, "value", mp.getValueType());
ohair@286 1372 }
ohair@286 1373 };
ohair@286 1374 }
ohair@286 1375
ohair@286 1376 private void writeKeyOrValue(ExplicitGroup seq, String tagName, NonElement<T, C> typeRef) {
ohair@286 1377 LocalElement key = seq.element().name(tagName);
ohair@286 1378 key.minOccurs(0);
ohair@286 1379 writeTypeRef(key, typeRef, "type");
ohair@286 1380 }
ohair@286 1381
ohair@286 1382 public void addGlobalAttribute(AttributePropertyInfo<T,C> ap) {
ohair@286 1383 attributeDecls.put( ap.getXmlName().getLocalPart(), ap );
ohair@286 1384 addDependencyTo(ap.getTarget().getTypeName());
ohair@286 1385 }
ohair@286 1386
ohair@286 1387 public void addGlobalElement(TypeRef<T,C> tref) {
ohair@286 1388 elementDecls.put( tref.getTagName().getLocalPart(), new ElementWithType(false,tref.getTarget()) );
ohair@286 1389 addDependencyTo(tref.getTarget().getTypeName());
ohair@286 1390 }
ohair@286 1391
ohair@286 1392 @Override
ohair@286 1393 public String toString() {
ohair@286 1394 StringBuilder buf = new StringBuilder();
ohair@286 1395 buf.append("[classes=").append(classes);
ohair@286 1396 buf.append(",elementDecls=").append(elementDecls);
ohair@286 1397 buf.append(",enums=").append(enums);
ohair@286 1398 buf.append("]");
ohair@286 1399 return super.toString();
ohair@286 1400 }
ohair@286 1401
ohair@286 1402 /**
ohair@286 1403 * Represents a global element declaration to be written.
ohair@286 1404 *
ohair@286 1405 * <p>
ohair@286 1406 * Because multiple properties can name the same global element even if
ohair@286 1407 * they have different Java type, the schema generator first needs to
ohair@286 1408 * walk through the model and decide what to generate for the given
ohair@286 1409 * element declaration.
ohair@286 1410 *
ohair@286 1411 * <p>
ohair@286 1412 * This class represents what will be written, and its {@link #equals(Object)}
ohair@286 1413 * method is implemented in such a way that two identical declarations
ohair@286 1414 * are considered as the same.
ohair@286 1415 */
ohair@286 1416 abstract class ElementDeclaration {
ohair@286 1417 /**
ohair@286 1418 * Returns true if two {@link ElementDeclaration}s are representing
ohair@286 1419 * the same schema fragment.
ohair@286 1420 */
ohair@286 1421 @Override
ohair@286 1422 public abstract boolean equals(Object o);
ohair@286 1423 @Override
ohair@286 1424 public abstract int hashCode();
ohair@286 1425
ohair@286 1426 /**
ohair@286 1427 * Generates the declaration.
ohair@286 1428 */
ohair@286 1429 public abstract void writeTo(String localName, Schema schema);
ohair@286 1430 }
ohair@286 1431
ohair@286 1432 /**
ohair@286 1433 * {@link ElementDeclaration} that refers to a {@link NonElement}.
ohair@286 1434 */
ohair@286 1435 class ElementWithType extends ElementDeclaration {
ohair@286 1436 private final boolean nillable;
ohair@286 1437 private final NonElement<T,C> type;
ohair@286 1438
ohair@286 1439 public ElementWithType(boolean nillable,NonElement<T, C> type) {
ohair@286 1440 this.type = type;
ohair@286 1441 this.nillable = nillable;
ohair@286 1442 }
ohair@286 1443
ohair@286 1444 public void writeTo(String localName, Schema schema) {
ohair@286 1445 TopLevelElement e = schema.element().name(localName);
ohair@286 1446 if(nillable)
ohair@286 1447 e.nillable(true);
ohair@286 1448 if (type != null) {
ohair@286 1449 writeTypeRef(e,type, "type");
ohair@286 1450 } else {
ohair@286 1451 e.complexType(); // refer to the nested empty complex type
ohair@286 1452 }
ohair@286 1453 e.commit();
ohair@286 1454 }
ohair@286 1455
ohair@286 1456 public boolean equals(Object o) {
ohair@286 1457 if (this == o) return true;
ohair@286 1458 if (o == null || getClass() != o.getClass()) return false;
ohair@286 1459
ohair@286 1460 final ElementWithType that = (ElementWithType) o;
ohair@286 1461 return type.equals(that.type);
ohair@286 1462 }
ohair@286 1463
ohair@286 1464 public int hashCode() {
ohair@286 1465 return type.hashCode();
ohair@286 1466 }
ohair@286 1467 }
ohair@286 1468 }
ohair@286 1469
ohair@286 1470 /**
ohair@286 1471 * Examine the specified element ref and determine if a swaRef attribute needs to be generated
ohair@286 1472 * @param typeRef
ohair@286 1473 */
ohair@286 1474 private boolean generateSwaRefAdapter(NonElementRef<T,C> typeRef) {
ohair@286 1475 return generateSwaRefAdapter(typeRef.getSource());
ohair@286 1476 }
ohair@286 1477
ohair@286 1478 /**
ohair@286 1479 * Examine the specified element ref and determine if a swaRef attribute needs to be generated
ohair@286 1480 */
ohair@286 1481 private boolean generateSwaRefAdapter(PropertyInfo<T,C> prop) {
ohair@286 1482 final Adapter<T,C> adapter = prop.getAdapter();
ohair@286 1483 if (adapter == null) return false;
ohair@286 1484 final Object o = navigator.asDecl(SwaRefAdapter.class);
ohair@286 1485 if (o == null) return false;
ohair@286 1486 return (o.equals(adapter.adapterType));
ohair@286 1487 }
ohair@286 1488
ohair@286 1489 /**
ohair@286 1490 * Debug information of what's in this {@link XmlSchemaGenerator}.
ohair@286 1491 */
ohair@286 1492 @Override
ohair@286 1493 public String toString() {
ohair@286 1494 StringBuilder buf = new StringBuilder();
ohair@286 1495 for (Namespace ns : namespaces.values()) {
ohair@286 1496 if(buf.length()>0) buf.append(',');
ohair@286 1497 buf.append(ns.uri).append('=').append(ns);
ohair@286 1498 }
ohair@286 1499 return super.toString()+'['+buf+']';
ohair@286 1500 }
ohair@286 1501
ohair@286 1502 /**
ohair@286 1503 * return the string representation of the processContents mode of the
ohair@286 1504 * give wildcard, or null if it is the schema default "strict"
ohair@286 1505 *
ohair@286 1506 */
ohair@286 1507 private static String getProcessContentsModeName(WildcardMode wc) {
ohair@286 1508 switch(wc) {
ohair@286 1509 case LAX:
ohair@286 1510 case SKIP:
ohair@286 1511 return wc.name().toLowerCase();
ohair@286 1512 case STRICT:
ohair@286 1513 return null;
ohair@286 1514 default:
ohair@286 1515 throw new IllegalStateException();
ohair@286 1516 }
ohair@286 1517 }
ohair@286 1518
ohair@286 1519
ohair@286 1520 /**
ohair@286 1521 * Relativizes a URI by using another URI (base URI.)
ohair@286 1522 *
ohair@286 1523 * <p>
ohair@286 1524 * For example, {@code relative("http://www.sun.com/abc/def","http://www.sun.com/pqr/stu") => "../abc/def"}
ohair@286 1525 *
ohair@286 1526 * <p>
ohair@286 1527 * This method only works on hierarchical URI's, not opaque URI's (refer to the
ohair@286 1528 * <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html">java.net.URI</a>
ohair@286 1529 * javadoc for complete definitions of these terms.
ohair@286 1530 *
ohair@286 1531 * <p>
ohair@286 1532 * This method will not normalize the relative URI.
ohair@286 1533 *
ohair@286 1534 * @return the relative URI or the original URI if a relative one could not be computed
ohair@286 1535 */
ohair@286 1536 protected static String relativize(String uri, String baseUri) {
ohair@286 1537 try {
ohair@286 1538 assert uri!=null;
ohair@286 1539
ohair@286 1540 if(baseUri==null) return uri;
ohair@286 1541
ohair@286 1542 URI theUri = new URI(escapeURI(uri));
ohair@286 1543 URI theBaseUri = new URI(escapeURI(baseUri));
ohair@286 1544
ohair@286 1545 if (theUri.isOpaque() || theBaseUri.isOpaque())
ohair@286 1546 return uri;
ohair@286 1547
ohair@286 1548 if (!equalsIgnoreCase(theUri.getScheme(), theBaseUri.getScheme()) ||
ohair@286 1549 !equal(theUri.getAuthority(), theBaseUri.getAuthority()))
ohair@286 1550 return uri;
ohair@286 1551
ohair@286 1552 String uriPath = theUri.getPath();
ohair@286 1553 String basePath = theBaseUri.getPath();
ohair@286 1554
ohair@286 1555 // normalize base path
ohair@286 1556 if (!basePath.endsWith("/")) {
ohair@286 1557 basePath = normalizeUriPath(basePath);
ohair@286 1558 }
ohair@286 1559
ohair@286 1560 if( uriPath.equals(basePath))
ohair@286 1561 return ".";
ohair@286 1562
ohair@286 1563 String relPath = calculateRelativePath(uriPath, basePath, fixNull(theUri.getScheme()).equals("file"));
ohair@286 1564
ohair@286 1565 if (relPath == null)
ohair@286 1566 return uri; // recursion found no commonality in the two uris at all
ohair@286 1567 StringBuilder relUri = new StringBuilder();
ohair@286 1568 relUri.append(relPath);
ohair@286 1569 if (theUri.getQuery() != null)
ohair@286 1570 relUri.append('?').append(theUri.getQuery());
ohair@286 1571 if (theUri.getFragment() != null)
ohair@286 1572 relUri.append('#').append(theUri.getFragment());
ohair@286 1573
ohair@286 1574 return relUri.toString();
ohair@286 1575 } catch (URISyntaxException e) {
ohair@286 1576 throw new InternalError("Error escaping one of these uris:\n\t"+uri+"\n\t"+baseUri);
ohair@286 1577 }
ohair@286 1578 }
ohair@286 1579
ohair@286 1580 private static String fixNull(String s) {
ohair@286 1581 if(s==null) return "";
ohair@286 1582 else return s;
ohair@286 1583 }
ohair@286 1584
ohair@286 1585 private static String calculateRelativePath(String uri, String base, boolean fileUrl) {
ohair@286 1586 // if this is a file URL (very likely), and if this is on a case-insensitive file system,
ohair@286 1587 // then treat it accordingly.
ohair@286 1588 boolean onWindows = File.pathSeparatorChar==';';
ohair@286 1589
ohair@286 1590 if (base == null) {
ohair@286 1591 return null;
ohair@286 1592 }
ohair@286 1593 if ((fileUrl && onWindows && startsWithIgnoreCase(uri,base)) || uri.startsWith(base)) {
ohair@286 1594 return uri.substring(base.length());
ohair@286 1595 } else {
ohair@286 1596 return "../" + calculateRelativePath(uri, getParentUriPath(base), fileUrl);
ohair@286 1597 }
ohair@286 1598 }
ohair@286 1599
ohair@286 1600 private static boolean startsWithIgnoreCase(String s, String t) {
ohair@286 1601 return s.toUpperCase().startsWith(t.toUpperCase());
ohair@286 1602 }
ohair@286 1603
ohair@286 1604 /**
ohair@286 1605 * JAX-RPC wants the namespaces to be sorted in the reverse order
ohair@286 1606 * so that the empty namespace "" comes to the very end. Don't ask me why.
ohair@286 1607 */
ohair@286 1608 private static final Comparator<String> NAMESPACE_COMPARATOR = new Comparator<String>() {
ohair@286 1609 public int compare(String lhs, String rhs) {
ohair@286 1610 return -lhs.compareTo(rhs);
ohair@286 1611 }
ohair@286 1612 };
ohair@286 1613
ohair@286 1614 private static final String newline = "\n";
ohair@286 1615 }

mercurial