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: */package org.jfree.report.modules.factories.report.flow;
031:
032: import java.io.ByteArrayInputStream;
033: import java.io.IOException;
034: import java.io.ObjectInputStream;
035:
036: import org.jfree.report.util.CharacterEntityParser;
037: import org.jfree.report.util.beans.BeanException;
038: import org.jfree.report.util.beans.ConverterRegistry;
039: import org.jfree.util.Log;
040: import org.jfree.util.ObjectUtilities;
041: import org.jfree.xmlns.parser.Base64;
042: import org.jfree.xmlns.parser.ParseException;
043: import org.jfree.xmlns.parser.PropertyReadHandler;
044: import org.xml.sax.Attributes;
045: import org.xml.sax.SAXException;
046:
047: /**
048: * Creation-Date: 09.04.2006, 12:58:24
049: *
050: * @author Thomas Morgner
051: */
052: public class AttributeReadHandler extends PropertyReadHandler {
053: private String encoding;
054: private String className;
055: private Object value;
056: private CharacterEntityParser entityParser;
057: private String namespace;
058:
059: public AttributeReadHandler() {
060: this .entityParser = CharacterEntityParser
061: .createXMLEntityParser();
062: }
063:
064: /**
065: * Done parsing.
066: *
067: * @throws SAXException if there is a parsing error.
068: */
069: protected void doneParsing() throws SAXException {
070: super .doneParsing();
071: try {
072: final String result = getResult();
073: if ("base64".equals(encoding)) {
074: final byte[] data = Base64.decode(result.trim()
075: .toCharArray());
076: final ByteArrayInputStream bin = new ByteArrayInputStream(
077: data);
078: final ObjectInputStream oin = new ObjectInputStream(bin);
079: value = oin.readObject();
080: } else {
081: if (className != null) {
082: final ClassLoader cl = ObjectUtilities
083: .getClassLoader(AttributeReadHandler.class);
084: final Class c = cl.loadClass(className);
085: ConverterRegistry.toPropertyValue(entityParser
086: .decodeEntities(result), c);
087: } else {
088: ConverterRegistry.toPropertyValue(entityParser
089: .decodeEntities(result), String.class);
090: }
091: }
092: } catch (BeanException e) {
093: throw new ParseException("Unable to set attribute '"
094: + getName() + "'", e, getLocator());
095: } catch (ClassNotFoundException e) {
096: throw new ParseException("Unable to set attribute '"
097: + getName() + "'", e, getLocator());
098: } catch (IOException e) {
099: throw new ParseException("Unable to set attribute '"
100: + getName() + "'", e, getLocator());
101: }
102: }
103:
104: /**
105: * Starts parsing.
106: *
107: * @param attrs the attributes.
108: * @throws SAXException if there is a parsing error.
109: */
110: protected void startParsing(final Attributes attrs)
111: throws SAXException {
112: super .startParsing(attrs);
113: namespace = attrs.getValue(getUri(), "namespace-uri");
114: if (namespace == null) {
115: namespace = getNamespace();
116: }
117:
118: className = attrs.getValue(getUri(), "class");
119: if (className == null) {
120: className = "java.lang.String";
121: }
122: encoding = attrs.getValue(getUri(), "encoding");
123: if (encoding == null) {
124: encoding = "text";
125: } else if (("text".equals(encoding) == false)
126: && "base64".equals(encoding) == false) {
127: Log
128: .warn("Invalid value for attribute 'encoding'. Defaulting to 'text'");
129: encoding = "text";
130: }
131: }
132:
133: public String getNamespace() {
134: return namespace;
135: }
136:
137: /**
138: * Returns the object for this element.
139: *
140: * @return the object.
141: */
142: public Object getObject() {
143: return value;
144: }
145: }
|