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