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: SectionReadHandler.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.modules.factories.report.base.NodeReadHandlerFactory;
036: import org.jfree.report.structure.Element;
037: import org.jfree.report.structure.Section;
038: import org.jfree.report.structure.StaticText;
039: import org.jfree.xmlns.parser.XmlReadHandler;
040: import org.xml.sax.Attributes;
041: import org.xml.sax.SAXException;
042:
043: /**
044: * Creation-Date: 09.04.2006, 14:45:57
045: *
046: * @author Thomas Morgner
047: */
048: public class SectionReadHandler extends AbstractElementReadHandler {
049: private Section section;
050: private StringBuffer textBuffer;
051: private ArrayList nodes;
052: private ArrayList operationsAfter;
053: private ArrayList operationsBefore;
054: private String repeat;
055:
056: public SectionReadHandler() {
057: nodes = new ArrayList();
058: operationsAfter = new ArrayList();
059: operationsBefore = new ArrayList();
060: }
061:
062: public SectionReadHandler(final Section section) {
063: this ();
064: this .section = section;
065: }
066:
067: protected Element getElement() {
068: if (section == null) {
069: section = new Section();
070: }
071: return section;
072: }
073:
074: /**
075: * Starts parsing.
076: *
077: * @param attrs the attributes.
078: * @throws SAXException if there is a parsing error.
079: */
080: protected void startParsing(final Attributes attrs)
081: throws SAXException {
082: super .startParsing(attrs);
083:
084: final String repeatValue = attrs.getValue(getUri(), "repeat");
085: if (repeatValue != null) {
086: repeat = repeatValue;
087: }
088:
089: if (FlowReportFactoryModule.NAMESPACE.equals(getUri()) == false) {
090: final Element element = getElement();
091: final int attrLength = attrs.getLength();
092: for (int i = 0; i < attrLength; i++) {
093: final String uri = attrs.getURI(i);
094: final String local = attrs.getLocalName(i);
095: if (FlowReportFactoryModule.NAMESPACE.equals(uri) == false) {
096: element.setAttribute(uri, local, attrs.getValue(i));
097: }
098: }
099: }
100: }
101:
102: protected void configureElement(final Element e) {
103: super .configureElement(e);
104:
105: final Section section = (Section) e;
106: if (repeat != null) {
107: section.setRepeat("true".equals(repeat));
108: }
109: }
110:
111: /**
112: * Returns the handler for a child element.
113: *
114: * @param tagName the tag name.
115: * @param atts the attributes.
116: * @return the handler or null, if the tagname is invalid.
117: * @throws SAXException if there is a parsing error.
118: */
119: protected XmlReadHandler getHandlerForChild(final String uri,
120: final String tagName, final Attributes atts)
121: throws SAXException {
122: if (textBuffer != null) {
123: nodes.add(new StaticText(textBuffer.toString()));
124: textBuffer = null;
125: }
126:
127: final XmlReadHandler elementTypeHanders = super
128: .getHandlerForChild(uri, tagName, atts);
129: if (elementTypeHanders != null) {
130: return elementTypeHanders;
131: }
132:
133: if (FlowReportFactoryModule.NAMESPACE.equals(uri)) {
134: if ("operation-after".equals(tagName)) {
135: final FlowOperationReadHandler frh = new FlowOperationReadHandler();
136: operationsAfter.add(frh);
137: return frh;
138: } else if ("operation-before".equals(tagName)) {
139: final FlowOperationReadHandler frh = new FlowOperationReadHandler();
140: operationsBefore.add(frh);
141: return frh;
142: }
143: }
144:
145: final NodeReadHandlerFactory factory = NodeReadHandlerFactory
146: .getInstance();
147: final NodeReadHandler handler = (NodeReadHandler) factory
148: .getHandler(uri, tagName);
149: if (handler != null) {
150: nodes.add(handler);
151: return handler;
152: }
153: return null;
154: }
155:
156: /**
157: * Done parsing.
158: *
159: * @throws SAXException if there is a parsing error.
160: */
161: protected void doneParsing() throws SAXException {
162: if (textBuffer != null) {
163: nodes.add(new StaticText(textBuffer.toString()));
164: textBuffer = null;
165: }
166:
167: final Section section = (Section) getElement();
168: configureElement(section);
169:
170: for (int i = 0; i < nodes.size(); i++) {
171: final Object wrapper = nodes.get(i);
172: if (wrapper instanceof StaticText) {
173: section.addNode((StaticText) wrapper);
174: } else if (wrapper instanceof NodeReadHandler) {
175: final NodeReadHandler nr = (NodeReadHandler) wrapper;
176: section.addNode(nr.getNode());
177: }
178: }
179: for (int i = 0; i < operationsAfter.size(); i++) {
180: final FlowOperationReadHandler handler = (FlowOperationReadHandler) operationsAfter
181: .get(i);
182: section.addOperationAfter(handler.getOperation());
183: }
184: for (int i = 0; i < operationsBefore.size(); i++) {
185: final FlowOperationReadHandler handler = (FlowOperationReadHandler) operationsBefore
186: .get(i);
187: section.addOperationBefore(handler.getOperation());
188: }
189: }
190:
191: /**
192: * This method is called to process the character data between element tags.
193: *
194: * @param ch the character buffer.
195: * @param start the start index.
196: * @param length the length.
197: * @throws SAXException if there is a parsing error.
198: */
199: public void characters(final char[] ch, final int start,
200: final int length) throws SAXException {
201: if (textBuffer == null) {
202: textBuffer = new StringBuffer();
203: }
204: textBuffer.append(ch, start, length);
205: }
206:
207: }
|