/***********************************************************************
 * Copyright (c) 2007 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Actuate Corporation - initial API and implementation
 ***********************************************************************/

package org.eclipse.birt.report.engine.layout.html.buffer;

import org.eclipse.birt.report.engine.content.IContent;
import org.eclipse.birt.report.engine.content.IReportContent;
import org.eclipse.birt.report.engine.content.IStyle;
import org.eclipse.birt.report.engine.emitter.ContentEmitterUtil;
import org.eclipse.birt.report.engine.emitter.IContentEmitter;
import org.eclipse.birt.report.engine.executor.IReportExecutor;
import org.eclipse.birt.report.engine.executor.ReportExecutorUtil;
import org.eclipse.birt.report.engine.ir.MasterPageDesign;
import org.eclipse.birt.report.engine.layout.LayoutUtil;
import org.eclipse.birt.report.engine.layout.html.HTMLLayoutContext;


public class DummyPageBuffer implements IPageBuffer
{

	protected HTMLLayoutContext context;
	protected IReportExecutor executor;
	protected boolean isFirstContent = false;
	protected IContent pageContent = null;
	protected IContentEmitter pageEmitter = null;

	public DummyPageBuffer( HTMLLayoutContext context, IReportExecutor executor )
	{
		this.context = context;
		this.executor = executor;
	}

	public void endContainer( IContent content, boolean finished,
			IContentEmitter emitter )
	{
		if ( emitter != null )
		{
			if(content.getContentType( ) == IContent.PAGE_CONTENT )
			{
				ContentEmitterUtil.endContent( pageContent, emitter );
			}
			else
			{
				ContentEmitterUtil.endContent( content, emitter );
			}
		}

	}

	public void startContainer( IContent content, boolean isFirst,
			IContentEmitter emitter )
	{
		if ( content.getContentType( ) == IContent.PAGE_CONTENT )
		{
			isFirstContent = true;
			pageContent = content;
			pageEmitter = emitter;
		}
		else
		{
			if(	isFirstContent)
			{
				startPageContent(content);
				isFirstContent = false;
			}
			if ( emitter != null )
			{
				ContentEmitterUtil.startContent( content, emitter );
			}
		}
	}

	public void startContent( IContent content, IContentEmitter emitter )
	{
		if(isFirstContent)
		{
			startPageContent(content);
			isFirstContent = false;
		}
		if ( emitter != null )
		{
			ContentEmitterUtil.startContent( content, emitter );
			ContentEmitterUtil.endContent( content, emitter );
		}

	}

	public boolean isRepeated( )
	{
		return false;
	}

	public void setRepeated( boolean isRepeated )
	{

	}

	protected void startPageContent( IContent firstContent )
	{
		String masterPage = null;
		IStyle style = firstContent.getStyle( );
		if(style!=null)
		{
			masterPage = style.getMasterPage( );
		}
		if ( pageContent == null || pageEmitter == null )
		{
			return;
		}
		if ( masterPage == null || "".equals( masterPage ) )
		{
			ContentEmitterUtil.startContent( pageContent, pageEmitter );
		}
		else
		{
			IReportContent report = pageContent.getReportContent( );
			MasterPageDesign defaultMasterPage = LayoutUtil
					.getDefaultMasterPage( report );
			if ( defaultMasterPage.getName( ).equals( masterPage ) )
			{
				ContentEmitterUtil.startContent( pageContent, pageEmitter );
			}
			else
			{
				pageContent = ReportExecutorUtil.executeMasterPage( executor,
						context.getPageNumber( ), LayoutUtil.getMasterPage( report,
								masterPage ) );
				if(pageContent!=null)
				{
					ContentEmitterUtil.startContent( pageContent, pageEmitter );
				}
			}
		}
	}

}

@@ -1184,17 +1184,16 @@
 ntext;%0A%0A
-%0A
 public c
@@ -1676,24 +1676,115 @@
 mitter )%0A%09%7B%0A
+%09%09if ( isFirstContent )%0A%09%09%7B%0A%09%09%09startPageContent( content );%0A%09%09%09isFirstContent = false;%0A%09%09%7D%0A
 %09%09if ( emitt
@@ -1805,17 +1805,19 @@
 %09%7B%0A%09%09%09if
-(
+ ( 
 content.
@@ -2274,10 +2274,11 @@
 %09%09if
-(%09
+ ( 
 isFi
@@ -2283,24 +2283,25 @@
 FirstContent
+ 
 )%0A%09%09%09%7B%0A%09%09%09%09s
@@ -2312,31 +2312,33 @@
 PageContent(
+ 
 content
+ 
 );%0A%09%09%09%09isFir
@@ -2540,17 +2540,19 @@
 %0A%09%7B%0A%09%09if
-(
+ ( 
 isFirstC
@@ -2557,16 +2557,17 @@
 tContent
+ 
 )%0A%09%09%7B%0A%09%09
@@ -2584,23 +2584,25 @@
 Content(
+ 
 content
+ 
 );%0A%09%09%09is
@@ -3023,20 +3023,25 @@
 %09%09if
-(
+ ( 
 style
+ 
 !=
+ 
 null
+ 
 )%0A%09%09
@@ -3719,16 +3719,8 @@
 age(
- report,
 %0A%09%09%09
@@ -3724,16 +3724,24 @@
 %09%09%09%09%09%09%09%09
+report, 
 masterPa
@@ -3754,17 +3754,19 @@
 ;%0A%09%09%09%09if
-(
+ ( 
 pageCont
@@ -3772,14 +3772,17 @@
 tent
+ 
 !=
+ 
 null
+ 
 )%0A%09%09

Fix Bugzilla  Bug 199702 ArrayIndexOutOfBoundsException is thrown out from HTML emitter when preview a emtpy report