
import java.io.File;
import java.util.HashMap;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.jdom2.input.SAXBuilder;


public class TagCounter extends DefaultHandler {

    private HashMap<String,Integer> counts = new HashMap<String, Integer>();

    /*public void countElements(Element e){
        String name = e.getName();

        if(counts.containsKey(name)){
            counts.put(name,counts.get(name)+1);
        } else {
            counts.put(name,1);
        }

        List<Element> elems = e.getChildren();
        for(int i=0;i<elems.size();i++){
            countElements(elems.get(i));
        }
    }
    public void main(String[] args) {
        System.out.println("Hello world!");

        File configFile = new File( "./imports/bme.xml");

        if (configFile.exists()) {
            try {
                SAXBuilder saxBuilder = new SAXBuilder();
                Document document = saxBuilder.build(configFile);

                Element rootElement = document.getRootElement();

                countElements(rootElement);

                printMap();

            } catch (Exception e) {
                System.err.println("Error reading the file: " + e.getMessage());
                e.printStackTrace();
            }
        } else {
            System.out.println("File does not exist: config.xml");
        }
    }*/

    public void printMap(){
        for(String key: counts.keySet()){
            System.out.println(key+" : " +counts.get(key)+"\n---");
        }
    }

    @Override
    public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if(counts.containsKey(qName)){
            counts.put(qName,counts.get(qName)+1);
        } else {
            counts.put(qName,1);
        }
    }
}