001package org.kuali.ole.batch.marc;
002
003import org.marc4j.ErrorHandler.Error;
004import org.marc4j.RecordStack;
005import org.marc4j.marc.Record;
006import org.xml.sax.InputSource;
007import org.marc4j.ErrorHandler;
008
009
010import java.io.InputStream;
011import java.util.List;
012import java.util.concurrent.ExecutionException;
013import java.util.concurrent.ExecutorService;
014import java.util.concurrent.Executors;
015import java.util.concurrent.Future;
016
017/**
018 * Created with IntelliJ IDEA.
019 * User: meenrajd
020 * Date: 9/2/13
021 * Time: 11:57 AM
022 * To change this template use File | Settings | File Templates.
023 */
024public class OLEMarcXmlReader implements OLEMarcReader {
025
026    private RecordStack queue;
027    private ErrorHandler errors;
028    private int errCount;
029
030    public OLEMarcXmlReader(InputSource input) throws ExecutionException, InterruptedException {
031        this.queue = new RecordStack();
032        this.errors = new OLEMarcErrorHandler();
033        /*ExecutorService executor = Executors.newSingleThreadExecutor();
034        //OLEMarcXmlParserThread producer = new OLEMarcXmlParserThread(queue, input, errors);
035        Runnable worker = new OLEMarcXmlParserThread(queue, input, errors);
036        Future future = executor.submit(worker);
037        if(future.get()==null){
038            executor.shutdown();
039        }*/
040        OLEMarcXmlParserThread producer = new OLEMarcXmlParserThread(queue, input, errors);
041        producer.start();
042    }
043
044    public OLEMarcXmlReader(InputStream input) throws ExecutionException, InterruptedException {
045        this(new InputSource(input));
046    }
047
048    /**
049     * Returns true if the iteration has more records, false otherwise.
050     *
051     * @return boolean - true if the iteration has more records, false otherwise
052     */
053    public boolean hasNext() {
054        return queue.hasNext();
055    }
056
057    /**
058     * Returns the next record in the iteration.
059     *
060     * @return Record - the record object
061     */
062    public Record next() {
063        return queue.pop();
064    }
065
066    @Override
067    public boolean hasErrors() {
068        return errors.hasErrors();
069    }
070
071    @SuppressWarnings("unchecked")
072    @Override
073    public List<Error> getErrors() {
074        return errors.getErrors();
075    }
076
077    @Override
078    public Error getError() {
079        return (Error) errors.getErrors().get(0);
080    }
081
082    public void clearErrors(){
083       errors.getErrors().clear();
084       OLEMarcErrorHandler errorHandler = (OLEMarcErrorHandler)errors;
085        errorHandler.getErrorMap().clear();
086    }
087
088}