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.Base64; 64 65 73 public class Base64BinaryDatatypeValidator extends AbstractStringValidator{ 74 75 76 77 public Base64BinaryDatatypeValidator () throws InvalidDatatypeFacetException{ 78 super( null, null, false ); 80 } 81 82 public Base64BinaryDatatypeValidator ( DatatypeValidator base, Hashtable facets, 83 boolean derivedByList ) throws InvalidDatatypeFacetException { 84 85 super (base, facets, derivedByList); 86 } 87 88 protected void assignAdditionalFacets(String key, Hashtable facets) throws InvalidDatatypeFacetException{ 89 throw new InvalidDatatypeFacetException( getErrorString(DatatypeMessageProvider.ILLEGAL_STRING_FACET, 90 DatatypeMessageProvider.MSG_NONE, new Object [] { key })); 91 } 92 93 94 protected void checkValueSpace (String content) throws InvalidDatatypeValueException { 95 if (getLength( content) < 0) { 96 throw new InvalidDatatypeValueException( "Value '"+content+"' is not encoded in Base64" ); 97 } 98 } 99 100 protected int getLength( String content) { 101 int x = 0; 102 try { 103 x = Base64.getDecodedDataLength(content.getBytes("utf-8")); 104 } 105 catch (UnsupportedEncodingException e) {} 106 return x; 107 } 108 109 public int compare( String value1, String value2 ){ 110 if (value1 == null || value2 == null) 111 return -1; 112 113 if (value1 == value2 || value1.equals(value2)) 114 return 0; 115 116 byte[] data1=Base64.decode(value1.getBytes()); 117 byte[] data2=Base64.decode(value2.getBytes()); 118 119 if (data1 == null || data2 == null) 120 return -1; 121 122 for (int i = 0; i < Math.min(data1.length, data2.length); i++) 123 if (data1[i] < data2[i]) 124 return -1; 125 else if (data1[i] > data2[i]) 126 return 1; 127 128 if (data1.length == data2.length) 129 return 0; 130 131 return data1.length > data2.length ? 1 : -1; 132 } 133 } 134 | Popular Tags |