package com.sun.tools.internal.xjc.generator.bean.field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.xml.bind.annotation.W3CDomHandler;
import javax.xml.bind.annotation.XmlList;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.XmlInlineBinaryData;
import javax.xml.namespace.QName;
import com.sun.codemodel.internal.JAnnotatable;
import com.sun.codemodel.internal.JClass;
import com.sun.codemodel.internal.JCodeModel;
import com.sun.codemodel.internal.JExpr;
import com.sun.codemodel.internal.JExpression;
import com.sun.codemodel.internal.JFieldVar;
import com.sun.codemodel.internal.JMod;
import com.sun.codemodel.internal.JType;
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlAnyElementWriter;
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlAttributeWriter;
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlElementRefWriter;
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlElementRefsWriter;
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlElementWriter;
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlElementsWriter;
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlSchemaTypeWriter;
import com.sun.tools.internal.xjc.generator.bean.ClassOutlineImpl;
import com.sun.tools.internal.xjc.model.CAttributePropertyInfo;
import com.sun.tools.internal.xjc.model.CElement;
import com.sun.tools.internal.xjc.model.CElementInfo;
import com.sun.tools.internal.xjc.model.CElementPropertyInfo;
import com.sun.tools.internal.xjc.model.CPropertyInfo;
import com.sun.tools.internal.xjc.model.CReferencePropertyInfo;
import com.sun.tools.internal.xjc.model.CTypeInfo;
import com.sun.tools.internal.xjc.model.CTypeRef;
import com.sun.tools.internal.xjc.model.CValuePropertyInfo;
import com.sun.tools.internal.xjc.model.nav.NClass;
import com.sun.tools.internal.xjc.outline.Aspect;
import static com.sun.tools.internal.xjc.outline.Aspect.IMPLEMENTATION;
import com.sun.tools.internal.xjc.outline.ClassOutline;
import com.sun.tools.internal.xjc.outline.FieldAccessor;
import com.sun.tools.internal.xjc.outline.FieldOutline;
import com.sun.tools.internal.xjc.reader.TypeUtil;
import com.sun.tools.internal.xjc.Options;
import com.sun.tools.internal.xjc.api.SpecVersion;
import com.sun.xml.internal.bind.api.impl.NameConverter;
import com.sun.xml.internal.bind.v2.TODO;
abstract class AbstractField implements FieldOutline {
protected final ClassOutlineImpl outline;
protected final CPropertyInfo prop;
protected final JCodeModel codeModel;
protected final JType implType;
protected final JType exposedType;
protected AbstractField( ClassOutlineImpl outline, CPropertyInfo prop ) {
this.outline = outline;
this.prop = prop;
this.codeModel = outline.parent().getCodeModel();
this.implType = getType(IMPLEMENTATION);
this.exposedType = getType(Aspect.EXPOSED);
}
public final ClassOutline parent() {
return outline;
}
public final CPropertyInfo getPropertyInfo() {
return prop;
}
protected void annotate( JAnnotatable field ) {
assert(field!=null);
if (prop instanceof CAttributePropertyInfo) {
annotateAttribute(field);
} else if (prop instanceof CElementPropertyInfo) {
annotateElement(field);
} else if (prop instanceof CValuePropertyInfo) {
field.annotate(XmlValue.class);
} else if (prop instanceof CReferencePropertyInfo) {
annotateReference(field);
}
outline.parent().generateAdapterIfNecessary(prop,field);
QName st = prop.getSchemaType();
if(st!=null)
field.annotate2(XmlSchemaTypeWriter.class)
.name(st.getLocalPart())
.namespace(st.getNamespaceURI());
if(prop.inlineBinaryData())
field.annotate(XmlInlineBinaryData.class);
}
private void annotateReference(JAnnotatable field) {
CReferencePropertyInfo rp = (CReferencePropertyInfo) prop;
TODO.prototype();
Collection<CElement> elements = rp.getElements();
XmlElementRefWriter refw;
if(elements.size()==1) {
refw = field.annotate2(XmlElementRefWriter.class);
CElement e = elements.iterator().next();
refw.name(e.getElementName().getLocalPart())
.namespace(e.getElementName().getNamespaceURI())
.type(e.getType().toType(outline.parent(),IMPLEMENTATION));
if(getOptions().target.isLaterThan(SpecVersion.V2_2))
refw.required(rp.isRequired());
} else
if(elements.size()>1) {
XmlElementRefsWriter refsw = field.annotate2(XmlElementRefsWriter.class);
for( CElement e : elements ) {
refw = refsw.value();
refw.name(e.getElementName().getLocalPart())
.namespace(e.getElementName().getNamespaceURI())
.type(e.getType().toType(outline.parent(),IMPLEMENTATION));
if(getOptions().target.isLaterThan(SpecVersion.V2_2))
refw.required(rp.isRequired());
}
}
if(rp.isMixed())
field.annotate(XmlMixed.class);
NClass dh = rp.getDOMHandler();
if(dh!=null) {
XmlAnyElementWriter xaew = field.annotate2(XmlAnyElementWriter.class);
xaew.lax(rp.getWildcard().allowTypedObject);
final JClass value = dh.toType(outline.parent(),IMPLEMENTATION);
if(!value.equals(codeModel.ref(W3CDomHandler.class))) {
xaew.value(value);
}
}
}
private void annotateElement(JAnnotatable field) {
CElementPropertyInfo ep = (CElementPropertyInfo) prop;
List<CTypeRef> types = ep.getTypes();
if(ep.isValueList()) {
field.annotate(XmlList.class);
}
assert ep.getXmlName()==null;
if (types.size() == 1) {
CTypeRef t = types.get(0);
writeXmlElementAnnotation(field, t, resolve(t,IMPLEMENTATION), false);
} else {
for (CTypeRef t : types) {
writeXmlElementAnnotation(field, t, resolve(t,IMPLEMENTATION), true);
}
xesw = null;
}
}
private void writeXmlElementAnnotation( JAnnotatable field, CTypeRef ctype, JType jtype,
boolean checkWrapper ) {
XmlElementWriter xew = null;
XmlNsForm formDefault = parent()._package().getElementFormDefault();
String propName = prop.getName(false);
String enclosingTypeNS;
if(parent().target.getTypeName()==null)
enclosingTypeNS = parent()._package().getMostUsedNamespaceURI();
else
enclosingTypeNS = parent().target.getTypeName().getNamespaceURI();
String generatedName = ctype.getTagName().getLocalPart();
if(!generatedName.equals(propName)) {
if(xew == null) xew = getXew(checkWrapper, field);
xew.name(generatedName);
}
String generatedNS = ctype.getTagName().getNamespaceURI();
if (((formDefault == XmlNsForm.QUALIFIED) && !generatedNS.equals(enclosingTypeNS)) ||
((formDefault == XmlNsForm.UNQUALIFIED) && !generatedNS.equals(""))) {
if(xew == null) xew = getXew(checkWrapper, field);
xew.namespace(generatedNS);
}
CElementPropertyInfo ep = (CElementPropertyInfo) prop;
if(ep.isRequired() && exposedType.isReference()) {
if(xew == null) xew = getXew(checkWrapper, field);
xew.required(true);
}
if(ep.isRequired() && !prop.isCollection())
jtype = jtype.unboxify();
if( !jtype.equals(exposedType) || (getOptions().runtime14 && prop.isCollection())) {
if(xew == null) xew = getXew(checkWrapper, field);
xew.type(jtype);
}
final String defaultValue = ctype.getDefaultValue();
if (defaultValue!=null) {
if(xew == null) xew = getXew(checkWrapper, field);
xew.defaultValue(defaultValue);
}
if (ctype.isNillable()) {
if(xew == null) xew = getXew(checkWrapper, field);
xew.nillable(true);
}
}
protected final Options getOptions() {
return parent().parent().getModel().options;
}
private XmlElementsWriter xesw = null;
private XmlElementWriter getXew(boolean checkWrapper, JAnnotatable field) {
XmlElementWriter xew;
if(checkWrapper) {
if(xesw==null) {
xesw = field.annotate2(XmlElementsWriter.class);
}
xew = xesw.value();
} else {
xew = field.annotate2(XmlElementWriter.class);
}
return xew;
}
private void annotateAttribute(JAnnotatable field) {
CAttributePropertyInfo ap = (CAttributePropertyInfo) prop;
QName attName = ap.getXmlName();
XmlAttributeWriter xaw = field.annotate2(XmlAttributeWriter.class);
final String generatedName = attName.getLocalPart();
final String generatedNS = attName.getNamespaceURI();
if(!generatedName.equals(ap.getName(false)) || !generatedName.equals(ap.getName(true)) || (outline.parent().getModel().getNameConverter() != NameConverter.standard)) {
xaw.name(generatedName);
}
if(!generatedNS.equals("")) {
xaw.namespace(generatedNS);
}
if(ap.isRequired()) {
xaw.required(true);
}
}
protected abstract class Accessor implements FieldAccessor {
protected final JExpression $target;
protected Accessor( JExpression $target ) {
this.$target = $target;
}
public final FieldOutline owner() {
return AbstractField.this;
}
public final CPropertyInfo getPropertyInfo() {
return prop;
}
}
protected final JFieldVar generateField( JType type ) {
return outline.implClass.field( JMod.PROTECTED, type, prop.getName(false) );
}
protected final JExpression castToImplType( JExpression exp ) {
if(implType==exposedType)
return exp;
else
return JExpr.cast(implType,exp);
}
protected JType getType(final Aspect aspect) {
if(prop.getAdapter()!=null)
return prop.getAdapter().customType.toType(outline.parent(),aspect);
final class TypeList extends ArrayList<JType> {
void add( CTypeInfo t ) {
add( t.getType().toType(outline.parent(),aspect) );
if(t instanceof CElementInfo) {
add( ((CElementInfo)t).getSubstitutionMembers());
}
}
void add( Collection<? extends CTypeInfo> col ) {
for (CTypeInfo typeInfo : col)
add(typeInfo);
}
}
TypeList r = new TypeList();
r.add(prop.ref());
JType t;
if(prop.baseType!=null)
t = prop.baseType;
else
t = TypeUtil.getCommonBaseType(codeModel,r);
if(prop.isUnboxable())
t = t.unboxify();
return t;
}
protected final List<Object> listPossibleTypes( CPropertyInfo prop ) {
List<Object> r = new ArrayList<Object>();
for( CTypeInfo tt : prop.ref() ) {
JType t = tt.getType().toType(outline.parent(),Aspect.EXPOSED);
if( t.isPrimitive() || t.isArray() )
r.add(t.fullName());
else {
r.add(t);
r.add("\n");
}
}
return r;
}
private JType resolve(CTypeRef typeRef,Aspect a) {
return outline.parent().resolve(typeRef,a);
}
}