* @param receivedMessage the actual message received.
* @param namespaces explicit namespace mappings for this construction.
* @return the constructed namespace context.
*/
public NamespaceContext buildContext(Message receivedMessage, Map<String, String> namespaces) {
SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
//first add default namespace definitions
if (namespaceMappings.size() > 0) {
simpleNamespaceContext.setBindings(namespaceMappings);
}
Map<String, String> dynamicBindings = XMLUtils.lookupNamespaces(receivedMessage.getPayload().toString());
if (!CollectionUtils.isEmpty(namespaces)) {
//dynamic binding of namespaces declarations in root element of received message
for (Entry<String, String> binding : dynamicBindings.entrySet()) {
//only bind namespace that is not present in explicit namespace bindings
if (!namespaces.containsValue(binding.getValue())) {
simpleNamespaceContext.bindNamespaceUri(binding.getKey(), binding.getValue());
}
}
//add explicit namespace bindings
simpleNamespaceContext.setBindings(namespaces);
} else {
simpleNamespaceContext.setBindings(dynamicBindings);
}
return simpleNamespaceContext;
}