Search in sources :

Example 11 with Context

use of org.apache.cxf.aegis.Context in project cxf by apache.

the class SoapArrayTypeTest method testUrTypeArray.

@Test
public void testUrTypeArray() throws Exception {
    Context context = getContext();
    // ur-type[4] nested elements have xsi:type
    ElementReader reader = new ElementReader(getClass().getResourceAsStream("arrayUrType1.xml"));
    Object[] objects = (Object[]) createArrayType(Object[].class).readObject(reader, context);
    reader.getXMLStreamReader().close();
    assertArrayEquals(new Object[] { 42, (float) 42.42, "Forty Two" }, objects);
    // ur-type[4] nested element name have a global schema type
    reader = new ElementReader(getClass().getResourceAsStream("arrayUrType2.xml"));
    objects = (Object[]) createArrayType(Object[].class).readObject(reader, context);
    reader.getXMLStreamReader().close();
    assertArrayEquals(Arrays.asList(objects).toString(), new Object[] { 42, new BigDecimal("42.42"), "Forty Two" }, objects);
}
Also used : Context(org.apache.cxf.aegis.Context) BigDecimal(java.math.BigDecimal) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) Test(org.junit.Test)

Example 12 with Context

use of org.apache.cxf.aegis.Context in project cxf by apache.

the class ElementDataWriter method write.

public void write(Object obj, MessagePartInfo part, Element output) {
    AegisType type = databinding.getType(part);
    if (type == null) {
        throw new Fault(new Message("NO_MESSAGE_FOR_PART", LOG));
    }
    Context context = new Context(databinding.getAegisContext());
    context.setAttachments(attachments);
    type = TypeUtil.getWriteType(databinding.getAegisContext(), obj, type);
    try {
        W3CDOMStreamWriter domWriter = new W3CDOMStreamWriter(output);
        ElementWriter writer = new ElementWriter(domWriter);
        MessageWriter w2 = writer.getElementWriter(part.getConcreteName());
        if (type.isNillable() && type.isWriteOuter() && obj == null) {
            w2.writeXsiNil();
            w2.close();
            return;
        }
        type.writeObject(obj, w2, context);
        w2.close();
    } catch (DatabindingException e) {
        throw new RuntimeException(e);
    }
}
Also used : Context(org.apache.cxf.aegis.Context) DatabindingException(org.apache.cxf.aegis.DatabindingException) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) Message(org.apache.cxf.common.i18n.Message) AegisType(org.apache.cxf.aegis.type.AegisType) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter) Fault(org.apache.cxf.interceptor.Fault) ElementWriter(org.apache.cxf.aegis.xml.stax.ElementWriter)

Example 13 with Context

use of org.apache.cxf.aegis.Context in project cxf by apache.

the class XMLStreamDataWriter method write.

public void write(Object obj, MessagePartInfo part, XMLStreamWriter output) {
    AegisType type = databinding.getType(part);
    if (type == null) {
        type = databinding.getTypeFromClass(obj.getClass());
    }
    if (type == null) {
        throw new Fault(new Message("NO_MESSAGE_FOR_PART", LOG, part));
    }
    Context context = new Context(databinding.getAegisContext());
    context.setAttachments(attachments);
    type = TypeUtil.getWriteType(databinding.getAegisContext(), obj, type);
    /*
         * We arrive here with a 'type' of the inner type if isWriteOuter is null.
         * However, in that case, the original type is available.
         */
    AegisType outerType = null;
    if (part != null) {
        outerType = part.getProperty("org.apache.cxf.aegis.outerType", AegisType.class);
    }
    try {
        if (obj == null) {
            if (part.getXmlSchema() instanceof XmlSchemaElement && ((XmlSchemaElement) part.getXmlSchema()).getMinOccurs() == 0) {
                // skip writing minOccurs=0 stuff if obj is null
                return;
            } else if (type.isNillable()) {
                ElementWriter writer = new ElementWriter(output);
                MessageWriter w2 = writer.getElementWriter(part.getConcreteName());
                w2.writeXsiNil();
                w2.close();
                return;
            }
        }
        ElementWriter writer = new ElementWriter(output);
        // outerType is only != null for a flat array.
        if (outerType == null) {
            MessageWriter w2 = writer.getElementWriter(part != null ? part.getConcreteName() : type.getSchemaType());
            type.writeObject(obj, w2, context);
            w2.close();
        } else {
            // it has better be an array (!)
            ArrayType aType = (ArrayType) outerType;
            // the part has to have a name or we can't do this.
            aType.writeObject(obj, writer, context, part.getConcreteName());
        }
    } catch (DatabindingException e) {
        throw new RuntimeException(e);
    }
}
Also used : Context(org.apache.cxf.aegis.Context) ArrayType(org.apache.cxf.aegis.type.basic.ArrayType) DatabindingException(org.apache.cxf.aegis.DatabindingException) Message(org.apache.cxf.common.i18n.Message) AegisType(org.apache.cxf.aegis.type.AegisType) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter) Fault(org.apache.cxf.interceptor.Fault) ElementWriter(org.apache.cxf.aegis.xml.stax.ElementWriter)

