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.xml.bind.v2.model.impl;
038:
039: import java.util.AbstractList;
040: import java.util.Collections;
041: import java.util.List;
042:
043: import javax.xml.bind.annotation.XmlElement;
044: import javax.xml.bind.annotation.XmlElements;
045: import javax.xml.bind.annotation.XmlList;
046: import javax.xml.namespace.QName;
047:
048: import com.sun.istack.FinalArrayList;
049: import com.sun.xml.bind.v2.model.core.ElementPropertyInfo;
050: import com.sun.xml.bind.v2.model.core.ID;
051: import com.sun.xml.bind.v2.model.core.PropertyKind;
052: import com.sun.xml.bind.v2.model.core.TypeInfo;
053: import com.sun.xml.bind.v2.model.core.TypeRef;
054: import com.sun.xml.bind.v2.runtime.IllegalAnnotationException;
055:
056: /**
057: * Common {@link ElementPropertyInfo} implementation used for both
058: * APT and runtime.
059: *
060: * @author Kohsuke Kawaguchi
061: */
062: class ElementPropertyInfoImpl<TypeT, ClassDeclT, FieldT, MethodT>
063: extends ERPropertyInfoImpl<TypeT, ClassDeclT, FieldT, MethodT>
064: implements ElementPropertyInfo<TypeT, ClassDeclT> {
065: /**
066: * Lazily computed.
067: * @see #getTypes()
068: */
069: private List<TypeRefImpl<TypeT, ClassDeclT>> types;
070:
071: private final List<TypeInfo<TypeT, ClassDeclT>> ref = new AbstractList<TypeInfo<TypeT, ClassDeclT>>() {
072: public TypeInfo<TypeT, ClassDeclT> get(int index) {
073: return getTypes().get(index).getTarget();
074: }
075:
076: public int size() {
077: return getTypes().size();
078: }
079: };
080:
081: /**
082: * Lazily computed.
083: * @see #isRequired()
084: */
085: private Boolean isRequired;
086:
087: /**
088: * @see #isValueList()
089: */
090: private final boolean isValueList;
091:
092: ElementPropertyInfoImpl(
093: ClassInfoImpl<TypeT, ClassDeclT, FieldT, MethodT> parent,
094: PropertySeed<TypeT, ClassDeclT, FieldT, MethodT> propertySeed) {
095: super (parent, propertySeed);
096:
097: isValueList = seed.hasAnnotation(XmlList.class);
098:
099: }
100:
101: public List<? extends TypeRefImpl<TypeT, ClassDeclT>> getTypes() {
102: if (types == null) {
103: types = new FinalArrayList<TypeRefImpl<TypeT, ClassDeclT>>();
104: XmlElement[] ann = null;
105:
106: XmlElement xe = seed.readAnnotation(XmlElement.class);
107: XmlElements xes = seed.readAnnotation(XmlElements.class);
108:
109: if (xe != null && xes != null) {
110: parent.builder
111: .reportError(new IllegalAnnotationException(
112: Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS
113: .format(nav().getClassName(
114: parent.getClazz())
115: + '#' + seed.getName(),
116: xe.annotationType()
117: .getName(),
118: xes.annotationType()
119: .getName()),
120: xe, xes));
121: }
122:
123: isRequired = true;
124:
125: if (xe != null)
126: ann = new XmlElement[] { xe };
127: else if (xes != null)
128: ann = xes.value();
129:
130: if (ann == null) {
131: // default
132: TypeT t = getIndividualType();
133: if (!nav().isPrimitive(t) || isCollection())
134: isRequired = false;
135: // nillableness defaults to true if it's collection
136: types.add(createTypeRef(calcXmlName((XmlElement) null),
137: t, isCollection(), null));
138: } else {
139: for (XmlElement item : ann) {
140: // TODO: handle defaulting in names.
141: QName name = calcXmlName(item);
142: TypeT type = reader().getClassValue(item, "type");
143: if (type
144: .equals(nav().ref(XmlElement.DEFAULT.class)))
145: type = getIndividualType();
146: if ((!nav().isPrimitive(type) || isCollection())
147: && !item.required())
148: isRequired = false;
149: types.add(createTypeRef(name, type,
150: item.nillable(), getDefaultValue(item
151: .defaultValue())));
152: }
153: }
154: types = Collections.unmodifiableList(types);
155: assert !types.contains(null);
156: }
157: return types;
158: }
159:
160: private String getDefaultValue(String value) {
161: if (value.equals("\u0000"))
162: return null;
163: else
164: return value;
165: }
166:
167: /**
168: * Used by {@link PropertyInfoImpl} to create new instances of {@link TypeRef}
169: */
170: protected TypeRefImpl<TypeT, ClassDeclT> createTypeRef(QName name,
171: TypeT type, boolean isNillable, String defaultValue) {
172: return new TypeRefImpl<TypeT, ClassDeclT>(this , name, type,
173: isNillable, defaultValue);
174: }
175:
176: public boolean isValueList() {
177: return isValueList;
178: }
179:
180: public boolean isRequired() {
181: if (isRequired == null)
182: getTypes(); // compute the value
183: return isRequired;
184: }
185:
186: public List<? extends TypeInfo<TypeT, ClassDeclT>> ref() {
187: return ref;
188: }
189:
190: public final PropertyKind kind() {
191: return PropertyKind.ELEMENT;
192: }
193:
194: protected void link() {
195: super .link();
196: for (TypeRefImpl<TypeT, ClassDeclT> ref : getTypes()) {
197: ref.link();
198: }
199:
200: if (isValueList()) {
201: // ugly test, because IDREF's are represented as text on the wire,
202: // it's OK to be a value list in that case.
203: if (id() != ID.IDREF) {
204: // check if all the item types are simple types
205: // this can't be done when we compute types because
206: // not all TypeInfos are available yet
207: for (TypeRefImpl<TypeT, ClassDeclT> ref : types) {
208: if (!ref.getTarget().isSimpleType()) {
209: parent.builder
210: .reportError(new IllegalAnnotationException(
211: Messages.XMLLIST_NEEDS_SIMPLETYPE
212: .format(nav()
213: .getTypeName(
214: ref
215: .getTarget()
216: .getType())),
217: this ));
218: break;
219: }
220: }
221: }
222:
223: if (!isCollection())
224: parent.builder
225: .reportError(new IllegalAnnotationException(
226: Messages.XMLLIST_ON_SINGLE_PROPERTY
227: .format(), this));
228: }
229: }
230: }
|