001 package edu.nrao.sss.model.source.parser; 002 003 import java.io.Reader; 004 import java.io.IOException; 005 006 import javax.xml.bind.JAXBException; 007 import javax.xml.stream.XMLStreamException; 008 009 import edu.nrao.sss.model.RepositoryException; 010 import edu.nrao.sss.model.source.Source; 011 import edu.nrao.sss.model.source.SourceCatalog; 012 013 /** 014 * A parser of source catalogs that are expressed in XML. 015 * <p> 016 * <b>Version Info:</b> 017 * <table style="margin-left:2em"> 018 * <tr><td>$Revision: 831 $</td></tr> 019 * <tr><td>$Date: 2007-08-20 10:44:24 -0600 (Mon, 20 Aug 2007) $</td></tr> 020 * <tr><td>$Author: dharland $</td></tr> 021 * </table></p> 022 * 023 * @author David M. Harland 024 * @since 2007-05-17 025 */ 026 public class XmlSourceCatalogReader 027 extends AbstractSourceCatalogReader 028 { 029 /** 030 * NOTE: This method closes the incoming Reader! 031 * 032 * @see AbstractSourceCatalogReader#read(java.io.Reader, 033 * edu.nrao.sss.model.source.SourceCatalog) 034 */ 035 public boolean read(Reader in, SourceCatalog destination) 036 { 037 try 038 { 039 //Creates a NEW catalog 040 catalog = SourceCatalog.fromXml(in); 041 042 if (sourceInfoOrigin != null) 043 setOriginOfInfo(catalog); 044 045 //New catalog should have null IDs, but the XML file may have brought 046 //IDs that could conflict with already stored catalogs & sources. 047 catalog.clearId(); 048 049 //If we were given a catalog, move data from new catalog to the given one 050 if (destination != null) 051 { 052 destination.setName (catalog.getName() ); 053 destination.addItems (catalog.getItems() ); 054 destination.addGroups(catalog.getGroups()); 055 056 catalog = destination; 057 } 058 059 in.close(); 060 061 readWasSuccessful = true; 062 } 063 catch (JAXBException ex) 064 { 065 Throwable e = ex.getLinkedException(); 066 if (e != null) 067 putError(0, e.getMessage()); 068 069 else 070 putError(0, ex.getMessage()); 071 072 catalog = null; 073 readWasSuccessful = false; 074 } 075 076 catch (XMLStreamException xe) 077 { 078 Throwable e = xe.getNestedException(); 079 if (e != null) 080 putError(0, e.getMessage()); 081 082 else 083 putError(0, xe.getMessage()); 084 085 catalog = null; 086 readWasSuccessful = false; 087 } 088 089 catch (IOException ioe) 090 { 091 putError(0, ioe.getMessage()); 092 catalog = null; 093 readWasSuccessful = false; 094 } 095 096 return readWasSuccessful; 097 } 098 099 //============================================================================ 100 // 101 //============================================================================ 102 103 /** Sets the originOfInformation property of all sources in catalog. */ 104 private void setOriginOfInfo(SourceCatalog catalog) 105 { 106 try 107 { 108 for (Source source : catalog.getSources()) 109 source.setOriginOfInformation(sourceInfoOrigin); 110 } 111 catch (RepositoryException ex) 112 { 113 throw new RuntimeException( 114 "SourceCatalog.getSources claimed to never throw this exception.", ex); 115 } 116 } 117 }