Source for org.jfree.report.flow.LibLayoutReportTarget

   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: LibLayoutReportTarget.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.flow;
  32: 
  33: import java.util.Iterator;
  34: import java.util.Map;
  35: 
  36: import org.jfree.layouting.LayoutProcess;
  37: import org.jfree.layouting.LayoutProcessState;
  38: import org.jfree.layouting.StateException;
  39: import org.jfree.layouting.input.style.StyleSheet;
  40: import org.jfree.layouting.layouter.context.DocumentContext;
  41: import org.jfree.layouting.layouter.feed.InputFeed;
  42: import org.jfree.layouting.layouter.feed.InputFeedException;
  43: import org.jfree.layouting.namespace.NamespaceCollection;
  44: import org.jfree.layouting.namespace.NamespaceDefinition;
  45: import org.jfree.layouting.output.OutputProcessor;
  46: import org.jfree.layouting.util.AttributeMap;
  47: import org.jfree.report.DataFlags;
  48: import org.jfree.report.DataSourceException;
  49: import org.jfree.report.JFreeReport;
  50: import org.jfree.report.JFreeReportInfo;
  51: import org.jfree.report.ReportProcessingException;
  52: import org.jfree.resourceloader.ResourceKey;
  53: import org.jfree.resourceloader.ResourceManager;
  54: 
  55: /**
  56:  * Creation-Date: 07.03.2006, 18:56:37
  57:  *
  58:  * @author Thomas Morgner
  59:  */
  60: public class LibLayoutReportTarget extends AbstractReportTarget
  61:   implements StatefullReportTarget
  62: {
  63:   protected static class LibLayoutReportTargetState
  64:       implements ReportTargetState
  65:   {
  66:     private LayoutProcessState layoutProcess;
  67:     private ReportJob reportJob;
  68:     private ResourceKey baseResourceKey;
  69:     private ResourceManager resourceManager;
  70:     private NamespaceCollection namespaceCollection;
  71: 
  72:     protected LibLayoutReportTargetState()
  73:     {
  74:     }
  75: 
  76:     public void fill (final LibLayoutReportTarget target) throws StateException
  77:     {
  78:       this.layoutProcess = target.getLayoutProcess().saveState();
  79:       this.reportJob = target.getReportJob();
  80:       this.baseResourceKey = target.getBaseResource();
  81:       this.resourceManager = target.getResourceManager();
  82:       this.namespaceCollection = target.getNamespaces();
  83:     }
  84: 
  85:     public ReportTarget restore(final OutputProcessor out)
  86:         throws StateException
  87:     {
  88:       final LayoutProcess layoutProcess = this.layoutProcess.restore(out);
  89:       return new LibLayoutReportTarget(reportJob,
  90:           baseResourceKey, resourceManager, layoutProcess, namespaceCollection);
  91:     }
  92:   }
  93: 
  94: 
  95:   private InputFeed feed;
  96:   private NamespaceCollection namespaces;
  97:   private LayoutProcess layoutProcess;
  98: 
  99:   /**
 100:    *
 101:    * @param reportJob
 102:    * @param baseResourceKey  may be null, if the report has not gone through the parser
 103:    * @param resourceManager may be null, a generic resource manager will be built
 104:    * @param layoutProcess
 105:    */
 106:   public LibLayoutReportTarget (final ReportJob reportJob,
 107:                                 final ResourceKey baseResourceKey,
 108:                                 final ResourceManager resourceManager,
 109:                                 final LayoutProcess layoutProcess)
 110:   {
 111:     super(reportJob, resourceManager, baseResourceKey);
 112: 
 113:     if (layoutProcess == null)
 114:     {
 115:       throw new NullPointerException();
 116:     }
 117:     this.layoutProcess = layoutProcess;
 118:     this.feed = layoutProcess.getInputFeed();
 119:   }
 120: 
 121:   protected LibLayoutReportTarget(final ReportJob reportJob,
 122:                                final ResourceKey baseResource,
 123:                                final ResourceManager resourceManager,
 124:                                final LayoutProcess layoutProcess,
 125:                                final NamespaceCollection namespaces)
 126:   {
 127:     this(reportJob, baseResource, resourceManager, layoutProcess);
 128:     this.namespaces = namespaces;
 129:   }
 130: 
 131:   public ReportTargetState saveState() throws StateException
 132:   {
 133:     final LibLayoutReportTargetState state = new LibLayoutReportTargetState();
 134:     state.fill(this);
 135:     return state;
 136:   }
 137: 
 138:   public void commit()
 139:   {
 140: 
 141:   }
 142: 
 143:   public NamespaceCollection getNamespaces()
 144:   {
 145:     return namespaces;
 146:   }
 147: 
 148:   public boolean isPagebreakEncountered()
 149:   {
 150:     return layoutProcess.isPagebreakEncountered();
 151:   }
 152: 
 153:   protected LayoutProcess getLayoutProcess()
 154:   {
 155:     return layoutProcess;
 156:   }
 157: 
 158:   protected InputFeed getInputFeed ()
 159:   {
 160:     return feed;
 161:   }
 162: 
 163:   public void startReport (final ReportStructureRoot report)
 164:           throws DataSourceException, ReportProcessingException
 165:   {
 166:     try
 167:     {
 168:       final InputFeed feed = getInputFeed();
 169:       feed.startDocument();
 170:       feed.startMetaInfo();
 171: 
 172:       feed.addDocumentAttribute(DocumentContext.BASE_RESOURCE_ATTR, report.getBaseResource());
 173:       feed.addDocumentAttribute(DocumentContext.RESOURCE_MANAGER_ATTR, report.getResourceManager());
 174: 
 175:       final String strictStyleMode = "false";
 176:       if ("true".equals(strictStyleMode))
 177:       {
 178:         feed.addDocumentAttribute(DocumentContext.STRICT_STYLE_MODE, Boolean.TRUE);
 179:       }
 180: 
 181:       final NamespaceDefinition[] namespaces = createDefaultNameSpaces();
 182:       for (int i = 0; i < namespaces.length; i++)
 183:       {
 184:         final NamespaceDefinition definition = namespaces[i];
 185:         feed.startMetaNode();
 186:         feed.setMetaNodeAttribute("type", "namespace");
 187:         feed.setMetaNodeAttribute("definition", definition);
 188:         feed.endMetaNode();
 189:       }
 190: 
 191:       if (report instanceof JFreeReport)
 192:       {
 193:         final JFreeReport realReport = (JFreeReport) report;
 194:         final int size = realReport.getStyleSheetCount();
 195:         for (int i = 0; i < size; i++)
 196:         {
 197:           final StyleSheet styleSheet = realReport.getStyleSheet(i);
 198:           feed.startMetaNode();
 199:           feed.setMetaNodeAttribute("type", "style");
 200:           feed.setMetaNodeAttribute("#content", styleSheet);
 201:           feed.endMetaNode();
 202:         }
 203:       }
 204: 
 205:       feed.endMetaInfo();
 206:       this.namespaces = feed.getNamespaceCollection();
 207:     }
 208:     catch (InputFeedException dse)
 209:     {
 210:       dse.printStackTrace();
 211:       throw new ReportProcessingException("Failed to process inputfeed", dse);
 212:     }
 213: 
 214:   }
 215: 
 216:   public void startElement (final AttributeMap attrs)
 217:           throws DataSourceException, ReportProcessingException
 218:   {
 219:     try
 220:     {
 221:       final String namespace = ReportTargetUtil.getNamespaceFromAttribute(attrs);
 222:       final String type = ReportTargetUtil.getElemenTypeFromAttribute(attrs);
 223:       final InputFeed feed = getInputFeed();
 224:       feed.startElement(namespace, type);
 225:       handleAttributes(attrs);
 226:     }
 227:     catch (InputFeedException e)
 228:     {
 229:       throw new ReportProcessingException("Failed to process inputfeed", e);
 230:     }
 231:   }
 232: 
 233:   public void processText(final String text)
 234:       throws DataSourceException, ReportProcessingException
 235:   {
 236:     try
 237:     {
 238:       final InputFeed feed = getInputFeed();
 239:       feed.addContent(text);
 240:     }
 241:     catch (InputFeedException e)
 242:     {
 243:       throw new ReportProcessingException("Failed to process inputfeed", e);
 244:     }
 245:   }
 246: 
 247:   public void processContent (final DataFlags value)
 248:           throws DataSourceException, ReportProcessingException
 249:   {
 250:     final InputFeed feed = getInputFeed();
 251:     try
 252:     {
 253:       feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE, "content", value.getValue());
 254:       feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE, "isChanged", String.valueOf(value.isChanged()));
 255:       feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE, "isDate", String.valueOf(value.isDate()));
 256:       feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE, "isNegative", String.valueOf(value.isNegative()));
 257:       feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE, "isNull", String.valueOf(value.isNull()));
 258:       feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE, "isNumber", String.valueOf(value.isNumeric()));
 259:       feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE, "isPositive", String.valueOf(value.isPositive()));
 260:       feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE, "isZero", String.valueOf(value.isZero()));
 261:     }
 262:     catch (InputFeedException e)
 263:     {
 264:       throw new ReportProcessingException("Failed to process inputfeed", e);
 265:     }
 266:   }
 267: 
 268:   public NamespaceDefinition getNamespaceByUri(final String uri)
 269:   {
 270:     if (uri == null)
 271:     {
 272:       return null;
 273:     }
 274:     return namespaces.getDefinition(uri);
 275:   }
 276: 
 277: 
 278:   protected void handleAttributes(final AttributeMap map)
 279:           throws ReportProcessingException
 280:   {
 281:     try
 282:     {
 283:       final InputFeed feed = getInputFeed();
 284:       final String[] namespaces = map.getNameSpaces();
 285:       for (int i = 0; i < namespaces.length; i++)
 286:       {
 287:         final String namespace = namespaces[i];
 288:         final Map localAttrs = map.getAttributes(namespace);
 289:         final Iterator it = localAttrs.entrySet().iterator();
 290:         while (it.hasNext())
 291:         {
 292:           final Map.Entry entry = (Map.Entry) it.next();
 293:           feed.setAttribute(namespace, (String) entry.getKey(), entry.getValue());
 294:         }
 295:       }
 296:     }
 297:     catch (InputFeedException e)
 298:     {
 299:       throw new ReportProcessingException("Failed to set attribute", e);
 300:     }
 301:   }
 302: 
 303:   public void endElement (final AttributeMap attrs)
 304:           throws DataSourceException, ReportProcessingException
 305:   {
 306:     final InputFeed feed = getInputFeed();
 307:     try
 308:     {
 309:       feed.endElement();
 310:     }
 311:     catch (InputFeedException e)
 312:     {
 313:       throw new ReportProcessingException("Failed to process inputfeed", e);
 314:     }
 315:   }
 316: 
 317:   public void endReport (final ReportStructureRoot report)
 318:           throws DataSourceException,
 319:           ReportProcessingException
 320:   {
 321:     try
 322:     {
 323:       getInputFeed().endDocument();
 324:     }
 325:     catch (InputFeedException e)
 326:     {
 327:       throw new ReportProcessingException("Failed to process inputfeed", e);
 328:     }
 329:   }
 330: 
 331: 
 332:   public void resetPagebreakFlag()
 333:   {
 334:     getInputFeed().resetPageBreakFlag();
 335:   }
 336: 
 337:   public String getExportDescriptor()
 338:   {
 339:     return getLayoutProcess().getOutputMetaData().getExportDescriptor();
 340:   }
 341: }