KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > dv > xs > HexBinaryDV


1 /*
2  * Copyright 2001, 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.dv.xs;
18
19 import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
20 import org.apache.xerces.impl.dv.ValidationContext;
21 import org.apache.xerces.impl.dv.util.ByteListImpl;
22 import org.apache.xerces.impl.dv.util.HexBin;
23
24 /**
25  * Represent the schema type "hexBinary"
26  *
27  * @xerces.internal
28  *
29  * @author Neeraj Bajaj, Sun Microsystems, inc.
30  * @author Sandy Gao, IBM
31  *
32  * @version $Id: HexBinaryDV.java,v 1.7 2004/10/06 14:56:46 mrglavas Exp $
33  */

34 public class HexBinaryDV extends TypeValidator {
35
36     public short getAllowedFacets(){
37         return (XSSimpleTypeDecl.FACET_LENGTH | XSSimpleTypeDecl.FACET_MINLENGTH | XSSimpleTypeDecl.FACET_MAXLENGTH | XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_ENUMERATION | XSSimpleTypeDecl.FACET_WHITESPACE );
38     }
39
40     public Object JavaDoc getActualValue(String JavaDoc content, ValidationContext context) throws InvalidDatatypeValueException {
41         byte[] decoded = HexBin.decode(content);
42         if (decoded == null)
43             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object JavaDoc[]{content, "hexBinary"});
44
45         return new XHex(decoded);
46     }
47
48     // length of a binary type is the number of bytes
49
public int getDataLength(Object JavaDoc value) {
50         return ((XHex)value).getLength();
51     }
52
53     private static final class XHex extends ByteListImpl {
54
55         public XHex(byte[] data) {
56             super(data);
57         }
58         public synchronized String JavaDoc toString() {
59             if (canonical == null) {
60                 canonical = HexBin.encode(data);
61             }
62             return canonical;
63         }
64         
65         public boolean equals(Object JavaDoc obj) {
66             if (!(obj instanceof XHex))
67                 return false;
68             byte[] odata = ((XHex)obj).data;
69             int len = data.length;
70             if (len != odata.length)
71                 return false;
72             for (int i = 0; i < len; i++) {
73                 if (data[i] != odata[i])
74                     return false;
75             }
76             return true;
77         }
78
79     }
80 }
81
Popular Tags