View Javadoc
1   package org.kuali.ole.batch.marc;
2   
3   import org.marc4j.ErrorHandler.Error;
4   import org.marc4j.RecordStack;
5   import org.marc4j.marc.Record;
6   import org.xml.sax.InputSource;
7   import org.marc4j.ErrorHandler;
8   
9   
10  import java.io.InputStream;
11  import java.util.List;
12  import java.util.concurrent.ExecutionException;
13  import java.util.concurrent.ExecutorService;
14  import java.util.concurrent.Executors;
15  import java.util.concurrent.Future;
16  
17  /**
18   * Created with IntelliJ IDEA.
19   * User: meenrajd
20   * Date: 9/2/13
21   * Time: 11:57 AM
22   * To change this template use File | Settings | File Templates.
23   */
24  public class OLEMarcXmlReader implements OLEMarcReader {
25  
26      private RecordStack queue;
27      private ErrorHandler errors;
28      private int errCount;
29  
30      public OLEMarcXmlReader(InputSource input) throws ExecutionException, InterruptedException {
31          this.queue = new RecordStack();
32          this.errors = new OLEMarcErrorHandler();
33          /*ExecutorService executor = Executors.newSingleThreadExecutor();
34          //OLEMarcXmlParserThread producer = new OLEMarcXmlParserThread(queue, input, errors);
35          Runnable worker = new OLEMarcXmlParserThread(queue, input, errors);
36          Future future = executor.submit(worker);
37          if(future.get()==null){
38              executor.shutdown();
39          }*/
40          OLEMarcXmlParserThread producer = new OLEMarcXmlParserThread(queue, input, errors);
41          producer.start();
42      }
43  
44      public OLEMarcXmlReader(InputStream input) throws ExecutionException, InterruptedException {
45          this(new InputSource(input));
46      }
47  
48      /**
49       * Returns true if the iteration has more records, false otherwise.
50       *
51       * @return boolean - true if the iteration has more records, false otherwise
52       */
53      public boolean hasNext() {
54          return queue.hasNext();
55      }
56  
57      /**
58       * Returns the next record in the iteration.
59       *
60       * @return Record - the record object
61       */
62      public Record next() {
63          return queue.pop();
64      }
65  
66      @Override
67      public boolean hasErrors() {
68          return errors.hasErrors();
69      }
70  
71      @SuppressWarnings("unchecked")
72      @Override
73      public List<Error> getErrors() {
74          return errors.getErrors();
75      }
76  
77      @Override
78      public Error getError() {
79          return (Error) errors.getErrors().get(0);
80      }
81  
82      public void clearErrors(){
83         errors.getErrors().clear();
84         OLEMarcErrorHandler errorHandler = (OLEMarcErrorHandler)errors;
85          errorHandler.getErrorMap().clear();
86      }
87  
88  }