GeomConverter/src/org/lcsim/geometry/compact
diff -u -r1.46 -r1.47
--- CompactReader.java 11 Mar 2011 19:22:20 -0000 1.46
+++ CompactReader.java 12 Mar 2011 00:37:28 -0000 1.47
@@ -28,7 +28,7 @@
* org.lcsim.geometry.GeometryReader class, which extends this.
*
* @author tonyj
- * @version $Id: CompactReader.java,v 1.46 2011/03/11 19:22:20 jeremy Exp $
+ * @version $Id: CompactReader.java,v 1.47 2011/03/12 00:37:28 jeremy Exp $
*
*/
public class CompactReader
@@ -88,7 +88,7 @@
readConstants(lccdd, det);
readRegions(lccdd, det);
readLimits(lccdd, det);
- //readMaterials(lccdd, det);
+ readMaterials(lccdd, det);
Map<String,Readout> readoutMap = readReadouts(lccdd, det);
readVisAttributes(lccdd, det);
readSubdetectors(lccdd, det, readoutMap);
@@ -230,26 +230,26 @@
}
}
}
-
- /*
- private void readMaterials(org.jdom.Element compact)
+
+ private void readMaterials(org.jdom.Element compact, Detector det)
{
+ // Setup XMLMatMgr's default data. This needs to be called before
+ // the compact materials are loaded in order to resolve references.
+ XMLMaterialManager.setup();
+
+ // Create XMLMatMgr for this detector's materials.
if (compact.getChild("materials") != null)
{
xmat = new XMLMaterialManager(compact.getChild("materials"));
+
+ // FIXME Need to call this here???
+ //matmgr.addReferencesFromCompact(lccdd);
}
- }
- */
-
- /** Create impl objects of materials and their references. */
-/*
- private void readMaterials(org.jdom.Element lccdd, Detector det) throws JDOMException
- {
- XMLMaterialManager matmgr = det.getMaterialManager();
- matmgr.addReferencesFromCompact(lccdd);
- matmgr.makeMaterials(null);
- }
-*/
+
+ // Set the detector's XMLMaterialManager, so it is accessible to
+ // clients such as the LCDD converter.
+ det.setXMLMaterialManager(xmat);
+ }
/**
* Create the VisAttributes objects from vis elements in the display block.
GeomConverter/src/org/lcsim/material
diff -u -r1.21 -r1.22
--- XMLMaterialManager.java 11 Mar 2011 19:22:20 -0000 1.21
+++ XMLMaterialManager.java 12 Mar 2011 00:37:28 -0000 1.22
@@ -12,7 +12,7 @@
import org.jdom.input.SAXBuilder;
/**
- * This class loads material data from XML files into the global {@link MaterialManager}.
+ * This class loads GDML data from a materials XML section into the {@link MaterialManager}.
*
* It is also used to keep track of compact material data for the conversion to/from
* compact descriptions.
@@ -21,14 +21,17 @@
* term "node" to refer to an XML element.
*
* @author jeremym
- * @version $Id: XMLMaterialManager.java,v 1.21 2011/03/11 19:22:20 jeremy Exp $
+ * @version $Id: XMLMaterialManager.java,v 1.22 2011/03/12 00:37:28 jeremy Exp $
*/
-// TODO Refactor material reference resolution spaghetti code.
-// TODO Replace parent/child structure with single manager.
-// FIXME Should not keep references to XML nodes from compact. To copy from compact to LCDD,
-// just the MaterialManager should be used to resolve references. XML can be constructed
-// from Material and MaterialElement objects rather than cloned from references to XML elements.
-// FIXME Move most of this logic to the CompactReader class.
+// TODO Refactor spaghetti/recursion code that does reference resolution.
+// TODO Replace parent/child structure of managers with a single manager or utility class.
+// TODO Move most of the logic for loading materials to the CompactReader class.
+// TODO Simplify the find/get methods for looking up XML elements. There are too many similar methods.
+// TODO Where possible, move additional methods into LCDDMaterialHelper class.
+// FIXME Should not keep references to XML nodes. To copy from compact to LCDD,
+// only the MaterialManager should be used to resolve references. LCDD material XML
+// can be constructed from Material and MaterialElement objects on the fly rather than cloned.
+// FIXME Deal with multiple unnecessary calls to setup() from clients.
public class XMLMaterialManager
{
// Location of elements resource in jar relative to org.lcsim.material pkg.
@@ -38,13 +41,13 @@
private static final String materialResource = "materials.xml";
// Instance of MaterialManager.
- private static MaterialManager mgr = MaterialManager.instance();
+ private static MaterialManager mgr;
// Static instance containing elements data.
- private static XMLMaterialManager elements = createMaterialElements();
+ private static XMLMaterialManager elements;
// Static instance containing materials data.
- private static XMLMaterialManager materials = createMaterials();
+ private static XMLMaterialManager materials;
// Parent manager of this one.
private XMLMaterialManager parent;
@@ -57,70 +60,54 @@
// Map of names to material element XML nodes known by this manager.
private HashMap<String, org.jdom.Element> elementMap = new LinkedHashMap<String, Element>();
-
+
/**
- * Reset state of static material data.
- */
- // TODO Call this from within compact.Detector?
- public static void reset()
+ * Setup static material data. This can be called to reset material data between runs
+ * with different Detectors that are run in the same Java process, such as a TestCase.
+ * This method must be called before the XMLMaterialManager is created for a compact document.
+ */
+ public static void setup()
{
- System.out.println("XMLMatMgr.reset()");
+ //System.out.println("XMLMatMgr.setup()");
- // Clear the material store.
+ // Clear the global material store.
MaterialManager.instance().clear();
+ mgr = MaterialManager.instance();
- // Build the element and material managers.
+ // Rebuild the default material data stores.
elements = createMaterialElements();
- materials = createMaterials();
+ materials = createMaterials();
}
/**
- * Get the map of XML material nodes known by this manager.
- * @return The map of names to material nodes.
- */
- public Map<String, Element> getMaterialXMLMap()
- {
- return materialMap;
- }
-
- /**
- * Get the map of XML chemical element nodes known by this manager.
- * @return The map of names to chemical element nodes.
- */
- public Map<String, Element> getElementXMLMap()
- {
- return elementMap;
- }
-
- /**
- * Create the static instance of the elements manager.
- * @return The manager containing elements data.
+ * Ctor for a manager with the given root XML node, probably pointing to the
+ * materials section of a compact detector description. Its parent is the default
+ * material manager.
+ * @param materialsRoot The XML element pointing to a materials section.
*/
- static private XMLMaterialManager createMaterialElements()
- {
- elements = new XMLMaterialManager();
- elements.loadElementsFromResource();
- return elements;
- }
-
- /**
- * Create the static instance of the material manager.
- * @return The manager containing default materials data.
- */
- static private XMLMaterialManager createMaterials()
+ public XMLMaterialManager(Element materialsRoot)
{
- materials = new XMLMaterialManager(elements);
- materials.loadMaterialsFromResource(materialResource);
- return materials;
+ // Bootstrap if static data is not setup yet.
+ if (elements == null)
+ {
+ setup();
+ }
+ parent = getDefaultMaterialManager();
+ setMaterialsRoot(materialsRoot);
+ loadMaterialsFromXML(materialsRoot);
}
-
+
/**
- * Get manager with default materials.
- * This method can be called to "bootstrap" the static material objects.
- * @return The manager with references to the default materials from the embedded resource.
+ * Get manager with default materials defined in the embedded resource file.
+ * @return The manager with references to the default material manager.
**/
public static XMLMaterialManager getDefaultMaterialManager()
{
+ // Bootstrap if static data is not setup yet.
+ if (elements == null)
+ {
+ setup();
+ }
return materials;
}
@@ -130,90 +117,32 @@
**/
public static XMLMaterialManager getElementsManager()
{
+ // Bootstrap if static data is not setup yet.
+ if (elements == null)
+ {
+ setup();
+ }
return elements;
}
-
- /**
- * The default ctor should not be used. Any "user" managers
- * must have a parent and/or a node pointing to a materials section.
- */
- private XMLMaterialManager()
- {}
-
+
/**
- * Ctor for manager with the given root XML node.
- * Its parent is the default material manager.
- * @param materialsRoot The XML element pointing to materials section.
+ * Get the map of XML material nodes known by this manager.
+ * @return The map of names to material nodes.
*/
- public XMLMaterialManager(Element materialsRoot)
+ public Map<String, Element> getMaterialXMLMap()
{
- parent = getDefaultMaterialManager();
- //MaterialXmlUtil.loadMaterials(materialsRoot);
- setMaterialsRoot(materialsRoot);
- loadMaterialsFromXML(materialsRoot);
+ return materialMap;
}
- /**
- * Ctor for XMLMaterialFactory with given parent and no initial material data.
- * @param p The parent manager.
- **/
- private XMLMaterialManager(XMLMaterialManager p)
- {
- parent = p;
- }
-
- /**
- * Set current document source.
- * @param d The Document with a materials node as its child.
- **/
- private void setCurrentDocument(Document d)
- {
- currentDoc = d;
- }
-
- /**
- * Check if this object has a parent manager.
- * @return True if this manager has a parent; false if not.
- **/
- protected boolean hasParentManager()
- {
- return (parent != null);
- }
-
/**
- * Get the current XML document known by this manager.
- * @return The current XML document.
- **/
- public Document getCurrentDocument()
+ * Get the map of XML chemical element nodes known by this manager.
+ * @return The map of names to chemical element nodes.
+ */
+ public Map<String, Element> getElementXMLMap()
{
- return currentDoc;
- }
-
- /**
- * Attempt to find the materials section in a document using two possible cases.
- *
- * 1) The materials section is the root element.
- * 2) The materials section is a child of the root element.
- *
- * @param d Document to search for materials element.
- */
- // FIXME This should be removed. Callers should have the materials node in hand.
- private static org.jdom.Element getMaterialsRoot(Document d)
- {
- org.jdom.Element m = null;
-
- if (d.getRootElement().getName() == "materials")
- {
- m = d.getRootElement();
- }
- else
- {
- m = d.getRootElement().getChild("materials");
- }
-
- return m;
+ return elementMap;
}
-
+
/**
* Add a material XML node to the lookup map.
* Does not create a Material object.
@@ -221,10 +150,7 @@
**/
public void addMaterialRef(org.jdom.Element e)
{
- if (materialMap.get(e.getAttributeValue("name")) == null)
- {
- materialMap.put(e.getAttributeValue("name"), e);
- }
+ materialMap.put(e.getAttributeValue("name"), e);
}
/**
@@ -241,6 +167,7 @@
* Find the XML element for a material.
* @return The XML element with name <code>matName</code> or null if doesn't exist.
*/
+ // FIXME Move to LCDDMatHelp
public org.jdom.Element getMaterialXML(String matName)
{
org.jdom.Element m = materialMap.get(matName);
@@ -255,7 +182,7 @@
}
return m;
- }
+ }
/**
* Find the XML node for a chemical element.
@@ -278,7 +205,7 @@
}
/**
- * Find a chemical element in the current document.
+ * Find a chemical element in a document.
* @return The XML node of the chemical element called <code>elemName</code>.
**/
public Element findMaterialElementXML(String elemName, Document d)
@@ -298,23 +225,21 @@
// Try the parent.
if (fnd == null && hasParentManager())
{
- fnd = parent.findXML(elemName);
+ fnd = parent.findMaterialElementXML(elemName);
}
return fnd;
}
-
+
/**
- * Find the XML node for the material or element called <code>elemName</code>.
- * @param elemName The name attribute value of the node.
- * @return The XML node with attribute name of <code>elemName</code> or null if not found.
- * @throws JDOMException
- */
- private org.jdom.Element findXML(String elemName)
+ * Get the current XML document known by this manager.
+ * @return The current XML document.
+ **/
+ public Document getCurrentDocument()
{
- return findMaterialElementXML(elemName, currentDoc);
+ return currentDoc;
}
-
+
/**
* Find a material in an XML document.
* @param matName The name of the material.
@@ -344,8 +269,101 @@
return fnd;
}
+
+ /**
+ * Create the static instance of the elements manager.
+ * @return The manager containing elements data.
+ */
+ static private XMLMaterialManager createMaterialElements()
+ {
+ elements = new XMLMaterialManager();
+ elements.loadElementsFromResource();
+ return elements;
+ }
+
+ /**
+ * Create the static instance of the material manager.
+ * @return The manager containing default materials data.
+ */
+ static private XMLMaterialManager createMaterials()
+ {
+ materials = new XMLMaterialManager(elements);
+ materials.loadMaterialsFromResource(materialResource);
+ return materials;
+ }
+
+ /**
+ * The default ctor should not be used. Any "user" managers
+ * must have a parent and/or a node pointing to a materials section.
+ */
+ private XMLMaterialManager()
+ {}
+
+ /**
+ * Ctor for XMLMaterialFactory with given parent and no initial material data.
+ * @param p The parent manager.
+ **/
+ private XMLMaterialManager(XMLMaterialManager p)
+ {
+ parent = p;
+ }
+
+ /**
+ * Set current document source.
+ * @param d The Document with a materials node as its child.
+ **/
+ private void setCurrentDocument(Document d)
+ {
+ currentDoc = d;
+ }
/**
+ * Check if this object has a parent manager.
+ * @return True if this manager has a parent; false if not.
+ **/
+ protected boolean hasParentManager()
+ {
+ return (parent != null);
+ }
+
+ /**
+ * Attempt to find the materials section in a document using two possible cases.
+ *
+ * 1) The materials section is the root element.
+ * 2) The materials section is a child of the root element.
+ *
+ * @param d Document to search for materials element.
+ */
+ // FIXME This should be removed. Callers should have the materials node in hand.
+ private static org.jdom.Element getMaterialsRoot(Document d)
+ {
+ org.jdom.Element m = null;
+
+ if (d.getRootElement().getName() == "materials")
+ {
+ m = d.getRootElement();
+ }
+ else
+ {
+ m = d.getRootElement().getChild("materials");
+ }
+
+ return m;
+ }
+
+ /**
+ * Find the XML node for the material or element called <code>elemName</code>
+ * in the current Document.
+ * @param elemName The name attribute value of the node.
+ * @return The XML node with attribute name of <code>elemName</code> or null if not found.
+ * @throws JDOMException
+ */
+ private org.jdom.Element findMaterialElementXML(String elemName)
+ {
+ return findMaterialElementXML(elemName, currentDoc);
+ }
+
+ /**
* Find a material in the manager's document.
* @param matName The name of the material.
* @return The node for the material called <code>matName</code> or null if not found.
@@ -505,7 +523,7 @@
*/
private void makeMaterialFromXML(Element matNode)
{
- System.out.println("makeMaterialFromXML - " + matNode.getAttributeValue("name"));
+ //System.out.println("makeMaterialFromXML - " + matNode.getAttributeValue("name"));
// Get the name of the material.
String name = matNode.getAttributeValue("name");