package no.divvun.Analyzer.Communication;

import no.divvun.Analyzer.Objects.Analysis;
import no.divvun.Analyzer.Objects.Element;
import no.divvun.Analyzer.Objects.Reading;

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

public class AnalysisResponseParser extends DefaultHandler {
	ElementContainer container;
	
	public AnalysisResponseParser(ElementContainer container) {
		this.container = container;
	}
	
	public ElementContainer getElementContainer() {
		return container;
	}
	
	public void startElement (String namespaceURI, String localName, String blockName, Attributes attributes)
		throws SAXException
	{
		if(blockName.equalsIgnoreCase(XMLTags.TAG_WORD)) {
			Analysis analysis = new Analysis();
			String word = attributes.getValue("form");
			if (word != null) {
				analysis.setWord(word);
//				Element element = new Element(new Reading(Reading.Type.FORM, word));
//				container.getAnalysis().add(element);
			}
			container.add(analysis);
		}
		else if(blockName.equalsIgnoreCase(XMLTags.TAG_READING)) {
			String analysis = attributes.getValue("analysis");
			String lemma = attributes.getValue("lemma");
			
			if (analysis != null && lemma != null) {
				Element element = new Element(new Reading(Reading.Type.ANALYSIS, analysis));
				element.getReading().setNext(new Reading(Reading.Type.LEMMA, lemma));
				container.getAnalysis().add(element);
			}
		}
	}
}
