001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.xjc.reader;
038:
039: import java.util.HashSet;
040: import java.util.List;
041: import java.util.Set;
042:
043: import javax.activation.MimeType;
044:
045: import com.sun.tools.xjc.model.CElementPropertyInfo;
046: import static com.sun.tools.xjc.model.CElementPropertyInfo.CollectionMode.*;
047: import com.sun.tools.xjc.model.CReferencePropertyInfo;
048: import com.sun.tools.xjc.model.CTypeRef;
049: import com.sun.tools.xjc.model.Multiplicity;
050: import com.sun.tools.xjc.model.nav.NType;
051: import com.sun.xml.bind.v2.model.core.Element;
052: import com.sun.xml.bind.v2.model.core.ID;
053:
054: /**
055: * Set of {@link Ref}.
056: *
057: * @author Kohsuke Kawaguchi
058: */
059: public final class RawTypeSet {
060:
061: public final Set<Ref> refs;
062:
063: /**
064: * True if this type set can form references to types.
065: */
066: public final Mode canBeTypeRefs;
067:
068: /**
069: * The occurence of the whole references.
070: */
071: public final Multiplicity mul;
072:
073: // computed inside canBeTypeRefs()
074: private CElementPropertyInfo.CollectionMode collectionMode;
075:
076: /**
077: * Should be called from one of the raw type set builders.
078: */
079: public RawTypeSet(Set<Ref> refs, Multiplicity m) {
080: this .refs = refs;
081: mul = m;
082: canBeTypeRefs = canBeTypeRefs();
083: }
084:
085: public CElementPropertyInfo.CollectionMode getCollectionMode() {
086: return collectionMode;
087: }
088:
089: public boolean isRequired() {
090: return mul.min > 0;
091: }
092:
093: /**
094: * Represents the possible binding option for this {@link RawTypeSet}.
095: */
096: public enum Mode {
097: /**
098: * This {@link RawTypeSet} can be either an reference property or
099: * an element property, and XJC recommends element property.
100: */
101: SHOULD_BE_TYPEREF(0),
102: /**
103: * This {@link RawTypeSet} can be either an reference property or
104: * an element property, and XJC recommends reference property.
105: */
106: CAN_BE_TYPEREF(1),
107: /**
108: * This {@link RawTypeSet} can be only bound to a reference property.
109: */
110: MUST_BE_REFERENCE(2);
111:
112: private final int rank;
113:
114: Mode(int rank) {
115: this .rank = rank;
116: }
117:
118: Mode or(Mode that) {
119: switch (Math.max(this .rank, that.rank)) {
120: case 0:
121: return SHOULD_BE_TYPEREF;
122: case 1:
123: return CAN_BE_TYPEREF;
124: case 2:
125: return MUST_BE_REFERENCE;
126: }
127: throw new AssertionError();
128: }
129: }
130:
131: /**
132: * Returns true if {@link #refs} can form refs of types.
133: *
134: * If there are multiple {@link Ref}s with the same type,
135: * we cannot make them into type refs. Or if any of the {@link Ref}
136: * says they cannot be in type refs, we cannot do that either.
137: *
138: * TODO: just checking if the refs are the same is not suffice.
139: * If two refs derive from each other, they cannot form a list of refs
140: * (because of a possible ambiguity).
141: */
142: private Mode canBeTypeRefs() {
143: Set<NType> types = new HashSet<NType>();
144:
145: collectionMode = mul.isAtMostOnce() ? NOT_REPEATED
146: : REPEATED_ELEMENT;
147:
148: // the way we compute this is that we start from the most optimistic value,
149: // and then gradually degrade as we find something problematic.
150: Mode mode = Mode.SHOULD_BE_TYPEREF;
151:
152: for (Ref r : refs) {
153: mode = mode.or(r.canBeType(this ));
154: if (mode == Mode.MUST_BE_REFERENCE)
155: return mode; // no need to continue the processing
156:
157: if (!types.add(r.toTypeRef(null).getTarget().getType()))
158: return Mode.MUST_BE_REFERENCE; // collision
159: if (r.isListOfValues()) {
160: if (refs.size() > 1 || !mul.isAtMostOnce())
161: return Mode.MUST_BE_REFERENCE; // restriction on @XmlList
162: collectionMode = REPEATED_VALUE;
163: }
164: }
165: return mode;
166: }
167:
168: public void addTo(CElementPropertyInfo prop) {
169: assert canBeTypeRefs != Mode.MUST_BE_REFERENCE;
170: if (mul.isZero())
171: return; // the property can't have any value
172:
173: List<CTypeRef> dst = prop.getTypes();
174: for (Ref t : refs)
175: dst.add(t.toTypeRef(prop));
176: }
177:
178: public void addTo(CReferencePropertyInfo prop) {
179: if (mul.isZero())
180: return; // the property can't have any value
181: for (Ref t : refs)
182: t.toElementRef(prop);
183: }
184:
185: public ID id() {
186: for (Ref t : refs) {
187: ID id = t.id();
188: if (id != ID.NONE)
189: return id;
190: }
191: return ID.NONE;
192: }
193:
194: public MimeType getExpectedMimeType() {
195: for (Ref t : refs) {
196: MimeType mt = t.getExpectedMimeType();
197: if (mt != null)
198: return mt;
199: }
200: return null;
201: }
202:
203: /**
204: * A reference to something.
205: *
206: * <p>
207: * A {@link Ref} can be either turned into {@link CTypeRef} to form
208: * an element property, or {@link Element} to form a reference property.
209: */
210: public static abstract class Ref {
211: /**
212: * @param ep
213: * the property to which the returned {@link CTypeRef} will be
214: * added to.
215: */
216: protected abstract CTypeRef toTypeRef(CElementPropertyInfo ep);
217:
218: protected abstract void toElementRef(CReferencePropertyInfo prop);
219:
220: /**
221: * Can this {@link Ref} be a type ref?
222: * @return false to veto.
223: * @param parent
224: */
225: protected abstract Mode canBeType(RawTypeSet parent);
226:
227: protected abstract boolean isListOfValues();
228:
229: /**
230: * When this {@link RawTypeSet} binds to a {@link CElementPropertyInfo},
231: * this method is used to determine if the property is ID or not.
232: */
233: protected abstract ID id();
234:
235: /**
236: * When this {@link RawTypeSet} binds to a {@link CElementPropertyInfo},
237: * this method is used to determine if the property has an associated expected MIME type or not.
238: */
239: protected MimeType getExpectedMimeType() {
240: return null;
241: }
242: }
243: }
|