001    package edu.nrao.sss.model.source.parser;
002    
003    import java.io.File;
004    import java.io.FileWriter;
005    import java.io.Writer;
006    import java.io.OutputStream;
007    import java.io.OutputStreamWriter;
008    import java.io.IOException;
009    
010    import java.util.List;
011    import java.util.ArrayList;
012    import java.util.zip.ZipOutputStream;
013    import java.util.zip.ZipEntry;
014    import java.util.zip.GZIPOutputStream;
015    
016    import edu.nrao.sss.model.source.SourceCatalog;
017    import edu.nrao.sss.util.FileCompressionFormat;
018    
019    /**
020     * A base class that implements the most common methods of the
021     * SourceCatalogWriter
022     * <p>
023     * <b>Version Info:</b>
024     * <table style="margin-left:2em">
025     *   <tr><td>$Revision: 714 $</td></tr>
026     *   <tr><td>$Date: 2007-06-13 10:36:05 -0600 (Wed, 13 Jun 2007) $</td></tr>
027     *   <tr><td>$Author: btruitt $</td></tr>
028     * </table></p>
029     */
030    public abstract class AbstractSourceCatalogWriter
031            implements SourceCatalogWriter
032    {
033      protected boolean writeWasSuccessful;
034      protected List<ParseError> errors;
035      
036            public AbstractSourceCatalogWriter()
037            {
038        errors = new ArrayList<ParseError>();
039        writeWasSuccessful = false;
040            }
041    
042      /**
043             * Must be overridden to perform the actual writing. All other write methods
044             * delegate to this one.
045       * @see SourceCatalogWriter#write(SourceCatalog, Writer)
046             * @throws NullPointerException if {@code cat} is NULL.
047       */
048      public abstract boolean write(SourceCatalog cat, Writer out);
049      
050      /**
051             * Delegates to {@link #write(SourceCatalog, Writer)}.
052             * @see SourceCatalogWriter#write(SourceCatalog, String)
053       */
054      public boolean write(SourceCatalog cat, String fileName)
055            {
056                    try
057                    {
058                            return write(cat, new FileWriter(new File(fileName)));
059                    }
060                    catch(IOException ioe)
061                    {
062                            putError(0, ioe.getMessage());
063                            writeWasSuccessful = false;
064                            return false;
065                    }
066            } 
067    
068      /**
069             * Delegates to {@link #write(SourceCatalog, OutputStream, FileCompressionFormat)}
070             * passing a format of UNCOMPRESSED
071       * @see SourceCatalogWriter#write(SourceCatalog, OutputStream)
072       */
073            public boolean write(SourceCatalog cat, OutputStream out)
074            {
075                    return write(cat, out, FileCompressionFormat.UNCOMPRESSED);
076            }
077    
078      /**
079             * Wraps the output stream in a {@link ZipOutputStream} or {@link GZIPOutputStream}
080             * as appropriate depending on {@code format}. If a ZipOutputStream is created,
081             * a single ZipEntry is added to it with a name of XXX oops, this won't work
082             * at all...What extension do I use?!
083       */
084            public boolean write(SourceCatalog cat, OutputStream out, FileCompressionFormat format)
085            {
086                    OutputStream wrappedOut = out;
087    
088                    //Wrap "out" in an appropriate compression filter if necessary, then wrap
089                    //that in an OutputStreamWriter and call write(cat, writer);
090                    if (format != null)
091                    {
092                            switch(format)
093                            {
094                                    case ZIP:
095                                            try
096                                            {
097                                                    wrappedOut = new ZipOutputStream(out);
098    
099                                                    //Prepare the ZipOutputStream to be writing the next (and hopefully
100                                                    //only) file.
101                                                    ((ZipOutputStream)wrappedOut).putNextEntry(new ZipEntry("catalog"));
102                                            }
103    
104                                            catch(IOException ioe)
105                                            {
106                                                    putError(0, ioe.getMessage());
107                                                    return false;
108                                            }
109                                            break;
110    
111                                    case GZ:
112                                            try
113                                            {
114                                                    wrappedOut = new GZIPOutputStream(out);
115                                            }
116    
117                                            catch(IOException ioe)
118                                            {
119                                                    putError(0, ioe.getMessage());
120                                                    return false;
121                                            }
122                                            break;
123    
124                                    //These cases will just use the default value of wrappedOut (i.e. out
125                                    //itself)
126    
127                                    //case UNCOMPRESSED:
128                                    //default:
129                            }
130                    }
131    
132                    return write(cat, new OutputStreamWriter(wrappedOut));
133            }
134      
135      /**
136       * @see SourceCatalogWriter#getErrors
137       */
138      public StringBuilder getErrors()
139      {
140        StringBuilder errorMsg = new StringBuilder();
141        
142        for (ParseError error : errors)
143          error.appendTo(errorMsg);
144        
145        return errorMsg;
146      }
147      
148      /**
149       * @see SourceCatalogWriter#getError(int)
150       */
151      public String getError(int index)
152      {
153        return errors.get(index).toString();
154      }
155    
156      /**
157       * @see SourceCatalogWriter#getErrorCount()
158       */
159      public int getErrorCount()
160      {
161        return errors.size();
162      }
163    
164      /**
165       * @see SourceCatalogWriter#getSuccess()
166       */
167      public boolean getSuccess()
168      {
169        return writeWasSuccessful;
170      }
171    
172      /** Adds a new error to our list. */
173      protected void putError(String message)
174      {
175        errors.add(new ParseError(0, message));
176      }
177    
178      /** Adds a new error to our list. */
179      protected void putError(int lineNum, String message)
180      {
181        errors.add(new ParseError(lineNum, message));
182      }
183    }