001    package edu.nrao.sss.model.source.parser;
002    
003    import java.io.IOException;
004    import java.io.LineNumberReader;
005    import java.io.Reader;
006    
007    import edu.nrao.sss.model.source.SourceCatalog;
008    
009    /**
010     */
011    public class PdBISourceCatalogReader
012      extends AbstractSourceCatalogReader
013    {
014      private PdBISourceReader sourceReader;
015      private LineNumberReader dataReader;
016      
017      /** Creates a new instance. */
018      public PdBISourceCatalogReader()
019      {
020        sourceReader = new PdBISourceReader();
021      }
022      
023      /** Prepares this instance to read from a new source. */
024      private void reset(Reader in, SourceCatalog destination)
025      {
026        catalog = (destination == null) ? new SourceCatalog() : destination;
027    
028        readWasSuccessful = false;
029        errors.clear();
030    
031        dataReader = new LineNumberReader(in);
032      }
033    
034      //============================================================================
035      // INTERFACE SourceCatalogReader
036      //============================================================================
037      
038      /**
039             * NOTE: This method closes the incoming Reader!
040             *
041       * @see AbstractSourceCatalogReader#read(java.io.Reader,
042       *      edu.nrao.sss.model.source.SourceCatalog)
043       */
044      public boolean read(Reader in, SourceCatalog destination)
045      {
046        reset(in, destination);
047        
048        String line = getNextLine();
049    
050        while (line != null)
051        {
052          if (sourceReader.textCouldBeData(line))
053          {
054            if (!sourceReader.read(line, dataReader.getLineNumber(), histRecPrefix))
055              errors.addAll(sourceReader.errors);
056            
057            if (sourceReader.source != null)
058            {
059              if (sourceInfoOrigin != null)
060                sourceReader.source.setOriginOfInformation(sourceInfoOrigin);
061    
062              catalog.addItem(sourceReader.source);
063            }
064          }
065          
066          line = getNextLine();
067        }
068        
069                    try
070                    {
071                            this.dataReader.close();
072                    }
073    
074                    catch(IOException ioe)
075                    {
076                            putError(0, "Error closing Reader: " + ioe.getMessage());
077                    }
078    
079        readWasSuccessful = getErrorCount() == 0;
080        
081        return readWasSuccessful;
082      }
083    
084      //============================================================================
085      // 
086      //============================================================================
087      
088      /** Reads lines, returning first one that does not cause an error. */
089      private String getNextLine()
090      {
091        int attempt = 0;
092        
093        while (++attempt <= 100)
094        {
095          try {
096            return dataReader.readLine();
097          }
098          catch (IOException ex) {
099            putError("I/O Exception: " + ex.getMessage());
100          }
101        }
102        
103        throw new RuntimeException("Failed " + attempt +
104                                   " times while attempting to read a line.");
105      }
106      
107      /** Adds a new parsing error to our list. */
108      private void putError(String message)
109      {
110        putError(dataReader.getLineNumber(), message);
111      }
112    }