<commit_before>
package org.eclipse.birt.report.engine.emitter.excel.layout;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.birt.report.engine.content.IContent;
import org.eclipse.birt.report.engine.content.IListContent;
import org.eclipse.birt.report.engine.content.ITableContent;
import org.eclipse.birt.report.engine.emitter.excel.ExcelUtil;
import org.eclipse.birt.report.engine.ir.DimensionType;


public class LayoutUtil
{
	public static int[] getColumnWidth( TableInfo table, int width )
	{
		int[] col = new int[table.getColumnCount( )];
		int tmp = 0;
		int nullNumber = 0;

		for ( int i = 0; i < table.getColumnCount( ); i++ )
		{
			int colwidth = table.getColumnWidth( i );
			// record columns which is not set column's width in report design.
			if ( colwidth == 0 )
			{
				nullNumber++;
				col[i] = -1;
			}
			else
			{
				col[i] = colwidth;
				tmp += col[i];
			}
		}

		// If columns are not set width, set the average width to them.
		if ( nullNumber != 0 )
		{
			int aveWidth = ( width - tmp ) / nullNumber;
			tmp = 0;

			for ( int i = 0; i < col.length; i++ )
			{
				if ( col[i] == -1 )
				{
					col[i] = aveWidth;
				}
				tmp += col[i];
			}

		}

		// Set the left width to the last column.
		col[col.length - 1] += width - tmp;

		return col;
	}
	
	public static TableInfo createTable(int col, int width)
	{
		return new DefaultTableInfo(col, width);
	}	
	
	public static TableInfo createTable(IListContent list, int width)
	{
		width = getElementWidth(list, width);
		int[] column = new int[] {width};
		return new DefaultTableInfo(column);
	}
	
	private static int getElementWidth(IContent content, int width)
	{
		DimensionType value = content.getWidth( );
		
		if(value != null)
		{
			try 
			{
				width = Math.min( ExcelUtil.covertDimensionType( value, width ), 
						            width );
			}
			catch(Exception e) 
			{
				
			}
		}
		
		return width;

	}
	
	public static TableInfo createTable(ITableContent table, int width)
	{		
		width = getElementWidth(table, width);
		
		int colcount = table.getColumnCount( );
		
		if ( colcount == 0 )
		{
			return null;
		}
		
		int[] index = new int[colcount];
		int know = 0;
		List unmount = new ArrayList();
		
		for(int i = 0; i < colcount; i++)
		{
			DimensionType value = table.getColumn( i ).getWidth( );  
			
			if( value == null)
			{
				unmount.add( new Integer(i) );
			}
			else
			{				
				try {
					index[i] = ExcelUtil.covertDimensionType( value, width ); 
					know += index[i];
				}
				catch(IllegalArgumentException ex)
				{
					unmount.add( new Integer(i) );
				}
			}	
		}		
		
		int left = width - know;
		
		if(left > 0 && unmount.size( ) == 0)
		{
			index[index.length - 1] = index[index.length - 1] + left; 
			return new DefaultTableInfo(index);
		}
		else if(left > 0 && unmount.size( ) > 0)
		{
			int[] size = split(left, unmount.size());			
			Iterator iter = unmount.iterator( );
			int i = 0;
			
			while(iter.hasNext( ))
			{
				int pos = ((Integer) iter.next()).intValue( );
				index[pos] = size[i];
				i++;
			}
			
			return new DefaultTableInfo(index);
		}
		else 
		{
			return new DefaultTableInfo(index);
		}	
	}
	
