GeomConverter/src/org/lcsim/material
diff -u -r1.15.2.2 -r1.15.2.3
--- XMLMaterialManager.java 6 Oct 2005 00:20:50 -0000 1.15.2.2
+++ XMLMaterialManager.java 6 Oct 2005 09:10:04 -0000 1.15.2.3
@@ -26,42 +26,42 @@
{
/** Location of elements resource. */
private static final String elementResource = "elements.xml";
-
+
/** Location of materials resource. */
- private static final String materialResource = "materials.xml";
-
+ private static final String materialResource = "materials.xml";
+
/** Static instance containing elements data. */
static private XMLMaterialManager elements = new XMLMaterialManager(true);
-
+
/** Static instance containing materials data. */
static private XMLMaterialManager materials = new XMLMaterialManager(elements);
-
+
/** State var for element load. */
private static boolean loadedElements = false;
-
+
/** State var for default material load. */
private static boolean loadedMaterials = false;
-
+
/** Parent factory for default lkp if not found here. */
private XMLMaterialManager parent;
-
+
/** Current XML document of this manager. */
private Document currentDoc;
-
+
/** Map of material names to materials. */
protected LinkedHashMap<String, org.jdom.Element> materialMap;
-
+
/** Map of element names to elements. */
protected LinkedHashMap<String, org.jdom.Element> elementMap;
-
+
private static MaterialFromGDMLCnv materialCnv = new MaterialFromGDMLCnv();
-
+
/** Return static manager containing chem element data. */
public static XMLMaterialManager elements()
{
return elements;
}
-
+
/** Return static manager with default composite materials and elements as parent. */
public static XMLMaterialManager materials()
{
@@ -69,10 +69,10 @@
{
setupMaterials();
}
-
+
return materials;
}
-
+
/** Setup materials in the static material instance manager. */
private static void setupMaterials()
{
@@ -92,7 +92,7 @@
System.err.println("WARNING: Ignored call to setupMaterials().");
}
}
-
+
/** Create a material manager that has elements as parent with additional materials from InputStream. */
private static XMLMaterialManager create(InputStream in) throws IOException
{
@@ -100,7 +100,7 @@
{
throw new IOException("InputStream is null.");
}
-
+
try
{
return new XMLMaterialManager(elements, in);
@@ -112,112 +112,112 @@
throw io;
}
}
-
+
/** Create a material manager that has elements as parent with additional materials from URL. */
public static XMLMaterialManager create(URL url) throws IOException
{
return create(url.openStream());
}
-
+
/** Create a material manager that has elements as parent with no initial material data. */
public static XMLMaterialManager create()
{
return new XMLMaterialManager(elements);
}
-
+
/** Create a material manager with given parent. */
public static XMLMaterialManager create(XMLMaterialManager p)
{
return new XMLMaterialManager(p);
}
-
+
/** Ctor for parentless XMLMaterialFactory with (optional) element data. */
public XMLMaterialManager(boolean loadElements)
{
parent = null;
createMaps();
-
+
if (loadElements)
{
loadElements();
}
}
-
+
private XMLMaterialManager(XMLMaterialManager p, InputStream in) throws JDOMException, IOException
{
parent = p;
createMaps();
loadFromStream(in);
}
-
+
/** Ctor for XMLMaterialFactory with given parent and no initial material data. */
private XMLMaterialManager(XMLMaterialManager p)
{
parent = p;
createMaps();
}
-
+
/** Create the HashMaps to store material and element objects. */
private void createMaps()
{
materialMap = new LinkedHashMap();
elementMap = new LinkedHashMap();
}
-
+
/** Get the material map. */
private LinkedHashMap<String, org.jdom.Element> getMaterialMap()
{
return materialMap;
}
-
+
/** Get the chem element map. */
private LinkedHashMap<String, org.jdom.Element> getElementMap()
{
return elementMap;
}
-
+
/** Clear contents of the maps. */
public void resetMaps()
{
materialMap.clear();
elementMap.clear();
}
-
+
public void setParentManager(XMLMaterialManager mgr)
{
parent = mgr;
}
-
+
/** Set current document source. */
private void setCurrentDocument(Document d)
{
currentDoc = d;
}
-
+
/** Return size of the material map. */
public int getNumberOfMaterials()
{
return materialMap.size();
}
-
+
/** Return size of the element map. */
public int getNumberOfElements()
{
return elementMap.size();
}
-
+
/** Check if this factory has a parent for default lookups. */
public boolean hasParentManager()
{
return (parent != null);
}
-
+
/** Get the current XML document. */
public Document getCurrentDocument()
{
return currentDoc;
}
-
+
/**
* Attempts to find the "materials" root element using two possible cases.
*
@@ -240,7 +240,7 @@
{
m = d.getRootElement().getChild("materials");
}
-
+
if ( m == null )
{
throw new JDOMException("Document has no materials element.");
@@ -250,16 +250,16 @@
{
throw new JDOMException("Document has no root element.");
}
-
+
return m;
}
-
+
/** Finds the materials element in the current document. */
private org.jdom.Element getMaterialsRoot() throws JDOMException
{
return getMaterialsRoot(currentDoc);
}
-
+
/** Add a material to the map. */
private void addMaterial(org.jdom.Element e) throws JDOMException
{
@@ -267,29 +267,29 @@
{
throw new JDOMException("org.jdom.Element argument is not named material.");
}
-
+
if (materialMap.get(e.getAttributeValue("name")) == null )
{
materialMap.put(e.getAttributeValue("name"), e);
}
}
-
+
/** Add an element to the map. */
private void addElement(org.jdom.Element e) throws JDOMException
- {
+ {
if ( e.getName() != "element")
{
throw new JDOMException("Element is not named element.");
}
-
+
elementMap.put(e.getAttributeValue("name"), e);
}
-
+
/** Find a material in the map. */
protected org.jdom.Element getMaterial(String matName)
- {
+ {
org.jdom.Element m = materialMap.get(matName);
-
+
if ( m == null )
{
if ( hasParentManager() )
@@ -297,16 +297,16 @@
//System.out.println("par lkp");
m = parent.getMaterial(matName);
}
- }
-
+ }
+
return m;
}
-
+
/** Find an element in the map. */
private org.jdom.Element getElement(String elemName)
{
org.jdom.Element e = elementMap.get(elemName);
-
+
if ( e == null )
{
if ( hasParentManager() )
@@ -314,10 +314,10 @@
e = parent.getElement(elemName);
}
}
-
+
return e;
}
-
+
/** Find an element in the current document without affecting the map. */
private org.jdom.Element findElementInDocument(String elemName, Document d) throws JDOMException
{
@@ -332,26 +332,26 @@
break;
}
}
-
+
/** Look in parent. */
if ( fnd == null && hasParentManager() )
{
fnd = parent.findElementInDocument(elemName);
}
-
+
return fnd;
}
-
+
private org.jdom.Element findElementInDocument(String elemName) throws JDOMException
{
return findElementInDocument(elemName, currentDoc);
}
-
+
/** Find a material in the current document without affecting the map. */
private org.jdom.Element findMaterialInDocument(String matName, Document d) throws JDOMException
{
org.jdom.Element me = getMaterialsRoot(d);
-
+
org.jdom.Element fnd = null;
for ( Object o : me.getChildren("material"))
{
@@ -362,51 +362,51 @@
break;
}
}
-
+
/** Look in parent. */
if ( fnd == null && hasParentManager() )
{
fnd = parent.findMaterialInDocument(matName);
}
-
+
return fnd;
}
-
+
private org.jdom.Element findMaterialInDocument(String matName) throws JDOMException
{
return findMaterialInDocument(matName, currentDoc);
}
-
+
/* Load all materials and elements from an InputStream. */
private void loadFromStream(InputStream in) throws JDOMException, IOException
{
setCurrentDocument(makeDocument(in) );
loadFromDocument();
}
-
+
private Document makeDocument(InputStream in) throws JDOMException, IOException
{
SAXBuilder builder = new SAXBuilder();
return builder.build(in);
}
-
+
/** Load all materials and elements from the current document. */
private void loadFromDocument() throws JDOMException
{
loadFromDocument(currentDoc);
}
-
+
/** Core method for loading material and element XML data from an external entity. */
private void loadFromDocument(Document d) throws JDOMException
{
org.jdom.Element root = getMaterialsRoot(d);
-
+
for ( org.jdom.Element e : (List<org.jdom.Element>) root.getChildren() )
{
String elemName = e.getName();
if ("material".equals(elemName))
{
- addMaterial(e);
+ addMaterial(e);
}
else if ("element".equals(elemName))
{
@@ -418,7 +418,7 @@
}
}
}
-
+
/**
* Recursively add the referenced materials or elements from a single material node.
* It will also automatically look in the parent XMLMaterialManager.
@@ -430,22 +430,22 @@
if ( "material".equals(e.getName()))
{
List l = e.getChildren("composite");
-
+
if ( l.isEmpty() )
{
l = e.getChildren("fraction");
}
-
+
if ( l.isEmpty() )
{
throw new JDOMException("Material does not contain fraction or composite tags.");
}
-
+
for ( org.jdom.Element ce : (List<org.jdom.Element>) l)
{
-
+
String ref = ce.getAttributeValue("ref");
-
+
/* Add if element. */
org.jdom.Element fnd = findElementInDocument(ref, d);
if (fnd != null)
@@ -456,7 +456,7 @@
else
{
fnd = findMaterialInDocument(ref, d);
-
+
if ( fnd != null )
{
/* Recursively add references of this material. */
@@ -468,7 +468,7 @@
}
}
}
-
+
/* Lastly, add the material that was passed in, after its references were resolved (recursively). */
addMaterial(e);
}
@@ -478,7 +478,7 @@
throw new JDOMException("Element passed to addReferenceElements() is null.");
}
}
-
+
/**
* 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.
@@ -490,29 +490,29 @@
{
throw new IllegalArgumentException("List cannot be null.");
}
-
+
org.jdom.Element m = getMaterial(matName);
-
+
if ( m != null )
{
if ( "material".equals(m.getName()))
{
List l = m.getChildren("composite");
-
+
if ( l.isEmpty() )
{
l = m.getChildren("fraction");
}
-
+
if ( l.isEmpty() )
{
throw new JDOMException("Material does not contain fraction or composite tags.");
}
-
+
for ( org.jdom.Element ce : (List<org.jdom.Element>) l )
{
String ref = ce.getAttributeValue("ref");
-
+
/* Add if element. */
org.jdom.Element fnd = getElement(ref);
if (fnd != null)
@@ -526,7 +526,7 @@
else
{
fnd = getMaterial(ref);
-
+
/* Recursively add references of this material. */
if ( fnd != null )
{
@@ -534,7 +534,7 @@
}
}
}
-
+
/* Lastly, add the material that was passed in after its references are recursively resolved. */
if ( !refs.contains(m))
{
@@ -547,33 +547,33 @@
}
}
}
-
+
private void addReferences(org.jdom.Element e) throws JDOMException
{
addReferences(e, currentDoc);
}
-
+
private Document cloneMaterialsRoot(org.jdom.Element e) throws JDOMException
{
if ( e.getName() != "materials" )
{
throw new JDOMException("Element passed as materials root has wrong name.");
}
-
+
org.jdom.Element matRoot = new org.jdom.Element("materials");
matRoot.setContent(e.cloneContent() );
Document d = new Document();
d.setRootElement(matRoot);
setCurrentDocument(d);
-
+
return currentDoc;
}
-
+
public void setMaterialsRoot(org.jdom.Element e) throws JDOMException
{
setCurrentDocument(cloneMaterialsRoot(e));
}
-
+
/** Copy all elements into LCDD. */
private void copyElementsToLCDD(LCDD lcdd)
{
@@ -583,38 +583,38 @@
lcdd.addElement((org.jdom.Element) e.clone());
}
}
-
+
/** Copy all materials into LCDD. */
private void copyMaterialsToLCDD(LCDD lcdd) throws JDOMException
{
for (Object o : getMaterialMap().values() )
{
- org.jdom.Element e = (org.jdom.Element) o;
-
+ org.jdom.Element e = (org.jdom.Element) o;
+
Material m = makeLCDDMaterial(e);
lcdd.addMaterial(m);
}
}
-
+
private static Material makeLCDDMaterial(org.jdom.Element m) throws JDOMException
{
if ( m.getName() != "material")
{
throw new JDOMException("XML element to make material is not of type material.");
}
-
+
Material mat = new Material(m.getAttributeValue("name"));
mat.addContent(m.cloneContent());
return mat;
}
-
+
private static org.jdom.Element makeLCDDElement(org.jdom.Element e) throws JDOMException
{
if (e.getName() != "element")
{
throw new JDOMException("XML element to make element is not of type element.");
}
-
+
org.jdom.Element ee = new org.jdom.Element("element");
ee.addContent(e.cloneContent());
ee.setAttribute("name", e.getAttributeValue("name"));
@@ -622,17 +622,17 @@
ee.setAttribute("Z", e.getAttributeValue("Z"));
return ee;
}
-
+
public void resolveLCDDMaterialReference(String name, LCDD lcdd) throws JDOMException
- {
+ {
List l = new ArrayList();
-
+
makeReferenceList(name, l);
-
+
for (Object o : l)
{
org.jdom.Element e = (org.jdom.Element) o;
-
+
/* material */
if ( "material".equals(e.getName()) )
{
@@ -647,26 +647,26 @@
}
}
}
-
+
/** Copy elements and materials into LCDD. */
public void copyToLCDD(LCDD lcdd) throws JDOMException
{
copyElementsToLCDD(lcdd);
copyMaterialsToLCDD(lcdd);
}
-
+
public void setLCDD(LCDD lcdd)
{
materialCnv.setLCDD(lcdd);
}
-
+
/** 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");
-
+
setMaterialsRoot(lccdd.getChild("materials"));
-
+
for (Iterator i = materials.getChildren().iterator(); i.hasNext(); )
{
org.jdom.Element element = (org.jdom.Element) i.next();
@@ -675,9 +675,8 @@
addReferences(element);
}
}
- MaterialManager.printMaterials();
}
-
+
/**
* Cache contents of the default chemical elements file, including their materials definitions.
*/
@@ -698,8 +697,8 @@
{
System.err.println("WARNING: Already loaded elements. loadElements() call was ignored.");
}
- }
-
+ }
+
/** Make org.lcsim.Material impl class for all org.jdom.Element in the map. */
public void makeMaterials(LCDD lcdd) throws JDOMException
{
@@ -710,36 +709,36 @@
++cnt;
}
}
-
+
/**
* Make org.lcsim.Material from this org.jdom.Element material tag in GDML format,
* defining referenced materials recursively.
*/
protected 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 )
- {
+ {
MaterialDefinitionType mdt = MaterialDefinitionType.getMaterialDefinitionType(materialElement);
String tagname = mdt.getTagName();
-
+
/** First create the referenced materials. */
for ( Object o : materialElement.getChildren(tagname) )
{
org.jdom.Element tag = (org.jdom.Element) o;
String ref = tag.getAttributeValue("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);
}
}
-}
\ No newline at end of file
+}