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.Base64;
26 import com.sun.org.apache.xerces.internal.impl.dv.util.ByteListImpl;
27 
28 /**
29  * Represent the schema type "base64Binary"
30  *
31  * @xerces.internal
32  *
33  * @author Neeraj Bajaj, Sun Microsystems, inc.
34  * @author Sandy Gao, IBM
35  *
36  * @version $Id: Base64BinaryDV.java,v 1.7 2010-11-01 04:39:46 joehw Exp $
37  */
38 public class Base64BinaryDV 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 = Base64.decode(content);
46         if (decoded == null)
47             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "base64Binary"});
48 
49         return new XBase64(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 ((XBase64)value).getLength();
55     }
56 
57     /**
58      * represent base64 data
59      */
60     private static final class XBase64 extends ByteListImpl {
61 
XBase64(byte[] data)62         public XBase64(byte[] data) {
63             super(data);
64         }
toString()65         public synchronized String toString() {
66             if (canonical == null) {
67                 canonical = Base64.encode(data);
68             }
69             return canonical;
70         }
71 
equals(Object obj)72         public boolean equals(Object obj) {
73             if (!(obj instanceof XBase64))
74                 return false;
75             byte[] odata = ((XBase64)obj).data;
76             int len = data.length;
77             if (len != odata.length)
78                 return false;
79             for (int i = 0; i < len; i++) {
80                 if (data[i] != odata[i])
81                     return false;
82             }
83             return true;
84         }
85 
hashCode()86         public int hashCode() {
87             int hash = 0;
88             for (int i = 0; i < data.length; ++i) {
89                 hash = hash * 37 + (((int) data[i]) & 0xff);
90             }
91             return hash;
92         }
93     }
94 } // class Base64BinaryDV
95