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);
}
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);
}
}
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);
}
}
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);
}
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);
}
Aggregations