001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.dv.xs;
019:
020: import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
021: import org.apache.xerces.impl.dv.ValidationContext;
022: import org.apache.xerces.impl.dv.util.Base64;
023: import org.apache.xerces.impl.dv.util.ByteListImpl;
024:
025: /**
026: * Represent the schema type "base64Binary"
027: *
028: * @xerces.internal
029: *
030: * @author Neeraj Bajaj, Sun Microsystems, inc.
031: * @author Sandy Gao, IBM
032: *
033: * @version $Id: Base64BinaryDV.java 446745 2006-09-15 21:43:58Z mrglavas $
034: */
035: public class Base64BinaryDV extends TypeValidator {
036:
037: public short getAllowedFacets() {
038: return (XSSimpleTypeDecl.FACET_LENGTH
039: | XSSimpleTypeDecl.FACET_MINLENGTH
040: | XSSimpleTypeDecl.FACET_MAXLENGTH
041: | XSSimpleTypeDecl.FACET_PATTERN
042: | XSSimpleTypeDecl.FACET_ENUMERATION | XSSimpleTypeDecl.FACET_WHITESPACE);
043: }
044:
045: public Object getActualValue(String content,
046: ValidationContext context)
047: throws InvalidDatatypeValueException {
048: byte[] decoded = Base64.decode(content);
049: if (decoded == null)
050: throw new InvalidDatatypeValueException(
051: "cvc-datatype-valid.1.2.1", new Object[] { content,
052: "base64Binary" });
053:
054: return new XBase64(decoded);
055: }
056:
057: // length of a binary type is the number of bytes
058: public int getDataLength(Object value) {
059: return ((XBase64) value).getLength();
060: }
061:
062: /**
063: * represent base64 data
064: */
065: private static final class XBase64 extends ByteListImpl {
066:
067: public XBase64(byte[] data) {
068: super (data);
069: }
070:
071: public synchronized String toString() {
072: if (canonical == null) {
073: canonical = Base64.encode(data);
074: }
075: return canonical;
076: }
077:
078: public boolean equals(Object obj) {
079: if (!(obj instanceof XBase64))
080: return false;
081: byte[] odata = ((XBase64) obj).data;
082: int len = data.length;
083: if (len != odata.length)
084: return false;
085: for (int i = 0; i < len; i++) {
086: if (data[i] != odata[i])
087: return false;
088: }
089: return true;
090: }
091:
092: public int hashCode() {
093: int hash = 0;
094: for (int i = 0; i < data.length; ++i) {
095: hash = hash * 37 + (((int) data[i]) & 0xff);
096: }
097: return hash;
098: }
099: }
100: } // class Base64BinaryDV
|