aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.xjc.model; aoqi@0: aoqi@0: import java.util.Collection; aoqi@0: import java.util.HashSet; aoqi@0: import java.util.Set; aoqi@0: import java.util.Map; aoqi@0: aoqi@0: import javax.activation.MimeType; aoqi@0: import javax.xml.bind.annotation.W3CDomHandler; aoqi@0: import javax.xml.namespace.QName; aoqi@0: aoqi@0: import com.sun.tools.internal.xjc.model.nav.NClass; aoqi@0: import com.sun.tools.internal.xjc.model.nav.NType; aoqi@0: import com.sun.tools.internal.xjc.model.nav.NavigatorImpl; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.ID; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.PropertyKind; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.ReferencePropertyInfo; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.WildcardMode; aoqi@0: import com.sun.xml.internal.xsom.XSComponent; aoqi@0: aoqi@0: import org.xml.sax.Locator; aoqi@0: aoqi@0: /** aoqi@0: * {@link ReferencePropertyInfo} for the compiler. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public final class CReferencePropertyInfo extends CPropertyInfo implements ReferencePropertyInfo { aoqi@0: aoqi@0: /** aoqi@0: * True if this property can never be absent legally. aoqi@0: */ aoqi@0: private final boolean required; aoqi@0: aoqi@0: /** aoqi@0: * List of referenced elements. aoqi@0: */ aoqi@0: private final Set elements = new HashSet(); aoqi@0: aoqi@0: private final boolean isMixed; aoqi@0: private WildcardMode wildcard; aoqi@0: private boolean dummy; aoqi@0: private boolean content; aoqi@0: private boolean isMixedExtendedCust = false; aoqi@0: aoqi@0: public CReferencePropertyInfo(String name, boolean collection, boolean required, boolean isMixed, XSComponent source, aoqi@0: CCustomizations customizations, Locator locator, boolean dummy, boolean content, boolean isMixedExtended) { // 'dummy' and 'content' here for NHIN fix - a hack in order to be able to handle extended mixed types better aoqi@0: super(name, (collection||isMixed) && (!dummy), source, customizations, locator); aoqi@0: this.isMixed = isMixed; aoqi@0: this.required = required; aoqi@0: this.dummy = dummy; aoqi@0: this.content = content; aoqi@0: this.isMixedExtendedCust = isMixedExtended; aoqi@0: } aoqi@0: aoqi@0: public Set ref() { aoqi@0: // if(wildcard==null && !isMixed()) aoqi@0: // return getElements(); aoqi@0: aoqi@0: // ugly hack to get the signature right for substitution groups aoqi@0: // when a class is generated for elements,they don't form a nice type hierarchy, aoqi@0: // so the Java types of the substitution members need to be taken into account aoqi@0: // when computing the signature aoqi@0: aoqi@0: final class RefList extends HashSet { aoqi@0: RefList() { aoqi@0: super(elements.size()); aoqi@0: addAll(elements); aoqi@0: } aoqi@0: @Override aoqi@0: public boolean addAll( Collection col ) { aoqi@0: boolean r = false; aoqi@0: for (CTypeInfo e : col) { aoqi@0: if(e instanceof CElementInfo) { aoqi@0: // UGLY. element substitution is implemented in a way that aoqi@0: // the derived elements are not assignable to base elements. aoqi@0: // so when we compute the signature, we have to take derived types aoqi@0: // into account aoqi@0: r |= addAll( ((CElementInfo)e).getSubstitutionMembers()); aoqi@0: } aoqi@0: r |= add(e); aoqi@0: } aoqi@0: return r; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: RefList r = new RefList(); aoqi@0: if(wildcard!=null) { aoqi@0: if(wildcard.allowDom) aoqi@0: r.add(CWildcardTypeInfo.INSTANCE); aoqi@0: if(wildcard.allowTypedObject) aoqi@0: // we aren't really adding an AnyType. aoqi@0: // this is a kind of hack to generate Object as a signature aoqi@0: r.add(CBuiltinLeafInfo.ANYTYPE); aoqi@0: } aoqi@0: if(isMixed()) aoqi@0: r.add(CBuiltinLeafInfo.STRING); aoqi@0: aoqi@0: return r; aoqi@0: } aoqi@0: aoqi@0: public Set getElements() { aoqi@0: return elements; aoqi@0: } aoqi@0: aoqi@0: public boolean isMixed() { aoqi@0: return isMixed; aoqi@0: } aoqi@0: aoqi@0: public boolean isDummy() { aoqi@0: return dummy; aoqi@0: } aoqi@0: aoqi@0: public boolean isContent() { aoqi@0: return content; aoqi@0: } aoqi@0: aoqi@0: public boolean isMixedExtendedCust() { aoqi@0: return isMixedExtendedCust; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * We'll never use a wrapper element in XJC. Always return null. aoqi@0: */ aoqi@0: @Deprecated aoqi@0: public QName getXmlName() { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Reference properties refer to elements, and none of the Java primitive type aoqi@0: * maps to an element. Thus a reference property is always unboxable. aoqi@0: */ aoqi@0: @Override aoqi@0: public boolean isUnboxable() { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // the same as above aoqi@0: @Override aoqi@0: public boolean isOptionalPrimitive() { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public V accept(CPropertyVisitor visitor) { aoqi@0: return visitor.onReference(this); aoqi@0: } aoqi@0: aoqi@0: public CAdapter getAdapter() { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public final PropertyKind kind() { aoqi@0: return PropertyKind.REFERENCE; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * A reference property can never be ID/IDREF because they always point to aoqi@0: * other element classes. aoqi@0: */ aoqi@0: public ID id() { aoqi@0: return ID.NONE; aoqi@0: } aoqi@0: aoqi@0: public WildcardMode getWildcard() { aoqi@0: return wildcard; aoqi@0: } aoqi@0: aoqi@0: public void setWildcard(WildcardMode mode) { aoqi@0: this.wildcard = mode; aoqi@0: } aoqi@0: aoqi@0: public NClass getDOMHandler() { aoqi@0: // TODO: support other DOM handlers aoqi@0: if(getWildcard()!=null) aoqi@0: return NavigatorImpl.create(W3CDomHandler.class); aoqi@0: else aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public MimeType getExpectedMimeType() { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public boolean isCollectionNillable() { aoqi@0: // in XJC, we never recognize a nillable collection pattern, so this is always false. aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public boolean isCollectionRequired() { aoqi@0: // in XJC, we never recognize a nillable collection pattern, so this is always false. aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // reference property cannot have a type. aoqi@0: public QName getSchemaType() { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public boolean isRequired() { aoqi@0: return required; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public QName collectElementNames(Map table) { aoqi@0: for (CElement e : elements) { aoqi@0: QName n = e.getElementName(); aoqi@0: if(table.containsKey(n)) aoqi@0: return n; aoqi@0: table.put(n,this); aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: }