poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ser...@apache.org
Subject svn commit: r1148095 - in /poi/trunk/src/scratchpad/src/org/apache/poi/hssf/converter: ExcelToHtmlConverter.java ExcelToHtmlUtils.java
Date Mon, 18 Jul 2011 23:21:47 GMT
Author: sergey
Date: Mon Jul 18 23:21:46 2011
New Revision: 1148095

URL: http://svn.apache.org/viewvc?rev=1148095&view=rev
Log:
simplify internal API

Modified:
    poi/trunk/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlUtils.java

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java?rev=1148095&r1=1148094&r2=1148095&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java Mon
Jul 18 23:21:46 2011
@@ -19,7 +19,6 @@ package org.apache.poi.hssf.converter;
 import java.io.File;
 import java.io.FileWriter;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -384,9 +383,8 @@ public class ExcelToHtmlConverter
         return useDivsToSpan;
     }
 
-    protected boolean processCell( HSSFWorkbook workbook, HSSFCell cell,
-            Element tableCellElement, int normalWidthPx, int maxSpannedWidthPx,
-            float normalHeightPt )
+    protected boolean processCell( HSSFCell cell, Element tableCellElement,
+            int normalWidthPx, int maxSpannedWidthPx, float normalHeightPt )
     {
         final HSSFCellStyle cellStyle = cell.getCellStyle();
 
@@ -464,6 +462,7 @@ public class ExcelToHtmlConverter
         final short cellStyleIndex = cellStyle.getIndex();
         if ( cellStyleIndex != 0 )
         {
+            HSSFWorkbook workbook = cell.getRow().getSheet().getWorkbook();
             String mainCssClass = getStyleClassName( workbook, cellStyle );
             if ( wrapInDivs )
             {
@@ -616,10 +615,10 @@ public class ExcelToHtmlConverter
     /**
      * @return maximum 1-base index of column that were rendered, zero if none
      */
-    protected int processRow( HSSFWorkbook workbook, HSSFSheet sheet,
-            CellRangeAddress[][] mergedRanges, HSSFRow row,
+    protected int processRow( CellRangeAddress[][] mergedRanges, HSSFRow row,
             Element tableRowElement )
     {
+        final HSSFSheet sheet = row.getSheet();
         final short maxColIx = row.getLastCellNum();
         if ( maxColIx <= 0 )
             return 0;
@@ -640,7 +639,7 @@ public class ExcelToHtmlConverter
             if ( !isOutputHiddenColumns() && sheet.isColumnHidden( colIx ) )
                 continue;
 
-            CellRangeAddress range = ExcelToHtmlUtils.getCellRangeAddress(
+            CellRangeAddress range = ExcelToHtmlUtils.getMergedRange(
                     mergedRanges, row.getRowNum(), colIx );
 
             if ( range != null
@@ -695,7 +694,7 @@ public class ExcelToHtmlConverter
             boolean emptyCell;
             if ( cell != null )
             {
-                emptyCell = processCell( workbook, cell, tableCellElement,
+                emptyCell = processCell( cell, tableCellElement,
                         getColumnWidth( sheet, colIx ), divWidthPx,
                         row.getHeight() / 20f );
             }
@@ -732,7 +731,7 @@ public class ExcelToHtmlConverter
         tableRowNumberCellElement.appendChild( text );
     }
 
-    protected void processSheet( HSSFWorkbook workbook, HSSFSheet sheet )
+    protected void processSheet( HSSFSheet sheet )
     {
         processSheetHeader( htmlDocumentFacade.getBody(), sheet );
 
@@ -745,49 +744,8 @@ public class ExcelToHtmlConverter
 
         Element tableBody = htmlDocumentFacade.createTableBody();
 
-        CellRangeAddress[][] mergedRanges = new CellRangeAddress[1][];
-        for ( int m = 0; m < sheet.getNumMergedRegions(); m++ )
-        {
-            final CellRangeAddress cellRangeAddress = sheet.getMergedRegion( m );
-
-            final int requiredHeight = cellRangeAddress.getLastRow() + 1;
-            if ( mergedRanges.length < requiredHeight )
-            {
-                CellRangeAddress[][] newArray = new CellRangeAddress[requiredHeight][];
-                System.arraycopy( mergedRanges, 0, newArray, 0,
-                        mergedRanges.length );
-                mergedRanges = newArray;
-            }
-
-            for ( int r = cellRangeAddress.getFirstRow(); r <= cellRangeAddress
-                    .getLastRow(); r++ )
-            {
-                final int requiredWidth = cellRangeAddress.getLastColumn() + 1;
-
-                CellRangeAddress[] rowMerged = mergedRanges[r];
-                if ( rowMerged == null )
-                {
-                    rowMerged = new CellRangeAddress[requiredWidth];
-                    mergedRanges[r] = rowMerged;
-                }
-                else
-                {
-                    final int rowMergedLength = rowMerged.length;
-                    if ( rowMergedLength < requiredWidth )
-                    {
-                        final CellRangeAddress[] newRow = new CellRangeAddress[requiredWidth];
-                        System.arraycopy( rowMerged, 0, newRow, 0,
-                                rowMergedLength );
-
-                        mergedRanges[r] = newRow;
-                        rowMerged = newRow;
-                    }
-                }
-
-                Arrays.fill( rowMerged, cellRangeAddress.getFirstColumn(),
-                        cellRangeAddress.getLastColumn() + 1, cellRangeAddress );
-            }
-        }
+        final CellRangeAddress[][] mergedRanges = ExcelToHtmlUtils
+                .buildMergedRangesMap( sheet );
 
         final List<Element> emptyRowElements = new ArrayList<Element>(
                 physicalNumberOfRows );
@@ -809,8 +767,8 @@ public class ExcelToHtmlConverter
                             tableRowElement.getTagName(), "r", "height:"
                                     + ( row.getHeight() / 20f ) + "pt;" ) );
 
-            int maxRowColumnNumber = processRow( workbook, sheet, mergedRanges,
-                    row, tableRowElement );
+            int maxRowColumnNumber = processRow( mergedRanges, row,
+                    tableRowElement );
 
             if ( maxRowColumnNumber == 0 )
             {
@@ -873,7 +831,7 @@ public class ExcelToHtmlConverter
         for ( int s = 0; s < workbook.getNumberOfSheets(); s++ )
         {
             HSSFSheet sheet = workbook.getSheetAt( s );
-            processSheet( workbook, sheet );
+            processSheet( sheet );
         }
 
         htmlDocumentFacade.updateStylesheet();

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlUtils.java?rev=1148095&r1=1148094&r2=1148095&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlUtils.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlUtils.java Mon Jul
18 23:21:46 2011
@@ -19,8 +19,10 @@ package org.apache.poi.hssf.converter;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.util.Arrays;
 
 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.hssf.util.HSSFColor;
 import org.apache.poi.ss.util.CellRangeAddress;
@@ -60,6 +62,61 @@ public class ExcelToHtmlUtils
         }
     }
 
+    /**
+     * Creates a map (i.e. two-dimensional array) filled with ranges. Allow fast
+     * retrieving {@link CellRangeAddress} of any cell, if cell is contained in
+     * range.
+     * 
+     * @see #getMergedRange(CellRangeAddress[][], int, int)
+     */
+    public static CellRangeAddress[][] buildMergedRangesMap( HSSFSheet sheet )
+    {
+        CellRangeAddress[][] mergedRanges = new CellRangeAddress[1][];
+        for ( int m = 0; m < sheet.getNumMergedRegions(); m++ )
+        {
+            final CellRangeAddress cellRangeAddress = sheet.getMergedRegion( m );
+
+            final int requiredHeight = cellRangeAddress.getLastRow() + 1;
+            if ( mergedRanges.length < requiredHeight )
+            {
+                CellRangeAddress[][] newArray = new CellRangeAddress[requiredHeight][];
+                System.arraycopy( mergedRanges, 0, newArray, 0,
+                        mergedRanges.length );
+                mergedRanges = newArray;
+            }
+
+            for ( int r = cellRangeAddress.getFirstRow(); r <= cellRangeAddress
+                    .getLastRow(); r++ )
+            {
+                final int requiredWidth = cellRangeAddress.getLastColumn() + 1;
+
+                CellRangeAddress[] rowMerged = mergedRanges[r];
+                if ( rowMerged == null )
+                {
+                    rowMerged = new CellRangeAddress[requiredWidth];
+                    mergedRanges[r] = rowMerged;
+                }
+                else
+                {
+                    final int rowMergedLength = rowMerged.length;
+                    if ( rowMergedLength < requiredWidth )
+                    {
+                        final CellRangeAddress[] newRow = new CellRangeAddress[requiredWidth];
+                        System.arraycopy( rowMerged, 0, newRow, 0,
+                                rowMergedLength );
+
+                        mergedRanges[r] = newRow;
+                        rowMerged = newRow;
+                    }
+                }
+
+                Arrays.fill( rowMerged, cellRangeAddress.getFirstColumn(),
+                        cellRangeAddress.getLastColumn() + 1, cellRangeAddress );
+            }
+        }
+        return mergedRanges;
+    }
+
     public static String getBorderStyle( short xlsBorder )
     {
         final String borderStyle;
@@ -111,18 +168,6 @@ public class ExcelToHtmlUtils
         return borderWidth;
     }
 
-    public static CellRangeAddress getCellRangeAddress(
-            CellRangeAddress[][] mergedRanges, int rowNumber, int columnNumber )
-    {
-        CellRangeAddress[] mergedRangeRowInfo = rowNumber < mergedRanges.length ? mergedRanges[rowNumber]
-                : null;
-        CellRangeAddress cellRangeAddress = mergedRangeRowInfo != null
-                && columnNumber < mergedRangeRowInfo.length ? mergedRangeRowInfo[columnNumber]
-                : null;
-
-        return cellRangeAddress;
-    }
-
     public static String getColor( HSSFColor color )
     {
         StringBuilder stringBuilder = new StringBuilder( 7 );
@@ -168,6 +213,25 @@ public class ExcelToHtmlUtils
         return pixels;
     }
 
+    /**
+     * @param mergedRanges
+     *            map of sheet merged ranges built with
+     *            {@link #buildMergedRangesMap(HSSFSheet)}
+     * @return {@link CellRangeAddress} from map if cell with specified row and
+     *         column numbers contained in found range, <tt>null</tt> otherwise
+     */
+    public static CellRangeAddress getMergedRange(
+            CellRangeAddress[][] mergedRanges, int rowNumber, int columnNumber )
+    {
+        CellRangeAddress[] mergedRangeRowInfo = rowNumber < mergedRanges.length ? mergedRanges[rowNumber]
+                : null;
+        CellRangeAddress cellRangeAddress = mergedRangeRowInfo != null
+                && columnNumber < mergedRangeRowInfo.length ? mergedRangeRowInfo[columnNumber]
+                : null;
+
+        return cellRangeAddress;
+    }
+
     static boolean isEmpty( String str )
     {
         return str == null || str.length() == 0;



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message