/*******************************************************************************
 * 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.internal.document;

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.util.HashMap;

import org.eclipse.birt.core.archive.IDocArchiveReader;
import org.eclipse.birt.core.archive.RAInputStream;
import org.eclipse.birt.core.util.IOUtil;
import org.eclipse.birt.report.engine.api.impl.ReportDocumentConstants;


public class PageIndexReader
{
	IDocArchiveReader reader;
	protected HashMap pages = new HashMap();
	
	long offset = 0;
	
	public PageIndexReader(IDocArchiveReader reader) throws IOException
	{
		this.reader = reader;
	}
	
	protected long getOffset(String masterPage)
	{
		Object value = pages.get( masterPage );
		if(value!=null && value instanceof Long)
		{
			return ((Long)value).longValue( );
		}
		else
		{
			return -1;
		}
	}
	
	public long getPageOffset(String masterPage) throws IOException
	{
		Object value = pages.get( masterPage );
		if(value!=null && value instanceof Long)
		{
			return ((Long)value).longValue( );
		}
		else
		{
			RAInputStream indexStream = null;
			try
			{
				indexStream = reader.getStream( ReportDocumentConstants.PAGE_INDEX_STREAM );
				DataInputStream input = new DataInputStream( indexStream );
				while (true)
				{
					String masterPageName = IOUtil.readString( input );
					long pageOffset = IOUtil.readLong( input );
					pages.put( masterPageName, new Long(pageOffset) );
				}
			}
			catch (EOFException eef)
			{
				
			}
			catch(IOException ex)
			{
 				throw ex;
			}
			finally
			{
				if(indexStream!=null)
				{
					try
					{
						indexStream.close( );
					}
					catch ( IOException e )
					{
						
					}
					indexStream = null;
				}
			}
			
			value = pages.get( masterPage );
			if(value!=null && value instanceof Long)
			{
				return ((Long)value).longValue( );
			}
		}
		return -1;
	}
	
	public void close()
	{
		
	}
	
}

@@ -697,16 +697,43 @@
 HashMap;
+%0Aimport java.util.Iterator;
 %0A%0Aimport
@@ -2397,16 +2397,164 @@
 );%0A%09%09%09%7D%0A
+%09%09%09Iterator iterator = pages.values( ).iterator( );%0A%09%09%09while ( iterator.hasNext( ) )%0A%09%09%09%7B%0A%09%09%09%09return ( (Long) iterator.next( ) ).longValue( );%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%09re
@@ -2558,18 +2558,17 @@
 %09return 
--1
+0
 ;%0A%09%7D%0A%09%0A%09

Fix ted 8252. The simple master page is not output in the document. when we export the file to pdf it can not find the simple master page , we should return the first page instead.