KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > XMLTypeCompiler


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

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.services.loader.ClassFactory;
25 import org.apache.derby.iapi.services.sanity.SanityManager;
26 import org.apache.derby.iapi.services.io.StoredFormatIds;
27 import org.apache.derby.iapi.sql.compile.TypeCompiler;
28
29 import org.apache.derby.iapi.error.StandardException;
30
31 import org.apache.derby.iapi.types.TypeId;
32 import org.apache.derby.iapi.types.DataTypeDescriptor;
33
34 import org.apache.derby.iapi.reference.ClassName;
35
36 /**
37  * This class implements TypeCompiler for the XML type.
38  */

39
40 public class XMLTypeCompiler extends BaseTypeCompiler
41 {
42     /**
43      * Tell whether this type (XML) can be compared to the given type.
44      * Says SQL/XML[2003] spec:
45      *
46      * 4.2.2 XML comparison and assignment
47      * "XML values are not comparable."
48      *
49      * @param otherType The TypeId of the other type.
50      */

51     public boolean comparable(TypeId otherType,
52                             boolean forEquals,
53                             ClassFactory cs)
54     {
55         // An XML value cannot be compared to any type--
56
// not even to other XML values.
57
return false;
58     }
59
60     /**
61      * Tell whether this type (XML) can be converted to the given type.
62      *
63      * An XML value can't be converted to any other type, per
64      * SQL/XML[2003] 6.3 <cast specification>
65      *
66      * @see TypeCompiler#convertible
67      */

68     public boolean convertible(TypeId otherType,
69                             boolean forDataTypeFunction)
70     {
71         // An XML value cannot be converted to any non-XML type. If
72
// user wants to convert an XML value to a string, then
73
// s/he must use the provided SQL/XML serialization operator
74
// (namely, XMLSERIALIZE).
75
return otherType.isXMLTypeId();
76     }
77
78     /**
79      * Tell whether this type (XML) is compatible with the given type.
80      *
81      * @param otherType The TypeId of the other type.
82      */

83     public boolean compatible(TypeId otherType)
84     {
85         // An XML value is not compatible (i.e. cannot be "coalesced")
86
// into any non-XML type.
87
return otherType.isXMLTypeId();
88     }
89
90     /**
91      * Tell whether this type (XML) can be stored into from the given type.
92      * Only XML values can be stored into an XML type, per SQL/XML spec:
93      *
94      * 4.2.2 XML comparison and assignment
95      * Values of XML type are assignable to sites of XML type.
96      *
97      * @param otherType The TypeId of the other type.
98      * @param cf A ClassFactory
99      */

100     public boolean storable(TypeId otherType, ClassFactory cf)
101     {
102         // The only type of value that can be stored as XML
103
// is an XML value. Strings are not allowed. If
104
// the user wants to store a string value as XML,
105
// s/he must use the provided XML parse operator
106
// (namely, XMLPARSE) to parse the string into
107
// XML.
108
return otherType.isXMLTypeId();
109     }
110
111     /**
112      * @see TypeCompiler#interfaceName
113      */

114     public String JavaDoc interfaceName() {
115         return ClassName.XMLDataValue;
116     }
117
118     /**
119      * @see TypeCompiler#getCorrespondingPrimitiveTypeName
120      */

121     public String JavaDoc getCorrespondingPrimitiveTypeName()
122     {
123         int formatId = getStoredFormatIdFromTypeId();
124         if (formatId == StoredFormatIds.XML_TYPE_ID)
125             return "org.apache.derby.iapi.types.XML";
126
127         if (SanityManager.DEBUG) {
128             SanityManager.THROWASSERT(
129                 "unexpected formatId in getCorrespondingPrimitiveTypeName(): "
130                 + formatId);
131         }
132
133         return null;
134     }
135
136     /**
137      * @see TypeCompiler#getCastToCharWidth
138      *
139      * While it is true XML values can't be cast to char, this method
140      * can get called before we finish type checking--so we return a dummy
141      * value here and let the type check throw the appropriate error.
142      */

143     public int getCastToCharWidth(DataTypeDescriptor dts)
144     {
145         return -1;
146     }
147
148     /**
149      * @see BaseTypeCompiler#nullMethodName
150      */

151     protected String JavaDoc nullMethodName()
152     {
153         int formatId = getStoredFormatIdFromTypeId();
154         if (formatId == StoredFormatIds.XML_TYPE_ID)
155             return "getNullXML";
156
157         if (SanityManager.DEBUG) {
158             SanityManager.THROWASSERT(
159                 "unexpected formatId in nullMethodName(): " + formatId);
160         }
161
162         return null;
163     }
164
165     /**
166      * @see BaseTypeCompiler#dataValueMethodName
167      */

168     protected String JavaDoc dataValueMethodName()
169     {
170         int formatId = getStoredFormatIdFromTypeId();
171         if (formatId == StoredFormatIds.XML_TYPE_ID)
172             return "getXMLDataValue";
173
174         if (SanityManager.DEBUG) {
175             SanityManager.THROWASSERT(
176                 "unexpected formatId in dataValueMethodName() - " + formatId);
177         }
178
179         return null;
180     }
181 }
182
Popular Tags