Source for org.jfree.report.flow.layoutprocessor.BufferedReportTarget

   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: BufferedReportTarget.java 2725 2007-04-01 18:49:29Z taqua $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: 
  32: package org.jfree.report.flow.layoutprocessor;
  33: 
  34: import java.util.ArrayList;
  35: 
  36: import org.jfree.layouting.namespace.NamespaceDefinition;
  37: import org.jfree.layouting.util.AttributeMap;
  38: import org.jfree.report.DataFlags;
  39: import org.jfree.report.DataSourceException;
  40: import org.jfree.report.ReportProcessingException;
  41: import org.jfree.report.flow.ReportStructureRoot;
  42: import org.jfree.report.flow.ReportTarget;
  43: 
  44: /**
  45:  * Todo: Document me!
  46:  *
  47:  * @author Thomas Morgner
  48:  * @since 05.03.2007
  49:  */
  50: public class BufferedReportTarget implements ReportTarget, Cloneable
  51: {
  52:   public static class RecordedCall
  53:   {
  54:     private int methodId;
  55:     private Object parameters;
  56: 
  57:     public RecordedCall(final int method, final Object parameters)
  58:     {
  59:       this.methodId = method;
  60:       this.parameters = parameters;
  61:     }
  62: 
  63:     public int getMethod()
  64:     {
  65:       return methodId;
  66:     }
  67: 
  68:     public Object getParameters()
  69:     {
  70:       return parameters;
  71:     }
  72:   }
  73: 
  74:   private static final int MTH_START_REPORT = 1;
  75:   private static final int MTH_START_ELEMENT = 2;
  76:   private static final int MTH_PROCESS_TEXT = 3;
  77:   private static final int MTH_PROCESS_CONTENT = 4;
  78:   private static final int MTH_END_ELEMENT = 5;
  79:   private static final int MTH_END_REPORT = 6;
  80: 
  81:   private ReportTarget target;
  82:   private ArrayList calls;
  83: 
  84:   public BufferedReportTarget()
  85:   {
  86:     this.calls = new ArrayList();
  87:   }
  88: 
  89:   public ReportTarget getTarget()
  90:   {
  91:     return target;
  92:   }
  93: 
  94:   public void setTarget(final ReportTarget target)
  95:   {
  96:     this.target = target;
  97:   }
  98: 
  99:   public void startReport(final ReportStructureRoot report)
 100:       throws DataSourceException, ReportProcessingException
 101:   {
 102:     calls.add(new RecordedCall
 103:         (BufferedReportTarget.MTH_START_REPORT, report));
 104:   }
 105: 
 106:   public void startElement(final AttributeMap attrs)
 107:       throws DataSourceException, ReportProcessingException
 108:   {
 109:     try
 110:     {
 111:       calls.add(new RecordedCall
 112:           (BufferedReportTarget.MTH_START_ELEMENT, attrs.clone()));
 113:     }
 114:     catch (CloneNotSupportedException e)
 115:     {
 116:       throw new ReportProcessingException("Failed to clone attributes", e);
 117:     }
 118:   }
 119: 
 120:   public void processText(final String text)
 121:       throws DataSourceException, ReportProcessingException
 122:   {
 123:     calls.add(new RecordedCall
 124:         (BufferedReportTarget.MTH_PROCESS_TEXT, text));
 125:   }
 126: 
 127:   public void processContent(final DataFlags value)
 128:       throws DataSourceException, ReportProcessingException
 129:   {
 130:     calls.add(new RecordedCall
 131:         (BufferedReportTarget.MTH_PROCESS_CONTENT, value));
 132:   }
 133: 
 134:   public void endElement(final AttributeMap attrs)
 135:       throws DataSourceException, ReportProcessingException
 136:   {
 137:     try
 138:     {
 139:       calls.add(new RecordedCall
 140:           (BufferedReportTarget.MTH_END_ELEMENT, attrs.clone()));
 141:     }
 142:     catch (CloneNotSupportedException e)
 143:     {
 144:       throw new ReportProcessingException("Failed to clone attributes", e);
 145:     }
 146:   }
 147: 
 148:   public void endReport(final ReportStructureRoot report)
 149:       throws DataSourceException, ReportProcessingException
 150:   {
 151:     calls.add(new RecordedCall
 152:         (BufferedReportTarget.MTH_END_REPORT, report));
 153:   }
 154: 
 155:   public String getExportDescriptor()
 156:   {
 157:     return target.getExportDescriptor();
 158:   }
 159: 
 160:   public NamespaceDefinition getNamespaceByUri(final String uri)
 161:   {
 162:     return target.getNamespaceByUri(uri);
 163:   }
 164: 
 165:   public void commit()
 166:       throws ReportProcessingException
 167:   {
 168:     // ignored ..
 169:   }
 170: 
 171:   public void close(final ReportTarget target)
 172:       throws ReportProcessingException, DataSourceException
 173:   {
 174:     final RecordedCall[] objects = (RecordedCall[])
 175:         calls.toArray(new RecordedCall[calls.size()]);
 176: 
 177:     for (int i = 0; i < objects.length; i++)
 178:     {
 179:       final RecordedCall call = objects[i];
 180:       switch(call.getMethod())
 181:       {
 182:         case BufferedReportTarget.MTH_START_REPORT:
 183:         {
 184:           target.startReport((ReportStructureRoot) call.getParameters());
 185:           break;
 186:         }
 187:         case BufferedReportTarget.MTH_START_ELEMENT:
 188:         {
 189:           target.startElement((AttributeMap) call.getParameters());
 190:           break;
 191:         }
 192:         case BufferedReportTarget.MTH_PROCESS_TEXT:
 193:         {
 194:           target.processText((String) call.getParameters());
 195:           break;
 196:         }
 197:         case BufferedReportTarget.MTH_PROCESS_CONTENT:
 198:         {
 199:           target.processContent((DataFlags) call.getParameters());
 200:           break;
 201:         }
 202:         case BufferedReportTarget.MTH_END_ELEMENT:
 203:         {
 204:           target.endElement((AttributeMap) call.getParameters());
 205:           break;
 206:         }
 207:         case BufferedReportTarget.MTH_END_REPORT:
 208:         {
 209:           target.endReport((ReportStructureRoot) call.getParameters());
 210:           break;
 211:         }
 212:         default:
 213:           throw new IllegalStateException("Invalid call recorded.");
 214:       }
 215:     }
 216:   }
 217: 
 218:   public Object clone ()
 219:   {
 220:     try
 221:     {
 222:       final BufferedReportTarget o = (BufferedReportTarget) super.clone();
 223:       o.calls = (ArrayList) calls.clone();
 224:       return o;
 225:     }
 226:     catch (CloneNotSupportedException e)
 227:     {
 228:       throw new IllegalStateException("Clone failed");
 229:     }
 230:   }
 231: }