import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;

import java.io.*;

public class SearchVisitor extends Visitor {

	private Element element = null;
	private int nth = 1; // find n-th element
	private int count = 1;
	private boolean success = false;

   public void setNth(int n) { nth = n; }
   public int getNth() { return nth; }
   public void reset() { count = 1; success = false; }
   public int getCount() { return count; }
   public boolean isSuccess() { return success; }
   public void resetSuccess() { success = false; }
	public Element getElement() { return element; }
   // override in extension:
   protected boolean test(Element node) {
		return false;
	}

   public void visit(Element node) throws VisitorException {
	   if (test(node) && count++ == nth) {
			success = true;
			element = node;
         throw new VisitorException("node found");
		}
	}
}