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