| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package rs.mgifos.mosquito.impl.pdm; |
| 20 | |
|
| 21 | |
import java.io.InputStream; |
| 22 | |
import java.util.Hashtable; |
| 23 | |
|
| 24 | |
import javax.xml.parsers.SAXParser; |
| 25 | |
import javax.xml.parsers.SAXParserFactory; |
| 26 | |
|
| 27 | |
import org.apache.log4j.Logger; |
| 28 | |
import org.xml.sax.Attributes; |
| 29 | |
import org.xml.sax.SAXException; |
| 30 | |
import org.xml.sax.helpers.DefaultHandler; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 7 | public class JClassResolver { |
| 40 | |
|
| 41 | 1 | private Logger logger = Logger.getLogger(this.getClass()); |
| 42 | |
|
| 43 | 1 | class SaxHandler extends DefaultHandler { |
| 44 | |
private static final String PDM_TYPE = "PDMType"; |
| 45 | |
|
| 46 | |
private static final String PREC = "Prec"; |
| 47 | |
|
| 48 | |
private static final String LEN = "Len"; |
| 49 | |
|
| 50 | 1 | private PDMType actualPDMType = null; |
| 51 | |
|
| 52 | |
public void startElement(String aUri, String aLocalName, String aQName, |
| 53 | |
Attributes attributes) throws SAXException { |
| 54 | |
try { |
| 55 | 8 | if (PDM_TYPE.equals(aQName)) { |
| 56 | 7 | String name = attributes.getValue("name"); |
| 57 | 7 | String jDefaultClass = attributes.getValue("jDefaultClass"); |
| 58 | 7 | actualPDMType = new PDMType(name, jDefaultClass); |
| 59 | 7 | ht.put(name, actualPDMType); |
| 60 | 7 | } else if (PREC.equals(aQName)) { |
| 61 | 0 | String length = attributes.getValue("length"); |
| 62 | 0 | String jClass = attributes.getValue("jClass"); |
| 63 | 0 | PrecType pt = new PrecType(Integer.parseInt(length), jClass); |
| 64 | 0 | actualPDMType.addPrec(pt); |
| 65 | 0 | } else if (LEN.equals(aQName)) { |
| 66 | 0 | String length = attributes.getValue("length"); |
| 67 | 0 | String jClass = attributes.getValue("jClass"); |
| 68 | 0 | LenType lt = new LenType(Integer.parseInt(length), jClass); |
| 69 | 0 | actualPDMType.addLen(lt); |
| 70 | |
} |
| 71 | 0 | } catch (Exception e) { |
| 72 | 0 | logger.error("Sax Parsing xml failed!"); |
| 73 | 8 | } |
| 74 | 8 | } |
| 75 | |
} |
| 76 | |
|
| 77 | 1 | private static JClassResolver INSTANCE = null; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public static JClassResolver getInstance() { |
| 85 | 4 | if (INSTANCE == null) |
| 86 | 1 | INSTANCE = new JClassResolver(); |
| 87 | 4 | return INSTANCE; |
| 88 | |
} |
| 89 | |
|
| 90 | 1 | private Hashtable<String, PDMType> ht = new Hashtable<String, PDMType>(); |
| 91 | |
|
| 92 | |
private JClassResolver() { |
| 93 | 1 | super(); |
| 94 | 1 | this.init(); |
| 95 | 1 | } |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public String getJClassName(String aType, int aLength, int aPrecision) { |
| 104 | 26 | String retVal = "java.lang.String"; |
| 105 | |
try { |
| 106 | 26 | retVal = ((PDMType) ht.get(aType)) |
| 107 | |
.getClassName(aLength, aPrecision); |
| 108 | 1 | } catch (Exception e) { |
| 109 | 1 | logger.warn("Can't resolve type: '" + aType + "'. Check the existence of this type in jclass_resolver.xml file! Default JClass is java.lang.String!"); |
| 110 | 1 | retVal = "java.lang.String"; |
| 111 | 25 | } |
| 112 | 26 | return retVal; |
| 113 | |
} |
| 114 | |
|
| 115 | |
private void init() { |
| 116 | |
try { |
| 117 | 1 | InputStream is = JClassResolver.class |
| 118 | |
.getResourceAsStream("jclass_resolver.xml"); |
| 119 | 1 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 120 | 1 | SAXParser saxParser = factory.newSAXParser(); |
| 121 | 1 | saxParser.parse(is, new SaxHandler()); |
| 122 | |
|
| 123 | 0 | } catch (Exception e) { |
| 124 | 0 | logger.error("Can't init resolver! Cause: " + e.getMessage()); |
| 125 | 1 | } |
| 126 | 1 | } |
| 127 | |
} |