Source for org.jfree.report.modules.factories.report.flow.SectionReadHandler

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: SectionReadHandler.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.modules.factories.report.flow;
  32: 
  33: import java.util.ArrayList;
  34: 
  35: import org.jfree.report.modules.factories.report.base.NodeReadHandler;
  36: import org.jfree.report.modules.factories.report.base.NodeReadHandlerFactory;
  37: import org.jfree.report.structure.Element;
  38: import org.jfree.report.structure.Section;
  39: import org.jfree.report.structure.StaticText;
  40: import org.jfree.xmlns.parser.XmlReadHandler;
  41: import org.xml.sax.Attributes;
  42: import org.xml.sax.SAXException;
  43: 
  44: /**
  45:  * Creation-Date: 09.04.2006, 14:45:57
  46:  *
  47:  * @author Thomas Morgner
  48:  */
  49: public class SectionReadHandler extends AbstractElementReadHandler
  50: {
  51:   private Section section;
  52:   private StringBuffer textBuffer;
  53:   private ArrayList nodes;
  54:   private ArrayList operationsAfter;
  55:   private ArrayList operationsBefore;
  56:   private String repeat;
  57: 
  58:   public SectionReadHandler()
  59:   {
  60:     nodes = new ArrayList();
  61:     operationsAfter = new ArrayList();
  62:     operationsBefore = new ArrayList();
  63:   }
  64: 
  65: 
  66:   public SectionReadHandler(final Section section)
  67:   {
  68:     this();
  69:     this.section = section;
  70:   }
  71: 
  72:   protected Element getElement()
  73:   {
  74:     if (section == null)
  75:     {
  76:       section = new Section();
  77:     }
  78:     return section;
  79:   }
  80: 
  81:   /**
  82:    * Starts parsing.
  83:    *
  84:    * @param attrs the attributes.
  85:    * @throws SAXException if there is a parsing error.
  86:    */
  87:   protected void startParsing(final Attributes attrs) throws SAXException
  88:   {
  89:     super.startParsing(attrs);
  90: 
  91:     final String repeatValue = attrs.getValue(getUri(), "repeat");
  92:     if (repeatValue != null)
  93:     {
  94:       repeat = repeatValue;
  95:     }
  96: 
  97:     if (FlowReportFactoryModule.NAMESPACE.equals(getUri()) == false)
  98:     {
  99:       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(final 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:         final NodeReadHandler nr = (NodeReadHandler) wrapper;
 202:         section.addNode(nr.getNode());
 203:       }
 204:     }
 205:     for (int i = 0; i < operationsAfter.size(); i++)
 206:     {
 207:       final FlowOperationReadHandler handler =
 208:               (FlowOperationReadHandler) operationsAfter.get(i);
 209:       section.addOperationAfter(handler.getOperation());
 210:     }
 211:     for (int i = 0; i < operationsBefore.size(); i++)
 212:     {
 213:       final 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: }