001    /**
002     * ========================================
003     * JFreeReport : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/
007     *
008     * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: AttributeReadHandler.java 3525 2007-10-16 11:43:48Z tmorgner $
027     * ------------
028     * (C) Copyright 2000-2005, by Object Refinery Limited.
029     * (C) Copyright 2005-2007, by Pentaho Corporation.
030     */
031    package org.jfree.report.modules.factories.report.flow;
032    
033    import java.io.ByteArrayInputStream;
034    import java.io.IOException;
035    import java.io.ObjectInputStream;
036    
037    import org.jfree.report.util.CharacterEntityParser;
038    import org.jfree.report.util.beans.BeanException;
039    import org.jfree.report.util.beans.ConverterRegistry;
040    import org.jfree.util.Log;
041    import org.jfree.util.ObjectUtilities;
042    import org.jfree.xmlns.parser.Base64;
043    import org.jfree.xmlns.parser.ParseException;
044    import org.jfree.xmlns.parser.PropertyReadHandler;
045    import org.xml.sax.Attributes;
046    import org.xml.sax.SAXException;
047    
048    /**
049     * Creation-Date: 09.04.2006, 12:58:24
050     *
051     * @author Thomas Morgner
052     */
053    public class AttributeReadHandler extends PropertyReadHandler
054    {
055      private String encoding;
056      private String className;
057      private Object value;
058      private CharacterEntityParser entityParser;
059      private String namespace;
060    
061      public AttributeReadHandler()
062      {
063        this.entityParser = CharacterEntityParser.createXMLEntityParser();
064      }
065    
066      /**
067       * Done parsing.
068       *
069       * @throws SAXException       if there is a parsing error.
070       */
071      protected void doneParsing() throws SAXException
072      {
073        super.doneParsing();
074        try
075        {
076          final String result = getResult();
077          if ("base64".equals(encoding))
078          {
079            final byte[] data = Base64.decode(result.trim().toCharArray());
080            final ByteArrayInputStream bin = new ByteArrayInputStream(data);
081            final ObjectInputStream oin = new ObjectInputStream(bin);
082            value = oin.readObject();
083          }
084          else
085          {
086            if (className != null)
087            {
088              final ClassLoader cl = ObjectUtilities.getClassLoader
089                      (AttributeReadHandler.class);
090              final Class c = cl.loadClass(className);
091              ConverterRegistry.toPropertyValue
092                      (entityParser.decodeEntities(result), c);
093            }
094            else
095            {
096              ConverterRegistry.toPropertyValue
097                      (entityParser.decodeEntities(result), String.class);
098            }
099          }
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    }