Source for org.jfree.xml.parser.Base64ReadHandler

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * ----------------------
  28:  * Base64ReadHandler.java
  29:  * ----------------------
  30:  * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: Base64ReadHandler.java,v 1.4 2005/11/08 14:16:51 mungady Exp $
  36:  *
  37:  * Changes 
  38:  * -------
  39:  * 11-Feb-2004 : Added standard header and Javadocs (DG);
  40:  *  
  41:  */
  42: 
  43: package org.jfree.xml.parser;
  44: 
  45: import java.io.ByteArrayInputStream;
  46: import java.io.IOException;
  47: import java.io.ObjectInputStream;
  48: 
  49: import org.jfree.xml.util.Base64;
  50: import org.xml.sax.SAXException;
  51: 
  52: /**
  53:  * A read handler for Base64 encoded elements.
  54:  * 
  55:  * @deprecated base64 encoded elements are no longer supported ...
  56:  */
  57: public class Base64ReadHandler extends AbstractXmlReadHandler {
  58:     
  59:     /** The encoded object. */
  60:     private String encodedObject;
  61:     
  62:     /**
  63:      * Creates a new handler.
  64:      */
  65:     public Base64ReadHandler() {
  66:         super();
  67:     }
  68: 
  69:     /**
  70:      * Process character data.
  71:      * 
  72:      * @param ch  the character buffer.
  73:      * @param start  the start index.
  74:      * @param length  the number of characters.
  75:      * 
  76:      * @throws SAXException ???.
  77:      */
  78:     public void characters(final char[] ch, final int start, final int length)
  79:         throws SAXException {
  80:         this.encodedObject = new String(ch, start, length);
  81:     }
  82: 
  83:     /**
  84:      * Returns the object under construction.
  85:      * 
  86:      * @return the object
  87:      * 
  88:      * @throws XmlReaderException ???.
  89:      */
  90:     public Object getObject() throws XmlReaderException {
  91:         try {
  92:             final byte[] bytes = Base64.decode(this.encodedObject.toCharArray());
  93:             final ObjectInputStream in =
  94:                 new ObjectInputStream(new ByteArrayInputStream(bytes));
  95:             return in.readObject();
  96:         } 
  97:         catch (IOException e) {
  98:             throw new XmlReaderException("Can't read class for <" + getTagName() + ">", e);
  99:         } 
 100:         catch (ClassNotFoundException e) {
 101:             throw new XmlReaderException("Class not found for <" + getTagName() + ">", e);
 102:         }
 103:     }
 104: }