1 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 { 32 private static final String RCSRevision = "$Revision: 1.2 $"; 33 private static final String RCSName = "$Name: $"; 34 private int minType = -1; 35 private Comparable min = null; 36 private int maxType = -1; 37 private Comparable max = null; 38 39 public Object clone() throws CloneNotSupportedException { 40 return super.clone(); 41 } 42 43 private boolean checkMin(Comparable value) { 44 return minType == -1 || value.compareTo(min) >= minType; 45 } 46 47 private boolean checkMax(Comparable value) { 48 return maxType == -1 || max.compareTo(value) >= maxType; 49 } 50 51 void checkParentMinExclusive(Comparable 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 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 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 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 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 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 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 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 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 value) { 180 min = value; 181 } 182 183 void setMaxValue(Comparable value) { 184 max = value; 185 } 186 187 void setMinValue(Comparable value, boolean inclusive) { 188 min = value; 189 minType = inclusive ? 0 : 1; 190 } 191 192 void setMaxValue(Comparable value, boolean inclusive) { 193 max = value; 194 maxType = inclusive ? 0 : 1; 195 } 196 197 Comparable getMinValue() { 198 return min; 199 } 200 201 Comparable 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 RCSRevision = "$Revision: 1.2 $"; 215 private static final String RCSName = "$Name: $"; 216 217 private ComparableConstraints constraints = null; 218 219 ComparableType(String name, int type) { 220 super(name, type); 221 } 222 223 public Object clone() throws CloneNotSupportedException { 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 value, boolean fixed) throws SchemaException { 236 initConstraints(); 237 Comparable min = toComparable(super.normalizedValue(value)); 238 constraints.checkParentMinExclusive(min); 239 constraints.setMinExclusive(min, fixed); 240 } 241 242 protected void setMinInclusive(String value, boolean fixed) throws SchemaException { 243 initConstraints(); 244 Comparable min = toComparable(super.normalizedValue(value)); 245 constraints.checkParentMinInclusive(min); 246 constraints.setMinInclusive(min, fixed); 247 } 248 249 protected void setMaxExclusive(String value, boolean fixed) throws SchemaException { 250 initConstraints(); 251 Comparable max = toComparable(super.normalizedValue(value)); 252 constraints.checkParentMaxExclusive(max); 253 constraints.setMaxExclusive(max, fixed); 254 } 255 256 protected void setMaxInclusive(String value, boolean fixed) throws SchemaException { 257 initConstraints(); 258 Comparable max = toComparable(super.normalizedValue(value)); 259 constraints.checkParentMaxInclusive(max); 260 constraints.setMaxInclusive(max, fixed); 261 } 262 263 protected void setMaxValue(Comparable value) { 264 constraints.setMaxValue(value); 265 } 266 267 protected void setMinValue(Comparable value) { 268 constraints.setMinValue(value); 269 } 270 271 272 protected Comparable checkComparable(Comparable comp) throws SchemaException { 273 if (constraints != null) 274 constraints.checkConstraints(comp); 275 return comp; 276 } 277 278 protected abstract Comparable toComparable(String value) throws SchemaException; 279 280 public void checkFacets(Object valueSpace) throws SchemaException { 281 super.checkFacets(valueSpace); 282 checkComparable((Comparable ) valueSpace); 283 } 284 285 protected Object toValueSpace(String value, ValidationContextProvider context) throws SchemaException { 286 return toComparable(value); 287 } 288 289 public Comparable getMinValue() { 290 if (constraints == null) 291 return null; 292 return constraints.getMinValue(); 293 } 294 295 public Comparable 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 |