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
22package com.sun.org.apache.xerces.internal.impl.dv.xs;
23
24import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
25import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
26import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
27import com.sun.org.apache.xerces.internal.impl.dv.util.ByteListImpl;
28
29/**
30 * Represent the schema type "base64Binary"
31 *
32 * @xerces.internal
33 *
34 * @author Neeraj Bajaj, Sun Microsystems, inc.
35 * @author Sandy Gao, IBM
36 *
37 */
38public class Base64BinaryDV 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 = 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
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
62        public XBase64(byte[] data) {
63            super(data);
64        }
65        public synchronized String toString() {
66            if (canonical == null) {
67                canonical = Base64.encode(data);
68            }
69            return canonical;
70        }
71
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
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