Search in sources :

Example 16 with ElementReader

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

the class StructTypeTest method testSimpleStruct.

@Test
public void testSimpleStruct() throws Exception {
    // Test reading
    ElementReader reader = new ElementReader(getClass().getResourceAsStream("struct1.xml"));
    Address address = (Address) addressType.readObject(reader, getLocalContext());
    validateShippingAddress(address);
    reader.getXMLStreamReader().close();
    // Test reading - no namespace on nested elements
    reader = new ElementReader(getClass().getResourceAsStream("struct2.xml"));
    address = (Address) addressType.readObject(reader, getLocalContext());
    validateShippingAddress(address);
    reader.getXMLStreamReader().close();
    // Test writing
    Element element = writeObjectToElement(addressType, address, getLocalContext());
    validateShippingAddress(element);
}
Also used : Element(org.w3c.dom.Element) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) Test(org.junit.Test)

Example 17 with ElementReader

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

the class EnumTypeTest method testTypeWithJaxbAnnotations.

/**
 * {@link https://issues.apache.org/jira/browse/CXF-7188}
 */
@Test
public void testTypeWithJaxbAnnotations() throws Exception {
    AegisType type = tm.getTypeCreator().createType(JaxbTestEnum.class);
    Element element = writeObjectToElement(type, JaxbTestEnum.VALUE1, getContext());
    assertEquals("Value1", element.getTextContent());
    XMLStreamReader xreader = StaxUtils.createXMLStreamReader(element);
    ElementReader reader = new ElementReader(xreader);
    Object value = type.readObject(reader, getContext());
    assertEquals(JaxbTestEnum.VALUE1, value);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) AegisType(org.apache.cxf.aegis.type.AegisType) Element(org.w3c.dom.Element) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) Test(org.junit.Test) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest)

Example 18 with ElementReader

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

the class EnumTypeTest method testType.

@Test
public void testType() throws Exception {
    EnumType type = new EnumType();
    type.setTypeClass(SmallEnum.class);
    type.setSchemaType(new QName("urn:test", "test"));
    tm.register(type);
    Element element = writeObjectToElement(type, SmallEnum.VALUE1, getContext());
    assertEquals("VALUE1", element.getTextContent());
    XMLStreamReader xreader = StaxUtils.createXMLStreamReader(element);
    ElementReader reader = new ElementReader(xreader);
    Object value = type.readObject(reader, getContext());
    assertEquals(SmallEnum.VALUE1, value);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) Test(org.junit.Test) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest)

Example 19 with ElementReader

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

the class DocumentType method readObject.

@Override
public Object readObject(MessageReader mreader, Context context) throws DatabindingException {
    try {
        XMLStreamReader reader = ((ElementReader) mreader).getXMLStreamReader();
        // we need to eat the surrounding element.
        reader.nextTag();
        Object tree = StaxUtils.read(null, new FragmentStreamReader(reader), true);
        // eat the end tag.
        reader.nextTag();
        return tree;
    } catch (XMLStreamException e) {
        throw new DatabindingException("Could not parse xml.", e);
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) FragmentStreamReader(org.apache.cxf.staxutils.FragmentStreamReader) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader)

Example 20 with ElementReader

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

the class BeanTest method testBean.

@Test
public void testBean() throws Exception {
    defaultContext();
    BeanType type = new BeanType();
    type.setTypeClass(SimpleBean.class);
    type.setTypeMapping(mapping);
    type.setSchemaType(new QName("urn:Bean", "bean"));
    // Test reading
    ElementReader reader = new ElementReader(getResourceAsStream("bean1.xml"));
    SimpleBean bean = (SimpleBean) type.readObject(reader, getContext());
    assertEquals("bleh", bean.getBleh());
    assertEquals("howdy", bean.getHowdy());
    reader.getXMLStreamReader().close();
    // Test reading with extra elements
    reader = new ElementReader(getResourceAsStream("bean2.xml"));
    bean = (SimpleBean) type.readObject(reader, getContext());
    assertEquals("bleh", bean.getBleh());
    assertEquals("howdy", bean.getHowdy());
    reader.getXMLStreamReader().close();
    // test <bleh/> element
    reader = new ElementReader(getResourceAsStream("bean7.xml"));
    bean = (SimpleBean) type.readObject(reader, getContext());
    assertEquals("", bean.getBleh());
    assertEquals("howdy", bean.getHowdy());
    reader.getXMLStreamReader().close();
    bean.setBleh("bleh");
    // Test writing
    Element element = writeObjectToElement(type, bean, getContext());
    assertValid("/b:root/b:bleh[text()='bleh']", element);
    assertValid("/b:root/b:howdy[text()='howdy']", element);
}
Also used : 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) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest) Test(org.junit.Test)

Aggregations

ElementReader (org.apache.cxf.aegis.xml.stax.ElementReader)34 Test (org.junit.Test)26 AbstractAegisTest (org.apache.cxf.aegis.AbstractAegisTest)16 QName (javax.xml.namespace.QName)13 Element (org.w3c.dom.Element)13 Context (org.apache.cxf.aegis.Context)11 XmlElement (javax.xml.bind.annotation.XmlElement)7 AegisType (org.apache.cxf.aegis.type.AegisType)7 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)7 SimpleBean (org.apache.cxf.aegis.services.SimpleBean)6 XMLStreamReader (javax.xml.stream.XMLStreamReader)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 AegisContext (org.apache.cxf.aegis.AegisContext)3 DatabindingException (org.apache.cxf.aegis.DatabindingException)3 BigDecimal (java.math.BigDecimal)2 MapNamespaceContext (org.apache.cxf.helpers.MapNamespaceContext)2 XmlSchema (org.apache.ws.commons.schema.XmlSchema)2 XmlSchemaComplexType (org.apache.ws.commons.schema.XmlSchemaComplexType)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1