try {
Document document = objectToXML(object, xmlDescriptor, isXMLRoot);
if ((result instanceof SAXResult) && (isFragment())) {
FragmentContentHandler fragmentHandler = new FragmentContentHandler(((SAXResult) result).getHandler());
XMLTransformer transformer = getTransformer();
if (isXMLRoot) {
String oldEncoding = transformer.getEncoding();
String oldVersion = transformer.getVersion();
if (((XMLRoot) object).getEncoding() != null) {
transformer.setEncoding(((XMLRoot) object).getEncoding());
}
if (((XMLRoot) object).getXMLVersion() != null) {
transformer.setVersion(((XMLRoot) object).getXMLVersion());
}
transformer.transform(document, fragmentHandler);
transformer.setEncoding(oldEncoding);
transformer.setVersion(oldVersion);
} else {
transformer.transform(document, fragmentHandler);
}
} else {
if (result.getClass().equals(staxResultClass)) {
try {
String namespace = null;
String localName = null;
if(isXMLRoot){
XMLRoot xmlRootObject = (XMLRoot)object;
if(xmlRootObject.getObject() != null && xmlRootObject.getObject() instanceof Node){
namespace = ((XMLRoot)object).getNamespaceURI();
localName = ((XMLRoot)object).getLocalName();
}
}
Object xmlStreamWriter = PrivilegedAccessHelper.invokeMethod(staxResultGetStreamWriterMethod, result);
if (xmlStreamWriter != null) {
Object domtostax = PrivilegedAccessHelper.newInstanceFromClass(domToStreamWriterClass);
PrivilegedAccessHelper.invokeMethod(writeToStreamMethod, domtostax, new Object[]{document,namespace, localName,xmlStreamWriter});
return;
} else {
Object xmlEventWriter = PrivilegedAccessHelper.invokeMethod(staxResultGetEventWriterMethod, result);
if(xmlEventWriter != null) {
Object domToEventWriter = PrivilegedAccessHelper.newInstanceFromClass(domToEventWriterClass);
PrivilegedAccessHelper.invokeMethod(writeToEventWriterMethod, domToEventWriter, new Object[]{document, namespace, localName, xmlEventWriter});
return;
}
}
} catch(Exception e) {
throw XMLMarshalException.marshalException(e);
}
}
XMLTransformer transformer = getTransformer();
if (isXMLRoot) {
String oldEncoding = transformer.getEncoding();
String oldVersion = transformer.getVersion();
if (((XMLRoot) object).getEncoding() != null) {
transformer.setEncoding(((XMLRoot) object).getEncoding());
}
if (((XMLRoot) object).getXMLVersion() != null) {
transformer.setVersion(((XMLRoot) object).getXMLVersion());
}
transformer.transform(document, result);
transformer.setEncoding(oldEncoding);
transformer.setVersion(oldVersion);
} else {
transformer.transform(document, result);
}
}
} catch (XMLPlatformException e) {
throw XMLMarshalException.marshalException(e);
}