KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > datatypes > ComparableType


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

22
23 package org.xquark.schema.datatypes;
24
25 import org.xquark.schema.SchemaException;
26 import org.xquark.schema.validation.ValidationContextProvider;
27
28
29 abstract class ComparableType extends EnumerableType {
30     
31     class ComparableConstraints implements Cloneable JavaDoc {
32         private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
33         private static final String JavaDoc RCSName = "$Name: $";
34         private int minType = -1;
35         private Comparable JavaDoc min = null;
36         private int maxType = -1;
37         private Comparable JavaDoc max = null;
38     
39         public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
40             return super.clone();
41         }
42
43         private boolean checkMin(Comparable JavaDoc value) {
44                 return minType == -1 || value.compareTo(min) >= minType;
45         }
46         
47         private boolean checkMax(Comparable JavaDoc value) {
48                 return maxType == -1 || max.compareTo(value) >= maxType;
49         }
50         
51         void checkParentMinExclusive(Comparable JavaDoc value) throws SchemaException {
52             if (!checkMin(value)) {
53                 if (minType == 1) {
54                     throw new SchemaException("minExclusive-valid-restriction.1");
55                 } else {
56                     throw new SchemaException("minExclusive-valid-restriction.3");
57                 }
58             }
59         }
60     
61         void checkParentMinInclusive(Comparable JavaDoc value) throws SchemaException {
62             if (!checkMin(value)) {
63                 if (minType == 1) {
64                     throw new SchemaException("minInclusive-valid-restriction.1");
65                 } else {
66                     throw new SchemaException("minInclusive-valid-restriction.3");
67                 }
68             }
69         }
70     
71         void checkParentMaxExclusive(Comparable JavaDoc value) throws SchemaException {
72             if (!checkMax(value)) {
73                 if (maxType == 1) {
74                     throw new SchemaException("maxExclusive-valid-restriction.2");
75                 } else {
76                     throw new SchemaException("maxExclusive-valid-restriction.1");
77                 }
78             }
79     
80         }
81     
82         void checkParentMaxInclusive(Comparable JavaDoc value) throws SchemaException {
83             if (!checkMax(value)) {
84                 if (maxType == 1) {
85                     throw new SchemaException("maxInclusive-valid-restriction.2");
86                 } else {
87                     throw new SchemaException("maxInclusive-valid-restriction.1");
88                 }
89             }
90         }
91     
92         void setMinExclusive(Comparable JavaDoc value, boolean fixed) throws SchemaException {
93             if ((isFixed(PrimitiveType.MIN_EXCLUSIVE_FACET) && !value.equals(min)) || isFixed(PrimitiveType.MIN_INCLUSIVE_FACET)) {
94                 throw new SchemaException("unknown-error-code.1");
95             }
96     
97             if (!checkMax(value)) {
98                 if (maxType == 1) {
99                     throw new SchemaException("minExclusive-valid-restriction.4");
100                 } else {
101                     throw new SchemaException("minExclusive-valid-restriction.2");
102                 }
103             }
104     
105             minType = 1;
106             min = value;
107             setFixed(PrimitiveType.MIN_EXCLUSIVE_FACET, fixed);
108         }
109     
110         void setMinInclusive(Comparable JavaDoc value, boolean fixed) throws SchemaException {
111             if ((isFixed(PrimitiveType.MIN_INCLUSIVE_FACET) && !value.equals(min)) || isFixed(PrimitiveType.MIN_EXCLUSIVE_FACET)) {
112                 throw new SchemaException("unknown-error-code.1");
113             }
114     
115             if (!checkMax(value)) {
116                 if (maxType == 1) {
117                     throw new SchemaException("minInclusive-valid-restriction.2");
118                 } else {
119                     throw new SchemaException("minInclusive-valid-restriction.4");
120                 }
121             }
122     
123             minType = 0;
124             min = value;
125             setFixed(PrimitiveType.MIN_INCLUSIVE_FACET, fixed);
126         }
127     
128         void setMaxExclusive(Comparable JavaDoc value, boolean fixed) throws SchemaException {
129             if ((isFixed(PrimitiveType.MAX_EXCLUSIVE_FACET) && !value.equals(max)) || isFixed(PrimitiveType.MAX_INCLUSIVE_FACET)) {
130                 throw new SchemaException("unknown-error-code.1");
131             }
132     
133             if (!checkMin(value)) {
134                 if (minType == 1) {
135                     throw new SchemaException("maxExclusive-valid-restriction.4");
136                 } else {
137                     throw new SchemaException("maxExclusive-valid-restriction.3");
138                 }
139             }
140     
141             maxType = 1;
142             max = value;
143             setFixed(PrimitiveType.MAX_EXCLUSIVE_FACET, fixed);
144         }
145     
146         void setMaxInclusive(Comparable JavaDoc value, boolean fixed) throws SchemaException {
147             if ((isFixed(PrimitiveType.MAX_INCLUSIVE_FACET) && !value.equals(max)) || isFixed(PrimitiveType.MAX_EXCLUSIVE_FACET)) {
148                 throw new SchemaException("unknown-error-code.1");
149             }
150     
151             if (!checkMin(value)) {
152                 if (minType == 1) {
153                     throw new SchemaException("maxInclusive-valid-restriction.4");
154                 } else {
155                     throw new SchemaException("maxInclusive-valid-restriction.3");
156                 }
157             }
158     
159             maxType = 0;
160             max = value;
161             setFixed(PrimitiveType.MAX_INCLUSIVE_FACET, fixed);
162         }
163     
164         void checkConstraints(Comparable JavaDoc value) throws SchemaException {
165             if (!checkMin(value)) {
166                 if (minType == 1)
167                     invalidFacet("cvc-minExclusive-valid", min, value.toString());
168                 else
169                     invalidFacet("cvc-minInclusive-valid", min, value.toString());
170             }
171             if (!checkMax(value)) {
172                 if (maxType == 1)
173                     invalidFacet("cvc-maxExclusive-valid", max, value.toString());
174                 else
175                     invalidFacet("cvc-maxInclusive-valid", max, value.toString());
176             }
177         }
178     
179         void setMinValue(Comparable JavaDoc value) {
180             min = value;
181         }
182     
183         void setMaxValue(Comparable JavaDoc value) {
184             max = value;
185         }
186     
187         void setMinValue(Comparable JavaDoc value, boolean inclusive) {
188             min = value;
189             minType = inclusive ? 0 : 1;
190         }
191     
192         void setMaxValue(Comparable JavaDoc value, boolean inclusive) {
193             max = value;
194             maxType = inclusive ? 0 : 1;
195         }
196     
197         Comparable JavaDoc getMinValue() {
198             return min;
199         }
200     
201         Comparable JavaDoc getMaxValue() {
202             return max;
203         }
204     
205         boolean isMinInclusive() {
206             return minType == 0;
207         }
208     
209         boolean isMaxInclusive() {
210             return maxType == 0;
211         }
212     
213     }
214     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
215     private static final String JavaDoc RCSName = "$Name: $";
216
217     private ComparableConstraints constraints = null;
218
219     ComparableType(String JavaDoc name, int type) {
220         super(name, type);
221     }
222
223     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
224         ComparableType result = (ComparableType) super.clone();
225         if (constraints != null)
226             result.constraints = (ComparableConstraints) constraints.clone();
227         return result;
228     }
229
230     private void initConstraints() {
231         if (constraints == null)
232             constraints = new ComparableConstraints();
233     }
234
235     protected void setMinExclusive(String JavaDoc value, boolean fixed) throws SchemaException {
236         initConstraints();
237         Comparable JavaDoc min = toComparable(super.normalizedValue(value));
238         constraints.checkParentMinExclusive(min);
239         constraints.setMinExclusive(min, fixed);
240     }
241
242     protected void setMinInclusive(String JavaDoc value, boolean fixed) throws SchemaException {
243         initConstraints();
244         Comparable JavaDoc min = toComparable(super.normalizedValue(value));
245         constraints.checkParentMinInclusive(min);
246         constraints.setMinInclusive(min, fixed);
247     }
248
249     protected void setMaxExclusive(String JavaDoc value, boolean fixed) throws SchemaException {
250         initConstraints();
251         Comparable JavaDoc max = toComparable(super.normalizedValue(value));
252         constraints.checkParentMaxExclusive(max);
253         constraints.setMaxExclusive(max, fixed);
254     }
255
256     protected void setMaxInclusive(String JavaDoc value, boolean fixed) throws SchemaException {
257         initConstraints();
258         Comparable JavaDoc max = toComparable(super.normalizedValue(value));
259         constraints.checkParentMaxInclusive(max);
260         constraints.setMaxInclusive(max, fixed);
261     }
262
263     protected void setMaxValue(Comparable JavaDoc value) {
264         constraints.setMaxValue(value);
265     }
266
267     protected void setMinValue(Comparable JavaDoc value) {
268         constraints.setMinValue(value);
269     }
270
271
272     protected Comparable JavaDoc checkComparable(Comparable JavaDoc comp) throws SchemaException {
273         if (constraints != null)
274             constraints.checkConstraints(comp);
275         return comp;
276     }
277
278     protected abstract Comparable JavaDoc toComparable(String JavaDoc value) throws SchemaException;
279
280     public void checkFacets(Object JavaDoc valueSpace) throws SchemaException {
281         super.checkFacets(valueSpace);
282         checkComparable((Comparable JavaDoc) valueSpace);
283     }
284
285     protected Object JavaDoc toValueSpace(String JavaDoc value, ValidationContextProvider context) throws SchemaException {
286         return toComparable(value);
287     }
288
289     public Comparable JavaDoc getMinValue() {
290         if (constraints == null)
291             return null;
292         return constraints.getMinValue();
293     }
294
295     public Comparable JavaDoc getMaxValue() {
296         if (constraints == null)
297             return null;
298         return constraints.getMaxValue();
299     }
300
301     public boolean isMinInclusive() {
302         if (constraints == null)
303             return false;
304         return constraints.isMinInclusive();
305     }
306
307     public boolean isMaxInclusive() {
308         if (constraints == null)
309             return false;
310         return constraints.isMaxInclusive();
311     }
312
313
314 }
315
Popular Tags