Search in sources :

Example 1 with NodeFilter

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());
    }
}
Also used : NodeIterator(org.w3c.dom.traversal.NodeIterator) DOMException(org.w3c.dom.DOMException) Node(org.w3c.dom.Node) NodeFilter(org.w3c.dom.traversal.NodeFilter)

Aggregations

DOMException (org.w3c.dom.DOMException)1 Node (org.w3c.dom.Node)1 NodeFilter (org.w3c.dom.traversal.NodeFilter)1 NodeIterator (org.w3c.dom.traversal.NodeIterator)1