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