Search in sources :

Example 1 with TypeMappingInfo

use of org.eclipse.persistence.jaxb.TypeMappingInfo in project metro-jax-ws by eclipse-ee4j.

the class JAXBBond method marshal.

@Override
public void marshal(T object, Result result) throws JAXBException {
    JAXBMarshaller marshaller = null;
    try {
        marshaller = parent.mpool.allocate();
        marshaller.setAttachmentMarshaller(null);
        marshaller.setProperty(jakarta.xml.bind.Marshaller.JAXB_FRAGMENT, true);
        if (mappingInfo != null) {
            if (isParameterizedType) {
                JAXBTypeElement jte = new JAXBTypeElement(mappingInfo.getXmlTagName(), object, (ParameterizedType) mappingInfo.getType());
                marshaller.marshal(jte, result, mappingInfo);
            } else {
                JAXBElement<T> elt = new JAXBElement<>(mappingInfo.getXmlTagName(), (Class<T>) mappingInfo.getType(), object);
                // marshaller.marshal(elt, result);
                marshaller.marshal(elt, result, mappingInfo);
            }
        } else {
            TypeMappingInfo tmi = null;
            if (object instanceof JAXBElement) {
                QName q = ((JAXBElement) object).getName();
                JAXBContext ctx = (JAXBContext) parent.getJAXBContext();
                Map<TypeMappingInfo, QName> mtq = ctx.getTypeMappingInfoToSchemaType();
                for (Map.Entry<TypeMappingInfo, QName> es : mtq.entrySet()) {
                    if (q.equals(es.getValue())) {
                        tmi = es.getKey();
                        break;
                    }
                }
            }
            if (tmi != null) {
                marshaller.marshal(object, result, tmi);
            } else {
                marshaller.marshal(object, result);
            }
        }
    } finally {
        if (marshaller != null) {
            marshaller.setAttachmentMarshaller(null);
            parent.mpool.replace(marshaller);
        }
    }
}
Also used : JAXBTypeElement(org.eclipse.persistence.jaxb.JAXBTypeElement) QName(javax.xml.namespace.QName) JAXBContext(org.eclipse.persistence.jaxb.JAXBContext) JAXBElement(jakarta.xml.bind.JAXBElement) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller) TypeMappingInfo(org.eclipse.persistence.jaxb.TypeMappingInfo) Map(java.util.Map)

Example 2 with TypeMappingInfo

use of org.eclipse.persistence.jaxb.TypeMappingInfo in project metro-jax-ws by eclipse-ee4j.

the class JAXBContextFactory method newContext.

