KickJava   Java API By Example, From Geeks To Geeks.

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


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.Base64;
22 import org.apache.xerces.impl.dv.util.ByteListImpl;
23
24 /**
25  * Represent the schema type "base64Binary"
26  *
27  * @xerces.internal
28  *
29  * @author Neeraj Bajaj, Sun Microsystems, inc.
30  * @author Sandy Gao, IBM
31  *
32  * @version $Id: Base64BinaryDV.java,v 1.7 2004/10/06 14:56:47 mrglavas Exp $
33  */

34 public class Base64BinaryDV 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 = Base64.decode(content);
42         if (decoded == null)
43             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object JavaDoc[]{content, "base64Binary"});
44
45         return new XBase64(decoded);
46     }
47
48     // length of a binary type is the number of bytes
49
public int getDataLength(Object JavaDoc value) {
50         return ((XBase64)value).getLength();
51     }
52
53     /**
54      * represent base64 data
55      */

56     private static final class XBase64 extends ByteListImpl {
57
58         public XBase64(byte[] data) {
59             super(data);
60         }
61         public synchronized String JavaDoc toString() {
62             if (canonical == null) {
63                 canonical = Base64.encode(data);
64             }
65             return canonical;
66         }
67         
68         public boolean equals(Object JavaDoc obj) {
69             if (!(obj instanceof XBase64))
70                 return false;
71             byte[] odata = ((XBase64)obj).data;
72             int len = data.length;
73             if (len != odata.length)
74                 return false;
75             for (int i = 0; i < len; i++) {
76                 if (data[i] != odata[i])
77                     return false;
78             }
79             return true;
80         }
81     }
82 } // class Base64BinaryDV
83
Popular Tags