Example usage for org.w3c.dom.traversal NodeIterator NodeIterator

List of usage examples for org.w3c.dom.traversal NodeIterator NodeIterator

Introduction

In this page you can find the example usage for org.w3c.dom.traversal NodeIterator NodeIterator.

Prototype

NodeIterator

Source Link

Usage

From source file:org.alfresco.repo.template.XSLTProcessorMethodInvoker.java

public Object invokeMethod(final String id, Object[] arguments) throws Exception {
    if (!PROCESSOR_METHODS.containsKey(id)) {
        throw new NullPointerException("unable to find method " + id);
    }/*  w ww  .  jav a2  s.c  o  m*/

    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());
    }
}