001/* 002 * To change this template, choose Tools | Templates 003 * and open the template in the editor. 004 */ 005 006package armyc2.c2sd.renderer.utilities; 007 008import java.io.IOException; 009import java.io.InputStream; 010import java.io.StringReader; 011import java.util.ArrayList; 012import java.util.HashMap; 013import java.util.Map; 014 015import javax.xml.parsers.DocumentBuilder; 016import javax.xml.parsers.DocumentBuilderFactory; 017import javax.xml.parsers.ParserConfigurationException; 018 019import org.w3c.dom.Document; 020import org.w3c.dom.Element; 021import org.w3c.dom.Node; 022import org.w3c.dom.NodeList; 023import org.xml.sax.InputSource; 024import org.xml.sax.SAXException; 025 026import android.util.Log; 027 028/** 029 * 030 * @author michael.spinelli 031 */ 032public class UnitSVGTable { 033 034 035 private static Boolean _initCalled = false; 036 private static UnitSVGTable _instance = null; 037 //private static SymbolTableThingy 038 private static Map<String, SVGPath> _UnitDefinitions = null; 039 040 private UnitSVGTable() 041 { 042 043 } 044 045 public static synchronized UnitSVGTable getInstance() 046 { 047 if(_instance == null) 048 _instance = new UnitSVGTable(); 049 050 return _instance; 051 } 052 053 054 055 /** 056 * must be called first 057 */ 058 public synchronized void init(String unitSVG) 059 { 060 if(_initCalled==false) 061 { 062 _instance = new UnitSVGTable(); 063 _UnitDefinitions = new HashMap<String, SVGPath>(); 064 065 066 067 String lookupXml = unitSVG.replace("&#x", "");//FileHandler.InputStreamToString(xmlStreamB); 068 069 //String lookupXml = FileHandler.fileToString("C:\\UnitFontMappings.xml"); 070 populateLookup(lookupXml); 071 072 073 _initCalled = true; 074 } 075 } 076 077 078 private void populateLookup(String xml) 079 { 080 081 Document doc = XMLParser.getDomElement(xml); 082 NodeList nl = doc.getElementsByTagName("glyph"); 083 SVGPath path; 084 for(int i = 0; i < nl.getLength(); i++) 085 { 086 String index = XMLParser.getAttribute((Element)nl.item(i), "unicode"); 087 String strPath = XMLParser.getAttribute((Element)nl.item(i), "d"); 088 089 if(strPath != null && strPath.equals("") != true && index != null && index.length() > 3 ) 090 { 091 index = index.replace("&#x", ""); 092 index = index.replace(";", ""); 093 path = new SVGPath(index, strPath); 094 index = path.getID(); 095 _UnitDefinitions.put(index, path); 096 } 097 098 } 099 }//end populateLookup 100 101 /** 102 * @name getSymbolDef 103 * 104 * @description Returns a SymbolDef from the SymbolDefTable that matches the passed in Symbol Id 105 * 106 * @param index String representation of the index number 107 * @return SVGPath 108 */ 109 public SVGPath getSVGPath(String index) 110 { 111 SVGPath returnVal = null; 112 try 113 { 114 if (_UnitDefinitions.containsKey(index)) 115 returnVal = new SVGPath(_UnitDefinitions.get(index)); 116 } 117 catch (Exception exc) 118 { 119 120 } 121 return returnVal; 122 } 123 124 public Boolean HasSVGPath(String index) 125 { 126 if (index != null && index.length() > 0) 127 return _UnitDefinitions.containsKey(index); 128 else 129 return false; 130 } 131 132}