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>minExclusive</b> constraining facet, defined in section
25 * "<a href="http://www.w3.org/TR/xmlschema-2/#rf-minExclusive">4.3.9 minExclusive</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>minExclusive</b> is the <i>exclusive lower bound</i>
30 * of the <i>value space</i> for a datatype with the <i>ordered</i> property.
31 * The value of <b>minExclusive</b> <i>must</i> be in the <i>value space</i>
32 * of the <i>base type</i> or be equal to {value} in {base type definition}.
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 MinExclusive extends Facet {
38
39 /**
40 * Serial version UID.
41 */
42 private static final long serialVersionUID = 9164023814934394681L;
43
44 /**
45 * Creates an instance of this class.
46 * @param value A value for this {@link Facet}.
47 */
48 public MinExclusive(final String value) {
49 super(Facet.MIN_EXCLUSIVE, value);
50 }
51
52 /**
53 * Checks whether the current facet overrides a facet of the base data type.
54 *
55 * <p><i>minExclusive</i> can override the following facets
56 * of the base data type:
57 *
58 * <ul>
59 * <li><i>minExclusive</i></li>
60 * <li>or <i>minInclusive</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.MIN_EXCLUSIVE)
72 || otherName.equals(Facet.MIN_INCLUSIVE);
73 }
74
75 /**
76 * Validation is performed according to section
77 * "<a href="http://www.w3.org/TR/xmlschema-2/#minExclusive-coss">4.3.9.4
78 * Constraints on minExclusive 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 // Schema Component Constraint: minInclusive and minExclusive
99 throw new SchemaException(
100 "It is an error for both minInclusive and minExclusive "
101 + "to be specified for the same datatype.");
102 } else if (otherName.equals(Facet.MAX_INCLUSIVE)
103 && getOwningType().isNumericType()
104 && this.toBigDecimal().compareTo(other.toBigDecimal()) >= 0) {
105 // Schema Component Constraint: minExclusive < maxInclusive
106 throw new SchemaException(
107 "It is an error for the value specified "
108 + "for minExclusive to be greater than "
109 + "or equal to the value specified for "
110 + "maxInclusive for the same datatype.");
111 }
112 }
113 if (baseFacets != null) {
114 while (baseFacets.hasMoreElements()) {
115 final Facet other = (Facet) baseFacets.nextElement();
116 final String otherName = other.getName();
117
118 // Schema Component Constraint: minExclusive valid restriction
119 // It is an error if any of the following conditions is true:
120 if (otherName.equals(Facet.MIN_EXCLUSIVE)
121 && getOwningType().isNumericType()
122 && this.toBigDecimal().compareTo(other.toBigDecimal()) < 0) {
123 // [1]
124 throw new SchemaException(
125 "It is an error if the following condition is true: "
126 + "minExclusive is among the members of {facets} of "
127 + "{base type definition} and {value} is less than "
128 + "the {value} of the parent minExclusive.");
129 } else if (otherName.equals(Facet.MAX_INCLUSIVE)
130 && getOwningType().isNumericType()
131 && this.toBigDecimal().compareTo(other.toBigDecimal()) > 0) {
132 // [2]
133 throw new SchemaException(
134 "It is an error if the following condition is true: "
135 + "maxInclusive is among the members of {facets} of "
136 + "{base type definition} and {value} is greater than "
137 + "the {value} of the parent maxInclusive.");
138 } else if (otherName.equals(Facet.MIN_INCLUSIVE)
139 && getOwningType().isNumericType()
140 && this.toBigDecimal().compareTo(other.toBigDecimal()) < 0) {
141 // [3]
142 throw new SchemaException(
143 "It is an error if the following condition is true: "
144 + "minInclusive is among the members of {facets} of "
145 + "{base type definition} and {value} is less than "
146 + "the {value} of the parent minInclusive.");
147 } else if (otherName.equals(Facet.MAX_EXCLUSIVE)
148 && getOwningType().isNumericType()
149 && this.toBigDecimal().compareTo(other.toBigDecimal()) >= 0) {
150 // [4]
151 throw new SchemaException(
152 "It is an error if the following condition is true: "
153 + "maxExclusive is among the members of {facets} of "
154 + "{base type definition} and {value} is greater than "
155 + "or equal to the {value} of the parent maxExclusive.");
156 }
157 }
158 }
159 }
160 }