}
if (genericType == null) {
genericType = type;
}
AegisContext context = getAegisContext(type, genericType);
AegisType aegisType = context.getTypeMapping().getType(genericType);
AegisWriter<XMLStreamWriter> aegisWriter = context.createXMLStreamWriter();
try {
W3CDOMStreamWriter w3cStreamWriter = new W3CDOMStreamWriter();
XMLStreamWriter spyingWriter = new PrefixCollectingXMLStreamWriter(w3cStreamWriter,
namespaceMap);
spyingWriter.writeStartDocument();
// use type qname as element qname?
aegisWriter.write(obj, aegisType.getSchemaType(), false, spyingWriter, aegisType);
spyingWriter.writeEndDocument();
spyingWriter.close();
Document dom = w3cStreamWriter.getDocument();
// ok, now the namespace map has all the prefixes.
String enc = HttpUtils.getSetEncoding(m, headers, "UTF-8");
XMLStreamWriter xmlStreamWriter = createStreamWriter(aegisType.getSchemaType(), enc, os);
xmlStreamWriter.writeStartDocument();
StaxUtils.copy(dom, xmlStreamWriter);
// Jettison needs, and StaxUtils.copy doesn't do it.
xmlStreamWriter.writeEndDocument();
xmlStreamWriter.flush();