use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.
the class WrapperClassGenerator method generatePackageInfo.
private void generatePackageInfo(String className, String ns, Class<?> clz) {
ClassWriter cw = createClassWriter();
String classFileName = periodToSlashes(className);
cw.visit(Opcodes.V1_5, Opcodes.ACC_ABSTRACT + Opcodes.ACC_INTERFACE, classFileName, null, "java/lang/Object", null);
boolean q = qualified;
SchemaInfo si = interfaceInfo.getService().getSchema(ns);
if (si != null) {
q = si.isElementFormQualified();
}
AnnotationVisitor av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlSchema;", true);
av0.visit("namespace", ns);
av0.visitEnum("elementFormDefault", getClassCode(XmlNsForm.class), q ? "QUALIFIED" : "UNQUALIFIED");
av0.visitEnd();
if (clz.getPackage() != null && clz.getPackage().getAnnotations() != null) {
for (Annotation ann : clz.getPackage().getAnnotations()) {
if (ann instanceof XmlJavaTypeAdapters) {
av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapters;", true);
generateXmlJavaTypeAdapters(av0, (XmlJavaTypeAdapters) ann);
av0.visitEnd();
} else if (ann instanceof XmlJavaTypeAdapter) {
av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapter;", true);
generateXmlJavaTypeAdapter(av0, (XmlJavaTypeAdapter) ann);
av0.visitEnd();
}
}
}
cw.visitEnd();
loadClass(className, clz, cw.toByteArray());
}
use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.
the class AbstractJAXBProvider method getActualType.
protected Class<?> getActualType(Class<?> type, Type genericType, Annotation[] anns) {
Class<?> theType = null;
if (JAXBElement.class.isAssignableFrom(type)) {
theType = InjectionUtils.getActualType(genericType);
} else {
theType = type;
}
XmlJavaTypeAdapter adapter = org.apache.cxf.jaxrs.utils.JAXBUtils.getAdapter(theType, anns);
theType = org.apache.cxf.jaxrs.utils.JAXBUtils.getTypeFromAdapter(adapter, theType, false);
return theType;
}
use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.
the class JAXBElementProvider method marshalCollection.
protected void marshalCollection(Class<?> originalCls, Object collection, Type genericType, String enc, 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;
QName qname = null;
if (firstObj instanceof JAXBElement) {
JAXBElement<?> el = (JAXBElement<?>) firstObj;
qname = el.getName();
actualClass = el.getDeclaredType();
} else {
qname = getCollectionWrapperQName(actualClass, genericType, firstObj, true);
}
if (qname == null) {
String message = new org.apache.cxf.common.i18n.Message("NO_COLLECTION_ROOT", BUNDLE).toString();
throw new WebApplicationException(Response.serverError().entity(message).build());
}
StringBuilder pi = new StringBuilder();
pi.append(XML_PI_START + (enc == null ? StandardCharsets.UTF_8.name() : enc) + "\"?>");
os.write(pi.toString().getBytes());
String startTag = null;
String endTag = null;
if (qname.getNamespaceURI().length() > 0) {
String prefix = nsPrefixes.get(qname.getNamespaceURI());
if (prefix == null) {
prefix = "ns1";
}
startTag = "<" + prefix + ":" + qname.getLocalPart() + " xmlns:" + prefix + "=\"" + qname.getNamespaceURI() + "\">";
endTag = "</" + prefix + ":" + qname.getLocalPart() + ">";
} else {
startTag = "<" + qname.getLocalPart() + ">";
endTag = "</" + qname.getLocalPart() + ">";
}
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, enc, os, anns, m, qname.getNamespaceURI());
while (it.hasNext()) {
marshalCollectionMember(JAXBUtils.useAdapter(it.next(), adapter, true), actualClass, genericType, enc, os, anns, m, qname.getNamespaceURI());
}
}
os.write(endTag.getBytes());
}
use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.
the class WrapperBeanFieldAnnotator method annotate.
public void annotate(final JavaAnnotatable field) {
JavaField jField = null;
if (field instanceof JavaField) {
jField = (JavaField) field;
} else {
throw new RuntimeException("WrapperBeanFiledAnnotator expect JavaField as input");
}
String rawName = jField.getRawName();
boolean hasEl = false;
for (Annotation ann : jField.getJaxbAnnotaions()) {
if (ann instanceof XmlMimeType) {
JAnnotation mimeAnno = new JAnnotation(XmlMimeType.class);
mimeAnno.addElement(new JAnnotationElement("value", ((XmlMimeType) ann).value()));
jField.addAnnotation(mimeAnno);
} else if (ann instanceof XmlJavaTypeAdapter) {
JAnnotation jaxbAnn = new JAnnotation(XmlJavaTypeAdapter.class);
jaxbAnn.addElement(new JAnnotationElement("value", ((XmlJavaTypeAdapter) ann).value()));
jaxbAnn.addElement(new JAnnotationElement("type", ((XmlJavaTypeAdapter) ann).type()));
jField.addAnnotation(jaxbAnn);
} else if (ann instanceof XmlAttachmentRef) {
JAnnotation jaxbAnn = new JAnnotation(XmlAttachmentRef.class);
jField.addAnnotation(jaxbAnn);
} else if (ann instanceof XmlList) {
JAnnotation jaxbAnn = new JAnnotation(XmlList.class);
jField.addAnnotation(jaxbAnn);
} else if (ann instanceof XmlElement) {
hasEl = true;
XmlElement el = (XmlElement) ann;
JAnnotation xmlElementAnnotation = new JAnnotation(XmlElement.class);
xmlElementAnnotation.addElement(new JAnnotationElement("name", el.name()));
if (!StringUtils.isEmpty(el.namespace())) {
xmlElementAnnotation.addElement(new JAnnotationElement("namespace", el.namespace()));
}
if (el.nillable()) {
xmlElementAnnotation.addElement(new JAnnotationElement("nillable", el.nillable(), true));
}
if (el.required()) {
xmlElementAnnotation.addElement(new JAnnotationElement("required", el.required(), true));
}
if (!StringUtils.isEmpty(el.defaultValue())) {
xmlElementAnnotation.addElement(new JAnnotationElement("defaultValue", el.defaultValue()));
}
jField.addAnnotation(xmlElementAnnotation);
}
}
if (!hasEl) {
JAnnotation xmlElementAnnotation = new JAnnotation(XmlElement.class);
xmlElementAnnotation.addElement(new JAnnotationElement("name", rawName));
if (!StringUtils.isEmpty(jField.getTargetNamespace())) {
xmlElementAnnotation.addElement(new JAnnotationElement("namespace", jField.getTargetNamespace()));
}
jField.addAnnotation(xmlElementAnnotation);
}
}
use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.
the class XmlJavaTypeAdapterAnnotator method annotate.
public void annotate(JavaAnnotatable jn) {
JAnnotation jaxbAnnotation = new JAnnotation(XmlJavaTypeAdapter.class);
jaxbAnnotation.addElement(new JAnnotationElement("value", adapter));
if (jn instanceof JavaParameter) {
JavaParameter jp = (JavaParameter) jn;
jp.addAnnotation("XmlJavaTypeAdapter", jaxbAnnotation);
} else if (jn instanceof JavaMethod) {
JavaMethod jm = (JavaMethod) jn;
jm.addAnnotation("XmlJavaTypeAdapter", jaxbAnnotation);
} else {
throw new RuntimeException("Annotation of " + jn.getClass() + " not supported.");
}
jf.addImport(XmlJavaTypeAdapter.class.getName());
jf.addImport(adapter.getName());
}
Aggregations