use of org.w3c.dom.traversal.NodeFilter in project alfresco-repository by Alfresco.
the class XSLTProcessorMethodInvoker method invokeMethod.
public Object invokeMethod(final String id, Object[] arguments) throws Exception {
if (!PROCESSOR_METHODS.containsKey(id)) {
throw new NullPointerException("unable to find method " + id);
}
final TemplateProcessorMethod method = PROCESSOR_METHODS.get(id);
arguments = this.convertArguments(arguments);
log.debug("invoking " + id + " with " + arguments.length);
Object result = method.exec(arguments);
log.debug(id + " returned a " + result);
if (result == null) {
return null;
} else if (result.getClass().isArray() && Node.class.isAssignableFrom(result.getClass().getComponentType())) {
log.debug("converting " + result + " to a node iterator");
final Node[] array = (Node[]) result;
return new NodeIterator() {
private int index = 0;
private boolean detached = false;
public void detach() {
if (log.isDebugEnabled())
log.debug("detaching NodeIterator");
this.detached = true;
}
public boolean getExpandEntityReferences() {
return true;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
public Node getRoot() {
return (array.length == 0 ? null : array[0].getOwnerDocument().getDocumentElement());
}
public NodeFilter getFilter() {
return new NodeFilter() {
public short acceptNode(final Node n) {
return NodeFilter.FILTER_ACCEPT;
}
};
}
public Node nextNode() throws DOMException {
if (log.isDebugEnabled())
log.debug("NodeIterator.nextNode(" + index + ")");
if (this.detached)
throw new DOMException(DOMException.INVALID_STATE_ERR, null);
return index == array.length ? null : array[index++];
}
public Node previousNode() throws DOMException {
if (log.isDebugEnabled())
log.debug("NodeIterator.previousNode(" + index + ")");
if (this.detached)
throw new DOMException(DOMException.INVALID_STATE_ERR, null);
return index == -1 ? null : array[index--];
}
};
} else if (result instanceof String || result instanceof Number || result instanceof Node) {
log.debug("returning " + result + " as is");
return result;
} else {
throw new IllegalArgumentException("unable to convert " + result.getClass().getName());
}
}
Aggregations