/**
 * 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.data.index.ws.bbq;

import java.util.Stack;

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

/**
 * Collection profile SAX handler.
 * @author Marek Horst
 * @version 0.7.6
 *
 */
public class CollectionProfileHandler extends DefaultHandler {
	
	protected static final Logger log = Logger.getLogger(CollectionProfileHandler.class);
	
	public static final String RESOURCE_IDENTIFIER_EL_NAME 	= "RESOURCE_IDENTIFIER";
	public static final String HEADER_EL_NAME				= "HEADER";
	public static final String RETRIEVAL_CONDITION_EL_NAME	= "RETRIEVAL_CONDITION";
	public static final String STATUS_EL_NAME				= "STATUS";
	
	/**
	 * Collection name is resource identifier.
	 */
	String collectionName = null;
	
	/**
	 * BBQuery is RETRIEVAL_CONDITION element content.
	 */
	String bbQuery = null;
	
	/**
	 * Stack of tag names.
	 */
	Stack<String> tagStack;
	
	/**
	 * BBQ element marker.
	 */
	boolean bbQueryElementFlag = false;
	
	/**
	 * Tag level. 0 means root.
	 */
	int tagLevel;
	
	/**
	 * Default constructor.
	 */
	public CollectionProfileHandler() {
		tagStack = new Stack<String>();
	}
	
	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#startDocument()
	 */
	public void startDocument() {
		tagLevel = 0;
		tagStack.clear();
		
		bbQueryElementFlag = false;
		
		collectionName = null;
		bbQuery = null;
	}
	
	/* (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 {
		if (isResIdElement(qName)) {
			collectionName = atts.getValue("value");
		} else if (isRetrievalCondElement(qName)) {
			bbQueryElementFlag = true;
		}
		tagLevel++;
		tagStack.push(qName);
	}
	
	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
	 */
	public void characters(char[] chars, int startIndex, int length) throws SAXException {
		if (bbQueryElementFlag) {
			if (bbQuery==null)
				bbQuery = new String(chars, startIndex, length);
			else
				bbQuery = bbQuery + 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 {
		tagLevel--;
		tagStack.pop();
		if (isRetrievalCondElement(qName))
			bbQueryElementFlag = false;
	}

	boolean isResIdElement(String qName) {
		if (tagLevel==2
				&& tagStack.peek().equalsIgnoreCase(HEADER_EL_NAME)
				&& qName.equalsIgnoreCase(RESOURCE_IDENTIFIER_EL_NAME))
			return true;
		else
			return false;
	}
	
	boolean isRetrievalCondElement(String qName) {
		if(tagLevel==3
				&& tagStack.peek().equalsIgnoreCase(STATUS_EL_NAME)
				&& qName.equalsIgnoreCase(RETRIEVAL_CONDITION_EL_NAME))
			return true;
		else
			return false;
	}
	
	/**
	 * Returns retrieved collection name.
	 * @return retrieved collection name
	 */
	public String getCollectionName() {
		return collectionName;
	}

	/**
	 * Returns retrieved bbQuery.
	 * @return retrieved bbQuery
	 */
	public String getBbQuery() {
		return bbQuery;
	}
}
