001 package edu.nrao.sss.model.source.parser; 002 003 import java.io.Reader; 004 import java.io.IOException; 005 006 import edu.nrao.sss.model.source.SourceCatalog; 007 008 /** 009 * A parser of source catalog text files in the Green Bank Telescope's format. 010 * See the <a href="http://wiki.gb.nrao.edu/bin/view/Data/SourceCatalogUserDoc"> 011 * Users Guide for Astrid Source Catalogs</a> for details on this format. 012 * <p> 013 * <b>Version Info:</b> 014 * <table style="margin-left:2em"> 015 * <tr><td>$Revision: 757 $</td></tr> 016 * <tr><td>$Date: 2007-07-06 14:13:35 -0600 (Fri, 06 Jul 2007) $</td></tr> 017 * <tr><td>$Author: dharland $ (last person to modify)</td></tr> 018 * </table></p> 019 * 020 * @author David M. Harland 021 * @since 2006-11-17 022 */ 023 public class GbtSourceCatalogReader 024 extends AbstractSourceCatalogReader 025 { 026 private GbtSourceHeaderReader headerReader; 027 028 /** Creates a new instance. */ 029 public GbtSourceCatalogReader() 030 { 031 headerReader = new GbtSourceHeaderReader(); 032 } 033 034 /** Prepares this instance to read from a new source. */ 035 private void reset(Reader in, SourceCatalog destination) 036 { 037 readWasSuccessful = false; 038 039 catalog = (destination == null) ? new SourceCatalog() : destination; 040 041 headerReader.reset(in); 042 } 043 044 /** 045 * NOTE: This method closes the incoming Reader! 046 * 047 * @see AbstractSourceCatalogReader#read(java.io.Reader, 048 * edu.nrao.sss.model.source.SourceCatalog) 049 */ 050 public boolean read(Reader in, SourceCatalog destination) 051 { 052 reset(in, destination); 053 054 //Unless we KNOW this is not a GBT catalog, try to parse it 055 if (headerReader.verifyCatalogType()) 056 { 057 //Delegate most of the work to source parsers. 058 GbtSourceReader sourceReader = headerReader.createSourceReader(); 059 060 //Keep reading until we reach end of stream 061 while (!headerReader.done) 062 { 063 //The source reader may have delegated to another source reader. 064 //Here we ensure that we're holding the reader actually used to 065 //populate the source. 066 sourceReader = sourceReader.read(headerReader); 067 068 //Don't use the getSource method because it never returns null 069 if (sourceReader.source != null) 070 { 071 if (sourceInfoOrigin != null) 072 sourceReader.source.setOriginOfInformation(sourceInfoOrigin); 073 074 catalog.addItem(sourceReader.source); 075 } 076 } 077 } 078 else 079 { 080 putError(headerReader.dataReader.getLineNumber(), 081 "This catalog is not a GBT source catalog."); 082 } 083 084 try 085 { 086 in.close(); 087 } 088 catch(IOException ioe) 089 { 090 putError(0, "Error closing Reader: " + ioe.getMessage()); 091 } 092 093 readWasSuccessful = headerReader.getErrorCount() == 0; 094 095 return readWasSuccessful; 096 } 097 098 /** 099 * @see SourceCatalogReader#getErrors() 100 */ 101 public StringBuilder getErrors() 102 { 103 return headerReader.getErrors().append(super.getErrors()); 104 } 105 106 /** 107 * @see SourceCatalogReader#getError(int) 108 */ 109 public String getError(int index) 110 { 111 String error; 112 113 int headerErrCount = headerReader.getErrorCount(); 114 115 if (index < headerErrCount) 116 { 117 error = headerReader.getError(index); 118 } 119 else 120 { 121 try { 122 error = super.getError(index - headerErrCount); 123 } 124 catch (IndexOutOfBoundsException ex) { 125 //The caught exception will be based on the indices 126 //of super, which must be translated to our own range before thrown 127 //to clients. 128 throw new IndexOutOfBoundsException("Index " + index + 129 " is not valid it must be >= 0 and < " + getErrorCount()); 130 } 131 } 132 133 return error; 134 } 135 136 /** 137 * @see SourceCatalogReader#getErrorCount() 138 */ 139 public int getErrorCount() 140 { 141 return headerReader.getErrorCount() + super.getErrorCount(); 142 } 143 144 //============================================================================ 145 // 146 //============================================================================ 147 /* 148 public static void main(String[] args) throws Exception 149 { 150 GbtSourceCatalogReader catReader = new GbtSourceCatalogReader(); 151 152 for (String urlText : args) 153 { 154 URL inputFile = new URL(urlText); 155 156 if (!catReader.read(inputFile.openStream())) 157 { 158 System.out.println("Trouble reading catalog from URL " + urlText); 159 System.out.println(catReader.getErrors().toString()); 160 } 161 162 try 163 { 164 SourceCatalog catalog = catReader.getCatalog(); 165 File tempFile = File.createTempFile(catalog.getName(), ".txt"); 166 FileWriter writer = new FileWriter(tempFile); 167 catalog.writeAsXmlTo(writer); 168 169 System.out.println("Wrote catalog '"+catalog.getName()+ 170 "' as XML to file " + tempFile.getAbsolutePath()); 171 } 172 catch (Exception ex) 173 { 174 System.out.println("Problem processing catalog from URL " + urlText); 175 System.out.println(". Original msg: " + ex.toString()); 176 System.out.println(); 177 System.out.println("Moving on to next catalog."); 178 } 179 } 180 } 181 */ 182 }