package manipsax;
import java.io.*;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 *
 * @author Tomáš Dlouhý
 */
public class saxmanip {
    static String fileName;

    public static void main(String[] args) {
        if (args.length < 2) {
           ShowHelp(); 
        } else {
           String input=args[0];
           String output="null";
           if (args.length >3) { output = args[2]; }
           if ( input.startsWith("-in")) {
                File fl = new File(args[1]);
               if (fl.exists() == true)  {
                  fileName = fl.getName();
                  RunProgram();
               } else { System.err.println("Error: Manipdom: inputfile file no found!");}
           }
        }
        
    }
    
    public static void ShowHelp() {
        System.out.println("Error: Run.sh for Manipsax: No valid syntax");
        System.out.println("    Required format: ");
        System.out.println("        run.sh -in <inputfile.xml>");
    }
        
    public static void RunProgram() {
		try {
			XMLReader parser = XMLReaderFactory.createXMLReader();
			InputSource source = new InputSource(fileName);
			parser.setContentHandler(new TContentHandler());
			parser.parse(source);
		} catch (Exception e) {

		}                
        
    }
}
class TContentHandler implements ContentHandler {
	Locator locator;
        String textyear;
        String search="Star";
        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();
               
	}

	public void startDocument() throws SAXException {
	}

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

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

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
	}

	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
	}

}

