001    package edu.nrao.sss.astronomy;
002    
003    /**
004     * A factory that produces readers of ephemeris table files.
005     * <p>
006     * <b>CVS Info:</b>
007     * <table style="margin-left:2em">
008     *   <tr><td>$Revision: 1707 $</td></tr>
009     *   <tr><td>$Date: 2008-11-14 10:23:59 -0700 (Fri, 14 Nov 2008) $</td></tr>
010     *   <tr><td>$Author: dharland $</td></tr>
011     * </table></p>
012     *  
013     * @author David M. Harland
014     * @since 2006-06-12
015     */
016    public class EphemerisTableReaderFactory
017    {
018      private static final EphemerisTableReaderFactory SHARED_FACTORY =
019        new EphemerisTableReaderFactory();
020      
021      /** Returns a shared factory. */
022      public static EphemerisTableReaderFactory getSharedInstance()
023      {
024        return SHARED_FACTORY;
025      }
026      
027      /** Creates a new factory. */
028      public EphemerisTableReaderFactory()
029      {
030        
031      }
032      
033      /**
034       * Creates a new ephemeris table reader for the given
035       * {@code readerName}.  The {@code readerName} may be
036       * either one of the codes that is understood by
037       * this factory, or the prefix of a class that is not
038       * known by this factory (see below).
039       * <p>
040       * <b><u>Known Reader Codes</u></b>
041       * <ul><tt>
042       *   <li>JPL</li>
043       * </tt></ul>
044       * The above codes are not case sensitive.</p>
045       * <p>
046       * <b><u>Class Name Conventions</u></b><br/>
047       * If this factory does not know the given name of the
048       * reader, it will attempt to locate a class that follows
049       * the naming convention:<pre>
050       *   XyzEphemerisTableReader</pre>
051       * The incoming reader name will be transformed so that
052       * its first character is upper case and the rest are
053       * lower case.  This means that the class name itself
054       * must follow the normal java naming conventions, but
055       * the parameter to this method is not case sensitive.</p>
056       * <p>
057       * If a reader of the given name cannot be found, <i>null</i>
058       * is returned.</p>
059       * 
060       * @param readerName the name of the reader that is desired.
061       * 
062       * @return a new ephemeris table reader.
063       * 
064       * @throws IllegalArgumentException if {@code readerName} cannot
065       *         be transformed into a reader.
066       */
067      public EphemerisTableReader getNewReader(String readerName)
068        throws IllegalArgumentException
069      {
070        EphemerisTableReader reader;
071        
072        //Look first for a known type of reader
073        if (readerName.equalsIgnoreCase("JPL"))
074          reader = new JplEphemerisTableReader();
075        //else if (readerName.equalsIgnoreCase("XYZ"))
076        //  reader = new ...
077        else
078        {
079          StringBuilder buff = new StringBuilder(readerName.toLowerCase());
080          
081          buff.setCharAt(0, Character.toUpperCase(buff.charAt(0)));
082          buff.append("EphemerisTableReader");
083          
084          try {
085            Class<?> readerClass = Class.forName(buff.toString());
086            
087            reader = (EphemerisTableReader)readerClass.newInstance();
088          }
089          catch (Exception ex) {
090            throw new IllegalArgumentException("Could not make reader for name '" +
091                                               readerName + "'.", ex);
092          }
093        }
094        
095        return reader;
096      }
097    }