Print

Print


Commit in GeomConverter/src/org/lcsim/material on MAIN
XMLMaterialManager.java+65-1021.9 -> 1.10
Various changes and updates, including change to new reference method for elements.

GeomConverter/src/org/lcsim/material
XMLMaterialManager.java 1.9 -> 1.10
diff -u -r1.9 -r1.10
--- XMLMaterialManager.java	30 Jun 2005 00:53:16 -0000	1.9
+++ XMLMaterialManager.java	1 Jul 2005 00:06:58 -0000	1.10
@@ -21,9 +21,6 @@
  *
  * Utility class for extracting material elements and their referenced elements
  * from a GDML, LCDD or Compact XML file.
- *
- * TODO: Return implementation classes for Material and Element rather than just XML elements.
- * TODO: Seems like an excessive number of methods are public
  */
 public class XMLMaterialManager
 {
@@ -87,6 +84,7 @@
             try
             {
                 materials.loadFromStream(XMLMaterialManager.class.getResourceAsStream((materialResource)));
+                //materials.makeMaterials(null);
             }
             catch (Exception e)
             {
@@ -102,21 +100,21 @@
     /** Create a material manager that has elements as parent with additional materials from InputStream. */
     public static XMLMaterialManager create(InputStream in) throws IOException
     {
-       if ( in == null )
-       {
+        if ( in == null )
+        {
             throw new IOException("InputStream is null.");
-       }
+        }
         
-       try
-       {
-           return new XMLMaterialManager(elements, in);
-       }
-       catch (JDOMException x)
-       {
-           IOException io = new IOException("Error parsing materials definitions");
-           io.initCause(x);
-           throw io;
-       }
+        try
+        {
+            return new XMLMaterialManager(elements, in);
+        }
+        catch (JDOMException x)
+        {
+            IOException io = new IOException("Error parsing materials definitions");
+            io.initCause(x);
+            throw io;
+        }
     }
     
     /** Create a material manager that has elements as parent with additional materials from URL. */
@@ -153,8 +151,8 @@
     {
         parent = p;
         createMaps();
-        loadFromStream(in);       
-    } 
+        loadFromStream(in);
+    }
     
     /** Ctor for XMLMaterialFactory with given parent and no initial material data. */
     private XMLMaterialManager(XMLMaterialManager p)
@@ -210,7 +208,7 @@
     public boolean isElement(org.jdom.Element e)
     {
         return "element".equals(e.getName());
-    }
+    }        
     
     /** Return size of the material map. */
     public int getNumberOfMaterials()
@@ -245,7 +243,7 @@
      * @param d Document to search for materials element.
      *
      */
-    org.jdom.Element getMaterialsRoot(Document d) throws JDOMException
+    public org.jdom.Element getMaterialsRoot(Document d) throws JDOMException
     {
         org.jdom.Element m = null;
         if (d.hasRootElement() )
@@ -273,7 +271,7 @@
     }
     
     /** Finds the materials element in the current document. */
-    org.jdom.Element getMaterialsRoot() throws JDOMException
+    public org.jdom.Element getMaterialsRoot() throws JDOMException
     {
         return getMaterialsRoot(currentDoc);
     }
@@ -442,20 +440,20 @@
         //mcount = ecount = 0;
         
         for ( org.jdom.Element e : (List<org.jdom.Element>) root.getChildren() )
-        {   
+        {
             String elemName = e.getName();
             if ("material".equals(elemName))
             {
                 addMaterial(e);
-
-              /* DEBUG: print materials read and track count. */
-              //System.out.println("read material: " + e.getAttributeValue("name") );
-              //++ecount;
+                
+                /* DEBUG: print materials read and track count. */
+                //System.out.println("read material: " + e.getAttributeValue("name") );
+                //++ecount;
             }
             else if ("element".equals(elemName))
             {
                 addElement(e);
-
+                
                 /* DEBUG: print elements read and track count. */
                 //System.out.println("read element: " + e.getAttributeValue("name") );
                 //++mcount;
@@ -482,6 +480,7 @@
      */
     public void addReferences(org.jdom.Element e, Document d) throws JDOMException
     {
+        //System.out.println("XMLMaterialManager.addReferences() - adding refs for " + e.getAttributeValue("name"));
         if ( e != null )
         {
             if ( "material".equals(e.getName()))
@@ -540,8 +539,8 @@
         }
     }
     
-    /** 
-     * Add to the list all XML elements, materials or chem elements, that are (sub)references of the named material. 
+    /**
+     * Add to the list all XML elements, materials or chem elements, that are (sub)references of the named material.
      * @param matName Name of material to start recursive reference lookup.
      * @param refs    List that will contain all references as XML elements.
      */
@@ -588,7 +587,7 @@
                     fnd = getMaterial(ref);
                     
                     /* Recursively add references of this material. */
-                    makeReferenceList(fnd.getAttributeValue("name"), refs);                    
+                    makeReferenceList(fnd.getAttributeValue("name"), refs);
                 }
             }
             
@@ -684,7 +683,7 @@
     
     public void resolveLCDDMaterialReference(String name, LCDD lcdd) throws JDOMException
     {
-        //System.out.println("lazy lkp for LCDD: " + name);                
+        //System.out.println("lazy lkp for LCDD: " + name);
         
         List l = new ArrayList();
         
@@ -709,40 +708,7 @@
             }
         }
     }
