/*
 * Created on 2004-09-04
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.skins;

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Reads the skin.xml and extracts the useful data
 * 
 * @author mikolaj
 */
public class SkinXmlReader extends DefaultHandler {

    /**
     *  The name of the skin extreacted from the file
     */
    public String fName;
    
    /**
     *  Required width and height of the screen extracted from the file 
     */
    public int fScreenWidth, fScreenHeight;
    
    public SkinXmlReader() {
        super();
    }
    
    Locator fLocator = null;

	public void setDocumentLocator(Locator locator) {
		fLocator = locator;  
	}
    
	protected int getNumber(String x, String attr, String elem)
            throws SAXParseException {
        int ret;
        try {
            if (x==null)
                throw new NumberFormatException();
            ret=Integer.parseInt(x);
        } catch (NumberFormatException e) {
            throw new SAXParseException("Expected a number attribute " + attr
                    + " of element " + elem, fLocator);
        }
        return ret;
    }    
    
	public void startElement(String uri, String localName,
            String qName, Attributes attributes)
     		throws SAXException
	{
	    fLevel++;
	    if (fLevel==2)
	        fMainTag=qName;
	    
	    if (qName.equals("skin"))
	        if (fLevel==1)
	            fName=attributes.getValue("name");
	    
	    if (qName.equals("screen") && fMainTag!=null &&
	            fMainTag.equals("requires"))
	    {
	        fScreenWidth=getNumber(attributes.getValue("width"), "width", "screen");
	        fScreenHeight=getNumber(attributes.getValue("height"), "height", "screen");
	    }
	}
	
    public void endElement(String uri, String localName, String qName)
    throws SAXException {
        fLevel--;
        if (fLevel==0)
            fMainTag=null;
    }
    
    int fLevel;
    String fMainTag;
}
