package com.sun.xml.internal.bind.v2.model.impl;
import java.util.AbstractList;
import java.util.Collections;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlList;
import javax.xml.namespace.QName;
import com.sun.istack.internal.FinalArrayList;
import com.sun.xml.internal.bind.v2.model.core.ElementPropertyInfo;
import com.sun.xml.internal.bind.v2.model.core.ID;
import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
import com.sun.xml.internal.bind.v2.model.core.TypeInfo;
import com.sun.xml.internal.bind.v2.model.core.TypeRef;
import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException;
class ElementPropertyInfoImpl<TypeT,ClassDeclT,FieldT,MethodT>
extends ERPropertyInfoImpl<TypeT,ClassDeclT,FieldT,MethodT>
implements ElementPropertyInfo<TypeT,ClassDeclT>
{
private List<TypeRefImpl<TypeT,ClassDeclT>> types;
private final List<TypeInfo<TypeT,ClassDeclT>> ref = new AbstractList<TypeInfo<TypeT,ClassDeclT>>() {
public TypeInfo<TypeT,ClassDeclT> get(int index) {
return getTypes().get(index).getTarget();
}
public int size() {
return getTypes().size();
}
};
private Boolean isRequired;
private final boolean isValueList;
ElementPropertyInfoImpl(
ClassInfoImpl<TypeT,ClassDeclT,FieldT,MethodT> parent,
PropertySeed<TypeT,ClassDeclT,FieldT,MethodT> propertySeed) {
super(parent, propertySeed);
isValueList = seed.hasAnnotation(XmlList.class);
}
public List<? extends TypeRefImpl<TypeT,ClassDeclT>> getTypes() {
if(types==null) {
types = new FinalArrayList<TypeRefImpl<TypeT,ClassDeclT>>();
XmlElement[] ann=null;
XmlElement xe = seed.readAnnotation(XmlElement.class);
XmlElements xes = seed.readAnnotation(XmlElements.class);
if(xe!=null && xes!=null) {
parent.builder.reportError(new IllegalAnnotationException(
Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS.format(
nav().getClassName(parent.getClazz())+'#'+seed.getName(),
xe.annotationType().getName(), xes.annotationType().getName()),
xe, xes ));
}
isRequired = true;
if(xe!=null)
ann = new XmlElement[]{xe};
else
if(xes!=null)
ann = xes.value();
if(ann==null) {
TypeT t = getIndividualType();
if(!nav().isPrimitive(t) || isCollection())
isRequired = false;
types.add(createTypeRef(calcXmlName((XmlElement)null),t,isCollection(),null));
} else {
for( XmlElement item : ann ) {
QName name = calcXmlName(item);
TypeT type = reader().getClassValue(item, "type");
if (nav().isSameType(type, nav().ref(XmlElement.DEFAULT.class)))
type = getIndividualType();
if((!nav().isPrimitive(type) || isCollection()) && !item.required())
isRequired = false;
types.add(createTypeRef(name, type, item.nillable(), getDefaultValue(item.defaultValue()) ));
}
}
types = Collections.unmodifiableList(types);
assert !types.contains(null);
}
return types;
}
private String getDefaultValue(String value) {
if(value.equals("\u0000"))
return null;
else
return value;
}
protected TypeRefImpl<TypeT,ClassDeclT> createTypeRef(QName name,TypeT type,boolean isNillable,String defaultValue) {
return new TypeRefImpl<TypeT,ClassDeclT>(this,name,type,isNillable,defaultValue);
}
public boolean isValueList() {
return isValueList;
}
public boolean isRequired() {
if(isRequired==null)
getTypes();
return isRequired;
}
public List<? extends TypeInfo<TypeT,ClassDeclT>> ref() {
return ref;
}
public final PropertyKind kind() {
return PropertyKind.ELEMENT;
}
protected void link() {
super.link();
for (TypeRefImpl<TypeT, ClassDeclT> ref : getTypes() ) {
ref.link();
}
if(isValueList()) {
if(id()!= ID.IDREF) {
for (TypeRefImpl<TypeT,ClassDeclT> ref : types) {
if(!ref.getTarget().isSimpleType()) {
parent.builder.reportError(new IllegalAnnotationException(
Messages.XMLLIST_NEEDS_SIMPLETYPE.format(
nav().getTypeName(ref.getTarget().getType())), this ));
break;
}
}
}
if(!isCollection())
parent.builder.reportError(new IllegalAnnotationException(
Messages.XMLLIST_ON_SINGLE_PROPERTY.format(), this
));
}
}
}