@Override
protected BindingContext newContext(BindingInfo bi) {
    Map<String, Source> extMapping = (Map<String, Source>) bi.properties().get(OXM_XML_OVERRIDE);
    Map<String, Object> properties = new HashMap<>();
    Map<TypeInfo, TypeMappingInfo> map = createTypeMappings(bi.typeInfos());
    // chen workaround for document-literal wrapper - new feature on eclipselink API requested
    for (TypeInfo tinfo : map.keySet()) {
        WrapperParameter wp = (WrapperParameter) tinfo.properties().get(WrapperParameter.class.getName());
        if (wp != null) {
            Class<?> wrpCls = (Class) tinfo.type;
            Element javaAttributes = null;
            for (ParameterImpl p : wp.getWrapperChildren()) {
                Element xmlelem = findXmlElement(p.getTypeInfo().properties());
                if (xmlelem != null) {
                    if (javaAttributes == null) {
                        javaAttributes = javaAttributes(wrpCls, extMapping);
                    }
                    xmlelem = (Element) javaAttributes.getOwnerDocument().importNode(xmlelem, true);
                    String fieldName = getFieldName(p, wrpCls);
                    xmlelem.setAttribute("java-attribute", fieldName);
                    javaAttributes.appendChild(xmlelem);
                }
            }
            if (wrpCls.getPackage() != null)
                wrpCls.getPackage().getName();
        }
    }
    // Source src = extMapping.get("com.sun.xml.ws.test.toplink.jaxws");
    // if (src != null){
    // TransformerFactory tf = TransformerFactory.newInstance();
    // try {
    // Transformer t = tf.newTransformer();
    // java.io.ByteArrayOutputStream bo = new java.io.ByteArrayOutputStream();
    // StreamResult sax = new StreamResult(bo);
    // t.transform(src, sax);
    // System.out.println(new String(bo.toByteArray()));
    // } catch (TransformerConfigurationException e) {
    // e.printStackTrace();
    // throw new WebServiceException(e.getMessage(), e);
    // } catch (TransformerException e) {
    // e.printStackTrace();
    // throw new WebServiceException(e.getMessage(), e);
    // }
    // }
    HashSet<Type> typeSet = new HashSet<>();
    HashSet<TypeMappingInfo> typeList = new HashSet<>();
    for (TypeMappingInfo tmi : map.values()) {
        typeList.add(tmi);
        typeSet.add(tmi.getType());
    }
    for (Class<?> clss : bi.contentClasses()) {
        if (!typeSet.contains(clss) && !WrapperComposite.class.equals(clss)) {
            typeSet.add(clss);
            TypeMappingInfo tmi = new TypeMappingInfo();
            tmi.setType(clss);
            typeList.add(tmi);
        }
    }
    TypeMappingInfo[] types = typeList.toArray(new TypeMappingInfo[0]);
    if (extMapping != null) {
        properties.put(OXM_XML_OVERRIDE, extMapping);
    }
    if (bi.getDefaultNamespace() != null) {
        properties.put(OXM_XML_ELEMENT, bi.getDefaultNamespace());
    }
    try {
        org.eclipse.persistence.jaxb.JAXBContext jaxbContext = (org.eclipse.persistence.jaxb.JAXBContext) org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(types, properties, bi.getClassLoader());
        return new JAXBContextWrapper(jaxbContext, map, bi.getSEIModel());
    } catch (JAXBException e) {
        throw new DatabindingException(e.getMessage(), e);
    }
}
Also used : HashMap(java.util.HashMap) XmlElement(jakarta.xml.bind.annotation.XmlElement) Element(org.w3c.dom.Element) XmlRootElement(jakarta.xml.bind.annotation.XmlRootElement) WrapperParameter(com.sun.xml.ws.model.WrapperParameter) JAXBContext(jakarta.xml.bind.JAXBContext) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) DatabindingException(com.sun.xml.ws.spi.db.DatabindingException) TypeMappingInfo(org.eclipse.persistence.jaxb.TypeMappingInfo) HashSet(java.util.HashSet) JAXBException(jakarta.xml.bind.JAXBException) TypeInfo(com.sun.xml.ws.spi.db.TypeInfo) GenericArrayType(java.lang.reflect.GenericArrayType) XmlType(jakarta.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ParameterImpl(com.sun.xml.ws.model.ParameterImpl) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with TypeMappingInfo

use of org.eclipse.persistence.jaxb.TypeMappingInfo in project eclipselink by eclipse-ee4j.

the class ConflictingListClassesTestCases method getTypeMappingInfos.

protected TypeMappingInfo[] getTypeMappingInfos() throws Exception {
    if (typeMappingInfos == null) {
        typeMappingInfos = new TypeMappingInfo[3];
        TypeMappingInfo tmi = new TypeMappingInfo();
        tmi.setXmlTagName(new QName("", "testTagName1"));
        tmi.setElementScope(ElementScope.Global);
        tmi.setType(List.class);
        typeMappingInfos[0] = tmi;
        TypeMappingInfo tmi2 = new TypeMappingInfo();
        tmi2.setXmlTagName(new QName("", "testTagName2"));
        tmi2.setElementScope(ElementScope.Global);
        tmi2.setType(List.class);
        typeMappingInfos[1] = tmi2;
        TypeMappingInfo tmi3 = new TypeMappingInfo();
        tmi3.setType(Employee.class);
        typeMappingInfos[2] = tmi3;
    }
    return typeMappingInfos;
}
Also used : QName(javax.xml.namespace.QName) TypeMappingInfo(org.eclipse.persistence.jaxb.TypeMappingInfo)

Example 4 with TypeMappingInfo

use of org.eclipse.persistence.jaxb.TypeMappingInfo in project eclipselink by eclipse-ee4j.

the class ConflictingListTypeTestCases method getTypeMappingInfos.

protected TypeMappingInfo[] getTypeMappingInfos() throws Exception {
    if (typeMappingInfos == null) {
        typeMappingInfos = new TypeMappingInfo[6];
        TypeMappingInfo tmi = new TypeMappingInfo();
        tmi.setXmlTagName(new QName("someUri", "testTagName1"));
        tmi.setElementScope(ElementScope.Global);
        tmi.setType(getClass().getField("testField").getGenericType());
        typeMappingInfos[0] = tmi;
        TypeMappingInfo tmi2 = new TypeMappingInfo();
        tmi2.setXmlTagName(new QName("someUri", "testTagName2"));
        tmi2.setElementScope(ElementScope.Global);
        tmi2.setType(getClass().getField("testField").getGenericType());
        typeMappingInfos[1] = tmi2;
        TypeMappingInfo tmi3 = new TypeMappingInfo();
        tmi3.setXmlTagName(new QName("someUri", "testTagName3"));
        tmi3.setElementScope(ElementScope.Global);
        tmi3.setType(List.class);
        Element xmlElement = getXmlElement("<xml-element type='org.eclipse.persistence.testing.jaxb.typemappinginfo.Employee' />");
        tmi3.setXmlElement(xmlElement);
        typeMappingInfos[2] = tmi3;
        TypeMappingInfo tmi4 = new TypeMappingInfo();
        tmi4.setXmlTagName(new QName("someUri", "testTagName4"));
        tmi4.setElementScope(ElementScope.Global);
        tmi4.setType(getClass().getField("testSimpleField").getGenericType());
        typeMappingInfos[3] = tmi4;
        TypeMappingInfo tmi5 = new TypeMappingInfo();
        tmi5.setXmlTagName(new QName("someUri", "testTagName5"));
        tmi5.setElementScope(ElementScope.Global);
        tmi5.setType(getClass().getField("testSimpleField").getGenericType());
        Element xmlElement2 = getXmlElement("<xml-element xml-list='true'/>");
        tmi5.setXmlElement(xmlElement2);
        typeMappingInfos[4] = tmi5;
        TypeMappingInfo tmi6 = new TypeMappingInfo();
        tmi6.setXmlTagName(new QName("someUri", "testTagName6"));
        tmi6.setElementScope(ElementScope.Global);
        tmi6.setType(getClass().getField("testSimpleField").getGenericType());
        Annotation[] annotations = getClass().getField("xmlListAnnotationField").getAnnotations();
        tmi6.setAnnotations(annotations);
        typeMappingInfos[5] = tmi6;
    }
    return typeMappingInfos;
}
Also used : QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) JAXBElement(jakarta.xml.bind.JAXBElement) TypeMappingInfo(org.eclipse.persistence.jaxb.TypeMappingInfo) Annotation(java.lang.annotation.Annotation)

