GeomConverter/src/org/lcsim/geometry/compact/converter/html
diff -N HtmlConverter.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ HtmlConverter.java 4 Jan 2011 21:59:13 -0000 1.1
@@ -0,0 +1,355 @@
+package org.lcsim.geometry.compact.converter.html;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.jdom.Element;
+import org.lcsim.geometry.Calorimeter;
+import org.lcsim.geometry.compact.Constant;
+import org.lcsim.geometry.compact.Detector;
+import org.lcsim.geometry.compact.Field;
+import org.lcsim.geometry.compact.Header;
+import org.lcsim.geometry.compact.Segmentation;
+import org.lcsim.geometry.compact.Subdetector;
+import org.lcsim.geometry.field.Solenoid;
+import org.lcsim.geometry.subdetector.AbstractLayeredSubdetector;
+
+/**
+ * Convert a compact description to an html information page.
+ *
+ * @author Jeremy McCormick <[log in to unmask]>
+ * @version $Id: HtmlConverter.java,v 1.1 2011/01/04 21:59:13 jeremy Exp $
+ */
+// TODO add css
+public class HtmlConverter
+{
+ private HtmlConverter()
+ {}
+
+ public static Element convert( Detector d )
+ {
+ // Convert to specific subclass for more method access.
+ Detector detector = ( org.lcsim.geometry.compact.Detector ) d;
+
+ // Root element.
+ Element root = new Element( "html" );
+
+ // Header.
+ Element head = new Element( "head" );
+ String detectorName = detector.getName();
+ Element title = new Element( "title" );
+ title.setText( detectorName );
+ head.addContent( title );
+ root.addContent( head );
+
+ // Body.
+ Element body = new Element( "body" );
+ root.addContent( body );
+
+ // Layout table.
+ Element tbl = new Element( "table" );
+ tbl.setAttribute( "cellspacing", "25" );
+ tbl.setAttribute( "width", "100%" );
+ body.addContent( tbl );
+
+ // Layout row.
+ Element td = addLayoutRow( tbl );
+
+ // Add detector header.
+ detectorHeader( td, detector );
+
+ // Layout row.
+ td = addLayoutRow( tbl );
+
+ // Links to defines and fields at bottom of page.
+ Element p = new Element( "p" );
+ p.addContent( new Link( "Go to Constants", "#defines" ) );
+ td.addContent( p );
+ p = new Element( "p" );
+ p.addContent( new Link( "Go to Fields", "#fields" ) );
+ td.addContent( p );
+
+ // Sort subdetectors alphabetically.
+ List<String> subdets = new ArrayList<String>();
+ subdets.addAll( detector.getSubdetectors().keySet() );
+ java.util.Collections.sort( subdets );
+
+ // Layout row.
+ td = addLayoutRow( tbl );
+
+ // Add subdetector index.
+ subdetectorIndex( td, detector, subdets );
+
+ // Header.
+ addHeader2( td, "Subdetector Details" );
+
+ // Add subdetectors.
+ for ( String subdetName : subdets )
+ {
+ // Layout row.
+ td = addLayoutRow( tbl );
+
+ // Add subdetector info.
+ subdetector( td, detector.getSubdetector( subdetName ) );
+ }
+
+ // Layout row.
+ td = addLayoutRow( tbl );
+
+ // Constants.
+ defines( td, detector );
+
+ // Layout row.
+ td = addLayoutRow( tbl );
+
+ // Fields.
+ fields( td, detector );
+
+ return root;
+ }
+
+ private static void subdetectorIndex( Element parent, Detector detector, List<String> names )
+ {
+ // Header.
+ addHeader2( parent, "Subdetector Table" );
+
+ // Table.
+ Element tbl = new Element( "table" );
+ tbl.setAttribute( "border", "1" );
+ tbl.setAttribute( "cellpadding", "2" );
+ parent.addContent( tbl );
+
+ // todo: addHeaderRow method
+ addRow( tbl, new Element( "b" ).setText( "Subdetector" ), new Element( "b" ).setText( "Readout" ), new Element( "b" ).setText( "System ID" ) );
+
+ for ( String subdetName : names )
+ {
+ Subdetector subdet = detector.getSubdetector( subdetName );
+ String readoutName = "-";
+ if ( subdet.getReadout() != null )
+ {
+ readoutName = subdet.getReadout().getName();
+ }
+
+ addRow( tbl, new Link( subdetName, "#" + subdetName ), readoutName, subdet.getSystemID() );
+ }
+ }
+
+ private static void detectorHeader( Element parent, Detector detector )
+ {
+ addHeader2( parent, "Summary" );
+
+ Element tbl = new Element( "table" );
+ tbl.setAttribute( "border", "1" );
+ tbl.setAttribute( "cellpadding", "2" );
+ parent.addContent( tbl );
+
+ Header header = detector.getHeader();
+
+ addLabeledRow( tbl, "Detector", detector.getName() );
+ addLabeledRow( tbl, "Title", header.getTitle() );
+ addLabeledRow( tbl, "Comment", header.getComment() );
+ addLabeledRow( tbl, "Author", header.getAuthor() );
+ addLabeledRow( tbl, "Status", header.getStatus() );
+ addLabeledRow( tbl, "Version", header.getVersion() );
+ addLabeledRow( tbl, "Documentation", new Link( header.getURL() ) );
+ addLabeledRow( tbl, "Zip File", new Link( "http://www.lcsim.org/detectors/" + detector.getName() + ".zip" ) );
+ }
+
+ private static void subdetector( Element parent, Subdetector subdet )
+ {
+ // Make bookmark in page.
+ bookmark( parent, subdet.getName() );
+
+ // Subdetector data table.
+ Element tbl = new Element( "table" );
+ tbl.setAttribute( "border", "1" );
+ tbl.setAttribute( "width", "80%" );
+ tbl.setAttribute( "cellpadding", "2" );
+ parent.addContent( tbl );
+
+ // Basic info.
+ addLabeledRow( tbl, "Subdetector", subdet.getName() );
+ addLabeledRow( tbl, "System ID", subdet.getSystemID() );
+ addLabeledRow( tbl, "Type", subdet.isCalorimeter() ? "Calorimeter" : "Tracker" );
+ addLabeledRow( tbl, "Class", subdet.getClass().getSimpleName() );
+ addLabeledRow( tbl, "Endcap", subdet.isEndcap() ? true : false );
+ addLabeledRow( tbl, "Readout", subdet.getReadout() != null ? subdet.getReadout().getName() : "NONE" );
+ addLabeledRow( tbl, "Tracking Vol", subdet.isInsideTrackingVolume() );
+ addLabeledRow( tbl, "ID Description", subdet.getReadout() != null ? subdet.getIDDecoder().getIDDescription().toString() : "NONE" );
+
+ if ( subdet instanceof AbstractLayeredSubdetector )
+ {
+ AbstractLayeredSubdetector layers = ( AbstractLayeredSubdetector ) subdet;
+
+ addLabeledRow( tbl, "Number Of Layers", layers.getNumberOfLayers() );
+ addLabeledRow( tbl, "Interaction Lengths", layers.getInteractionLengths() );
+ addLabeledRow( tbl, "Radiation Lengths", layers.getRadiationLengths() );
+ }
+
+ // Calorimeter info.
+ if ( subdet instanceof Calorimeter )
+ {
+ Calorimeter cal = ( Calorimeter ) subdet;
+ addLabeledRow( tbl, "Calorimeter Type", cal.getCalorimeterType().toString() );
+ addLabeledRow( tbl, "Number Of Sides", cal.getNumberOfSides() );
+ addLabeledRow( tbl, "Inner Radius", cal.getInnerRadius() + " mm" );
+ addLabeledRow( tbl, "Outer Radius", cal.getOuterRadius() + " mm" );
+ addLabeledRow( tbl, "Z Length", cal.getZLength() + " mm" );
+ addLabeledRow( tbl, "Inner Z", cal.getInnerZ() + " mm" );
+ addLabeledRow( tbl, "Outer Z", cal.getOuterZ() + " mm" );
+ addLabeledRow( tbl, "Section Phi", cal.getSectionPhi() + " radians" );
+ if ( subdet.getReadout() != null && subdet.getReadout().getIDDecoder() instanceof Segmentation )
+ {
+ Segmentation seg = ( Segmentation ) subdet.getReadout().getIDDecoder();
+ addLabeledRow( tbl, "Segmentation Type", seg.getClass().getSimpleName() );
+ }
+ addLabeledRow( tbl, "Cell Size U", subdet.getReadout() != null ? cal.getCellSizeU() + " mm" : "NA" );
+ addLabeledRow( tbl, "Cell Size V", subdet.getReadout() != null ? cal.getCellSizeV() + " mm" : "NA" );
+ }
+ }
+
+ private static void defines( Element parent, Detector detector )
+ {
+ bookmark( parent, "defines" );
+
+ addHeader2( parent, "Constants" );
+
+ Element tbl = new Element( "table" );
+ tbl.setAttribute( "border", "1" );
+ tbl.setAttribute( "width", "50%" );
+ tbl.setAttribute( "cellpadding", "1" );
+ parent.addContent( tbl );
+
+ // Alpha sort.
+ Map<String, Constant> cmap = detector.getConstants();
+ List<String> constants = new ArrayList<String>( cmap.keySet() );
+ java.util.Collections.sort( constants );
+
+ for ( String key : constants )
+ {
+ addLabeledRow( tbl, key, cmap.get( key ).getValue() );
+ }
+ }
+
+ private static void fields( Element parent, Detector detector )
+ {
+ bookmark( parent, "fields" );
+
+ addHeader2( parent, "Fields" );
+
+ Element tbl = new Element( "table" );
+ parent.addContent( tbl );
+ tbl.setAttribute( "border", "1" );
+ tbl.setAttribute( "width", "50%" );
+
+ for ( Map.Entry<String, Field> entry : detector.getFields().entrySet() )
+ {
+ Field field = entry.getValue();
+ addLabeledRow( tbl, "Field", entry.getKey() );
+ addLabeledRow( tbl, "Type", field.getClass().getSimpleName() );
+ if ( field instanceof Solenoid )
+ {
+ Solenoid solenoid = ( Solenoid ) field;
+ addLabeledRow( tbl, "Inner Field", solenoid.getInnerField()[ 2 ] + " TeV" );
+ addLabeledRow( tbl, "Outer Field", solenoid.getOuterField()[ 2 ] + " TeV" );
+ addLabeledRow( tbl, "Z Max", solenoid.getZMax() + " mm" );
+ addLabeledRow( tbl, "Outer Radius 2", solenoid.getOuterRadius2() + " mm" );
+ }
+ }
+ }
+
+ private static void addHeader2( Element parent, String text )
+ {
+ Element h = new Element( "h2" );
+ h.setText( text );
+ parent.addContent( h );
+ }
+
+ private static Element addLayoutRow( Element tbl )
+ {
+ Element tr = new Element( "tr" );
+ tbl.addContent( tr );
+ Element td = new Element( "td" );
+ tr.addContent( td );
+ return td;
+ }
+
+
+ private static void addRow( Element table, Object... values )
+ {
+ if ( !table.getName().equals( "table" ) )
+ {
+ throw new RuntimeException( "Element is not a <table>." );
+ }
+
+ Element tr = new Element( "tr" );
+ table.addContent( tr );
+
+ for ( Object value : values )
+ {
+ Element td = new Element( "td" );
+ if ( value instanceof String )
+ td.setText( ( String ) value );
+ else if ( value instanceof Element )
+ td.addContent( ( Element ) value );
+ else
+ td.setText( value.toString() );
+ tr.addContent( td );
+ }
+ }
+
+ private static void addLabeledRow( Element table, String label, Object value )
+ {
+ if ( !table.getName().equals( "table" ) )
+ {
+ throw new RuntimeException( "Element is not a <table>." );
+ }
+
+ Element tr = new Element( "tr" );
+ table.addContent( tr );
+
+ // Label.
+ Element td = new Element( "td" );
+ td.setAttribute( "width", "30%" );
+ Element b = new Element( "b" );
+ td.addContent( b );
+ b.setText( label );
+ tr.addContent( td );
+
+ // Value.
+ td = new Element( "td" );
+ tr.addContent( td );
+ if ( value instanceof String )
+ td.setText( ( String ) value );
+ else if ( value instanceof Element )
+ td.addContent( ( Element ) value );
+ else
+ td.setText( value.toString() );
+ }
+
+ private static void bookmark( Element parent, String text )
+ {
+ Element a = new Element( "a" );
+ a.setAttribute( "name", text );
+ parent.addContent( a );
+ }
+
+ private static class Link extends Element
+ {
+ Link( String label, String url )
+ {
+ super( "a" );
+ this.setAttribute( "href", url );
+ this.setText( label );
+ }
+
+ Link( String url )
+ {
+ super( "a" );
+ this.setAttribute( "href", url );
+ this.setText( url );
+ }
+ }
+}
\ No newline at end of file