KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > jdbc > typing > JavaTypeInfo


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  * Copyright (C) 2003 XQuark Group.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
19  * You can also get it at http://www.gnu.org/licenses/lgpl.html
20  *
21  * For more information on this software, see http://www.xquark.org.
22  */

23
24 package org.xquark.jdbc.typing;
25
26 import org.xquark.schema.SimpleType;
27 import org.xquark.schema.datatypes.PrimitiveType;
28
29 /** This class describes, for a datum to map, features of the database
30  * corresponding type such as DDL type creation string.
31  * <p>This class is used for generating native dbms type creation string.</p>
32  */

33 public class JavaTypeInfo extends XMLTypeInfo
34 {
35     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
36     private static final String JavaDoc RCSName = "$Name: $";
37     
38     //////////////////////////////////////////////////////////////////////////
39
// CONSTANTS
40
//////////////////////////////////////////////////////////////////////////
41
public final static int JAVA_STRING = 0;
42     public final static int JAVA_BOOLEAN = 1;
43     public final static int JAVA_BIG_DECIMAL = 2;
44     public final static int JAVA_FLOAT = 3;
45     public final static int JAVA_DOUBLE = 4;
46     public final static int JAVA_LONG = 5;
47     
48     //below not recognized by JDBC
49

50     public final static int JAVA_LIST = 6;
51     public final static int JAVA_UNKNOWN = 7;
52     
53     public final static int SCHEMA_DURATION = 8;
54     public final static int SCHEMA_DATETIME = 9;
55     public final static int SCHEMA_BYTE_ARRAY = 10;
56     public final static int SCHEMA_URI = 11;
57     public final static int SCHEMA_QNAME = 12;
58
59     /** mapping between schema type and java types assumed by
60      * PrimitiveType.Convert() & toXMLString()
61      */

62     public static int JAVA_TYPE_BINDINGS[] = new int[33]; // (19 base primitive types + 3, non-dense + index 0 unused)
63

64     static
65     {
66         // mapping between schema type and java types assumed by PrimitiveType.Convert() & toXMLString()
67
JAVA_TYPE_BINDINGS[PrimitiveType.STRING] = JAVA_STRING;
68         JAVA_TYPE_BINDINGS[PrimitiveType.BOOLEAN] = JAVA_BOOLEAN;
69         JAVA_TYPE_BINDINGS[PrimitiveType.DECIMAL] = JAVA_BIG_DECIMAL; // or Long if possible
70
JAVA_TYPE_BINDINGS[PrimitiveType.FLOAT] = JAVA_FLOAT;
71         JAVA_TYPE_BINDINGS[PrimitiveType.DOUBLE] = JAVA_DOUBLE;
72         JAVA_TYPE_BINDINGS[PrimitiveType.QNAME] = SCHEMA_QNAME;
73         JAVA_TYPE_BINDINGS[PrimitiveType.NOTATION] = SCHEMA_QNAME;
74         JAVA_TYPE_BINDINGS[PrimitiveType.DURATION] = SCHEMA_DURATION;
75         JAVA_TYPE_BINDINGS[PrimitiveType.DATE_TIME] = SCHEMA_DATETIME;
76         JAVA_TYPE_BINDINGS[PrimitiveType.TIME] = SCHEMA_DATETIME;
77         JAVA_TYPE_BINDINGS[PrimitiveType.DATE] = SCHEMA_DATETIME;
78         JAVA_TYPE_BINDINGS[PrimitiveType.GYEAR_MONTH] = SCHEMA_DATETIME;
79         JAVA_TYPE_BINDINGS[PrimitiveType.GYEAR] = SCHEMA_DATETIME;
80         JAVA_TYPE_BINDINGS[PrimitiveType.GMONTH_DAY] = SCHEMA_DATETIME;
81         JAVA_TYPE_BINDINGS[PrimitiveType.GDAY] = SCHEMA_DATETIME;
82         JAVA_TYPE_BINDINGS[PrimitiveType.GMONTH] = SCHEMA_DATETIME;
83         JAVA_TYPE_BINDINGS[PrimitiveType.HEX_BINARY] = SCHEMA_BYTE_ARRAY;
84         JAVA_TYPE_BINDINGS[PrimitiveType.BASE64_BINARY] = SCHEMA_BYTE_ARRAY;
85         JAVA_TYPE_BINDINGS[PrimitiveType.ANY_URI] = SCHEMA_URI;
86         JAVA_TYPE_BINDINGS[PrimitiveType.UNION] = JAVA_UNKNOWN;
87         JAVA_TYPE_BINDINGS[PrimitiveType.LIST] = JAVA_LIST;
88         JAVA_TYPE_BINDINGS[PrimitiveType.ANYSIMPLETYPE] = JAVA_STRING;
89         
90     }
91     
92     //////////////////////////////////////////////////////////////////////////
93
// DATA
94
//////////////////////////////////////////////////////////////////////////
95

96     /** Object maintaining information on mapping between XML and Java types.
97      */

98     protected int javaType = JAVA_STRING; // for default mapping
99

100     
101     //////////////////////////////////////////////////////////////////////////
102
// CONSTRUCTORS & ACCESSORS
103
//////////////////////////////////////////////////////////////////////////
104
protected JavaTypeInfo() {}
105     
106     public JavaTypeInfo(String JavaDoc XMLType) { setXMLType(XMLType);}
107     
108     public JavaTypeInfo(SimpleType XMLType) { setXMLType(XMLType);}
109     
110     public void setXMLType(String JavaDoc XMLType)
111     {
112         super.setXMLType(XMLType);
113         initialize();
114     }
115     
116     public void setXMLType(SimpleType XMLType)
117     {
118         super.setXMLType(XMLType);
119         initialize();
120     }
121     
122     /** Set metadata info from mapping between JDBC and native SQL types
123      * @param typeMap a loaded TypeMap
124      */

125     private void initialize()
126     {
127         javaType = JAVA_TYPE_BINDINGS[sType.getPrimitive().getType()];
128     }
129     
130     //////////////////////////////////////////////////////////////////////////
131
// PUBLIC METHODS
132
//////////////////////////////////////////////////////////////////////////
133
public int getJavaType()
134     {
135         return javaType;
136     }
137     
138     //////////////////////////////////////////////////////////////////////////
139
// UTILITIES
140
//////////////////////////////////////////////////////////////////////////
141
public String JavaDoc toString()
142     {
143         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
144         buf.append("# NativeTypeInfo\n");
145         buf.append("\tXML = ");
146         
147         PrimitiveType pType = sType.getPrimitive();
148         buf.append(pType);
149         buf.append("[maxLen=");
150         buf.append(pType.getMaxLength());
151         buf.append(", totDig=");
152         buf.append(pType.getTotalDigits());
153         buf.append(", fracDig=");
154         buf.append(pType.getFractionDigits());
155         buf.append(", min=");
156         buf.append(pType.getMinValue());
157         buf.append(", max=");
158         buf.append(pType.getMaxValue());
159         
160         buf.append("]\n");
161         buf.append("\tJAVA = ");
162         buf.append(javaType);
163         buf.append("\n\n");
164         
165         return buf.toString();
166     }
167 }
168
Popular Tags