1 package JSci.chemistry; 2 3 import java.io.IOException ; 4 import java.lang.Double ; 5 import java.util.Collections ; 6 import java.util.Map ; 7 import java.util.HashMap ; 8 import javax.xml.parsers.*; 9 import org.w3c.dom.*; 10 import org.xml.sax.SAXException ; 11 import JSci.chemistry.periodictable.*; 12 13 18 public final class PeriodicTable { 19 private static final Map table = new HashMap (); 20 private static Map symbols; 21 22 private PeriodicTable() {} 23 26 public static String getName(String symbol) { 27 if(symbols == null) 28 symbols = loadIndex(); 29 return (String )symbols.get(symbol); 30 } 31 34 public static Element getElement(String 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 47 private static Map 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 index = new HashMap (); 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 e) { 66 return Collections.EMPTY_MAP; 67 } catch(SAXException e) { 68 return Collections.EMPTY_MAP; 69 } 70 } 71 74 private static Element loadElement(String 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 group=root.getAttributes().getNamedItem("series").getNodeValue(); 81 NodeList nl=root.getChildNodes(); 82 String name=findStringValue(nl,"name"); 83 String 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 e) { 118 return null; 119 } catch(SAXException e) { 120 return null; 121 } 122 } 123 private static String findStringValue(NodeList nl,String 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 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 |