001/* ========================================================================
002 * JCommon : a free general purpose class library for the Java(tm) platform
003 * ========================================================================
004 *
005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006 * 
007 * Project Info:  http://www.jfree.org/jcommon/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it 
010 * under the terms of the GNU Lesser General Public License as published by 
011 * the Free Software Foundation; either version 2.1 of the License, or 
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but 
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022 * USA.  
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025 * in the United States and other countries.]
026 * 
027 * ----------------------
028 * Base64ReadHandler.java
029 * ----------------------
030 * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
031 *
032 * Original Author:  Thomas Morgner;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 * $Id: Base64ReadHandler.java,v 1.4 2005/11/08 14:16:51 mungady Exp $
036 *
037 * Changes 
038 * -------
039 * 11-Feb-2004 : Added standard header and Javadocs (DG);
040 *  
041 */
042
043package org.jfree.xml.parser;
044
045import java.io.ByteArrayInputStream;
046import java.io.IOException;
047import java.io.ObjectInputStream;
048
049import org.jfree.xml.util.Base64;
050import org.xml.sax.SAXException;
051
052/**
053 * A read handler for Base64 encoded elements.
054 * 
055 * @deprecated base64 encoded elements are no longer supported ...
056 */
057public class Base64ReadHandler extends AbstractXmlReadHandler {
058    
059    /** The encoded object. */
060    private String encodedObject;
061    
062    /**
063     * Creates a new handler.
064     */
065    public Base64ReadHandler() {
066        super();
067    }
068
069    /**
070     * Process character data.
071     * 
072     * @param ch  the character buffer.
073     * @param start  the start index.
074     * @param length  the number of characters.
075     * 
076     * @throws SAXException ???.
077     */
078    public void characters(final char[] ch, final int start, final int length)
079        throws SAXException {
080        this.encodedObject = new String(ch, start, length);
081    }
082
083    /**
084     * Returns the object under construction.
085     * 
086     * @return the object
087     * 
088     * @throws XmlReaderException ???.
089     */
090    public Object getObject() throws XmlReaderException {
091        try {
092            final byte[] bytes = Base64.decode(this.encodedObject.toCharArray());
093            final ObjectInputStream in =
094                new ObjectInputStream(new ByteArrayInputStream(bytes));
095            return in.readObject();
096        } 
097        catch (IOException e) {
098            throw new XmlReaderException("Can't read class for <" + getTagName() + ">", e);
099        } 
100        catch (ClassNotFoundException e) {
101            throw new XmlReaderException("Class not found for <" + getTagName() + ">", e);
102        }
103    }
104}