View Javadoc

1   //-------------------------------------------------------------------------
2   // FILE: NativeToXmlImpl.java
3   // PACKAGE: org.astrogrid.ace.utils
4   //
5   // DATE       AUTHOR    NOTES
6   // ----       ------    -----
7   // 14/10/02   KEA       Initial prototype
8   // 04/12/02   KEA       Revised to deal with new Ace Schema (v.1_0).
9   //                      NB this is an interim release.
10  // 13/12/02   KEA       Added support for new "Wavelength" XML tag
11  //-------------------------------------------------------------------------
12  
13  
14  package org.astrogrid.ace.utils;
15  
16  import java.io.BufferedReader;
17  import java.io.Reader;
18  import java.io.Writer;
19  import java.io.IOException;
20  
21  import java.util.NoSuchElementException;
22  import java.util.StringTokenizer;
23  import java.util.Vector;
24  
25  
26  /***
27   * <p>Fairly dumb tokenizer-based implementation of file format converter
28   * for conversions from SExtractor native parameter files to Ace XML
29   * format.
30   *
31   * <p>See Translator.java for a usage example, and Tester.java
32   * for a run-time test harness.
33   *
34   * <p>TO DO:  <ul>
35   * <li>More robust error-checking and handling?</li>
36   * <li>Nicer exception-throwing and handling?</li>
37   * </ul>
38   *
39   *
40   * @see org.astrogrid.ace.utils.NativeToXmlIfc
41   * @see org.astrogrid.ace.utils.Translator
42   * @see org.astrogrid.ace.utils.Tester
43   *
44   * @author Kona Andrews,
45   * <a href="mailto:kea@ast.cam.ac.uk">kea@ast.cam.ac.uk</a>
46   * @version 1.0
47   *
48   *
49   * (c) Copyright Astrogrid 2002; all rights reserved.
50   * See http://www.astrogrid.org/code_licence.html for terms of usage.
51   */
52  public class NativeToXmlImpl implements NativeToXmlIfc
53  {
54     /***
55      * Hardwired XML output file preamble
56      */
57     protected static final String startString =
58        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
59     +  "<ace:AceInputDoc\n"
60     +  "  xmlns:ace=\"http://www.astrogrid.org/namespace/AceInput-1_0\"\n"
61     +  "  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
62     +  "  xsi:schemaLocation="
63     +  "\"http://www.astrogrid.org/namespace/AceInput-1_0\n"
64     +  "  http://astrogrid.ast.cam.ac.uk/namespace/AceInput-1_0.xsd\">\n\n";
65  
66     /***
67      * Hardwired last entry in XML output file
68      */
69     protected static final String endString = "</ace:AceInputDoc>\n";
70  
71     /***
72      * Tag names / output tokens representing Booleans: these need to
73      * be handled separately as SExtractor needs "Y/y/N/n" rather than
74      * "true/false" in its native config files.
75      */
76     protected static final String[] booleans = { "CLEAN",
77                                                  "FILTER",
78                                                  "FITS_UNSIGNED",
79                                                  "WEIGHT_GAIN" };
80     /***
81      * Tag names / output tokens to be omitted from the output XML.
82      */
83     protected static final String[] omits = { "CATALOG_NAME",
84                                               "CATALOG_TYPE",
85                                               "PARAMETERS_NAME" };
86     /***
87      * Dummy constructor - does nothing.
88      */
89      public NativeToXmlImpl()
90      {
91      }
92  
93     /***
94      * Converts SExtractor input (configuration file *.sex and catalog
95      * parameter file *.param) to Ace XML output.
96      *
97      * @param configInReader  A (pre-initialised) reader for the input
98      *     SExtractor configuration parameter file (*.sex).
99      *
100     * @param catalogInReader A (pre-initialised) writer for the output
101     *     SExtractor catalog parameter file (*.param).
102     *
103     * @param xmlOutWriter  A (pre-initialised) writer for the output
104     *     Ace XML file.
105     *
106     * @param imageToCatalog  The name of the input image file for SExtractor.
107     *
108     * @param imageToMeasure  The name of the second (optional) input image
109     *     file for SExtractor.
110     */
111    public void makeXml(Reader configInReader,
112                         Reader catalogInReader,
113                         Writer xmlOutWriter,
114                         String imageToCatalog,
115                         String imageToMeasure,
116                         String wavelength) throws IOException
117    {
118       // Write toplevel info to file
119       //
120       xmlOutWriter.write(startString);
121 
122       xmlOutWriter.write("\t<Wavelength>\n\t\t<arg>" + wavelength
123                + "</arg>\n\t</Wavelength>\n\n");
124 
125       xmlOutWriter.write("\t<ImageToCatalog>\n\t\t<arg>" + imageToCatalog
126                + "</arg>\n\t</ImageToCatalog>\n\n");
127 
128       xmlOutWriter.write("\t<ImageToMeasure>\n\t\t<arg>" + imageToMeasure
129                + "</arg>\n\t</ImageToMeasure>\n\n");
130 
131       // Process config parameters
132       makeSexXml(configInReader, xmlOutWriter);
133        
134        
135       // Process catalog parameters
136       makeParamXml(catalogInReader, xmlOutWriter);
137 
138        
139        xmlOutWriter.write(endString);
140    }
141 
142    public static void startXml(Writer xmlOut) throws IOException
143    {
144       xmlOut.write(startString);
145    }
146    
147    public static void endXml(Writer xmlOut) throws IOException
148    {
149       xmlOut.write(endString);
150       xmlOut.close();
151  
152    }
153    
154    
155    /***
156     * Converts SExtractor input configuration file *.sex only, to a file
157     * with the relevent XML tags.  NB the result may not be valid, as it
158     * may not be complete without other input information.
159     *
160     * @param configInReader  A (pre-initialised) reader for the input
161     *     SExtractor configuration parameter file (*.sex).
162     *
163     * @param xmlOutWriter  A (pre-initialised) writer for the output
164     *     Ace XML file.
165     *
166     */
167    public static void makeSexXml(Reader sexIn, Writer xmlOut) throws IOException
168    {
169       BufferedReader config = new BufferedReader(sexIn);
170       String configLine = config.readLine();
171       int lineCount = 1;
172       while (!(configLine == null))
173       {
174          // Throw away any trailing comments
175          //
176          int index = configLine.indexOf('#');
177          if (index != -1)  // Trailing comment present
178          {
179             configLine = configLine.substring(0,index);
180          }
181 
182          // Tokenize line with tokens: {" \t=,;\""}; (from SEx source code)
183          //
184          StringTokenizer tokenizer =
185                new StringTokenizer(configLine," \t=,;\"",false);
186 
187          if ((tokenizer.countTokens() != 0)) // Not blank/comment line
188          {
189             //Get keyword
190             //
191             String keyword;
192             try
193             {
194                keyword = tokenizer.nextToken();
195             }
196             catch(NoSuchElementException e)
197             {
198                throw new IOException(
199                      "Expected a leading keyword on line "
200                   +  Integer.toString(lineCount)
201                   +  " of configuration file, got null");
202             }
203             // Check whether we should omit this keyword
204             //
205             boolean skip = false;
206             for (int i = 0; i < omits.length; i++)
207             {
208                if (omits[i].equals(keyword) )
209                {
210                   skip = true;
211                }
212             }
213             if (!skip)
214             {
215                xmlOut.write("\t<" + keyword + ">\n");
216 
217                //Get first argument (must be at least 1)
218                //
219                String arg;
220                try
221                {
222                   arg = tokenizer.nextToken();
223                }
224                catch(NoSuchElementException e)
225                {
226                   throw new IOException(
227                         "Expected an argument for keyword " +  keyword
228                      +  " on line "
229                      +  Integer.toString(lineCount)
230                      +  " of configuration file, got null");
231                }
232                // Booleans are a special case
233                //
234                boolean done = false;
235                for (int i = 0; i < booleans.length; i++)
236                {
237                   if (keyword.equals(booleans[i]))
238                   {
239                      if ((arg.equals("Y")) || (arg.equals("y")))
240                      {
241                         arg = "true";
242                      }
243                      else if ((arg.equals("N")) || (arg.equals("n")))
244                      {
245                         arg = "false";
246                      }
247                      else
248                      {
249                         throw new IOException("Unexpected token '" + arg
250                            + "' for boolean keyword " + keyword);
251                      }
252                   }
253                }
254                xmlOut.write("\t\t<arg>" + arg + "</arg>\n");
255 
256                while (tokenizer.hasMoreTokens())
257                {
258                   arg = tokenizer.nextToken();
259                   xmlOut.write("\t\t<arg>" + arg + "</arg>\n");
260                }
261                xmlOut.write("\t</" + keyword + ">\n\n");
262             }
263          }
264          lineCount = lineCount + 1;
265          configLine = config.readLine();
266       }
267       
268    }
269 
270    /***
271     * Converts SExtractor param configuration file *.param only, to a file
272     * with the relevent XML tags.  NB the result may not be valid, as it
273     * may not be complete without other input information.
274     *
275     * @param paramInReader A (pre-initialised) reader for the output
276     *     SExtractor catalog parameter file (*.param).
277     *
278     * @param xmlOutWriter  A (pre-initialised) writer for the output
279     *     Ace XML file.
280     *
281     */
282    public static void makeParamXml(Reader paramIn, Writer xmlOutWriter) throws IOException
283    {
284       BufferedReader catalog = new BufferedReader(paramIn);
285       Vector plainTag = new Vector();
286       Vector oneVector = new Vector();
287       Vector twoVector = new Vector();
288 
289       // Read all lines into the appropriate vector
290       //
291       String catalogLine = catalog.readLine();
292       int lineCount = 1;
293       while (!(catalogLine == null))
294       {
295          // Throw away any trailing comments
296          //
297          int index = catalogLine.indexOf('#');
298          if (index != -1)  // Trailing comment present
299          {
300             catalogLine = catalogLine.substring(0,index);
301          }
302 
303          StringTokenizer tokenizer =
304                new StringTokenizer(catalogLine,"(,)",false);
305 
306          int tokenCount = tokenizer.countTokens();
307          if ( (tokenCount >= 1) && (tokenCount <=3) ) // HAVE KEYWORD
308          {
309             plainTag.add("\t\t<arg>" + tokenizer.nextToken() + "</arg>\n");
310          }
311          catalogLine = catalog.readLine();
312       }
313       if (plainTag.size() > 0)
314       {
315          xmlOutWriter.write("\t<OUTPUT_COLUMNS>\n");
316 
317          for (int i = 0; i < plainTag.size(); i++)
318          {
319             String entry = (String)plainTag.get(i);
320             xmlOutWriter.write(entry);
321          }
322          xmlOutWriter.write("\t</OUTPUT_COLUMNS>\n\n");
323       }
324    }
325    
326 }
327 //-------------------------------------------------------------------------