You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by se...@apache.org on 2011/07/22 11:42:34 UTC
svn commit: r1149528 - in /poi/trunk:
src/scratchpad/src/org/apache/poi/hwpf/converter/
src/scratchpad/testcases/org/apache/poi/hwpf/
src/scratchpad/testcases/org/apache/poi/hwpf/converter/ test-data/document/
Author: sergey
Date: Fri Jul 22 09:42:32 2011
New Revision: 1149528
URL: http://svn.apache.org/viewvc?rev=1149528&view=rev
Log:
better processing of word tables in cases different rows have different cell widths
Added:
poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/converter/AbstractWordUtilsTest.java
poi/trunk/test-data/document/table-merges.doc (with props)
Modified:
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordUtils.java
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java
poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/AllHWPFTests.java
poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java
Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordUtils.java?rev=1149528&r1=1149527&r2=1149528&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordUtils.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordUtils.java Fri Jul 22 09:42:32 2011
@@ -20,6 +20,8 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Set;
+import java.util.TreeSet;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFDocumentCore;
@@ -29,6 +31,9 @@ import org.apache.poi.hwpf.model.ListLev
import org.apache.poi.hwpf.model.ListTables;
import org.apache.poi.hwpf.usermodel.BorderCode;
import org.apache.poi.hwpf.usermodel.Paragraph;
+import org.apache.poi.hwpf.usermodel.Table;
+import org.apache.poi.hwpf.usermodel.TableCell;
+import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.w3c.dom.Attr;
@@ -44,6 +49,42 @@ public class AbstractWordUtils
public static final float TWIPS_PER_INCH = 1440.0f;
public static final int TWIPS_PER_PT = 20;
+ /**
+ * Creates array of all possible cell edges. In HTML (and FO) cells from
+ * different rows and same column should have same width, otherwise spanning
+ * shall be used.
+ *
+ * @param table
+ * table to build cell edges array from
+ * @return array of cell edges (including leftest one) in twips
+ */
+ static int[] buildTableCellEdgesArray( Table table )
+ {
+ Set<Integer> edges = new TreeSet<Integer>();
+
+ for ( int r = 0; r < table.numRows(); r++ )
+ {
+ TableRow tableRow = table.getRow( r );
+ for ( int c = 0; c < tableRow.numCells(); c++ )
+ {
+ TableCell tableCell = tableRow.getCell( c );
+
+ edges.add( Integer.valueOf( tableCell.getLeftEdge() ) );
+ edges.add( Integer.valueOf( tableCell.getLeftEdge()
+ + tableCell.getWidth() ) );
+ }
+ }
+
+ Integer[] sorted = edges.toArray( new Integer[edges.size()] );
+ int[] result = new int[sorted.length];
+ for ( int i = 0; i < sorted.length; i++ )
+ {
+ result[i] = sorted[i].intValue();
+ }
+
+ return result;
+ }
+
static boolean canBeMerged( Node node1, Node node2, String requiredTagName )
{
if ( node1.getNodeType() != Node.ELEMENT_NODE
Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java?rev=1149528&r1=1149527&r2=1149528&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java Fri Jul 22 09:42:32 2011
@@ -489,6 +489,8 @@ public class WordToFoConverter extends A
Element tableHeader = foDocumentFacade.createTableHeader();
Element tableBody = foDocumentFacade.createTableBody();
+ final int[] tableCellEdges = WordToHtmlUtils
+ .buildTableCellEdgesArray( table );
final int tableRows = table.numRows();
int maxColumns = Integer.MIN_VALUE;
@@ -504,6 +506,8 @@ public class WordToFoConverter extends A
Element tableRowElement = foDocumentFacade.createTableRow();
WordToFoUtils.setTableRowProperties( tableRow, tableRowElement );
+ // index of current element in tableCellEdges[]
+ int currentEdgeIndex = 0;
final int rowCells = tableRow.numCells();
for ( int c = 0; c < rowCells; c++ )
{
@@ -521,30 +525,22 @@ public class WordToFoConverter extends A
tableCellElement, r == 0, r == tableRows - 1, c == 0,
c == rowCells - 1 );
- if ( tableCell.isFirstMerged() )
+ int colSpan = 0;
+ int cellRightEdge = tableCell.getLeftEdge()
+ + tableCell.getWidth();
+ while ( tableCellEdges[currentEdgeIndex] < cellRightEdge )
{
- int count = 0;
- for ( int c1 = c; c1 < rowCells; c1++ )
- {
- TableCell nextCell = tableRow.getCell( c1 );
- if ( nextCell.isMerged() )
- count++;
- if ( !nextCell.isMerged() )
- break;
- }
- tableCellElement.setAttribute( "number-columns-spanned", ""
- + count );
- }
- else
- {
- if ( c == rowCells - 1 && c != maxColumns - 1 )
- {
- tableCellElement.setAttribute(
- "number-columns-spanned", ""
- + ( maxColumns - c ) );
- }
+ colSpan++;
+ currentEdgeIndex++;
}
+ if ( colSpan == 0 )
+ continue;
+
+ if ( colSpan != 1 )
+ tableCellElement.setAttribute( "number-columns-spanned",
+ String.valueOf( colSpan ) );
+
if ( tableCell.isFirstVerticallyMerged() )
{
int count = 0;
@@ -559,8 +555,9 @@ public class WordToFoConverter extends A
if ( !nextCell.isVerticallyMerged() )
break;
}
- tableCellElement.setAttribute( "number-rows-spanned", ""
- + count );
+ if ( count > 1 )
+ tableCellElement.setAttribute( "number-rows-spanned",
+ String.valueOf( count ) );
}
processParagraphes( wordDocument, tableCellElement, tableCell,
Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java?rev=1149528&r1=1149527&r2=1149528&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java Fri Jul 22 09:42:32 2011
@@ -557,6 +557,8 @@ public class WordToHtmlConverter extends
Element tableHeader = htmlDocumentFacade.createTableHeader();
Element tableBody = htmlDocumentFacade.createTableBody();
+ final int[] tableCellEdges = WordToHtmlUtils
+ .buildTableCellEdgesArray( table );
final int tableRows = table.numRows();
int maxColumns = Integer.MIN_VALUE;
@@ -573,14 +575,13 @@ public class WordToHtmlConverter extends
StringBuilder tableRowStyle = new StringBuilder();
WordToHtmlUtils.addTableRowProperties( tableRow, tableRowStyle );
+ // index of current element in tableCellEdges[]
+ int currentEdgeIndex = 0;
final int rowCells = tableRow.numCells();
for ( int c = 0; c < rowCells; c++ )
{
TableCell tableCell = tableRow.getCell( c );
- if ( tableCell.isMerged() && !tableCell.isFirstMerged() )
- continue;
-
if ( tableCell.isVerticallyMerged()
&& !tableCell.isFirstVerticallyMerged() )
continue;
@@ -600,43 +601,41 @@ public class WordToHtmlConverter extends
r == 0, r == tableRows - 1, c == 0, c == rowCells - 1,
tableCellStyle );
- if ( tableCell.isFirstMerged() )
+ int colSpan = 0;
+ int cellRightEdge = tableCell.getLeftEdge()
+ + tableCell.getWidth();
+ while ( tableCellEdges[currentEdgeIndex] < cellRightEdge )
{
- int count = 0;
- for ( int c1 = c; c1 < rowCells; c1++ )
- {
- TableCell nextCell = tableRow.getCell( c1 );
- if ( nextCell.isMerged() )
- count++;
- if ( !nextCell.isMerged() )
- break;
- }
- tableCellElement.setAttribute( "colspan", "" + count );
+ colSpan++;
+ currentEdgeIndex++;
}
- else
+
+ if ( colSpan == 0 )
+ continue;
+
+ if ( colSpan != 1 )
{
- if ( c == rowCells - 1 && c != maxColumns - 1 )
- {
- tableCellElement.setAttribute( "colspan", ""
- + ( maxColumns - c ) );
- }
+ tableCellElement.setAttribute( "colspan",
+ String.valueOf( colSpan ) );
}
if ( tableCell.isFirstVerticallyMerged() )
{
- int count = 0;
- for ( int r1 = r; r1 < tableRows; r1++ )
+ int count = 1;
+ for ( int r1 = r + 1; r1 < tableRows; r1++ )
{
TableRow nextRow = table.getRow( r1 );
if ( nextRow.numCells() < c )
break;
TableCell nextCell = nextRow.getCell( c );
- if ( nextCell.isVerticallyMerged() )
- count++;
- if ( !nextCell.isVerticallyMerged() )
+ if ( !nextCell.isVerticallyMerged()
+ || nextCell.isFirstVerticallyMerged() )
break;
+ count++;
}
- tableCellElement.setAttribute( "rowspan", "" + count );
+ if ( count > 1 )
+ tableCellElement.setAttribute( "rowspan",
+ String.valueOf( count ) );
}
processParagraphes( hwpfDocument, tableCellElement, tableCell,
Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/AllHWPFTests.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/AllHWPFTests.java?rev=1149528&r1=1149527&r2=1149528&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/AllHWPFTests.java (original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/AllHWPFTests.java Fri Jul 22 09:42:32 2011
@@ -19,7 +19,7 @@ package org.apache.poi.hwpf;
import junit.framework.Test;
import junit.framework.TestSuite;
-
+import org.apache.poi.hwpf.converter.AbstractWordUtilsTest;
import org.apache.poi.hwpf.converter.TestWordToFoConverter;
import org.apache.poi.hwpf.converter.TestWordToHtmlConverter;
import org.apache.poi.hwpf.extractor.TestDifferentRoutes;
@@ -72,6 +72,7 @@ public final class AllHWPFTests
// org.apache.poi.hwpf.converter
// suite.addTestSuite( TestWordToConverterSuite.class );
+ suite.addTestSuite( AbstractWordUtilsTest.class );
suite.addTestSuite( TestWordToFoConverter.class );
suite.addTestSuite( TestWordToHtmlConverter.class );
Added: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/converter/AbstractWordUtilsTest.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/converter/AbstractWordUtilsTest.java?rev=1149528&view=auto
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/converter/AbstractWordUtilsTest.java (added)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/converter/AbstractWordUtilsTest.java Fri Jul 22 09:42:32 2011
@@ -0,0 +1,53 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.hwpf.converter;
+
+import org.apache.poi.hwpf.usermodel.Range;
+
+import junit.framework.TestCase;
+import org.apache.poi.hwpf.HWPFDocument;
+import org.apache.poi.hwpf.HWPFTestDataSamples;
+import org.apache.poi.hwpf.usermodel.Table;
+
+/**
+ * Test cases for {@link AbstractWordUtils}
+ *
+ * @author Sergey Vladimirov (vlsergey {at} gmail {dot} com)
+ */
+public class AbstractWordUtilsTest extends TestCase
+{
+ /**
+ * Test case for {@link AbstractWordUtils#buildTableCellEdgesArray(Table)}
+ */
+ public void testBuildTableCellEdgesArray()
+ {
+ HWPFDocument document = HWPFTestDataSamples
+ .openSampleFile( "table-merges.doc" );
+ final Range range = document.getRange();
+ Table table = range.getTable( range.getParagraph( 0 ) );
+
+ int[] result = AbstractWordUtils.buildTableCellEdgesArray( table );
+ assertEquals( 6, result.length );
+
+ assertEquals( 0000, result[0] );
+ assertEquals( 1062, result[1] );
+ assertEquals( 5738, result[2] );
+ assertEquals( 6872, result[3] );
+ assertEquals( 8148, result[4] );
+ assertEquals( 9302, result[5] );
+ }
+}
Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java?rev=1149528&r1=1149527&r2=1149528&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java (original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java Fri Jul 22 09:42:32 2011
@@ -190,6 +190,14 @@ public class TestWordToHtmlConverter ext
getHtmlText( "innertable.doc" );
}
+ public void testTableMerges() throws Exception
+ {
+ String result = getHtmlText( "table-merges.doc" );
+
+ assertContains( result, "<td class=\"td1\" colspan=\"3\">" );
+ assertContains( result, "<td class=\"td2\" colspan=\"2\">" );
+ }
+
public void testO_kurs_doc() throws Exception
{
getHtmlText( "o_kurs.doc" );
Added: poi/trunk/test-data/document/table-merges.doc
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/document/table-merges.doc?rev=1149528&view=auto
==============================================================================
Binary file - no diff available.
Propchange: poi/trunk/test-data/document/table-merges.doc
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org