Coverage Report - rs.mgifos.mosquito.impl.pdm.JClassResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
JClassResolver
92%
23/25
100%
2/2
0
JClassResolver$SaxHandler
48%
10/21
67%
4/6
0
 
 1  
 /* 
 2  
  * This file is part of Mosquito meta-loader.
 3  
  *
 4  
  * Mosquito meta-loader is free software; you can redistribute it and/or modify
 5  
  * it under the terms of the GNU Lesser General Public License as published by
 6  
  * the Free Software Foundation; either version 3 of the License, or
 7  
  * (at your option) any later version.
 8  
  *
 9  
  * Mosquito meta-loader is distributed in the hope that it will be useful,
 10  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
  * GNU Lesser General Public License for more details.
 13  
  *
 14  
  * You should have received a copy of the GNU Lesser General Public License
 15  
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 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  
  * Java class name resolver. Mapping is described within the XML file
 34  
  * jclass_resolver.xml.
 35  
  * 
 36  
  * @author <a href="mailto:nikola.petkov@gmail.com">Nikola Petkov
 37  
  *         &lt;nikola.petkov@gmail.com&gt;</a>
 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  
          * Returns singleton's instance
 81  
          * 
 82  
          * @return singleton's instance
 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  
          * @param aType
 99  
          * @param aLength
 100  
          * @param aPrecision
 101  
          * @return Java class name for the specified input type (one of the T_XXX)
 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  
 }