1 57 58 package org.enhydra.apache.xerces.validators.datatype; 59 60 import java.io.UnsupportedEncodingException ; 61 import java.util.Hashtable ; 62 63 import org.enhydra.apache.xerces.utils.HexBin; 64 65 73 public class HexBinaryDatatypeValidator extends AbstractStringValidator{ 74 75 76 public HexBinaryDatatypeValidator () throws InvalidDatatypeFacetException{ 77 super ( null, null, false ); 79 } 80 81 public HexBinaryDatatypeValidator ( DatatypeValidator base, Hashtable facets, 82 boolean derivedByList ) throws InvalidDatatypeFacetException { 83 84 super (base, facets, derivedByList); 85 } 86 87 protected void assignAdditionalFacets(String key, Hashtable facets) throws InvalidDatatypeFacetException{ 88 throw new InvalidDatatypeFacetException( getErrorString(DatatypeMessageProvider.ILLEGAL_STRING_FACET, 89 DatatypeMessageProvider.MSG_NONE, new Object [] { key })); 90 } 91 92 93 protected void checkValueSpace (String content) throws InvalidDatatypeValueException { 94 if (getLength(content) < 0) { 95 throw new InvalidDatatypeValueException( "Value '"+content+"' is not encoded in Hex" ); 96 } 97 } 98 99 protected int getLength( String content) { 100 int x = 0; 101 try { 102 x = HexBin.getDecodedDataLength(content.getBytes("utf-8")); 103 } 104 catch (UnsupportedEncodingException e) {} 105 return x; 106 } 107 108 109 public Object clone() throws CloneNotSupportedException { 110 HexBinaryDatatypeValidator newObj = null; 111 try { 112 newObj = new HexBinaryDatatypeValidator(); 113 114 newObj.fLocale = fLocale; 115 newObj.fBaseValidator = fBaseValidator; 116 newObj.fLength = fLength; 117 newObj.fMaxLength = fMaxLength; 118 newObj.fMinLength = fMinLength; 119 newObj.fPattern = fPattern; 120 newObj.fRegex = fRegex; 121 newObj.fEnumeration = fEnumeration; 122 newObj.fFacetsDefined = fFacetsDefined; 123 } catch ( InvalidDatatypeFacetException ex) { 124 ex.printStackTrace(); 125 } 126 return newObj; 127 } 128 129 public int compare( String value1, String value2 ){ 130 if (value1 == null || value2 == null) 131 return -1; 132 133 if (value1 == value2 || value1.equals(value2)) 134 return 0; 135 136 byte[] data1=HexBin.decode(value1.getBytes()); 137 byte[] data2=HexBin.decode(value2.getBytes()); 138 139 if (data1 == null || data2 == null) 140 return -1; 141 142 for (int i = 0; i < Math.min(data1.length, data2.length); i++) 143 if (data1[i] < data2[i]) 144 return -1; 145 else if (data1[i] > data2[i]) 146 return 1; 147 148 if (data1.length == data2.length) 149 return 0; 150 151 return data1.length > data2.length ? 1 : -1; 152 } 153 } 154 155 | Popular Tags |