1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package com.sun.org.apache.xerces.internal.impl.dv.xs;
23 
24 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
25 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
26 import com.sun.org.apache.xerces.internal.impl.dv.util.ByteListImpl;
27 import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
28 
29 /**
30  * Represent the schema type "hexBinary"
31  *
32  * @xerces.internal
33  *
34  * @author Neeraj Bajaj, Sun Microsystems, inc.
35  * @author Sandy Gao, IBM
36  *
37  */
38 public class HexBinaryDV extends TypeValidator {
39 
getAllowedFacets()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 
getActualValue(String content, ValidationContext context)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
getDataLength(Object value)53     public int getDataLength(Object value) {
54         return ((XHex)value).getLength();
55     }
56 
57     private static final class XHex extends ByteListImpl {
58 
XHex(byte[] data)59         public XHex(byte[] data) {
60             super(data);
61         }
toString()62         public synchronized String toString() {
63             if (canonical == null) {
64                 canonical = HexBin.encode(data);
65             }
66             return canonical;
67         }
68 
equals(Object obj)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 
hashCode()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 }
92