001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.xjc.model;
027:
028: import java.util.Collection;
029: import java.util.HashSet;
030: import java.util.Set;
031:
032: import javax.activation.MimeType;
033: import javax.xml.bind.annotation.W3CDomHandler;
034: import javax.xml.namespace.QName;
035:
036: import com.sun.tools.internal.xjc.model.nav.NClass;
037: import com.sun.tools.internal.xjc.model.nav.NType;
038: import com.sun.tools.internal.xjc.model.nav.NavigatorImpl;
039: import com.sun.xml.internal.bind.v2.model.core.ID;
040: import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
041: import com.sun.xml.internal.bind.v2.model.core.ReferencePropertyInfo;
042: import com.sun.xml.internal.bind.v2.model.core.WildcardMode;
043: import com.sun.xml.internal.xsom.XSComponent;
044:
045: import org.xml.sax.Locator;
046:
047: /**
048: * {@link ReferencePropertyInfo} for the compiler.
049: *
050: * @author Kohsuke Kawaguchi
051: */
052: public final class CReferencePropertyInfo extends CPropertyInfo
053: implements ReferencePropertyInfo<NType, NClass> {
054:
055: /**
056: * List of referenced elements.
057: */
058: private final Set<CElement> elements = new HashSet<CElement>();
059:
060: private final boolean isMixed;
061: private WildcardMode wildcard;
062:
063: public CReferencePropertyInfo(String name, boolean collection,
064: boolean isMixed, XSComponent source,
065: CCustomizations customizations, Locator locator) {
066: super (name, collection || isMixed, source, customizations,
067: locator);
068: this .isMixed = isMixed;
069: }
070:
071: public Set<? extends CTypeInfo> ref() {
072: // if(wildcard==null && !isMixed())
073: // return getElements();
074:
075: // ugly hack to get the signature right for substitution groups
076: // when a class is generated for elements,they don't form a nice type hierarchy,
077: // so the Java types of the substitution members need to be taken into account
078: // when computing the signature
079:
080: final class RefList extends HashSet<CTypeInfo> {
081: RefList() {
082: super (elements.size());
083: addAll(elements);
084: }
085:
086: @Override
087: public boolean addAll(Collection<? extends CTypeInfo> col) {
088: boolean r = false;
089: for (CTypeInfo e : col) {
090: if (e instanceof CElementInfo) {
091: // UGLY. element substitution is implemented in a way that
092: // the derived elements are not assignable to base elements.
093: // so when we compute the signature, we have to take derived types
094: // into account
095: r |= addAll(((CElementInfo) e)
096: .getSubstitutionMembers());
097: }
098: r |= add(e);
099: }
100: return r;
101: }
102: }
103:
104: RefList r = new RefList();
105: if (wildcard != null) {
106: if (wildcard.allowDom)
107: r.add(CWildcardTypeInfo.INSTANCE);
108: if (wildcard.allowTypedObject)
109: // we aren't really adding an AnyType.
110: // this is a kind of hack to generate Object as a signature
111: r.add(CBuiltinLeafInfo.ANYTYPE);
112: }
113: if (isMixed())
114: r.add(CBuiltinLeafInfo.STRING);
115:
116: return r;
117: }
118:
119: public Set<CElement> getElements() {
120: return elements;
121: }
122:
123: public boolean isMixed() {
124: return isMixed;
125: }
126:
127: /**
128: * We'll never use a wrapper element in XJC. Always return null.
129: */
130: @Deprecated
131: public QName getXmlName() {
132: return null;
133: }
134:
135: /**
136: * Reference properties refer to elements, and none of the Java primitive type
137: * maps to an element. Thus a reference property is always unboxable.
138: */
139: @Override
140: public boolean isUnboxable() {
141: return false;
142: }
143:
144: // the same as above
145: @Override
146: public boolean isOptionalPrimitive() {
147: return false;
148: }
149:
150: public <V> V accept(CPropertyVisitor<V> visitor) {
151: return visitor.onReference(this );
152: }
153:
154: public CAdapter getAdapter() {
155: return null;
156: }
157:
158: public final PropertyKind kind() {
159: return PropertyKind.REFERENCE;
160: }
161:
162: /**
163: * A reference property can never be ID/IDREF because they always point to
164: * other element classes.
165: */
166: public ID id() {
167: return ID.NONE;
168: }
169:
170: public WildcardMode getWildcard() {
171: return wildcard;
172: }
173:
174: public void setWildcard(WildcardMode mode) {
175: this .wildcard = mode;
176: }
177:
178: public NClass getDOMHandler() {
179: // TODO: support other DOM handlers
180: if (getWildcard() != null)
181: return NavigatorImpl.create(W3CDomHandler.class);
182: else
183: return null;
184: }
185:
186: public MimeType getExpectedMimeType() {
187: return null;
188: }
189:
190: public boolean isCollectionNillable() {
191: // in XJC, we never recognize a nillable collection pattern, so this is always false.
192: return false;
193: }
194: }
|