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.AbstractList;
029: import java.util.ArrayList;
030: import java.util.List;
031:
032: import javax.activation.MimeType;
033: import javax.xml.namespace.QName;
034:
035: import com.sun.tools.internal.xjc.model.nav.NClass;
036: import com.sun.tools.internal.xjc.model.nav.NType;
037: import com.sun.tools.internal.xjc.reader.RawTypeSet;
038: import com.sun.xml.internal.bind.v2.model.core.ElementPropertyInfo;
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.xsom.XSComponent;
042:
043: import org.xml.sax.Locator;
044:
045: /**
046: * {@link ElementPropertyInfo} for the compiler.
047: *
048: * @author Kohsuke Kawaguchi
049: */
050: public final class CElementPropertyInfo extends CPropertyInfo implements
051: ElementPropertyInfo<NType, NClass> {
052:
053: /**
054: * True if this property can never be absent legally.
055: */
056: private final boolean required;
057:
058: private final MimeType expectedMimeType;
059: /**
060: *
061: * <p>
062: * Currently, this is set inside {@link RawTypeSet} in a very ugly way.
063: */
064: private CAdapter adapter;
065:
066: private final boolean isValueList;
067:
068: private ID id;
069:
070: /**
071: * List of referenced types.
072: */
073: private final List<CTypeRef> types = new ArrayList<CTypeRef>();
074:
075: private final List<CNonElement> ref = new AbstractList<CNonElement>() {
076: public CNonElement get(int index) {
077: return getTypes().get(index).getTarget();
078: }
079:
080: public int size() {
081: return getTypes().size();
082: }
083: };
084:
085: // TODO: shouldn't they get id and expectedMimeType from TypeUses of CTypeRef?
086: public CElementPropertyInfo(String name, CollectionMode collection,
087: ID id, MimeType expectedMimeType, XSComponent source,
088: CCustomizations customizations, Locator locator,
089: boolean required) {
090: super (name, collection.col, source, customizations, locator);
091: this .required = required;
092: this .id = id;
093: this .expectedMimeType = expectedMimeType;
094: this .isValueList = collection.val;
095: }
096:
097: public ID id() {
098: return id;
099: }
100:
101: public List<CTypeRef> getTypes() {
102: return types;
103: }
104:
105: public List<CNonElement> ref() {
106: return ref;
107: }
108:
109: /**
110: * XJC never uses the wrapper element. Always return null.
111: */
112: @Deprecated
113: public QName getXmlName() {
114: return null;
115: }
116:
117: public boolean isCollectionNillable() {
118: // in XJC, we never recognize a nillable collection pattern, so this is always false.
119: return false;
120: }
121:
122: public boolean isRequired() {
123: return required;
124: }
125:
126: public boolean isValueList() {
127: return isValueList;
128: }
129:
130: public boolean isUnboxable() {
131: if (!isCollection() && !required)
132: // if the property can be legally absent, it's not unboxable
133: return false;
134: // we need to have null to represent the absence of value. not unboxable.
135: for (CTypeRef t : getTypes()) {
136: if (t.isNillable())
137: return false;
138: }
139: return super .isUnboxable();
140: }
141:
142: @Override
143: public boolean isOptionalPrimitive() {
144: // we need to have null to represent the absence of value. not unboxable.
145: for (CTypeRef t : getTypes()) {
146: if (t.isNillable())
147: return false;
148: }
149: return !isCollection() && !required && super .isUnboxable();
150: }
151:
152: public <V> V accept(CPropertyVisitor<V> visitor) {
153: return visitor.onElement(this );
154: }
155:
156: public CAdapter getAdapter() {
157: return adapter;
158: }
159:
160: public void setAdapter(CAdapter a) {
161: assert adapter == null;
162: adapter = a;
163: }
164:
165: public final PropertyKind kind() {
166: return PropertyKind.ELEMENT;
167: }
168:
169: public MimeType getExpectedMimeType() {
170: return expectedMimeType;
171: }
172:
173: public static enum CollectionMode {
174: NOT_REPEATED(false, false), REPEATED_ELEMENT(true, false), REPEATED_VALUE(
175: true, true);
176:
177: private final boolean col, val;
178:
179: CollectionMode(boolean col, boolean val) {
180: this .col = col;
181: this .val = val;
182: }
183:
184: public boolean isRepeated() {
185: return col;
186: }
187: }
188: }
|