1 package org.lcsim.geometry.compact.converter.heprep;
2
3 import hep.graphics.heprep.HepRep;
4 import hep.graphics.heprep.HepRepFactory;
5 import hep.graphics.heprep.HepRepWriter;
6 import java.io.BufferedInputStream;
7 import java.io.BufferedOutputStream;
8 import java.io.ByteArrayInputStream;
9 import java.io.ByteArrayOutputStream;
10 import java.io.FileInputStream;
11 import java.io.FileOutputStream;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import javax.swing.filechooser.FileFilter;
15 import org.jdom.input.SAXBuilder;
16 import org.lcsim.geometry.GeometryReader;
17 import org.lcsim.geometry.Detector;
18 import org.lcsim.geometry.compact.converter.Converter;
19
20
21
22
23
24 public class Main implements Converter
25 {
26 private static boolean validateDefault = false;
27 private boolean validate;
28
29
30
31 public static void main(String[] args) throws Exception
32 {
33 if (args.length < 1 || args.length >2) usage();
34 InputStream in = new BufferedInputStream(new FileInputStream(args[0]));
35 OutputStream out = args.length == 1 ? System.out : new BufferedOutputStream(new FileOutputStream(args[1]));
36 new Main().convert(args[0],in,out);
37 }
38 public Main()
39 {
40 this(validateDefault);
41 }
42 public Main(boolean validate)
43 {
44 this.validate = validate;
45 }
46 public void convert(String inputFileName, InputStream in, OutputStream out) throws Exception
47 {
48 GeometryReader reader = new GeometryReader();
49 Detector det = reader.read(in);
50
51 HepRep heprep = createHepRep(det);
52
53 if (validate)
54 {
55 ByteArrayOutputStream stream = new ByteArrayOutputStream();
56 writeHepRep(heprep,stream);
57 stream.close();
58
59 SAXBuilder builder = new SAXBuilder();
60 builder.setValidation(true);
61 builder.setFeature("http://apache.org/xml/features/validation/schema",true);
62 builder.build(new ByteArrayInputStream(stream.toByteArray()));
63 }
64
65 if (out != null) writeHepRep(heprep,out);
66 }
67 private static void writeHepRep(HepRep heprep, OutputStream out) throws Exception
68 {
69 HepRepWriter writer = HepRepFactory.create().createHepRepWriter(out,false,false);
70 writer.write(heprep,"test");
71 writer.close();
72 }
73 private static HepRep createHepRep(Detector det) throws Exception
74 {
75 HepRepFactory factory = HepRepFactory.create();
76 HepRep root = factory.createHepRep();
77 det.appendHepRep(factory,root);
78 return root;
79 }
80 private static void usage()
81 {
82 System.out.println("java "+Main.class.getName()+" <compact> [<heprep>]");
83 System.exit(0);
84 }
85
86 public FileFilter getFileFilter()
87 {
88 return new HepRepFileFilter();
89 }
90
91 public String getOutputFormat()
92 {
93 return "heprep";
94 }
95
96 private static class HepRepFileFilter extends FileFilter
97 {
98 public boolean accept(java.io.File file)
99 {
100 return file.isDirectory() || file.getName().endsWith(".heprep");
101 }
102
103 public String getDescription()
104 {
105 return "HepRep file (*.heprep)";
106 }
107 }
108 }