View Javadoc

1   package org.lcsim.detector.converter.compact;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.util.Map.Entry;
6   
7   import org.jdom.Document;
8   import org.jdom.JDOMException;
9   import org.jdom.input.SAXBuilder;
10  import org.lcsim.units.clhep.Constants;
11  import org.lcsim.util.xml.JDOMExpressionFactory;
12  
13  public class CompactDocumentBuilder
14  {
15      public static Document build( String resource ) throws JDOMException, IOException
16      {
17          return build( CompactDocumentBuilder.class.getResourceAsStream( resource ) );
18      }
19  
20      public static Document build( InputStream in ) throws JDOMException, IOException
21      {
22          JDOMExpressionFactory eval = new JDOMExpressionFactory();
23          registerCLHEPConstants( eval );
24          SAXBuilder builder = new SAXBuilder();
25          builder.setFactory( eval );
26          Document doc = builder.build( in );
27          return doc;
28      }
29  
30      public static void registerCLHEPConstants( JDOMExpressionFactory f )
31      {
32          Constants units = Constants.getInstance();
33          for ( Entry< String, Double > unit : units.entrySet() )
34          {
35              f.addConstant( unit.getKey(), unit.getValue() );
36          }
37      }
38  }