Search in sources :

Example 1 with ElementWriter

use of org.apache.cxf.aegis.xml.stax.ElementWriter in project cxf by apache.

the class BeanTest method testAttributeMapDifferentNS.

@Test
public void testAttributeMapDifferentNS() throws Exception {
    defaultContext();
    BeanTypeInfo info = new BeanTypeInfo(SimpleBean.class, "urn:Bean");
    info.mapAttribute("howdy", new QName("urn:Bean2", "howdy"));
    info.mapAttribute("bleh", new QName("urn:Bean2", "bleh"));
    info.setTypeMapping(mapping);
    BeanType type = new BeanType(info);
    type.setTypeClass(SimpleBean.class);
    type.setTypeMapping(mapping);
    type.setSchemaType(new QName("urn:Bean", "bean"));
    ElementReader reader = new ElementReader(getResourceAsStream("bean8.xml"));
    SimpleBean bean = (SimpleBean) type.readObject(reader, getContext());
    assertEquals("bleh", bean.getBleh());
    assertEquals("howdy", bean.getHowdy());
    reader.getXMLStreamReader().close();
    // Test writing
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ElementWriter writer = new ElementWriter(bos, "root", "urn:Bean");
    type.writeObject(bean, writer, getContext());
    writer.close();
    writer.flush();
    bos.close();
    Document doc = StaxUtils.read(new ByteArrayInputStream(bos.toByteArray()));
    Element element = doc.getDocumentElement();
    addNamespace("b2", "urn:Bean2");
    assertValid("/b:root[@b2:bleh='bleh']", element);
    assertValid("/b:root[@b2:howdy='howdy']", element);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) QName(javax.xml.namespace.QName) SimpleBean(org.apache.cxf.aegis.services.SimpleBean) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Element(org.w3c.dom.Element) XmlElement(javax.xml.bind.annotation.XmlElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ElementWriter(org.apache.cxf.aegis.xml.stax.ElementWriter) Document(org.w3c.dom.Document) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest) Test(org.junit.Test)

Example 2 with ElementWriter

use of org.apache.cxf.aegis.xml.stax.ElementWriter in project cxf by apache.

the class AbstractEncodedTest method writeRef.

public Element writeRef(Object instance) {
    AegisType type = mapping.getType(instance.getClass());
    assertNotNull("no type found for " + instance.getClass().getName());
    // create the document
    Element element = createElement("urn:Bean", "root", "b");
    MapNamespaceContext namespaces = new MapNamespaceContext();
    // we should not add the out namespace here, as it is not a part of root element
    /*for (Map.Entry<String, String> entry : getNamespaces().entrySet()) {
            namespaces.addNamespace(entry.getKey(), entry.getValue());
        }*/
    ElementWriter rootWriter = getElementWriter(element, namespaces);
    Context context = getContext();
    // get AegisType based on the object instance
    assertNotNull("type is null", type);
    // write the ref
    SoapRefType soapRefType = new SoapRefType(type);
    MessageWriter cwriter = rootWriter.getElementWriter(soapRefType.getSchemaType());
    soapRefType.writeObject(instance, cwriter, context);
    cwriter.close();
    // write the trailing blocks (referenced objects)
    trailingBlocks.writeBlocks(rootWriter, context);
    return element;
}
Also used : MapNamespaceContext(org.apache.cxf.helpers.MapNamespaceContext) Context(org.apache.cxf.aegis.Context) AegisContext(org.apache.cxf.aegis.AegisContext) AegisType(org.apache.cxf.aegis.type.AegisType) Element(org.w3c.dom.Element) MapNamespaceContext(org.apache.cxf.helpers.MapNamespaceContext) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter) ElementWriter(org.apache.cxf.aegis.xml.stax.ElementWriter)

Example 3 with ElementWriter

use of org.apache.cxf.aegis.xml.stax.ElementWriter in project cxf by apache.

