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 MesureableType extends EnumerableType { 30 private static final String RCSRevision = "$Revision: 1.1 $"; 31 private static final String RCSName = "$Name: $"; 32 33 private int length = -1; 34 private int minLength = -1; 35 private int maxLength = -1; 36 37 MesureableType(String name, int type) { 38 super(name, type); 39 } 40 41 protected void setLength(String value, boolean fixed) throws SchemaException { 42 int len; 43 try { 44 len = Integer.parseInt(value.trim()); 45 } catch (Exception nfe) { 46 throw new SchemaException("unknown-error-code.2"); 48 } 49 50 if (length != -1 && length != len) { 51 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 value, boolean fixed) throws SchemaException { 64 int len; 65 try { 66 len = Integer.parseInt(value.trim()); 67 } catch (Exception nfe) { 68 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 value, boolean fixed) throws SchemaException { 89 int len; 90 try { 91 len = Integer.parseInt(value.trim()); 92 } catch (Exception nfe) { 93 throw new SchemaException("unknown-error-code.2"); 95 } 96 97 if ((maxLength != -1 && maxLength < len) || (super.isFixed(PrimitiveType.MAX_LENGTH_FACET) && maxLength != len)) { 98 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 valueSpace) throws SchemaException { 115 if (length != -1 && len != length) 116 super.invalidFacet("cvc-length-valid", new Integer (length), valueSpace.toString()); 117 118 if (minLength != -1 && len < minLength) 119 super.invalidFacet("cvc-minLength-valid", new Integer (minLength), valueSpace.toString()); 120 121 if (maxLength != -1 && len > maxLength) 122 super.invalidFacet("cvc-maxLength-valid", new Integer (maxLength), valueSpace.toString()); 123 } 124 125 protected Object toValueSpace(String 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 |