package sax;
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
//import cz.XmlTester.TestJava;

/**
 *
 * @author Tomáš Dlouhý
 */
public class TestSax {
    static String fileName="../data.xml";
            
    public static void run() {
		try {
			XMLReader parser = XMLReaderFactory.createXMLReader();
			InputSource source = new InputSource(fileName);
			parser.setContentHandler(new TContentHandler());
			parser.parse(source);
		} catch (Exception e) {
			    System.err.println("File cannot be loaded");
		}                
        
    }
}
class TContentHandler implements ContentHandler {
	Locator locator;
        String textyear;
        String search="2008";
        int count = 0;

	public void characters(char[] ch, int start, int length) throws SAXException {
              /* StringBuffer SB = (new StringBuffer().append(ch, start, length));
               textyear=SB.toString();
               */

		String text = new StringBuffer().append(ch, start, length).toString();
		text = text.replace('\n', ' ');					
		textyear=text;
	}

	public void startDocument() throws SAXException {
	}

	public void endDocument() throws SAXException {
		System.out.println("Pocet Filmu vyrobenych v roce \""+search +"\" v databazi je: " + count);
	}

	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		
              /*if (localName.startsWith("movie")) {                 
                 if (textyear=="") { //textyear.startsWith(search))  { 
                    count += 1;
                 }
              } */

		// ziskani jmena elementu
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// ziskani jmena elementu
		if (localName.startsWith("year")) {
			  if (textyear.startsWith(search)) {
			      count += 1;
			  } 
		}
	}

	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub
	}

	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
	}

	public void processingInstruction(String target, String data)
			throws SAXException {
		// TODO Auto-generated method stub
	}

	public void setDocumentLocator(Locator locator) {
		this.locator = locator;
	}

	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub
	}

	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
		// TODO Auto-generated method stub
	}

}

