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