package com.sun.xml.internal.bind.v2.model.impl;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.namespace.QName;
import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader;
import com.sun.xml.internal.bind.v2.model.core.ClassInfo;
import com.sun.xml.internal.bind.v2.model.core.Element;
import com.sun.xml.internal.bind.v2.model.core.ElementInfo;
import com.sun.xml.internal.bind.v2.model.core.NonElement;
import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
import com.sun.xml.internal.bind.v2.model.core.ReferencePropertyInfo;
import com.sun.xml.internal.bind.v2.model.core.WildcardMode;
import com.sun.xml.internal.bind.v2.model.nav.Navigator;
import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException;
import java.util.Iterator;
class ReferencePropertyInfoImpl<T,C,F,M>
extends ERPropertyInfoImpl<T,C,F,M>
implements ReferencePropertyInfo<T,C>, DummyPropertyInfo<T, C, F, M>
{
private Set<Element<T,C>> types;
private Set<ReferencePropertyInfoImpl<T,C,F,M>> subTypes = new LinkedHashSet<ReferencePropertyInfoImpl<T,C,F,M>>();
private final boolean isMixed;
private final WildcardMode wildcard;
private final C domHandler;
private Boolean isRequired;
public ReferencePropertyInfoImpl(
ClassInfoImpl<T,C,F,M> classInfo,
PropertySeed<T,C,F,M> seed) {
super(classInfo, seed);
isMixed = seed.readAnnotation(XmlMixed.class) != null;
XmlAnyElement xae = seed.readAnnotation(XmlAnyElement.class);
if(xae==null) {
wildcard = null;
domHandler = null;
} else {
wildcard = xae.lax()?WildcardMode.LAX:WildcardMode.SKIP;
domHandler = nav().asDecl(reader().getClassValue(xae,"value"));
}
}
public Set<? extends Element<T,C>> ref() {
return getElements();
}
public PropertyKind kind() {
return PropertyKind.REFERENCE;
}
public Set<? extends Element<T,C>> getElements() {
if(types==null)
calcTypes(false);
assert types!=null;
return types;
}
private void calcTypes(boolean last) {
XmlElementRef[] ann;
types = new LinkedHashSet<Element<T,C>>();
XmlElementRefs refs = seed.readAnnotation(XmlElementRefs.class);
XmlElementRef ref = seed.readAnnotation(XmlElementRef.class);
if(refs!=null && ref!=null) {
parent.builder.reportError(new IllegalAnnotationException(
Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS.format(
nav().getClassName(parent.getClazz())+'#'+seed.getName(),
ref.annotationType().getName(), refs.annotationType().getName()),
ref, refs ));
}
if(refs!=null)
ann = refs.value();
else {
if(ref!=null)
ann = new XmlElementRef[]{ref};
else
ann = null;
}
isRequired = !isCollection();
if(ann!=null) {
Navigator<T,C,F,M> nav = nav();
AnnotationReader<T,C,F,M> reader = reader();
final T defaultType = nav.ref(XmlElementRef.DEFAULT.class);
final C je = nav.asDecl(JAXBElement.class);
for( XmlElementRef r : ann ) {
boolean yield;
T type = reader.getClassValue(r,"type");
if(nav().isSameType(type, defaultType))
type = nav.erasure(getIndividualType());
if(nav.getBaseClass(type,je)!=null)
yield = addGenericElement(r);
else
yield = addAllSubtypes(type);
if(isRequired && !isRequired(r))
isRequired = false;
if(last && !yield) {
if(nav().isSameType(type, nav.ref(JAXBElement.class))) {
parent.builder.reportError(new IllegalAnnotationException(
Messages.NO_XML_ELEMENT_DECL.format(
getEffectiveNamespaceFor(r), r.name()),
this
));
} else {
parent.builder.reportError(new IllegalAnnotationException(
Messages.INVALID_XML_ELEMENT_REF.format(type),this));
}
return;
}
}
}
for (ReferencePropertyInfoImpl<T, C, F, M> info : subTypes) {
PropertySeed sd = info.seed;
refs = sd.readAnnotation(XmlElementRefs.class);
ref = sd.readAnnotation(XmlElementRef.class);
if (refs != null && ref != null) {
parent.builder.reportError(new IllegalAnnotationException(
Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS.format(
nav().getClassName(parent.getClazz())+'#'+seed.getName(),
ref.annotationType().getName(), refs.annotationType().getName()),
ref, refs ));
}
if (refs != null) {
ann = refs.value();
} else {
if (ref != null) {
ann = new XmlElementRef[]{ref};
} else {
ann = null;
}
}
if (ann != null) {
Navigator<T,C,F,M> nav = nav();
AnnotationReader<T,C,F,M> reader = reader();
final T defaultType = nav.ref(XmlElementRef.DEFAULT.class);
final C je = nav.asDecl(JAXBElement.class);
for( XmlElementRef r : ann ) {
boolean yield;
T type = reader.getClassValue(r,"type");
if (nav().isSameType(type, defaultType)) {
type = nav.erasure(getIndividualType());
}
if (nav.getBaseClass(type,je) != null) {
yield = addGenericElement(r, info);
} else {
yield = addAllSubtypes(type);
}
if(last && !yield) {
if(nav().isSameType(type, nav.ref(JAXBElement.class))) {
parent.builder.reportError(new IllegalAnnotationException(
Messages.NO_XML_ELEMENT_DECL.format(
getEffectiveNamespaceFor(r), r.name()),
this
));
} else {
parent.builder.reportError(new IllegalAnnotationException(
Messages.INVALID_XML_ELEMENT_REF.format(),this));
}
return;
}
}
}
}
types = Collections.unmodifiableSet(types);
}
public boolean isRequired() {
if(isRequired==null)
calcTypes(false);
return isRequired;
}
private static boolean is2_2 = true;
private boolean isRequired(XmlElementRef ref) {
if(!is2_2) return true;
try {
return ref.required();
} catch(LinkageError e) {
is2_2 = false;
return true;
}
}
private boolean addGenericElement(XmlElementRef r) {
String nsUri = getEffectiveNamespaceFor(r);
return addGenericElement(parent.owner.getElementInfo(parent.getClazz(),new QName(nsUri,r.name())));
}
private boolean addGenericElement(XmlElementRef r, ReferencePropertyInfoImpl<T,C,F,M> info) {
String nsUri = info.getEffectiveNamespaceFor(r);
ElementInfo ei = parent.owner.getElementInfo(info.parent.getClazz(), new QName(nsUri, r.name()));
types.add(ei);
return true;
}
private String getEffectiveNamespaceFor(XmlElementRef r) {
String nsUri = r.namespace();
XmlSchema xs = reader().getPackageAnnotation( XmlSchema.class, parent.getClazz(), this );
if(xs!=null && xs.attributeFormDefault()== XmlNsForm.QUALIFIED) {
if(nsUri.length()==0)
nsUri = parent.builder.defaultNsUri;
}
return nsUri;
}
private boolean addGenericElement(ElementInfo<T,C> ei) {
if(ei==null)
return false;
types.add(ei);
for( ElementInfo<T,C> subst : ei.getSubstitutionMembers() )
addGenericElement(subst);
return true;
}
private boolean addAllSubtypes(T type) {
Navigator<T,C,F,M> nav = nav();
NonElement<T,C> t = parent.builder.getClassInfo(nav.asDecl(type),this);
if(!(t instanceof ClassInfo))
return false;
boolean result = false;
ClassInfo<T,C> c = (ClassInfo<T,C>) t;
if(c.isElement()) {
types.add(c.asElement());
result = true;
}
for( ClassInfo<T,C> ci : parent.owner.beans().values() ) {
if(ci.isElement() && nav.isSubClassOf(ci.getType(),type)) {
types.add(ci.asElement());
result = true;
}
}
for( ElementInfo<T,C> ei : parent.owner.getElementMappings(null).values()) {
if(nav.isSubClassOf(ei.getType(),type)) {
types.add(ei);
result = true;
}
}
return result;
}
@Override
protected void link() {
super.link();
calcTypes(true);
}
public final void addType(PropertyInfoImpl<T,C,F,M> info) {
subTypes.add((ReferencePropertyInfoImpl)info);
}
public final boolean isMixed() {
return isMixed;
}
public final WildcardMode getWildcard() {
return wildcard;
}
public final C getDOMHandler() {
return domHandler;
}
}