Source for org.jfree.report.modules.factories.report.flow.AttributeReadHandler

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: AttributeReadHandler.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.modules.factories.report.flow;
  32: 
  33: import java.io.ByteArrayInputStream;
  34: import java.io.IOException;
  35: import java.io.ObjectInputStream;
  36: 
  37: import org.jfree.report.util.CharacterEntityParser;
  38: import org.jfree.report.util.beans.BeanException;
  39: import org.jfree.report.util.beans.ConverterRegistry;
  40: import org.jfree.util.Log;
  41: import org.jfree.util.ObjectUtilities;
  42: import org.jfree.xmlns.parser.Base64;
  43: import org.jfree.xmlns.parser.ParseException;
  44: import org.jfree.xmlns.parser.PropertyReadHandler;
  45: import org.xml.sax.Attributes;
  46: import org.xml.sax.SAXException;
  47: 
  48: /**
  49:  * Creation-Date: 09.04.2006, 12:58:24
  50:  *
  51:  * @author Thomas Morgner
  52:  */
  53: public class AttributeReadHandler extends PropertyReadHandler
  54: {
  55:   private String encoding;
  56:   private String className;
  57:   private Object value;
  58:   private CharacterEntityParser entityParser;
  59:   private String namespace;
  60: 
  61:   public AttributeReadHandler()
  62:   {
  63:     this.entityParser = CharacterEntityParser.createXMLEntityParser();
  64:   }
  65: 
  66:   /**
  67:    * Done parsing.
  68:    *
  69:    * @throws SAXException       if there is a parsing error.
  70:    */
  71:   protected void doneParsing() throws SAXException
  72:   {
  73:     super.doneParsing();
  74:     try
  75:     {
  76:       final String result = getResult();
  77:       if ("base64".equals(encoding))
  78:       {
  79:         final byte[] data = Base64.decode(result.trim().toCharArray());
  80:         final ByteArrayInputStream bin = new ByteArrayInputStream(data);
  81:         final ObjectInputStream oin = new ObjectInputStream(bin);
  82:         value = oin.readObject();
  83:       }
  84:       else
  85:       {
  86:         if (className != null)
  87:         {
  88:           final ClassLoader cl = ObjectUtilities.getClassLoader
  89:                   (AttributeReadHandler.class);
  90:           final Class c = cl.loadClass(className);
  91:           ConverterRegistry.toPropertyValue
  92:                   (entityParser.decodeEntities(result), c);
  93:         }
  94:         else
  95:         {
  96:           ConverterRegistry.toPropertyValue
  97:                   (entityParser.decodeEntities(result), String.class);
  98:         }
  99:       }
 100:     }
 101:     catch (BeanException e)
 102:     {
 103:       throw new ParseException("Unable to set attribute '" + getName() + "'", e, getLocator());
 104:     }
 105:     catch (ClassNotFoundException e)
 106:     {
 107:       throw new ParseException("Unable to set attribute '" + getName() + "'", e, getLocator() );
 108:     }
 109:     catch (IOException e)
 110:     {
 111:       throw new ParseException("Unable to set attribute '" + getName() + "'", e, getLocator());
 112:     }
 113:   }
 114: 
 115:   /**
 116:    * Starts parsing.
 117:    *
 118:    * @param attrs the attributes.
 119:    * @throws SAXException if there is a parsing error.
 120:    */
 121:   protected void startParsing(final Attributes attrs) throws SAXException
 122:   {
 123:     super.startParsing(attrs);
 124:     namespace = attrs.getValue(getUri(), "namespace-uri");
 125:     if (namespace == null)
 126:     {
 127:       namespace = getNamespace();
 128:     }
 129: 
 130:     className = attrs.getValue(getUri(), "class");
 131:     if (className == null)
 132:     {
 133:       className = "java.lang.String";
 134:     }
 135:     encoding = attrs.getValue(getUri(), "encoding");
 136:     if (encoding == null)
 137:     {
 138:       encoding = "text";
 139:     }
 140:     else if (("text".equals(encoding) == false) && "base64".equals(encoding) == false)
 141:     {
 142:       Log.warn ("Invalid value for attribute 'encoding'. Defaulting to 'text'");
 143:       encoding = "text";
 144:     }
 145:   }
 146: 
 147:   public String getNamespace()
 148:   {
 149:     return namespace;
 150:   }
 151: 
 152:   /**
 153:    * Returns the object for this element.
 154:    *
 155:    * @return the object.
 156:    */
 157:   public Object getObject()
 158:   {
 159:     return value;
 160:   }
 161: }