the class AegisXMLStreamDataWriter method write.

/**
 * Write an object to the output. This method always writes xsi:type attributes.
 * @param obj The object to write.
 * @param elementName the QName of the XML Element.
 * @param optional set this for minOccurs = 0. It omits null elements.
 * @param output the output stream
 * @param aegisType the aegis type. This may be null if the object is non-null
 * and the type of the object is covered in the mapping. Warning, for collections
 * this will not do what you want, you must call the alternative version of
 * write that takes a {@link java.lang.reflect.Type}.
 * @throws Exception
 */
public void write(Object obj, QName elementName, boolean optional, XMLStreamWriter output, AegisType aegisType) throws Exception {
    if (obj == null && aegisType == null && !optional) {
        Message message = new Message("WRITE_NULL_NEEDS_TYPE", LOG);
        throw new DatabindingException(message);
    }
    if (obj != null && aegisType == null) {
        aegisType = getContext().getTypeMapping().getType(obj.getClass());
        if (aegisType == null) {
            TypeCreator creator = getContext().getTypeMapping().getTypeCreator();
            aegisType = creator.createType(obj.getClass());
        }
    }
    if (obj != null) {
        // look for overrides declared in the context.
        aegisType = TypeUtil.getWriteType(aegisContext, obj, aegisType);
    }
    if (aegisType == null) {
        Message message = new Message("WRITE_NEEDS_TYPE", LOG, obj);
        throw new DatabindingException(message);
    }
    if (obj == null) {
        if (optional) {
            // minOccurs = 0
            return;
        }
        if (aegisType.isNillable() && aegisType.isWriteOuter()) {
            ElementWriter writer = new ElementWriter(output);
            MessageWriter w2 = writer.getElementWriter(elementName);
            w2.writeXsiNil();
            w2.close();
            return;
        }
    }
    ElementWriter writer = new ElementWriter(output);
    MessageWriter w2 = writer.getElementWriter(elementName);
    if (getContext().isWriteXsiTypes() && aegisType.getSchemaType() != null) {
        // if we know the type, write it. We are standalone, and the reader needs it.
        w2.writeXsiType(aegisType.getSchemaType());
    }
    aegisType.writeObject(obj, w2, context);
    w2.close();
}
Also used : Message(org.apache.cxf.common.i18n.Message) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter) ElementWriter(org.apache.cxf.aegis.xml.stax.ElementWriter) TypeCreator(org.apache.cxf.aegis.type.TypeCreator)

Example 4 with ElementWriter

use of org.apache.cxf.aegis.xml.stax.ElementWriter 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 5 with ElementWriter

use of org.apache.cxf.aegis.xml.stax.ElementWriter 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)

Aggregations

ElementWriter (org.apache.cxf.aegis.xml.stax.ElementWriter)6 MessageWriter (org.apache.cxf.aegis.xml.MessageWriter)4 Context (org.apache.cxf.aegis.Context)3 AegisType (org.apache.cxf.aegis.type.AegisType)3 Message (org.apache.cxf.common.i18n.Message)3 Element (org.w3c.dom.Element)3 DatabindingException (org.apache.cxf.aegis.DatabindingException)2 MapNamespaceContext (org.apache.cxf.helpers.MapNamespaceContext)2 Fault (org.apache.cxf.interceptor.Fault)2 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 XmlElement (javax.xml.bind.annotation.XmlElement)1 QName (javax.xml.namespace.QName)1 AbstractAegisTest (org.apache.cxf.aegis.AbstractAegisTest)1 AegisContext (org.apache.cxf.aegis.AegisContext)1 SimpleBean (org.apache.cxf.aegis.services.SimpleBean)1 TypeCreator (org.apache.cxf.aegis.type.TypeCreator)1 ArrayType (org.apache.cxf.aegis.type.basic.ArrayType)1 ElementReader (org.apache.cxf.aegis.xml.stax.ElementReader)1