Example 14 with Context

use of org.apache.cxf.aegis.Context in project cxf by apache.

the class XMLStreamReaderMappingTest method getContext.

protected Context getContext() {
    AegisContext globalContext = new AegisContext();
    globalContext.initialize();
    globalContext.setTypeMapping(mapping);
    return new Context(globalContext);
}
Also used : Context(org.apache.cxf.aegis.Context) AegisContext(org.apache.cxf.aegis.AegisContext) AegisContext(org.apache.cxf.aegis.AegisContext)

Example 15 with Context

use of org.apache.cxf.aegis.Context in project cxf by apache.

the class QualificationTest method testXmlDefaultUnqualifiedAttribute.

@Test
public void testXmlDefaultUnqualifiedAttribute() throws Exception {
    AegisContext context = new AegisContext();
    context.initialize();
    TypeMapping mapping = context.getTypeMapping();
    AegisType type = mapping.getTypeCreator().createType(XmlMappedAttributeBean.class);
    type.setSchemaType(new QName("urn:Bean", "bean"));
    Context messageContext = new Context(context);
    XmlMappedAttributeBean bean = new XmlMappedAttributeBean();
    Element element = writeObjectToElement(type, bean, messageContext);
    assertValid("/b:root[@attrXmlString]", element);
    assertXPathEquals("/b:root/@attrXmlString", "attrXml", element);
}
Also used : Context(org.apache.cxf.aegis.Context) AegisContext(org.apache.cxf.aegis.AegisContext) XmlMappedAttributeBean(org.apache.cxf.aegis.services.XmlMappedAttributeBean) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) AegisContext(org.apache.cxf.aegis.AegisContext) TypeMapping(org.apache.cxf.aegis.type.TypeMapping) Test(org.junit.Test) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest)

Aggregations

Context (org.apache.cxf.aegis.Context)21 Test (org.junit.Test)13 AegisContext (org.apache.cxf.aegis.AegisContext)11 ElementReader (org.apache.cxf.aegis.xml.stax.ElementReader)11 AegisType (org.apache.cxf.aegis.type.AegisType)10 Element (org.w3c.dom.Element)7 QName (javax.xml.namespace.QName)5 AbstractAegisTest (org.apache.cxf.aegis.AbstractAegisTest)5 MapNamespaceContext (org.apache.cxf.helpers.MapNamespaceContext)5 XmlMappedAttributeBean (org.apache.cxf.aegis.services.XmlMappedAttributeBean)4 TypeMapping (org.apache.cxf.aegis.type.TypeMapping)4 DatabindingException (org.apache.cxf.aegis.DatabindingException)3 MessageWriter (org.apache.cxf.aegis.xml.MessageWriter)3 ElementWriter (org.apache.cxf.aegis.xml.stax.ElementWriter)3 BigDecimal (java.math.BigDecimal)2 AttributeBean (org.apache.cxf.aegis.services.AttributeBean)2 TypeCreationOptions (org.apache.cxf.aegis.type.TypeCreationOptions)2 Message (org.apache.cxf.common.i18n.Message)2 Fault (org.apache.cxf.interceptor.Fault)2 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)2