View Javadoc

1   package org.lcsim.geometry.compact.converter;
2   
3   import java.io.BufferedInputStream;
4   import java.io.BufferedOutputStream;
5   import java.io.File;
6   import java.io.FileInputStream;
7   import java.io.FileOutputStream;
8   import java.io.InputStream;
9   import java.io.OutputStream;
10  import java.util.ArrayList;
11  import java.util.Iterator;
12  import java.util.List;
13  import javax.imageio.spi.ServiceRegistry;
14  
15  /**
16   * A main routine which can be used to invoke other main routines.
17   * @author tonyj
18   */
19  public class Main
20  {
21     private String inFile;
22     private String outFile;
23     private String format;
24     private Converter converter;
25     
26     String getInputFile()
27     {
28        return inFile;
29     }
30     String getOutputFile()
31     {
32        return outFile;
33     }
34     String getFormat()
35     {
36        return format;
37     }
38     void parseArgs(String[] args) throws InvalidArgumentException
39     {
40        for (int i=0; i<args.length; i++)
41        {
42           String arg = args[i];
43           if ("-o".equals(arg))
44           {
45              i++;
46              if (i >= args.length) usage();
47              format = args[i];
48           }
49           else if (arg.startsWith("-")) usage();
50           else if (inFile == null) inFile = arg;
51           else if (outFile == null) outFile = arg;
52           else throw new InvalidArgumentException();
53        }
54        
55        if (format != null)
56        {
57           for (Converter c : getConverters())
58           {
59              if (c.getOutputFormat().equalsIgnoreCase(format))
60              {
61                 converter = c;
62                 break;
63              }
64           }
65           if (converter == null) throw new InvalidArgumentException("Sorry, could not find converter for format: "+format);
66        }
67        // If not explicitly set try to guess output type from output file
68        else if (outFile != null)
69        {
70           File out = new File(outFile);
71           for (Converter c : getConverters())
72           {
73              if (c.getFileFilter().accept(out))
74              {
75                 converter = c;
76                 break;
77              }
78           }
79           if (converter == null) throw new InvalidArgumentException("Sorry, could not find converter for output file");
80        }
81     }
82     public static void main(String[] args)
83     {
84        Main main = new Main();
85        try
86        {
87           main.parseArgs(args);
88        }
89        catch (InvalidArgumentException x)
90        {
91           String message = x.getMessage();
92           if (message != null) System.err.println(message);
93           usage();
94        }
95        if (main.outFile == null)
96        {
97           MainGUI.main(args);
98        }
99        else if (main.converter != null)
100       {
101           try 
102           {
103               main.run();
104           }
105           catch (Exception x)
106           {  
107               throw new RuntimeException(x);
108           }
109       }
110    }
111    private void run() throws Exception
112    {
113       InputStream in = new BufferedInputStream(new FileInputStream(inFile));
114       OutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
115       converter.convert(inFile,in,out);
116       in.close();
117       out.close();
118    }
119    
120    private static void usage()
121    {
122       System.out.println("java "+Main.class.getName()+" [-o <format>] [<input> [<output>]]");
123       System.exit(0);
124    }
125    static List<Converter> getConverters()
126    {
127       Iterator<Converter> iter = getServices(Converter.class);
128       List<Converter> result = new ArrayList<Converter>();
129       while (iter.hasNext()) result.add(iter.next());
130       return result;
131    }
132    private static <T> Iterator<T> getServices(Class<T> providerClass)
133    {
134       return ServiceRegistry.lookupProviders(providerClass,Main.class.getClassLoader());
135    }
136    class InvalidArgumentException extends Exception
137    {
138       InvalidArgumentException()
139       {
140          super();
141       }
142       InvalidArgumentException(String message)
143       {
144          super(message);
145       }
146    }
147 }