1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2001, 2002,2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl.dv.xs;
  22 
  23 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
  24 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
  25 import com.sun.org.apache.xerces.internal.impl.dv.util.ByteListImpl;
  26 import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
  27 
  28 /**
  29  * Represent the schema type "hexBinary"
  30  *
  31  * @xerces.internal
  32  *
  33  * @author Neeraj Bajaj, Sun Microsystems, inc.
  34  * @author Sandy Gao, IBM
  35  *
  36  * @version $Id: HexBinaryDV.java,v 1.7 2010-11-01 04:39:47 joehw Exp $
  37  */
  38 public class HexBinaryDV extends TypeValidator {
  39 
  40     public short getAllowedFacets(){
  41         return (XSSimpleTypeDecl.FACET_LENGTH | XSSimpleTypeDecl.FACET_MINLENGTH | XSSimpleTypeDecl.FACET_MAXLENGTH | XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_ENUMERATION | XSSimpleTypeDecl.FACET_WHITESPACE );
  42     }
  43 
  44     public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException {
  45         byte[] decoded = HexBin.decode(content);
  46         if (decoded == null)
  47             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "hexBinary"});
  48 
  49         return new XHex(decoded);
  50     }
  51 
  52     // length of a binary type is the number of bytes
  53     public int getDataLength(Object value) {
  54         return ((XHex)value).getLength();
  55     }
  56 
  57     private static final class XHex extends ByteListImpl {
  58 
  59         public XHex(byte[] data) {
  60             super(data);
  61         }
  62         public synchronized String toString() {
  63             if (canonical == null) {
  64                 canonical = HexBin.encode(data);
  65             }
  66             return canonical;
  67         }
  68 
  69         public boolean equals(Object obj) {
  70             if (!(obj instanceof XHex))
  71                 return false;
  72             byte[] odata = ((XHex)obj).data;
  73             int len = data.length;
  74             if (len != odata.length)
  75                 return false;
  76             for (int i = 0; i < len; i++) {
  77                 if (data[i] != odata[i])
  78                     return false;
  79             }
  80             return true;
  81         }
  82 
  83         public int hashCode() {
  84             int hash = 0;
  85             for (int i = 0; i < data.length; ++i) {
  86                 hash = hash * 37 + (((int) data[i]) & 0xff);
  87             }
  88             return hash;
  89         }
  90     }
  91 }