}
public class Rename implements XPathFunction {
public Object evaluate(List args) throws XPathFunctionException {
if (args.size() < 2)
throw new XPathFunctionException(new FaultException(new QName(Namespaces.ODE_EXTENSION_NS, "renameInvalidSource"), "Invalid arguments"));
if (__log.isDebugEnabled()) {
__log.debug("rename call(context=" + _ectx + " args=" + args + ")");
}
Element targetElmt;
QName elementQName = null, elementTypeQName = null;
try {
if (args.get(0) instanceof List) {
List elmts = (List) args.get(0);
if (elmts.size() != 1) throw new XPathFunctionException(
new FaultException(_oxpath.getOwner().constants.qnSelectionFailure,
"The bpws:rename function MUST be passed a single " +
"element node."));
targetElmt = (Element) elmts.get(0);
} else if (args.get(0) instanceof NodeWrapper) {
targetElmt = (Element) ((NodeWrapper) args.get(0)).getUnderlyingNode();
} else if (args.get(0) instanceof Element) {
targetElmt = (Element) args.get(0);
} else {
throw new XPathFunctionException("Unexpected argument type: "+args.get(0).getClass());
}
String localName = null, namespaceUri = null, prefix = null;
if (args.get(1) instanceof QNameValue) {
QNameValue qNameValue = (QNameValue) args.get(1);
namespaceUri = qNameValue.getNamespaceURI();
localName = qNameValue.getLocalName();
prefix = qNameValue.getPrefix();
} else if (args.get(1) instanceof List) {
List elmts = (List) args.get(1);
if (elmts.size() != 1) throw new XPathFunctionException(
new FaultException(_oxpath.getOwner().constants.qnSelectionFailure,
"The bpws:rename function MUST be passed a single " +
"element node."));
Element nameElmt = (Element) elmts.get(0);
namespaceUri = nameElmt.getNamespaceURI();
localName = nameElmt.getLocalName();
prefix = nameElmt.getPrefix();
} else if (args.get(1) instanceof NodeWrapper) {
Element nameElmt = (Element) ((NodeWrapper) args.get(1)).getUnderlyingNode();
namespaceUri = nameElmt.getNamespaceURI();
localName = nameElmt.getLocalName();
prefix = nameElmt.getPrefix();
} else if (args.get(1) instanceof Element) {
Element nameElmt = (Element) args.get(1);
namespaceUri = nameElmt.getNamespaceURI();
localName = nameElmt.getLocalName();
prefix = nameElmt.getPrefix();
} else if (args.get(1) instanceof String) {
String qName = (String) args.get(1);
if (qName.contains(":")) {
int index = qName.indexOf(":");
prefix = qName.substring(0, index);
localName = qName.substring(index + 1);
} else {
localName = qName;
}
} else if (args.get(1) instanceof QName) {
QName qName = (QName) args.get(1);
namespaceUri = qName.getNamespaceURI();
localName = qName.getLocalPart();
prefix = qName.getPrefix();
} else {
throw new XPathFunctionException("Unexpected argument type: "+args.get(1).getClass());
}
if (namespaceUri == null) {
namespaceUri = targetElmt.lookupNamespaceURI(prefix);
}
elementQName = new QName(namespaceUri, localName, prefix);
if (args.size() > 2) {
if (args.get(2) instanceof QNameValue) {
QNameValue qNameValue = (QNameValue) args.get(2);
namespaceUri = qNameValue.getNamespaceURI();
localName = qNameValue.getLocalName();
prefix = qNameValue.getPrefix();
} else if (args.get(2) instanceof NodeWrapper) {
Element nameElmt = (Element) ((NodeWrapper) args.get(2)).getUnderlyingNode();
namespaceUri = nameElmt.getNamespaceURI();
localName = nameElmt.getLocalName();
prefix = nameElmt.getPrefix();
} else if (args.get(2) instanceof Element) {
Element nameElmt = (Element) args.get(2);
namespaceUri = nameElmt.getNamespaceURI();
localName = nameElmt.getLocalName();
prefix = nameElmt.getPrefix();
} else if (args.get(2) instanceof String) {
String qName = (String) args.get(2);
if (qName.contains(":")) {
int index = qName.indexOf(":");
prefix = qName.substring(0, index);
localName = qName.substring(index + 1);
} else {
localName = qName;
}
} else {
throw new XPathFunctionException("Unexpected argument type: "+args.get(2).getClass());
}
if (namespaceUri == null) {
namespaceUri = targetElmt.lookupNamespaceURI(prefix);
}
elementTypeQName = new QName(namespaceUri, localName, prefix);;
}
} catch (IllegalArgumentException e) {
throw new XPathFunctionException(
new FaultException(_oxpath.getOwner().constants.qnInvalidExpressionValue,
"Invalid argument: URI Template expected. " + args.get(0), e));
} catch (ClassCastException e) {
throw new XPathFunctionException(
new FaultException(_oxpath.getOwner().constants.qnSelectionFailure,
"The bpws:rename function MUST be passed a single " +
"element node."));
}
Element parentElmt = (Element) targetElmt.getParentNode();
NodeList children = parentElmt.getChildNodes();