1 /*
2 * Copyright 2008 Werner Guttmann
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.exolab.castor.xml.schema.facets;
17
18 import java.util.Enumeration;
19
20 import org.exolab.castor.xml.schema.Facet;
21 import org.exolab.castor.xml.schema.SchemaException;
22
23 /**
24 * An implementation of <b>maxInclusive</b> constraining facet, defined in section
25 * "<a href="http://www.w3.org/TR/xmlschema-2/#rf-maxInclusive">4.3.7 maxInclusive</a>"
26 * of "<a href="http://www.w3.org/TR/xmlschema-2/">XML Schema Part 2:
27 * Datatypes Second Edition</a>" document.
28 *
29 * <p>[Definition:] <b>maxInclusive</b> is the <i>inclusive upper bound</i>
30 * of the <i>value space</i> for a datatype with the <i>ordered</i> property.
31 * The value of <b>maxInclusive</b> <i>must</i> be in the <i>value space</i>
32 * of the <i>base type</i>.
33 *
34 * @author <a href="mailto:sergei.ivanov@mail.ru">Sergei Ivanov</a>
35 * @version $Revision: 6465 $ $Date: 2006-04-13 06:47:36 -0600 (Thu, 13 Apr 2006) $
36 */
37 public final class MaxInclusive extends Facet {
38
39 /**
40 * Serial version UID.
41 */
42 private static final long serialVersionUID = 2826634377769846916L;
43
44 /**
45 * Creates an instance of this class.
46 * @param value A value for this {@link Facet}.
47 */
48 public MaxInclusive(final String value) {
49 super(Facet.MAX_INCLUSIVE, value);
50 }
51
52 /**
53 * Checks whether the current facet overrides a facet of the base data type.
54 *
55 * <p><i>maxInclusive</i> can override the following facets
56 * of the base data type:
57 *
58 * <ul>
59 * <li><i>maxExclusive</i></li>
60 * <li>or <i>maxInclusive</i></li>
61 * </ul>
62 *
63 * @param baseFacet a facet of the base data type
64 * @return <code>true</code>,
65 * if the current facet overrides <code>baseFacet</code>;
66 * <code>false</code>, otherwise.
67 * @see #checkConstraints(Enumeration, Enumeration)
68 */
69 public boolean overridesBase(final Facet baseFacet) {
70 final String otherName = baseFacet.getName();
71 return otherName.equals(Facet.MAX_EXCLUSIVE)
72 || otherName.equals(Facet.MAX_INCLUSIVE);
73 }
74
75 /**
76 * Validation is performed according to section
77 * "<a href="http://www.w3.org/TR/xmlschema-2/#maxInclusive-coss">4.3.7.4
78 * Constraints on maxInclusive Schema Components</a>"
79 * of "<a href="http://www.w3.org/TR/xmlschema-2/">XML Schema Part 2:
80 * Datatypes Second Edition</a>" document.
81 *
82 * @param localFacets local facets of the data type
83 * @param baseFacets merged facets of the base data type
84 * @throws SchemaException when the current facet does not satisfy
85 * schema component validation constraints
86 */
87 public void checkConstraints(
88 final Enumeration localFacets, final Enumeration baseFacets)
89 throws SchemaException {
90
91 while (localFacets.hasMoreElements()) {
92 final Facet other = (Facet) localFacets.nextElement();
93 if (this == other) {
94 continue; // do not check against self
95 }
96 final String otherName = other.getName();
97 if (otherName.equals(Facet.MIN_INCLUSIVE)
98 && getOwningType().isNumericType()
99 && other.toBigDecimal().compareTo(this.toBigDecimal()) > 0) {
100 // Schema Component Constraint: minInclusive <= maxInclusive
101 throw new SchemaException(
102 "It is an error for the value specified "
103 + "for minInclusive to be greater than "
104 + "the value specified for maxInclusive "
105 + "for the same datatype.");
106 }
107 }
108 if (baseFacets != null) {
109 while (baseFacets.hasMoreElements()) {
110 final Facet other = (Facet) baseFacets.nextElement();
111 final String otherName = other.getName();
112
113 // Schema Component Constraint: maxInclusive valid restriction
114 // It is an error if any of the following conditions is true:
115 if (otherName.equals(Facet.MAX_INCLUSIVE)
116 && getOwningType().isNumericType()
117 && this.toBigDecimal().compareTo(other.toBigDecimal()) > 0) {
118 // [1]
119 throw new SchemaException(
120 "It is an error if the following condition is true: "
121 + "maxInclusive is among the members of {facets} "
122 + "of {base type definition} and {value} is greater than "
123 + "the {value} of the parent maxInclusive.");
124 } else if (otherName.equals(Facet.MAX_EXCLUSIVE)
125 && getOwningType().isNumericType()
126 && this.toBigDecimal().compareTo(other.toBigDecimal()) >= 0) {
127 // [2]
128 throw new SchemaException(
129 "It is an error if the following condition is true: "
130 + "maxExclusive is among the members of {facets} "
131 + "of {base type definition} and {value} is greater than "
132 + "or equal to the {value} of the parent maxExclusive.");
133 } else if (otherName.equals(Facet.MIN_INCLUSIVE)
134 && getOwningType().isNumericType()
135 && this.toBigDecimal().compareTo(other.toBigDecimal()) < 0) {
136 // [3]
137 throw new SchemaException(
138 "It is an error if the following condition is true: "
139 + "minInclusive is among the members of {facets} "
140 + "of {base type definition} and {value} is less than "
141 + "the {value} of the parent minInclusive.");
142 } else if (otherName.equals(Facet.MIN_EXCLUSIVE)
143 && getOwningType().isNumericType()
144 && this.toBigDecimal().compareTo(other.toBigDecimal()) <= 0) {
145 // [4]
146 throw new SchemaException(
147 "It is an error if the following condition is true: "
148 + "minExclusive is among the members of {facets} "
149 + "of {base type definition} and {value} is less than "
150 + "or equal to the {value} of the parent minExclusive.");
151 }
152 }
153 }
154 }
155 }