-    
-    /**
-     * This is called "lazily" within LCDD.getMaterial() when a material is not found.
-     */
-    public Material addMaterialReferenceToLCDD(String name, LCDD lcdd) throws JDOMException
-    {
-        /** First look for the material. */
-        org.jdom.Element m = getMaterial(name);
         
-        /** Throw if material was not found. */
-        if ( m == null )
-        {
-            throw new JDOMException("Material does not exist: " + name);
-        }
-        
-        /** Lookup associated single element. */
-        org.jdom.Element e = null;
-        String elemName = name+"_e";
-        e = getElement(elemName);
-        
-        /** Add the element [sic] to LCDD, checking if it exists already. */
-        if ( lcdd.getElement(elemName) == null)
-        {
-            lcdd.addElement((org.jdom.Element) e.clone());
-        }
-        
-        /** Add the material to LCDD. */
-        Material mat = makeLCDDMaterial(m);
-        lcdd.addMaterial(mat);
-        
-        /** Return the Material to caller. */
-        return mat;
-    }
-    
     /** Copy elements and materials into LCDD. */
     public void copyToLCDD(LCDD lcdd) throws JDOMException
     {
@@ -755,7 +721,7 @@
         materialCnv.setLCDD(lcdd);
     }
     
-    /** Read materials from a compact description and cache materials and their dependencies in this manager. */
+    /** Read materials from a compact description and cache materials and their dependencies into this manager. */
     public void addReferencesFromCompact(org.jdom.Element lccdd) throws JDOMException
     {
         org.jdom.Element materials = lccdd.getChild("materials");
@@ -766,7 +732,7 @@
         {
             org.jdom.Element element = (org.jdom.Element) i.next();
             if ("material".equals(element.getName()))
-            {
+            {                
                 addReferences(element);
             }
         }
@@ -797,60 +763,57 @@
     /**
      *
      */
-    public org.jdom.Element getDefaultMaterial() 
+    public org.jdom.Element getDefaultMaterial()
     {
         return getMaterial(defaultMaterialName);
-    }        
+    }
     
     /** Make org.lcsim.Material impl class for all org.jdom.Element in the map. */
-    void makeMaterials(LCDD lcdd) throws JDOMException
+    public void makeMaterials(LCDD lcdd) throws JDOMException
     {
+        //System.out.println("XMLMaterialManager.makeMaterials()");
+        int cnt = 0;
         for (Element me : materialMap.values() )
-        {
+        {            
             makeMaterial(me, lcdd);
+            ++cnt;
         }
+        //System.out.println("made " + cnt + " materials");
     }
     
     /**
-     * Make org.lcsim.Material from this org.jdom.Element material tag in GDML format, 
-     * defining referenced materials recursively. 
+     * Make org.lcsim.Material from this org.jdom.Element material tag in GDML format,
+     * defining referenced materials recursively.
      */
-    void makeMaterial(org.jdom.Element materialElement, LCDD lcdd) throws JDOMException
-    {    
-        System.out.println("makeMaterial - " + materialElement.getAttributeValue("name") );
-        
-        MaterialDefinitionType mdt = MaterialDefinitionType.getMaterialDefinitionType(materialElement);   
-        String tagname = mdt.getTagName();
-        
-        /** First need to add referenced materials. */
-        for ( Object o : materialElement.getChildren(tagname) )
+    public void makeMaterial(org.jdom.Element materialElement, LCDD lcdd) throws JDOMException
+    {        
+        /* Add the referenced material if it does not exist already. */
+        if ( MaterialManager.getMaterial(materialElement.getAttributeValue("name")) == null )
         {
-            org.jdom.Element tag = (org.jdom.Element) o;
-            String ref = tag.getAttributeValue("ref");
-            
-            System.out.println("child ref: " + ref);
+            //System.out.println("XMLMaterialManager.makeMaterial() - " + materialElement.getAttributeValue("name") );
             
-            org.jdom.Element refMatElem = getMaterial(ref);       
+            MaterialDefinitionType mdt = MaterialDefinitionType.getMaterialDefinitionType(materialElement);
+            String tagname = mdt.getTagName();
             
-            /* Call this function recursively to add referenced materials. */
-            if ( refMatElem != null )
+            /** First create the referenced materials. */
+            for ( Object o : materialElement.getChildren(tagname) )
             {
-                this.makeMaterial(refMatElem, lcdd);
-            }
-            /*
-            else 
-            {
-                if ( getElement(ref) == null )
-                {
-                    throw new JDOMException("Not a valid element or material reference: " + ref);
+                org.jdom.Element tag = (org.jdom.Element) o;
+                String ref = tag.getAttributeValue("ref");
+                
+                //System.out.println("child ref: " + ref);
+                
+                org.jdom.Element refMatElem = getMaterial(ref);
+                
+                /* Call this function recursively to add referenced materials. */
+                if ( refMatElem != null )
+                {                    
+                    this.makeMaterial(refMatElem, lcdd);
                 }
             }
-             */
+            
+            /* Now that deps are added for this material, make impl object. */            
+            materialCnv.makeMaterial(materialElement, lcdd);
         }
-        
-        /* Now that deps are added for this material, make impl object. */
-        //org.lcsim.material.Material material = 
-        materialCnv.makeMaterial(materialElement, lcdd);
-        //MaterialManager.addMaterial(material);
     }
 }
\ No newline at end of file
CVSspam 0.2.8