001 package edu.nrao.sss.model.source.parser; 002 003 import java.io.Writer; 004 import edu.nrao.sss.model.source.SourceCatalog; 005 006 /** 007 * A parser of source catalogs that are expressed in XML. 008 * <p> 009 * <b>Version Info:</b> 010 * <table style="margin-left:2em"> 011 * <tr><td>$Revision: 714 $</td></tr> 012 * <tr><td>$Date: 2007-06-13 10:36:05 -0600 (Wed, 13 Jun 2007) $</td></tr> 013 * <tr><td>$Author: btruitt $</td></tr> 014 * </table></p> 015 * 016 * @author David M. Harland 017 * @since 2007-05-17 018 */ 019 public class XmlSourceCatalogWriter 020 extends AbstractSourceCatalogWriter 021 { 022 private Exception writeError; 023 024 /** 025 * NOTE: This method closes the incoming Writer! 026 * 027 * @see AbstractSourceCatalogWriter#write(SourceCatalog, Writer) 028 */ 029 public boolean write(SourceCatalog cat, Writer out) 030 { 031 try 032 { 033 cat.writeAsXmlTo(out); 034 writeWasSuccessful = true; 035 } 036 catch (Exception ex) 037 { 038 writeError = ex; 039 writeWasSuccessful = false; 040 } 041 042 //Try to close the writer. 043 try 044 { 045 out.close(); 046 } 047 catch(Exception ex) 048 { 049 //If we already have an error, keep that original error message instead 050 //of this one, as the previous error likely caused this one. 051 if (writeError == null) 052 { 053 writeError = ex; 054 writeWasSuccessful = false; 055 } 056 } 057 058 return writeWasSuccessful; 059 } 060 061 /** 062 * @see SourceCatalogReader#getErrors 063 */ 064 public StringBuilder getErrors() 065 { 066 return new StringBuilder(writeError == null ? "" : writeError.getMessage()); 067 } 068 069 /** 070 * @see SourceCatalogReader#getError(int) 071 */ 072 public String getError(int index) 073 { 074 if (index != 0) 075 throw new IndexOutOfBoundsException( 076 "This reader has either zero or one errors. Use index of zero."); 077 078 return writeError == null ? "" : writeError.getMessage(); 079 } 080 081 /** 082 * @see SourceCatalogReader#getErrorCount() 083 */ 084 public int getErrorCount() 085 { 086 return writeError == null ? 0 : 1; 087 } 088 }