1 56 package org.objectstyle.cayenne.dba; 57 58 import java.io.IOException ; 59 import java.io.InputStream ; 60 import java.net.URL ; 61 import java.sql.Types ; 62 import java.util.ArrayList ; 63 import java.util.HashMap ; 64 import java.util.List ; 65 import java.util.Map ; 66 67 import org.apache.log4j.Logger; 68 import org.objectstyle.cayenne.CayenneRuntimeException; 69 import org.objectstyle.cayenne.util.Util; 70 import org.xml.sax.Attributes ; 71 import org.xml.sax.InputSource ; 72 import org.xml.sax.SAXException ; 73 import org.xml.sax.XMLReader ; 74 import org.xml.sax.helpers.DefaultHandler ; 75 76 82 public class TypesHandler { 83 private static Logger logObj = Logger.getLogger(TypesHandler.class); 84 85 private static Map handlerMap = new HashMap (); 86 87 protected Map typesMap; 88 89 92 public static TypesHandler getHandler(URL typesConfig) { 93 synchronized (handlerMap) { 94 TypesHandler handler = (TypesHandler) handlerMap.get(typesConfig); 95 96 if (handler == null) { 97 handler = new TypesHandler(typesConfig); 98 handlerMap.put(typesConfig, handler); 99 } 100 101 return handler; 102 } 103 } 104 105 111 public TypesHandler(URL typesConfig) { 112 try { 113 InputStream in = typesConfig.openStream(); 114 115 try { 116 XMLReader parser = Util.createXmlReader(); 117 TypesParseHandler ph = new TypesParseHandler(); 118 parser.setContentHandler(ph); 119 parser.setErrorHandler(ph); 120 parser.parse(new InputSource (in)); 121 122 typesMap = ph.getTypes(); 123 } 124 catch (Exception ex) { 125 throw new CayenneRuntimeException( 126 "Error creating TypesHandler '" + typesConfig + "'.", 127 ex); 128 } 129 finally { 130 try { 131 in.close(); 132 } 133 catch (IOException ioex) { 134 } 135 } 136 } 137 catch (IOException ioex) { 138 throw new CayenneRuntimeException( 139 "Error opening config file '" + typesConfig + "'.", 140 ioex); 141 } 142 } 143 144 public String [] externalTypesForJdbcType(int type) { 145 return (String []) typesMap.get(new Integer (type)); 146 } 147 148 151 final class TypesParseHandler extends DefaultHandler { 152 private static final String JDBC_TYPE_TAG = "jdbc-type"; 153 private static final String DB_TYPE_TAG = "db-type"; 154 private static final String NAME_ATTR = "name"; 155 156 private Map types = new HashMap (); 157 private List currentTypes = new ArrayList (); 158 private int currentType = TypesMapping.NOT_DEFINED; 159 160 public Map getTypes() { 161 return types; 162 } 163 164 public void startElement( 165 String namespaceURI, 166 String localName, 167 String qName, 168 Attributes atts) 169 throws SAXException { 170 if (JDBC_TYPE_TAG.equals(localName)) { 171 currentTypes.clear(); 172 String strType = atts.getValue("", NAME_ATTR); 173 174 try { 176 currentType = Types .class.getDeclaredField(strType).getInt(null); 177 } 178 catch (Exception ex) { 179 currentType = TypesMapping.NOT_DEFINED; 180 logObj.info("type not found: '" + strType + "', ignoring."); 181 } 182 } 183 else if (DB_TYPE_TAG.equals(localName)) { 184 currentTypes.add(atts.getValue("", NAME_ATTR)); 185 } 186 } 187 188 public void endElement(String namespaceURI, String localName, String qName) 189 throws SAXException { 190 if (JDBC_TYPE_TAG.equals(localName) 191 && currentType != TypesMapping.NOT_DEFINED) { 192 String [] typesAsArray = new String [currentTypes.size()]; 193 types.put(new Integer (currentType), currentTypes.toArray(typesAsArray)); 194 } 195 } 196 } 197 } 198 | Popular Tags |