public void invoke(IXMLWriter xmlWriter, Object payload) throws IOException, WsException {
if (logger.isDebugEnabled()) {
logger.debug("Attempting to marshall payload '" + payload + "'");
}
if (!(payload instanceof IMarshallable)) {
throw new WsException("Payload object must have a defined mapping");
}
// find first difference between current namespaces and those needed for payload
String[] nss = xmlWriter.getNamespaces();
int limit = Math.min(nss.length, m_namespaces.length);
int index = 0;
for (; index < limit; index++) {
if (!nss[index].equals(m_namespaces[index])) {
break;
}
}
if (index < m_namespaces.length) {
// push payload namespaces as extension
int base = xmlWriter.getNamespaceCount();
String[] extnss = new String[m_namespaces.length - index];
System.arraycopy(m_namespaces, index, extnss, 0, extnss.length);
xmlWriter.pushExtensionNamespaces(extnss);
// build translation table to correct namespaces when accessed by index
int[] xlates = new int[m_namespaces.length];
for (int i = 0; i < xlates.length; i++) {
if (i < index) {
xlates[i] = i;
} else {
xlates[i] = base + i - index;
}
}
xmlWriter.pushTranslationTable(xlates);
}
try {
m_marshaller.setXmlWriter(xmlWriter);
try {
((IMarshallable) payload).marshal(m_marshaller);
} catch (JiBXException e) {
throw new WsException("Unable to marshal payload.", e);
}
} finally {
if (index < m_namespaces.length) {
xmlWriter.popTranslationTable();
xmlWriter.popExtensionNamespaces();