KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > TypesHandler


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dba;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.sql.Types JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.apache.cayenne.CayenneRuntimeException;
32 import org.apache.cayenne.util.Util;
33 import org.xml.sax.Attributes JavaDoc;
34 import org.xml.sax.InputSource JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36 import org.xml.sax.XMLReader JavaDoc;
37 import org.xml.sax.helpers.DefaultHandler JavaDoc;
38
39 /**
40  * TypesHandler provides JDBC-RDBMS types mapping. Loads types info from
41  * an XML file.
42  *
43  * @author Andrus Adamchik
44  */

45 public class TypesHandler {
46
47     private static Map JavaDoc handlerMap = new HashMap JavaDoc();
48
49     protected Map JavaDoc typesMap;
50
51     /**
52      * @since 1.1
53      */

54     public static TypesHandler getHandler(URL JavaDoc 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     /**
68      * Creates new TypesHandler loading configuration info from the XML
69      * file specified as <code>typesConfigPath</code> parameter.
70      *
71      * @since 1.1
72      */

73     public TypesHandler(URL JavaDoc typesConfig) {
74         try {
75             InputStream JavaDoc in = typesConfig.openStream();
76
77             try {
78                 XMLReader JavaDoc parser = Util.createXmlReader();
79                 TypesParseHandler ph = new TypesParseHandler();
80                 parser.setContentHandler(ph);
81                 parser.setErrorHandler(ph);
82                 parser.parse(new InputSource JavaDoc(in));
83
84                 typesMap = ph.getTypes();
85             }
86             catch (Exception JavaDoc ex) {
87                 throw new CayenneRuntimeException(
88                     "Error creating TypesHandler '" + typesConfig + "'.",
89                     ex);
90             }
91             finally {
92                 try {
93                     in.close();
94                 }
95                 catch (IOException JavaDoc ioex) {
96                 }
97             }
98         }
99         catch (IOException JavaDoc ioex) {
100             throw new CayenneRuntimeException(
101                 "Error opening config file '" + typesConfig + "'.",
102                 ioex);
103         }
104     }
105
106     public String JavaDoc[] externalTypesForJdbcType(int type) {
107         return (String JavaDoc[]) typesMap.get(new Integer JavaDoc(type));
108     }
109
110     /**
111      * Helper class to load types data from XML.
112      */

113     final class TypesParseHandler extends DefaultHandler JavaDoc {
114         private static final String JavaDoc JDBC_TYPE_TAG = "jdbc-type";
115         private static final String JavaDoc DB_TYPE_TAG = "db-type";
116         private static final String JavaDoc NAME_ATTR = "name";
117
118         private Map JavaDoc types = new HashMap JavaDoc();
119         private List JavaDoc currentTypes = new ArrayList JavaDoc();
120         private int currentType = TypesMapping.NOT_DEFINED;
121
122         public Map JavaDoc getTypes() {
123             return types;
124         }
125
126         public void startElement(
127             String JavaDoc namespaceURI,
128             String JavaDoc localName,
129             String JavaDoc qName,
130             Attributes JavaDoc atts)
131             throws SAXException JavaDoc {
132             if (JDBC_TYPE_TAG.equals(localName)) {
133                 currentTypes.clear();
134                 String JavaDoc strType = atts.getValue("", NAME_ATTR);
135
136                 // convert to Types int value
137
try {
138                     currentType = Types JavaDoc.class.getDeclaredField(strType).getInt(null);
139                 }
140                 catch (Exception JavaDoc 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 JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
150             throws SAXException JavaDoc {
151             if (JDBC_TYPE_TAG.equals(localName)
152                 && currentType != TypesMapping.NOT_DEFINED) {
153                 String JavaDoc[] typesAsArray = new String JavaDoc[currentTypes.size()];
154                 types.put(new Integer JavaDoc(currentType), currentTypes.toArray(typesAsArray));
155             }
156         }
157     }
158 }
159
Popular Tags