/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.checks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import de.hft.stuttgart.citydoctor2.check.error.SchematronError;

/**
 * This is the content handler for parsing the Svrl xml files created by a
 * Schematron run. They are parsed and error objects are created and possibly
 * associated to GML-IDs.
 * 
 * @author Matthias Betz
 *
 */
public class SvrlContentHandler implements ContentHandler {

	private boolean nextElementIsErrorText = false;
	private boolean nextIsTextContent = false;

	private StringBuilder buffer;

	private Map<String, SchematronError> featureErrors;
	private List<SchematronError> generalErrors;

	public SvrlContentHandler() {
		featureErrors = new HashMap<>();
		generalErrors = new ArrayList<>();
	}

	public Map<String, SchematronError> getFeatureErrors() {
		return featureErrors;
	}

	public List<SchematronError> getGeneralErrors() {
		return generalErrors;
	}

	@Override
	public void setDocumentLocator(Locator locator) {
		// not needed
	}

	@Override
	public void startDocument() throws SAXException {
		// not needed
	}

	@Override
	public void endDocument() throws SAXException {
		// not needed
	}

	@Override
	public void startPrefixMapping(String prefix, String uri) throws SAXException {
		// not needed
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// not needed
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
		if ("failed-assert".equals(localName)) {
			nextElementIsErrorText = true;
		} else if (nextElementIsErrorText && "text".equals(localName)) {
			nextIsTextContent = true;
			buffer = new StringBuilder();
			nextElementIsErrorText = false;
		} else {
			nextElementIsErrorText = false;
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		// not needed
		if (nextIsTextContent && "text".equals(localName)) {
			String text = buffer.toString();
			int index = text.indexOf(": ");
			if (index == -1) {
				// general error
				generalErrors.add(new SchematronError(text));
			} else {
				String gmlId = text.substring(0, index);
				featureErrors.put(gmlId, new SchematronError(text));
			}
			buffer = null;
			nextIsTextContent = false;
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		if (nextIsTextContent) {
			buffer.append(ch, start, length);
		}
	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
		// not needed
	}

	@Override
	public void processingInstruction(String target, String data) throws SAXException {
		// not needed
	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// not needed
	}

}