Example 5 with TypeMappingInfo

use of org.eclipse.persistence.jaxb.TypeMappingInfo in project eclipselink by eclipse-ee4j.

the class ArrayWithAnnotationsTestCases method getTypeMappingInfos.

protected TypeMappingInfo[] getTypeMappingInfos() throws Exception {
    if (typeMappingInfos == null) {
        typeMappingInfos = new TypeMappingInfo[1];
        TypeMappingInfo ti = new TypeMappingInfo();
        ti.setXmlTagName(XML_TAG_NAME);
        ti.setType(Float[].class);
        ti.setElementScope(ElementScope.Global);
        Field fld = getClass().getField("f");
        ti.setAnnotations(fld.getAnnotations());
        typeMappingInfos[0] = ti;
    }
    return typeMappingInfos;
}
Also used : Field(java.lang.reflect.Field) TypeMappingInfo(org.eclipse.persistence.jaxb.TypeMappingInfo)

Aggregations

TypeMappingInfo (org.eclipse.persistence.jaxb.TypeMappingInfo)94 QName (javax.xml.namespace.QName)83 Annotation (java.lang.annotation.Annotation)19 Type (java.lang.reflect.Type)18 JAXBContext (org.eclipse.persistence.jaxb.JAXBContext)17 HashMap (java.util.HashMap)6 JAXBContext (jakarta.xml.bind.JAXBContext)5 JAXBElement (jakarta.xml.bind.JAXBElement)4 Map (java.util.Map)4 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)4 Element (org.w3c.dom.Element)4 JAXBException (org.eclipse.persistence.exceptions.JAXBException)3 JAXBMarshaller (org.eclipse.persistence.jaxb.JAXBMarshaller)3 TypeInfo (com.sun.xml.ws.spi.db.TypeInfo)2 XmlElement (jakarta.xml.bind.annotation.XmlElement)2 XmlList (jakarta.xml.bind.annotation.XmlList)2 XmlRootElement (jakarta.xml.bind.annotation.XmlRootElement)2 StringWriter (java.io.StringWriter)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 ArrayList (java.util.ArrayList)2