use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.
the class JSONProvider method marshalCollection.
protected void marshalCollection(Class<?> originalCls, Object collection, Type genericType, String encoding, OutputStream os, MediaType m, Annotation[] anns) throws Exception {
Class<?> actualClass = InjectionUtils.getActualType(genericType);
actualClass = getActualType(actualClass, genericType, anns);
Collection<?> c = originalCls.isArray() ? Arrays.asList((Object[]) collection) : (Collection<?>) collection;
Iterator<?> it = c.iterator();
Object firstObj = it.hasNext() ? it.next() : null;
String startTag = null;
String endTag = null;
if (!dropCollectionWrapperElement) {
QName qname = null;
if (firstObj instanceof JAXBElement) {
JAXBElement<?> el = (JAXBElement<?>) firstObj;
qname = el.getName();
actualClass = el.getDeclaredType();
} else {
qname = getCollectionWrapperQName(actualClass, genericType, firstObj, false);
}
String prefix = "";
if (!ignoreNamespaces) {
prefix = namespaceMap.get(qname.getNamespaceURI());
if (prefix != null) {
if (prefix.length() > 0) {
prefix += ".";
}
} else if (qname.getNamespaceURI().length() > 0) {
prefix = "ns1.";
}
}
prefix = (prefix == null) ? "" : prefix;
startTag = "{\"" + prefix + qname.getLocalPart() + "\":[";
endTag = "]}";
} else if (serializeAsArray) {
startTag = "[";
endTag = "]";
} else {
startTag = "{";
endTag = "}";
}
os.write(startTag.getBytes());
if (firstObj != null) {
XmlJavaTypeAdapter adapter = org.apache.cxf.jaxrs.utils.JAXBUtils.getAdapter(firstObj.getClass(), anns);
marshalCollectionMember(JAXBUtils.useAdapter(firstObj, adapter, true), actualClass, genericType, encoding, os);
while (it.hasNext()) {
os.write(",".getBytes());
marshalCollectionMember(JAXBUtils.useAdapter(it.next(), adapter, true), actualClass, genericType, encoding, os);
}
}
os.write(endTag.getBytes());
}
use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.
the class WrapperClassGenerator method addJAXBAnnotations.
private boolean addJAXBAnnotations(FieldVisitor fv, List<Annotation> jaxbAnnos, String name) {
AnnotationVisitor av0;
boolean addedEl = false;
for (Annotation ann : jaxbAnnos) {
if (ann instanceof XmlMimeType) {
av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlMimeType;", true);
av0.visit("value", ((XmlMimeType) ann).value());
av0.visitEnd();
} else if (ann instanceof XmlJavaTypeAdapter) {
av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapter;", true);
generateXmlJavaTypeAdapter(av0, (XmlJavaTypeAdapter) ann);
av0.visitEnd();
} else if (ann instanceof XmlAttachmentRef) {
av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlAttachmentRef;", true);
av0.visitEnd();
} else if (ann instanceof XmlList) {
av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlList;", true);
av0.visitEnd();
} else if (ann instanceof XmlElement) {
addedEl = true;
XmlElement el = (XmlElement) ann;
av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElement;", true);
if ("##default".equals(el.name())) {
av0.visit("name", name);
} else {
av0.visit("name", el.name());
}
av0.visit("nillable", el.nillable());
av0.visit("required", el.required());
av0.visit("namespace", el.namespace());
av0.visit("defaultValue", el.defaultValue());
if (el.type() != XmlElement.DEFAULT.class) {
av0.visit("type", el.type());
}
av0.visitEnd();
} else if (ann instanceof XmlElementWrapper) {
XmlElementWrapper el = (XmlElementWrapper) ann;
av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElementWrapper;", true);
av0.visit("name", el.name());
av0.visit("nillable", el.nillable());
av0.visit("required", el.required());
av0.visit("namespace", el.namespace());
av0.visitEnd();
}
}
return addedEl;
}
use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.
the class WrapperClassGenerator method generateXmlJavaTypeAdapters.
private void generateXmlJavaTypeAdapters(AnnotationVisitor av, XmlJavaTypeAdapters adapters) {
AnnotationVisitor av1 = av.visitArray("value");
for (XmlJavaTypeAdapter adapter : adapters.value()) {
AnnotationVisitor av2 = av1.visitAnnotation(null, "Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapter;");
generateXmlJavaTypeAdapter(av2, adapter);
av2.visitEnd();
}
av1.visitEnd();
}
use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.
the class JAXBUtils method getTypeFromAdapter.
public static Class<?> getTypeFromAdapter(XmlJavaTypeAdapter adapter, Class<?> theType, boolean boundType) {
if (adapter != null) {
if (adapter.type() != XmlJavaTypeAdapter.DEFAULT.class) {
theType = adapter.type();
} else {
Type topAdapterType = adapter.value().getGenericSuperclass();
Class<?> superClass = adapter.value().getSuperclass();
while (superClass != null) {
Class<?> nextSuperClass = superClass.getSuperclass();
if (nextSuperClass != null && !Object.class.equals(nextSuperClass)) {
topAdapterType = superClass.getGenericSuperclass();
}
superClass = nextSuperClass;
}
Type[] types = InjectionUtils.getActualTypes(topAdapterType);
if (types != null && types.length == 2) {
int index = boundType ? 1 : 0;
theType = InjectionUtils.getActualType(types[index]);
}
}
}
return theType;
}
use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.
the class JAXBUtils method getAdapter.
public static XmlJavaTypeAdapter getAdapter(Class<?> objectClass, Annotation[] anns) {
XmlJavaTypeAdapter typeAdapter = AnnotationUtils.getAnnotation(anns, XmlJavaTypeAdapter.class);
if (typeAdapter == null) {
typeAdapter = objectClass.getAnnotation(XmlJavaTypeAdapter.class);
if (typeAdapter == null) {
// lets just try the 1st interface for now
Class<?>[] interfaces = objectClass.getInterfaces();
typeAdapter = interfaces.length > 0 ? interfaces[0].getAnnotation(XmlJavaTypeAdapter.class) : null;
}
}
return typeAdapter;
}
Aggregations