KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > typing > QTypeAtom


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.xquery.typing;
25
26 import org.xquark.schema.SimpleType;
27 import org.xquark.schema.datatypes.PrimitiveType;
28 import org.xquark.xquery.parser.XQueryException;
29
30 public class QTypeAtom extends QTypeItem implements QTypeVisitable {
31     private static final String JavaDoc RCSRevision = "$Revision: 1.6 $";
32     private static final String JavaDoc RCSName = "$Name: $";
33
34     public QTypeAtom(SimpleType simpleType) {
35         setType(simpleType);
36         subclass = ATOM;
37         occurence = OCC_1_1;
38     }
39     public QTypeAtom(SimpleType simpleType, byte occurence) {
40         setType(simpleType);
41         subclass = ATOM;
42         this.occurence = occurence;
43     }
44
45     // #############################################################################
46
// VISITOR STUFF
47
// #############################################################################
48

49     public void accept(QTypeVisitor visitor) throws XQueryException {
50         visitor.visit(this);
51     }
52
53     // get/set
54
// public SimpleType getSimpleType() {
55
// return (SimpleType)type;
56
// }
57

58     // equals
59

60     public boolean equals(Object JavaDoc qtype) {
61         if (!(qtype instanceof QTypeAtom))
62             return false;
63         // if (((QTypeAtom) qtype).getOccurence() != occurence)
64
// return false;
65
return compare(type, ((QTypeAtom) qtype).getSimpleType());
66     }
67
68     // tells whether qtype is numeric
69
public boolean isNumeric() {
70         if (type == null)
71             return false;
72         else
73             return isNumeric(type);
74     }
75
76     // tells whether qtype is boolean
77
public boolean isBoolean() {
78         if (type == null)
79             return false;
80         else
81             return isBoolean(type);
82     }
83
84     // tells whether qtype is integer
85
public boolean isInteger() {
86         if (type == null)
87             return false;
88         else
89             return isInteger(type);
90     }
91
92     // tells whether qtype is string
93
public boolean isString() {
94         if (type == null)
95             return false;
96         else
97             return isString(type);
98     }
99
100     // tells whether qtype is an atom
101
public boolean isAtom() {
102         return true;
103     }
104
105     // // tells whether qtype can react to text()
106
// public boolean isText() {
107
// return false;
108
// }
109

110     // tells whether qtype is a date or collection of dates
111
public boolean isDate() {
112         if (type == null)
113             return false;
114         else
115             return isDate(type);
116     }
117
118     // // tells whether qtype is a attribte or collection or sequence
119
// public boolean isAttribute() {
120
// return false;
121
// }
122

123     // // tells whether qtype is a ID, IDREF or IDREFS type
124
// public boolean isIDAttribute() {
125
// return false;
126
// }
127

128     // // tells whether qtype is a node
129
// public boolean isNode() {
130
// return false;
131
// }
132

133     // // tells whether qtype is a node with simple type
134
// public boolean isSimpleTypeNode() {
135
// return false;
136
// }
137

138     // // tells whether qtype is a collection
139
// public boolean isCollection() {
140
// return false;
141
// }
142

143     // // tells whether qtype is a document
144
// public boolean isDocument() {
145
// return false;
146
// }
147

148     // // tells whether qtype is multiple
149
// public boolean isMultiple() {
150
// return false;
151
// }
152

153     // tells whether qtype is atomic (atom or element with simple content
154
public boolean isAtomic() {
155         if (type == null)
156             return false;
157         else
158             return !isMultiple();
159     }
160
161     // // tells whether qtype is mixed
162
// public boolean isMixed() {
163
// return false;
164
// }
165

166     // // tells whether qtype is element
167
// public boolean isElement() {
168
// return false;
169
// }
170

171     // tells whether qtype is qname
172
public boolean isQName() {
173         if (type == null)
174             return false;
175         else
176             return isQName(type);
177     }
178
179     public boolean canBeComparedTo(QType compType) {
180         if (compType == null)
181             return false;
182         if (!(compType instanceof QTypeAtom))
183             return compType.canBeComparedTo(this);
184         if (type == null)
185             return true;
186         if (((QTypeAtom) compType).getSimpleType() == null)
187             return true;
188         return canBeCompared(((SimpleType) type).getPrimitive().getType(), ((QTypeAtom) compType).getSimpleType().getPrimitive().getType());
189     }
190
191     public boolean canBeCastedInto(int castType) {
192         if (castType == -1)
193             return false;
194         int myCastType = getCastType();
195         if (myCastType == -1)
196             return false;
197         return QType.castTab[myCastType][castType];
198     }
199
200     public int getCastType() {
201         if (type == null)
202             return QType.PRIMITIVE_anySimpleType;
203         switch (((SimpleType) type).getPrimitive().getType()) {
204             //case PrimitiveType.VOID : return -1;
205
case PrimitiveType.ANYSIMPLETYPE :
206                 return QType.PRIMITIVE_anySimpleType;
207             case PrimitiveType.STRING :
208                 return QType.PRIMITIVE_string;
209             case PrimitiveType.BOOLEAN :
210                 return QType.PRIMITIVE_boolean;
211             case PrimitiveType.DECIMAL :
212                 return QType.PRIMITIVE_decimal;
213             case PrimitiveType.DOUBLE :
214                 return QType.PRIMITIVE_double;
215             case PrimitiveType.FLOAT :
216                 return QType.PRIMITIVE_float;
217             case PrimitiveType.DURATION :
218                 return QType.PRIMITIVE_duration;
219             case PrimitiveType.DATE_TIME :
220                 return QType.PRIMITIVE_dateTime;
221             case PrimitiveType.TIME :
222                 return QType.PRIMITIVE_time;
223             case PrimitiveType.DATE :
224                 return QType.PRIMITIVE_date;
225             case PrimitiveType.GYEAR_MONTH :
226                 return QType.PRIMITIVE_gYearMonth;
227             case PrimitiveType.GYEAR :
228                 return QType.PRIMITIVE_gYear;
229             case PrimitiveType.GMONTH_DAY :
230                 return QType.PRIMITIVE_gMonthDay;
231             case PrimitiveType.GDAY :
232                 return QType.PRIMITIVE_gDay;
233             case PrimitiveType.GMONTH :
234                 return QType.PRIMITIVE_gMonth;
235             case PrimitiveType.HEX_BINARY :
236                 return QType.PRIMITIVE_hexBinary;
237             case PrimitiveType.BASE64_BINARY :
238                 return QType.PRIMITIVE_base64Binary;
239             case PrimitiveType.ANY_URI :
240                 return QType.PRIMITIVE_anyURI;
241             case PrimitiveType.QNAME :
242                 return QType.PRIMITIVE_Qname;
243             case PrimitiveType.NOTATION :
244                 return QType.PRIMITIVE_NOTATION;
245         }
246         return -1;
247     }
248     /*
249     // not bounded values !!!
250     public static final int PRIMITIVE_anySimpleType = 1;
251     public static final int PRIMITIVE_dayTimeDuration = 10;
252     public static final int PRIMITIVE_yearMonthDuration = 21;
253     */

254
255     // clone
256
public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
257         QTypeAtom newObj = new QTypeAtom((SimpleType) type, occurence);
258         return newObj;
259     }
260     // print
261
public String JavaDoc toSimpleString() {
262         if (type == null)
263             return "QTypeAtom(anySimpleType)";
264         else
265             return "QTypeAtom(" + type.getName() + ")";
266     }
267
268     public String JavaDoc toString() {
269         return "QTypeAtom(\n" + type + ")";
270     }
271
272 }
273
Popular Tags