/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-04 The eXist Project
 *  http://exist-db.org
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id$
 */
package org.exist.xquery.value;

import java.text.Collator;

import org.apache.xmlrpc.Base64;
import org.exist.xquery.XPathException;

public class Base64Binary extends AtomicValue {

    private byte[] data;
    
    public Base64Binary(byte[] data) {
        super();
        this.data = data;
    }

    public int getType() {
        return Type.BASE64_BINARY;
    }
    
    public byte[] getBinaryData() {
        return data;
    }
    
    public String getStringValue() throws XPathException {
        return new String(Base64.encode(data));
    }

    public AtomicValue convertTo(int requiredType) throws XPathException {
        if (requiredType == Type.BASE64_BINARY)
            return this;
        throw new XPathException("cannot convert " + Type.getTypeName(getType()) + " to " + Type.getTypeName(requiredType));
    }

    public boolean compareTo(Collator collator, int operator, AtomicValue other)
            throws XPathException {
        throw new XPathException("Cannot compare values of type xs:base64Binary");
    }

    public int compareTo(Collator collator, AtomicValue other)
            throws XPathException {
        throw new XPathException("Cannot compare values of type xs:base64Binary");
    }

    public AtomicValue max(Collator collator, AtomicValue other)
            throws XPathException {
        throw new XPathException("Cannot compare values of type xs:base64Binary");
    }

    public AtomicValue min(Collator collator, AtomicValue other)
            throws XPathException {
        throw new XPathException("Cannot compare values of type xs:base64Binary");
    }
    
    public int conversionPreference(Class javaClass) {
        if (javaClass.isArray() && javaClass.isInstance(Byte.class))
            return 0;
        return Integer.MAX_VALUE;
    }
    
    public Object toJavaObject(Class target) throws XPathException {
        if(target.isAssignableFrom(Base64Binary.class))
            return this;
        if (target.isArray() && target == Byte.class)
            return data;
        throw new XPathException("cannot convert value of type " + Type.getTypeName(getType()) +
            " to Java object of type " + target.getName());
    }
}
@@ -895,16 +895,61 @@
 value;%0A%0A
+import java.io.UnsupportedEncodingException;%0A
 import j
@@ -1544,22 +1544,23 @@
 n %7B%0A    
-    if
+%09switch
  (requir
@@ -1565,19 +1565,29 @@
 iredType
- ==
+) %7B%0A    %09case
  Type.BA
@@ -1601,43 +1601,242 @@
 NARY
-)
+: 
 %0A    
-        return this;%0A    
+%09%09return this;%0A    %09case Type.STRING: %0A    %09%09try %7B%0A%09%09%09%09%09return new StringValue(new String(data, %22UTF-8%22));%0A%09%09%09%09%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09%09%09return new StringValue(new String(data));%0A%09%09%09%09%7D%0A    %09default:%0A
     
+%09%09
 thro
@@ -1944,24 +1944,31 @@
 iredType));%0A
+    %09%7D%0A
     %7D%0A%0A    p

Allow a value of type xs:base64 to be cast to xs:string.