package eu.dnetlib.functionality.recommendation.parser;

import org.xml.sax.*;

import eu.dnetlib.functionality.recommendation.domain.Document;
import org.xml.sax.helpers.DefaultHandler;

public class DriverDocumentHandler extends DefaultHandler {

    private String idField;
    private String authorField;
    private String titleField;

    /**
     * @return the id_field
     */
    public String getIdField() {
        return idField;
    }

    /**
     * @param aId_field the id_field to set
     */
    public void setIdField( String aIdField ) {
        idField = aIdField;
    }

    /**
     * @return the author_field
     */
    public String getAuthorField() {
        return authorField;
    }

    /**
     * @param aAuthor_field the author_field to set
     */
    public void setAuthorField( String aAuthorField ) {
        authorField = aAuthorField;
    }

    /**
     * @return the title_field
     */
    public String getTitleField() {
        return titleField;
    }

    /**
     * @param aTitle_field the title_field to set
     */
    public void setTitleField( String aTitleField ) {
        titleField = aTitleField;
    }
    
    private Document doc = new Document();
    private String currentElement = null;

    public Document getDocument() {
        return getDoc();
    }

    
    @Override
    public void startElement (String uri, String name, String qName, Attributes atts) {
        //System.out.println( "name is " + name );
        //System.out.println( "parameters are is " + idField + " " + authorField + " " + titleField );
        if ( name.equalsIgnoreCase( getIdField()) ) {
            currentElement = getIdField();
        } else if ( name.equalsIgnoreCase( getAuthorField()) ) {
            currentElement = getAuthorField();
        } else if ( name.equalsIgnoreCase( getTitleField()) ) {
            currentElement = getTitleField();
        } else {
            currentElement = null;
        }
    }

    
    @Override
    public void endElement (String uri, String name, String qName) {
        if ( name.equalsIgnoreCase( getIdField()) || name.equalsIgnoreCase( getAuthorField()) || name.equalsIgnoreCase( getTitleField()) ) {
            currentElement = null;
        }
    }
    
    @Override
    public void characters( char ch[], int start, int length ) {
        //System.out.println( "Current field is " + currentElement );
        if( currentElement == null )
            return;
        String value = new String( ch, start, length );
        if ( !value.trim().equals( "" ) ) {
            if ( currentElement.equalsIgnoreCase( getIdField()) ) {
                this.doc.setId( value );
            } else if ( currentElement.equalsIgnoreCase( getAuthorField()) ) {
                this.doc.setAuthor( value );
            } else if ( currentElement.equalsIgnoreCase( getTitleField()) ) {
                this.doc.setTitle( value );
            }
        }
    }

    /**
     * @return the doc
     */
    public Document getDoc() {
        return doc;
    }
}