View Javadoc

1   /**
2    * @author Jeremy McCormick <jeremym@slac.stanford.edu>
3    * @version $Id: Main.java,v 1.1 2011/01/26 01:19:39 jeremy Exp $
4    */
5   package org.lcsim.geometry.compact.converter.svg;
6   
7   import java.io.BufferedInputStream;
8   import java.io.BufferedOutputStream;
9   import java.io.FileInputStream;
10  import java.io.FileOutputStream;
11  import java.io.InputStream;
12  import java.io.OutputStream;
13  
14  import javax.swing.filechooser.FileFilter;
15  
16  import org.jdom.Document;
17  import org.jdom.Element;
18  import org.jdom.output.Format;
19  import org.jdom.output.XMLOutputter;
20  import org.lcsim.geometry.Detector;
21  import org.lcsim.geometry.GeometryReader;
22  import org.lcsim.geometry.compact.converter.Converter;
23  
24  public class Main implements Converter
25  {
26      /**
27       * @param args the command line arguments
28       */
29      public static void main( String[] args ) throws Exception
30      {
31          if ( args.length < 1 || args.length > 2 )
32              usage();
33          InputStream in = new BufferedInputStream( new FileInputStream( args[ 0 ] ) );
34          OutputStream out = args.length == 1 ? System.out : new BufferedOutputStream( new FileOutputStream( args[ 1 ] ) );
35          new Main().convert( args[ 0 ], in, out );
36      }
37  
38      public Main()
39      {}
40  
41      public void convert( String inputFileName, InputStream in, OutputStream out ) throws Exception
42      {
43          // Read in detector.
44          GeometryReader reader = new GeometryReader();
45          Detector detector = reader.read( in );
46  
47          // Create the HTML.
48          Element root = SvgConverter.convert( detector );
49  
50          // Create the document.
51          Document doc = new Document();
52          doc.setRootElement( root );                
53  
54          // Write out the document.
55          XMLOutputter outputter = new XMLOutputter();
56          outputter.setFormat( Format.getPrettyFormat() );
57          outputter.output( doc, out );
58          out.close();
59      }
60  
61      private static void usage()
62      {
63          System.out.println( "java " + Main.class.getName() + " <compact> [<svg>]" );
64          System.exit( 0 );
65      }
66  
67      public FileFilter getFileFilter()
68      {
69          return new HepRepFileFilter();
70      }
71  
72      public String getOutputFormat()
73      {
74          return "svg";
75      }
76  
77      private static class HepRepFileFilter extends FileFilter
78      {
79          public boolean accept( java.io.File file )
80          {
81              return file.isDirectory() || file.getName().endsWith( ".svg" );
82          }
83  
84          public String getDescription()
85          {
86              return "HTML file (*.svg)";
87          }
88      }
89  }