/**
 * Copyright 2008-2009 DRIVER PROJECT (ICM UW)
 * Original author: Marek Horst
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dnetlib.common.ws.harv.handler;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import eu.dnetlib.common.ws.harv.FieldData;

/**
 * DMF handler for parsing DMF documents.
 * Note: supportedFields and identifierFieldName are case insensitive!
 * @author mhorst
 *
 */
public class DMFHandler extends DefaultHandler implements IDriverHandler {

	protected static final Logger log = Logger.getLogger(DMFHandler.class);
	
	Collection<String> supportedFields;
	
	List<FieldData> processedFields;
	
	FieldData currentField;
	
	/**
	 * Field name containing document identifier.
	 */
	String identifierFieldName;
	
	/**
	 * Field content of identifier.
	 */
	String identifierFieldContent;
	
	public DMFHandler(Collection<String> supportedFields,
			String identifierFieldName) {
		this.supportedFields = supportedFields;
		if (identifierFieldName==null)
			throw new InvalidParameterException("Identifier field name cannot be null!");
		this.identifierFieldName = identifierFieldName;
		if (!this.supportedFields.contains(this.identifierFieldName)) {
			this.supportedFields.add(this.identifierFieldName);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#startDocument()
	 */
	public void startDocument() {
		identifierFieldContent = null;
		currentField = null;
		processedFields = new ArrayList<FieldData>();
	}
	
	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	public void startElement(String namespaceURI, String localName,
            String qName, Attributes atts) throws SAXException {
		qName = qName.toLowerCase();
		if (supportedFields.contains(qName))
			currentField = new FieldData(qName,null);
	}
	
	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
	 */
	public void characters(char[] chars, int startIndex, int length) throws SAXException {
		if (currentField!=null) {
//			changed the way of parsing element content
//			long data is truncated, what causes more than 1 characters() method 
//			call for single element
			if (currentField.getFieldContent()==null)
				currentField.setFieldContent(new String(chars, 
						startIndex, length));
			else
				currentField.setFieldContent(currentField.getFieldContent()
						+ new String(chars, startIndex, length));
		}
	}
	
	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void endElement(String namespaceURI, String localName,
            String qName) throws SAXException {
		if (currentField!=null) {
			if (currentField.getFieldContent()!=null) {
				if (identifierFieldName.equals(currentField.getFieldName()))
					identifierFieldContent = currentField.getFieldContent();
				processedFields.add(currentField);
			}
			currentField = null;
		}
	}
	

	/* (non-Javadoc)
	 * @see eu.dnetlib.data.index.ws.harv.handler.IDriverHandler#getProcessedFields()
	 */
	public List<FieldData> getProcessedFields() {
		return processedFields;
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.data.index.ws.harv.handler.IDriverHandler#getIdentifierFieldContent()
	 */
	public String getIdentifierFieldContent() {
		return identifierFieldContent;
	}
}
