1 19 20 package org.apache.cayenne.dba; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.net.URL ; 25 import java.sql.Types ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.apache.cayenne.CayenneRuntimeException; 32 import org.apache.cayenne.util.Util; 33 import org.xml.sax.Attributes ; 34 import org.xml.sax.InputSource ; 35 import org.xml.sax.SAXException ; 36 import org.xml.sax.XMLReader ; 37 import org.xml.sax.helpers.DefaultHandler ; 38 39 45 public class TypesHandler { 46 47 private static Map handlerMap = new HashMap (); 48 49 protected Map typesMap; 50 51 54 public static TypesHandler getHandler(URL typesConfig) { 55 synchronized (handlerMap) { 56 TypesHandler handler = (TypesHandler) handlerMap.get(typesConfig); 57 58 if (handler == null) { 59 handler = new TypesHandler(typesConfig); 60 handlerMap.put(typesConfig, handler); 61 } 62 63 return handler; 64 } 65 } 66 67 73 public TypesHandler(URL typesConfig) { 74 try { 75 InputStream in = typesConfig.openStream(); 76 77 try { 78 XMLReader parser = Util.createXmlReader(); 79 TypesParseHandler ph = new TypesParseHandler(); 80 parser.setContentHandler(ph); 81 parser.setErrorHandler(ph); 82 parser.parse(new InputSource (in)); 83 84 typesMap = ph.getTypes(); 85 } 86 catch (Exception ex) { 87 throw new CayenneRuntimeException( 88 "Error creating TypesHandler '" + typesConfig + "'.", 89 ex); 90 } 91 finally { 92 try { 93 in.close(); 94 } 95 catch (IOException ioex) { 96 } 97 } 98 } 99 catch (IOException ioex) { 100 throw new CayenneRuntimeException( 101 "Error opening config file '" + typesConfig + "'.", 102 ioex); 103 } 104 } 105 106 public String [] externalTypesForJdbcType(int type) { 107 return (String []) typesMap.get(new Integer (type)); 108 } 109 110 113 final class TypesParseHandler extends DefaultHandler { 114 private static final String JDBC_TYPE_TAG = "jdbc-type"; 115 private static final String DB_TYPE_TAG = "db-type"; 116 private static final String NAME_ATTR = "name"; 117 118 private Map types = new HashMap (); 119 private List currentTypes = new ArrayList (); 120 private int currentType = TypesMapping.NOT_DEFINED; 121 122 public Map getTypes() { 123 return types; 124 } 125 126 public void startElement( 127 String namespaceURI, 128 String localName, 129 String qName, 130 Attributes atts) 131 throws SAXException { 132 if (JDBC_TYPE_TAG.equals(localName)) { 133 currentTypes.clear(); 134 String strType = atts.getValue("", NAME_ATTR); 135 136 try { 138 currentType = Types .class.getDeclaredField(strType).getInt(null); 139 } 140 catch (Exception ex) { 141 currentType = TypesMapping.NOT_DEFINED; 142 } 143 } 144 else if (DB_TYPE_TAG.equals(localName)) { 145 currentTypes.add(atts.getValue("", NAME_ATTR)); 146 } 147 } 148 149 public void endElement(String namespaceURI, String localName, String qName) 150 throws SAXException { 151 if (JDBC_TYPE_TAG.equals(localName) 152 && currentType != TypesMapping.NOT_DEFINED) { 153 String [] typesAsArray = new String [currentTypes.size()]; 154 types.put(new Integer (currentType), currentTypes.toArray(typesAsArray)); 155 } 156 } 157 } 158 } 159 | Popular Tags |