KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > chemistry > PeriodicTable


1 package JSci.chemistry;
2
3 import java.io.IOException JavaDoc;
4 import java.lang.Double JavaDoc;
5 import java.util.Collections JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import javax.xml.parsers.*;
9 import org.w3c.dom.*;
10 import org.xml.sax.SAXException JavaDoc;
11 import JSci.chemistry.periodictable.*;
12
13 /**
14 * This class provides access to the elements of the periodic table.
15 * @version 0.2
16 * @author Mark Hale
17 */

18 public final class PeriodicTable {
19         private static final Map JavaDoc table = new HashMap JavaDoc();
20         private static Map JavaDoc symbols;
21
22         private PeriodicTable() {}
23         /**
24         * Returns the name for a symbol.
25         */

26         public static String JavaDoc getName(String JavaDoc symbol) {
27                 if(symbols == null)
28                         symbols = loadIndex();
29                 return (String JavaDoc)symbols.get(symbol);
30         }
31         /**
32         * Returns an element.
33         */

34         public static Element getElement(String JavaDoc name) {
35                 name = name.toLowerCase();
36                 Element element = (Element) table.get(name);
37                 if(element == null) {
38                         element = loadElement("periodictable/"+name+".xml");
39                         if(element != null)
40                                 table.put(name, element);
41                 }
42                 return element;
43         }
44         /**
45         * Loads the XML index.
46         */

47         private static Map JavaDoc loadIndex() {
48                 DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
49                 try {
50                         DocumentBuilder docBuilder=docFactory.newDocumentBuilder();
51                         Document doc=docBuilder.parse(PeriodicTable.class.getResource("periodictable/index.xml").toString());
52                         Node root=doc.getDocumentElement();
53                         NodeList nl=root.getChildNodes();
54                         Map JavaDoc index = new HashMap JavaDoc();
55                         for(int i=0; i<nl.getLength(); i++) {
56                                 Node node = nl.item(i);
57                                 if(node.getNodeName().equals("element")) {
58                                         NamedNodeMap attr = node.getAttributes();
59                                         index.put(attr.getNamedItem("symbol").getNodeValue(), attr.getNamedItem("name").getNodeValue());
60                                 }
61                         }
62                         return index;
63                 } catch(ParserConfigurationException e) {
64                         return Collections.EMPTY_MAP;
65                 } catch(IOException JavaDoc e) {
66                         return Collections.EMPTY_MAP;
67                 } catch(SAXException JavaDoc e) {
68                         return Collections.EMPTY_MAP;
69                 }
70         }
71         /**
72         * Loads an element from its XML resource.
73         */

74         private static Element loadElement(String JavaDoc resname) {
75                 DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
76                 try {
77                         DocumentBuilder docBuilder=docFactory.newDocumentBuilder();
78                         Document doc=docBuilder.parse(PeriodicTable.class.getResource(resname).toString());
79                         Node root=doc.getDocumentElement();
80                         String JavaDoc group=root.getAttributes().getNamedItem("series").getNodeValue();
81                         NodeList nl=root.getChildNodes();
82                         String JavaDoc name=findStringValue(nl,"name");
83                         String JavaDoc symbol=findStringValue(nl,"symbol");
84                         Element elem;
85                         if(group.equals("non-metal"))
86                                 elem=new NonMetal(name,symbol);
87                         else if(group.equals("halogen"))
88                                 elem=new Halogen(name,symbol);
89                         else if(group.equals("noble-gas"))
90                                 elem=new NobleGas(name,symbol);
91                         else if(group.equals("metal"))
92                                 elem=new Metal(name,symbol);
93                         else if(group.equals("alkali-metal"))
94                                 elem=new AlkaliMetal(name,symbol);
95                         else if(group.equals("alkali-earth-metal"))
96                                 elem=new AlkaliEarthMetal(name,symbol);
97                         else if(group.equals("rare-earth-metal"))
98                                 elem=new RareEarthMetal(name,symbol);
99                         else if(group.equals("transition-metal"))
100                                 elem=new TransitionMetal(name,symbol);
101                         else
102                                 elem=new Element(name,symbol);
103                         elem.setAtomicNumber(Integer.parseInt(findStringValue(nl,"atomic-number")));
104                         elem.setMassNumber(Integer.parseInt(findStringValue(nl,"mass-number")));
105                         elem.setElectronegativity(findDoubleValue(nl,"electronegativity"));
106                         elem.setCovalentRadius(findDoubleValue(nl,"covalent-radius"));
107                         elem.setAtomicRadius(findDoubleValue(nl,"atomic-radius"));
108                         elem.setMeltingPoint(findDoubleValue(nl,"melting-point"));
109                         elem.setBoilingPoint(findDoubleValue(nl,"boiling-point"));
110                         elem.setDensity(findDoubleValue(nl,"density"));
111                         elem.setSpecificHeat(findDoubleValue(nl,"specific-heat"));
112                         elem.setElectricalConductivity(findDoubleValue(nl,"electrical-conductivity"));
113                         elem.setThermalConductivity(findDoubleValue(nl,"thermal-conductivity"));
114                         return elem;
115                 } catch(ParserConfigurationException e) {
116                         return null;
117                 } catch(IOException JavaDoc e) {
118                         return null;
119                 } catch(SAXException JavaDoc e) {
120                         return null;
121                 }
122         }
123         private static String JavaDoc findStringValue(NodeList nl,String JavaDoc name) {
124                 Node item;
125                 for(int i=0;i<nl.getLength();i++) {
126                         item=nl.item(i);
127                         if(item.getNodeName().equals(name))
128                                 return item.getFirstChild().getNodeValue();
129                 }
130                 return "";
131         }
132         private static double findDoubleValue(NodeList nl,String JavaDoc name) {
133                 Node item;
134                 for(int i=0;i<nl.getLength();i++) {
135                         item=nl.item(i);
136                         if(item.getNodeName().equals(name))
137                                 return Double.parseDouble(item.getFirstChild().getNodeValue());
138                 }
139                 return Double.NaN;
140         }
141 }
142
143
Popular Tags