KickJava   Java API By Example, From Geeks To Geeks.

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


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 MesureableType extends EnumerableType {
30     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
31     private static final String JavaDoc RCSName = "$Name: $";
32
33     private int length = -1;
34     private int minLength = -1;
35     private int maxLength = -1;
36
37     MesureableType(String JavaDoc name, int type) {
38         super(name, type);
39     }
40
41     protected void setLength(String JavaDoc value, boolean fixed) throws SchemaException {
42         int len;
43         try {
44             len = Integer.parseInt(value.trim());
45         } catch (Exception JavaDoc nfe) {
46             // "illegal length value in " + this + " : " + value;
47
throw new SchemaException("unknown-error-code.2");
48         }
49
50         if (length != -1 && length != len) {
51             // "length = " + value + " in " + this + ", but his parent's length = " + length;
52
throw new SchemaException("length-valid-restriction");
53         }
54
55         if (minLength != -1 || maxLength != -1) {
56             throw new SchemaException("length-minLength-maxLength");
57         }
58
59         length = len;
60         super.setFixed(PrimitiveType.LENGTH_FACET, fixed);
61     }
62
63     protected void setMinLength(String JavaDoc value, boolean fixed) throws SchemaException {
64         int len;
65         try {
66             len = Integer.parseInt(value.trim());
67         } catch (Exception JavaDoc nfe) {
68             // "illegal facet value";
69
throw new SchemaException("unknown-error-code.2");
70         }
71
72         if ((minLength != -1 && minLength > len) || (super.isFixed(PrimitiveType.MIN_LENGTH_FACET) && minLength != len)) {
73             throw new SchemaException("minLength-valid-restriction");
74         }
75
76         if (length != -1) {
77             throw new SchemaException("length-minLength-maxLength");
78         }
79
80         if (maxLength != -1 && len > maxLength) {
81             throw new SchemaException("minLength-less-than-equal-to-maxLength");
82         }
83
84         minLength = len;
85         super.setFixed(PrimitiveType.MIN_LENGTH_FACET, fixed);
86     }
87
88     protected void setMaxLength(String JavaDoc value, boolean fixed) throws SchemaException {
89         int len;
90         try {
91             len = Integer.parseInt(value.trim());
92         } catch (Exception JavaDoc nfe) {
93             // "illegal facet value";
94
throw new SchemaException("unknown-error-code.2");
95         }
96
97         if ((maxLength != -1 && maxLength < len) || (super.isFixed(PrimitiveType.MAX_LENGTH_FACET) && maxLength != len)) {
98             // "maxLength = " + value + " in " + this + ", but his parent's maxLength = " + maxLength;
99
throw new SchemaException("maxLength-valid-restriction");
100         }
101
102         if (length != -1) {
103             throw new SchemaException("length-minLength-maxLength");
104         }
105
106         if (minLength != -1 && len < minLength) {
107             throw new SchemaException("minLength-less-than-equal-to-maxLength");
108         }
109
110         maxLength = len;
111         super.setFixed(PrimitiveType.MAX_LENGTH_FACET, fixed);
112     }
113
114     protected void checkLength(int len, Object JavaDoc valueSpace) throws SchemaException {
115         if (length != -1 && len != length)
116             super.invalidFacet("cvc-length-valid", new Integer JavaDoc(length), valueSpace.toString());
117
118         if (minLength != -1 && len < minLength)
119             super.invalidFacet("cvc-minLength-valid", new Integer JavaDoc(minLength), valueSpace.toString());
120
121         if (maxLength != -1 && len > maxLength)
122             super.invalidFacet("cvc-maxLength-valid", new Integer JavaDoc(maxLength), valueSpace.toString());
123     }
124
125     protected Object JavaDoc toValueSpace(String JavaDoc value, ValidationContextProvider context) throws SchemaException {
126         return value.toString();
127     }
128
129     public int getLength() {
130         return length;
131     }
132     public int getMinLength() {
133         return minLength;
134     }
135     public int getMaxLength() {
136         return maxLength;
137     }
138 }
139
Popular Tags