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