package com.wrycan.testcase;


import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl;

import java.io.IOException;
import org.xml.sax.InputSource;



public class Thing2Filter extends XMLFilterImpl{

    private boolean inMatch = false;
    private int elementLocator;
    private boolean doneMatching = false;

    public Thing2Filter(XMLReader parent){
        super(parent);
    }

    @Override
    public void startDocument() throws SAXException{
        doneMatching = false;
        super.startDocument();
    }

    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes attrs) throws SAXException
    {
        if (localName.equals("thing2") && !doneMatching) { // start matching when the first thing2 is hit
            inMatch = true;
        }

        if(inMatch) {
            super.startElement(namespaceURI, localName, qName, attrs);
        }
    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException
    {
        if(inMatch) {
            super.endElement(namespaceURI, localName, qName);
        }
        if (localName.equals("thing2")) { // match is over once end of first thing2 is hit
            inMatch = false;
            doneMatching = true;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if(inMatch) {
            super.characters(ch, start, length);
        }
    }

    @Override
    public void startPrefixMapping(java.lang.String prefix, java.lang.String uri) throws SAXException{
        super.startPrefixMapping(prefix, uri);
    }

    @Override
    public void endPrefixMapping(java.lang.String prefix) throws SAXException{
        super.endPrefixMapping(prefix);
    }

}
