/*******************************************************************************
 * Copyright (c) 2004 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.model.api.util;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.core.IModuleModel;
import org.eclipse.birt.report.model.elements.ReportDesign;
import org.eclipse.birt.report.model.util.ReportDesignSerializer;
import org.eclipse.birt.report.model.writer.DocumentWriter;

/**
 * Utility class for the serialize a report design in which all the elements
 * localize their property values from the referred external resources, such as
 * library elements, library embedded images.
 */

public class DocumentUtil
{

	/**
	 * Writes the report design to the given output stream. The caller must call
	 * <code>onSave</code> if the save succeeds.
	 * 
	 * @param designHandle
	 *            the report design to serialize
	 * 
	 * @param out
	 *            the output stream to which the design is written.
	 * @throws IOException
	 *             if the file cannot be written to the output stream
	 *             successfully.
	 */

	public static ReportDesignHandle serialize( ReportDesignHandle designHandle,
			OutputStream out ) throws IOException
	{
		assert out != null;
		if ( designHandle == null )
			return null;

		ReportDesign target = null;
		ReportDesign source = (ReportDesign) designHandle.getModule( );

		// localize element property value
		List list = (List) source.getLocalProperty( source.getRoot( ),
				IModuleModel.LIBRARIES_PROP );
		if ( list == null || list.size( ) == 0 )
		{
			new DocumentWriter( (ReportDesign) designHandle.getModule( ) )
					.write( out );
			return designHandle;
		}

		target = localizeDesign( source );

		assert target != null;

		// use the writer for the document, not the general design writer
		new DocumentWriter( target ).write( out );
		
		return target.handle( );
	}

	/**
	 * Gets a localized report design based on the source design.
	 * 
	 * @param source
	 *            the source design
	 * @return the localized report design based on the source design
	 */

	static ReportDesign localizeDesign( ReportDesign source )
	{
		assert source != null;

		ReportDesignSerializer visitor = new ReportDesignSerializer( );
		source.apply( visitor );

		return visitor.getTarget( );
	}

}

@@ -775,16 +775,66 @@
 eModel;%0A
+import org.eclipse.birt.report.model.core.Module;%0A
 import o
@@ -1713,17 +1713,20 @@
 rialize(
- 
+%0A%09%09%09
 ReportDe
@@ -1749,20 +1749,17 @@
 nHandle,
-%0A%09%09%09
+ 
 OutputSt
@@ -1768,17 +1768,20 @@
 am out )
- 
+%0A%09%09%09
 throws I
@@ -2151,76 +2151,193 @@
 %0A%09%09%09
-new DocumentWriter( (ReportDesign) designHandle.getModule( ) )%0A%09%09%09%09%09
+// need to make a copy for the original one to avoid possible%0A%09%09%09// multiple thread issues.%0A%0A%09%09%09Module copy = (Module) designHandle.copy( );%0A%09%09%09new DocumentWriter( (ReportDesign) copy )
 .wri

make copy of design even if there is no library to ensure multiple thread safety.