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