	public static int[] split(int width, int count)
	{
		int[] size = new int[count];		
		int per = (int) width / count;
		
		for(int i = 0; i < count - 1; i++)
		{
			size[i] = per;<commit_msg>Fix bugzilla bug 243986:Failed to generate Excel from attached rptdocument<commit_after>
package org.eclipse.birt.report.engine.emitter.excel.layout;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.birt.report.engine.content.IContent;
import org.eclipse.birt.report.engine.content.IListContent;
import org.eclipse.birt.report.engine.content.ITableContent;
import org.eclipse.birt.report.engine.emitter.excel.ExcelUtil;
import org.eclipse.birt.report.engine.ir.DimensionType;


public class LayoutUtil
{
	public static int[] getColumnWidth( TableInfo table, int width )
	{
		int[] col = new int[table.getColumnCount( )];
		int tmp = 0;
		int nullNumber = 0;

		for ( int i = 0; i < table.getColumnCount( ); i++ )
		{
			int colwidth = table.getColumnWidth( i );
			// record columns which is not set column's width in report design.
			if ( colwidth == 0 )
			{
				nullNumber++;
				col[i] = -1;
			}
			else
			{
				col[i] = colwidth;
				tmp += col[i];
			}
		}

		// If columns are not set width, set the average width to them.
		if ( nullNumber != 0 )
		{
			int aveWidth = ( width - tmp ) / nullNumber;
			tmp = 0;

			for ( int i = 0; i < col.length; i++ )
			{
				if ( col[i] == -1 )
				{
					col[i] = aveWidth;
				}
				tmp += col[i];
			}

		}

		// Set the left width to the last column.
		col[col.length - 1] += width - tmp;

		return col;
	}
	
	public static TableInfo createTable(int col, int width)
	{
		return new DefaultTableInfo(col, width);
	}	
	
	public static TableInfo createTable(IListContent list, int width)
	{
		width = getElementWidth(list, width);
		int[] column = new int[] {width};
		return new DefaultTableInfo(column);
	}
	
	private static int getElementWidth(IContent content, int width)
	{
		DimensionType value = content.getWidth( );
		
		if(value != null)
		{
			try 
			{
				width = Math.min( ExcelUtil.covertDimensionType( value, width ), 
						            width );
			}
			catch(Exception e) 
			{
				
			}
		}
		
		return width;

	}
	
	public static TableInfo createTable(ITableContent table, int width)
	{		
		width = getElementWidth(table, width);
		
		int colcount = table.getColumnCount( );
		
		if ( colcount == 0 )
		{
			return null;
		}
		
		int[] index = new int[colcount];
		int know = 0;
		List unmount = new ArrayList();
		
		for(int i = 0; i < colcount; i++)
		{
			DimensionType value = table.getColumn( i ).getWidth( );  
			
			if( value == null)
			{
				unmount.add( new Integer(i) );
			}
			else
			{				
				try {
					index[i] = ExcelUtil.covertDimensionType( value, width ); 
					know += index[i];
				}
				catch(IllegalArgumentException ex)
				{
					unmount.add( new Integer(i) );
				}
			}	
		}		
		
		int left = width - know;
		
		if(left > 0 && unmount.size( ) == 0)
		{
			index[index.length - 1] = index[index.length - 1] + left; 
			return new DefaultTableInfo(index);
		}
		else if(left < 0 )
		{
			return new DefaultTableInfo(split(width, colcount));
		}
		else if(left > 0 && unmount.size( ) > 0)
		{
			int[] size = split(left, unmount.size());			
			Iterator iter = unmount.iterator( );
			int i = 0;
			
			while(iter.hasNext( ))
			{
				int pos = ((Integer) iter.next()).intValue( );
				index[pos] = size[i];
				i++;
			}
			
			return new DefaultTableInfo(index);
		}
		else 
		{
			return new DefaultTableInfo(index);
		}	
	}
	
	public static int[] split(int width, int count)
	{
		int[] size = new int[count];		
		int per = (int) width / count;
		
		for(int i = 0; i < count - 1; i++)
		{
			size[i] = per;
{'fraction_non_alphanumeric': 0.13419540229885057, 'fraction_numerical': 0.007327586206896552, 'mean_word_length': 2.5551583248212464, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1226271', 'n_tokens_mistral': 2999, 'n_tokens_neox': 2413, 'n_words': 625}