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: AbstractElementReadHandler.java,v 1.3 2007/04/01 18:49:27 taqua Exp $
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.util.ArrayList;
034
035 import org.jfree.report.modules.factories.report.base.NodeReadHandler;
036 import org.jfree.report.structure.Element;
037 import org.jfree.report.structure.Node;
038 import org.jfree.xmlns.parser.AbstractXmlReadHandler;
039 import org.jfree.xmlns.parser.PropertyReadHandler;
040 import org.jfree.xmlns.parser.RootXmlReadHandler;
041 import org.jfree.xmlns.parser.XmlReadHandler;
042 import org.xml.sax.Attributes;
043 import org.xml.sax.SAXException;
044
045 /**
046 * Creation-Date: 09.04.2006, 13:55:36
047 *
048 * @author Thomas Morgner
049 */
050 public abstract class AbstractElementReadHandler
051 extends AbstractXmlReadHandler implements NodeReadHandler
052 {
053 private boolean virtual;
054 private boolean enabled;
055 private String style;
056 private ArrayList expressionHandlers;
057 private ArrayList styleExpressionHandlers;
058 private ArrayList attributeExpressionHandlers;
059 private ArrayList attributeHandlers;
060 private ArrayList stylePropertyHandlers;
061 private DisplayConditionReadHandler displayConditionReadHandler;
062
063 protected AbstractElementReadHandler()
064 {
065 expressionHandlers = new ArrayList();
066 styleExpressionHandlers = new ArrayList();
067 attributeExpressionHandlers = new ArrayList();
068 stylePropertyHandlers = new ArrayList();
069 attributeHandlers = new ArrayList();
070 }
071
072 public boolean isEnabled()
073 {
074 return enabled;
075 }
076
077 public String getStyle()
078 {
079 return style;
080 }
081
082 /**
083 * Initialises the handler.
084 *
085 * @param rootHandler the root handler.
086 * @param tagName the tag name.
087 */
088 public void init(final RootXmlReadHandler rootHandler,
089 final String uri,
090 final String tagName)
091 {
092 super.init(rootHandler, uri, tagName);
093
094 final Element element = getElement();
095 element.setNamespace(uri);
096 element.setType(tagName);
097 }
098
099 /**
100 * Starts parsing.
101 *
102 * @param attrs the attributes.
103 * @throws SAXException if there is a parsing error.
104 */
105 protected void startParsing(final Attributes attrs) throws SAXException
106 {
107 super.startParsing(attrs);
108 style = attrs.getValue(FlowReportFactoryModule.NAMESPACE, "style");
109 final String enabledValue = attrs.getValue(FlowReportFactoryModule.NAMESPACE, "enabled");
110 if (enabledValue != null)
111 {
112 enabled = "true".equals(enabledValue);
113 }
114 else
115 {
116 enabled = true;
117 }
118
119 final String virtualValue = attrs.getValue(FlowReportFactoryModule.NAMESPACE, "virtual");
120 if (virtualValue != null)
121 {
122 virtual = "true".equals(virtualValue);
123 }
124 else
125 {
126 virtual = false;
127 }
128 }
129
130 /**
131 * Returns the handler for a child element.
132 *
133 * @param tagName the tag name.
134 * @param atts the attributes.
135 * @return the handler or null, if the tagname is invalid.
136 * @throws SAXException if there is a parsing error.
137 * @throws XmlReaderException if there is a reader error.
138 */
139 protected XmlReadHandler getHandlerForChild(final String uri,
140 final String tagName,
141 final Attributes atts)
142 throws SAXException
143 {
144 if (FlowReportFactoryModule.NAMESPACE.equals(uri))
145 {
146 if ("expression".equals(tagName))
147 {
148 ExpressionReadHandler erh = new ExpressionReadHandler();
149 expressionHandlers.add(erh);
150 return erh;
151 }
152 if ("style-expression".equals(tagName))
153 {
154 StyleExpressionReadHandler erh = new StyleExpressionReadHandler();
155 styleExpressionHandlers.add(erh);
156 return erh;
157 }
158 if ("style-property".equals(tagName))
159 {
160 PropertyReadHandler erh = new PropertyReadHandler();
161 stylePropertyHandlers.add(erh);
162 return erh;
163 }
164 if ("attribute-expression".equals(tagName))
165 {
166 AttributeExpressionReadHandler erh = new AttributeExpressionReadHandler();
167 attributeExpressionHandlers.add(erh);
168 return erh;
169 }
170 if ("attribute".equals(tagName))
171 {
172 AttributeReadHandler erh = new AttributeReadHandler();
173 attributeHandlers.add(erh);
174 return erh;
175 }
176 if ("display-condition".equals(tagName))
177 {
178 displayConditionReadHandler = new DisplayConditionReadHandler();
179 return displayConditionReadHandler;
180 }
181 }
182 return null;
183 }
184
185 protected void configureElement(Element e)
186 {
187 if (displayConditionReadHandler != null)
188 {
189 e.setDisplayCondition(displayConditionReadHandler.getExpression());
190 }
191 for (int i = 0; i < expressionHandlers.size(); i++)
192 {
193 final ExpressionReadHandler handler =
194 (ExpressionReadHandler) expressionHandlers.get(i);
195 e.addExpression(handler.getExpression());
196 }
197 for (int i = 0; i < styleExpressionHandlers.size(); i++)
198 {
199 final StyleExpressionReadHandler handler =
200 (StyleExpressionReadHandler) styleExpressionHandlers .get(i);
201 e.setStyleExpression(handler.getStyleKey(), handler.getExpression());
202 }
203 for (int i = 0; i < stylePropertyHandlers.size(); i++)
204 {
205
206 final PropertyReadHandler handler =
207 (PropertyReadHandler) stylePropertyHandlers .get(i);
208 e.getStyle().setPropertyValueAsString(handler.getName(), handler.getResult());
209 }
210 for (int i = 0; i < attributeExpressionHandlers.size(); i++)
211 {
212 final AttributeExpressionReadHandler handler =
213 (AttributeExpressionReadHandler) attributeExpressionHandlers .get(
214 i);
215 e.setAttributeExpression(handler.getAttributeName(),
216 handler.getExpression());
217 }
218 for (int i = 0; i < attributeHandlers.size(); i++)
219 {
220 final AttributeReadHandler handler =
221 (AttributeReadHandler) attributeHandlers .get(i);
222 e.setAttribute(handler.getNamespace(), handler.getName(), handler.getObject());
223 }
224 e.setEnabled(enabled);
225 e.setVirtual(virtual);
226 if (style != null)
227 {
228 e.setAttribute(FlowReportFactoryModule.NAMESPACE,"style", style);
229 }
230 }
231
232 protected abstract Element getElement();
233
234 public final Node getNode()
235 {
236 return getElement();
237 }
238
239 /**
240 * Returns the object for this element or null, if this element does not
241 * create an object.
242 *
243 * @return the object.
244 * @throws XmlReaderException if there is a parsing error.
245 */
246 public Object getObject() throws SAXException
247 {
248 return getElement();
249 }
250 }