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