@Override
    public void endElement(String uri, String localName, String qName) {
        // we need to find out if the element ending now corresponded
        // to matches in some stacks
        // first, get the pre number of the element that ends now:
        if (openNodesPreNumbers.isEmpty()) {
            // We have no open elements, ignore this element
            return;
        }
        // now look for Match objects having this pre number:
        //for (TPEStack stack : rootStack.getDescendantStacks()) {
        //    if (stack.getPatternNode().getName().equals(qName) && stack.top().getStatus() == Match.STATUS.OPEN && stack.top().getPre() == preOfLastOpen) {
        if (current.getName().equals(qName)) {
            int preOfLastOpen = openNodesPreNumbers.pop();
            TPEStack stack = current.getStack();
            // all descendants of this Match have been traversed by now.
            Match m = stack.pop();

            boolean incomplete = false;

            // check if valuePredicates are in the match
            if (current.getStack().getPatternNode().getValuePredicate() != null
                    && current.getStack().getPatternNode().getValuePredicate().equals(m.getTextValue())) {
                incomplete = true;
            }

            // check if m has child matches for all children
            // of its pattern node
            for (PatternNode pChild : current.getStack().getPatternNode().getChildren()) {
                // pChild is a child of the query node for which m was created
                Collection<Match> childMatches = m.getChildren().get(pChild);
                if (childMatches.isEmpty() && !pChild.isOptional()) {
                    // m lacks a child Match for the pattern node pChild
                    incomplete = true;
                    break;
                }
            }

            if (incomplete) {
                // we remove m from its Stack, detach it from its parent etc.
                if (m != rootMatch) {
                    m.getParent().removeChild(m);
                } else {
                    rootMatch = null;
                }
            }
            m.close();
            current = current.getParent();
        }
        //}
    }