View Javadoc

1   package org.lcsim.geometry;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   
6   import org.jdom.JDOMException;
7   import org.lcsim.detector.converter.compact.DetectorConverter;
8   import org.lcsim.geometry.compact.CompactElementFactory;
9   import org.lcsim.geometry.compact.CompactReader;
10  import org.lcsim.geometry.compact.Field;
11  import org.lcsim.geometry.compact.Segmentation;
12  import org.lcsim.geometry.compact.Subdetector;
13  import org.lcsim.util.xml.ElementFactory.ElementCreationException;
14  import org.lcsim.detector.DetectorStore;
15  
16  /**
17   * The GeometryReader extends the CompactReader and creates specific types based on the type attribute of an XML
18   * element. The type string must match a class within the registered package for that base class. Currently supported
19   * base classes with specific types are Subdetector, Segmentation, and Field.
20   * <p>
21   * For example, this compact XML will instantiate a Subdetector with specific type of 'CylindricalBarrelCalorimeter'.<br/> 
22   * <pre>
23   * <detector type="CylindricalBarrelCalorimeter" ... /> Support added for detailed geometry representation.
24   * </pre>
25   * 
26   * @author Tony Johnson, SLAC
27   * @author Jeremy McCormick, SLAC
28   */
29  public class GeometryReader extends CompactReader {
30  
31      boolean buildDetailed = true;
32  
33      public GeometryReader() {
34          super(new GeometryFactory());
35      }
36  
37      public Detector read(InputStream in) throws IOException, JDOMException, ElementCreationException {
38          
39          // Build a compact description with support for specific types.
40          Detector detector = (Detector) super.read(in);
41  
42          // Clear existing detector store.
43          DetectorStore.getInstance().clear();
44  
45          // Build the detailed detector description.
46          if (buildDetailed) {
47              DetectorConverter cnv = new DetectorConverter();
48              cnv.registerSubdetectorConverters();
49              cnv.convert(detector, getDocument());
50          }
51  
52          // Clear the cached document.
53          resetDocument();
54  
55          return detector;
56      }
57  
58      public boolean buildDetailed() {
59          return buildDetailed;
60      }
61  
62      public void setBuildDetailed(boolean buildDetailed) {
63          this.buildDetailed = buildDetailed;
64      }
65  
66      static class GeometryFactory extends CompactElementFactory {
67          GeometryFactory() {
68              super();
69              register(Detector.class);
70              register(Subdetector.class, "org.lcsim.geometry.subdetector");
71              register(Segmentation.class, "org.lcsim.geometry.segmentation");
72              register(Field.class, "org.lcsim.geometry.field");
73          }
74      }
75  }