import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Section;
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.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.w3c.dom.Document;
return fontReplacer;
}
+ protected int getNumberColumnsSpanned( int[] tableCellEdges,
+ int currentEdgeIndex, TableCell tableCell )
+ {
+ int nextEdgeIndex = currentEdgeIndex;
+ int colSpan = 0;
+ int cellRightEdge = tableCell.getLeftEdge() + tableCell.getWidth();
+ while ( tableCellEdges[nextEdgeIndex] < cellRightEdge )
+ {
+ colSpan++;
+ nextEdgeIndex++;
+ }
+ return colSpan;
+ }
+
+ protected int getNumberRowsSpanned( Table table, int currentRowIndex,
+ int currentColumnIndex, TableCell tableCell )
+ {
+ if ( !tableCell.isFirstVerticallyMerged() )
+ return 1;
+
+ final int numRows = table.numRows();
+
+ int count = 1;
+ for ( int r1 = currentRowIndex + 1; r1 < numRows; r1++ )
+ {
+ TableRow nextRow = table.getRow( r1 );
+ if ( nextRow.numCells() < currentColumnIndex )
+ break;
+ TableCell nextCell = nextRow.getCell( currentColumnIndex );
+ if ( !nextCell.isVerticallyMerged()
+ || nextCell.isFirstVerticallyMerged() )
+ break;
+ count++;
+ }
+ return count;
+ }
+
+ protected int getTableCellEdgesIndexSkipCount( Table table, int r,
+ int[] tableCellEdges, int currentEdgeIndex, int c,
+ TableCell tableCell )
+ {
+ TableCell upperCell = null;
+ for ( int r1 = r - 1; r1 >= 0; r1-- )
+ {
+ final TableCell prevCell = table.getRow( r1 ).getCell( c );
+ if ( prevCell != null && prevCell.isFirstVerticallyMerged() )
+ {
+ upperCell = prevCell;
+ break;
+ }
+ }
+ if ( upperCell == null )
+ {
+ logger.log( POILogger.WARN, "First vertically merged cell for ",
+ tableCell, " not found" );
+ return 0;
+ }
+
+ return getNumberColumnsSpanned( tableCellEdges, currentEdgeIndex,
+ tableCell );
+ }
+
protected abstract void outputCharacters( Element block,
CharacterRun characterRun, String text );
}
if ( text.endsWith( "\r" )
- || ( text.charAt( text.length() - 1 ) == BEL_MARK && currentTableLevel != 0 ) )
+ || ( text.charAt( text.length() - 1 ) == BEL_MARK && currentTableLevel != Integer.MIN_VALUE ) )
text = text.substring( 0, text.length() - 1 );
{
// Non-required hyphens to zero-width space
stringBuilder.append( UNICODECHAR_ZERO_WIDTH_SPACE );
}
- else
+ else if ( charChar > 0x20 || charChar == 0x09
+ || charChar == 0x0A || charChar == 0x0D )
{
stringBuilder.append( charChar );
}
public class WordToFoConverter extends AbstractWordConverter
{
+ /**
+ * Holds properties values, applied to current <tt>fo:block</tt> element.
+ * Those properties shall not be doubled in children <tt>fo:inline</tt>
+ * elements.
+ */
+ private static class BlockProperies
+ {
+ final boolean pBold;
+ final String pFontName;
+ final int pFontSize;
+ final boolean pItalic;
+
+ public BlockProperies( String pFontName, int pFontSize, boolean pBold,
+ boolean pItalic )
+ {
+ this.pFontName = pFontName;
+ this.pFontSize = pFontSize;
+ this.pBold = pBold;
+ this.pItalic = pItalic;
+ }
+ }
+
private static final POILogger logger = POILogFactory
.getLogger( WordToFoConverter.class );
{
TableCell tableCell = tableRow.getCell( c );
- if ( tableCell.isMerged() && !tableCell.isFirstMerged() )
- continue;
-
if ( tableCell.isVerticallyMerged()
&& !tableCell.isFirstVerticallyMerged() )
+ {
+ currentEdgeIndex += getTableCellEdgesIndexSkipCount( table,
+ r, tableCellEdges, currentEdgeIndex, c, tableCell );
continue;
+ }
Element tableCellElement = foDocumentFacade.createTableCell();
WordToFoUtils.setTableCellProperties( tableRow, tableCell,
tableCellElement, r == 0, r == tableRows - 1, c == 0,
c == rowCells - 1 );
- int colSpan = 0;
- int cellRightEdge = tableCell.getLeftEdge()
- + tableCell.getWidth();
- while ( tableCellEdges[currentEdgeIndex] < cellRightEdge )
- {
- colSpan++;
- currentEdgeIndex++;
- }
+ int colSpan = getNumberColumnsSpanned( tableCellEdges,
+ currentEdgeIndex, tableCell );
+ currentEdgeIndex += colSpan;
if ( colSpan == 0 )
continue;
tableCellElement.setAttribute( "number-columns-spanned",
String.valueOf( colSpan ) );
- if ( tableCell.isFirstVerticallyMerged() )
- {
- int count = 0;
- for ( int r1 = r; 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() )
- break;
- }
- if ( count > 1 )
- tableCellElement.setAttribute( "number-rows-spanned",
- String.valueOf( count ) );
- }
+ final int rowSpan = getNumberRowsSpanned( table, r, c,
+ tableCell );
+ if ( rowSpan > 1 )
+ tableCellElement.setAttribute( "number-rows-spanned",
+ String.valueOf( rowSpan ) );
processParagraphes( wordDocument, tableCellElement, tableCell,
table.getTableLevel() );
tableRowElement.appendChild( tableCellElement );
}
- if ( tableRow.isTableHeader() )
+ if ( tableRowElement.hasChildNodes() )
{
- tableHeader.appendChild( tableRowElement );
- }
- else
- {
- tableBody.appendChild( tableRowElement );
+ if ( tableRow.isTableHeader() )
+ {
+ tableHeader.appendChild( tableRowElement );
+ }
+ else
+ {
+ tableBody.appendChild( tableRowElement );
+ }
}
}
final Element tableElement = foDocumentFacade.createTable();
+ tableElement.setAttribute( "table-layout", "fixed" );
if ( tableHeader.hasChildNodes() )
{
tableElement.appendChild( tableHeader );
}
}
- /**
- * Holds properties values, applied to current <tt>fo:block</tt> element.
- * Those properties shall not be doubled in children <tt>fo:inline</tt>
- * elements.
- */
- private static class BlockProperies
- {
- final boolean pBold;
- final String pFontName;
- final int pFontSize;
- final boolean pItalic;
-
- public BlockProperies( String pFontName, int pFontSize, boolean pBold,
- boolean pItalic )
- {
- this.pFontName = pFontName;
- this.pFontSize = pFontSize;
- this.pBold = pBold;
- this.pItalic = pItalic;
- }
- }
-
}
public class WordToHtmlConverter extends AbstractWordConverter
{
+ /**
+ * Holds properties values, applied to current <tt>p</tt> element. Those
+ * properties shall not be doubled in children <tt>span</tt> elements.
+ */
+ private static class BlockProperies
+ {
+ final String pFontName;
+ final int pFontSize;
+
+ public BlockProperies( String pFontName, int pFontSize )
+ {
+ this.pFontName = pFontName;
+ this.pFontSize = pFontSize;
+ }
+ }
+
private static final POILogger logger = POILogFactory
.getLogger( WordToHtmlConverter.class );
if ( tableCell.isVerticallyMerged()
&& !tableCell.isFirstVerticallyMerged() )
+ {
+ currentEdgeIndex += getTableCellEdgesIndexSkipCount( table,
+ r, tableCellEdges, currentEdgeIndex, c, tableCell );
continue;
+ }
Element tableCellElement;
if ( tableRow.isTableHeader() )
r == 0, r == tableRows - 1, c == 0, c == rowCells - 1,
tableCellStyle );
- int colSpan = 0;
- int cellRightEdge = tableCell.getLeftEdge()
- + tableCell.getWidth();
- while ( tableCellEdges[currentEdgeIndex] < cellRightEdge )
- {
- colSpan++;
- currentEdgeIndex++;
- }
+ int colSpan = getNumberColumnsSpanned( tableCellEdges,
+ currentEdgeIndex, tableCell );
+ currentEdgeIndex += colSpan;
if ( colSpan == 0 )
continue;
if ( colSpan != 1 )
- {
tableCellElement.setAttribute( "colspan",
String.valueOf( colSpan ) );
- }
- if ( tableCell.isFirstVerticallyMerged() )
- {
- 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()
- || nextCell.isFirstVerticallyMerged() )
- break;
- count++;
- }
- if ( count > 1 )
- tableCellElement.setAttribute( "rowspan",
- String.valueOf( count ) );
- }
+ final int rowSpan = getNumberRowsSpanned( table, r, c,
+ tableCell );
+ if ( rowSpan > 1 )
+ tableCellElement.setAttribute( "rowspan",
+ String.valueOf( rowSpan ) );
processParagraphes( hwpfDocument, tableCellElement, tableCell,
table.getTableLevel() );
}
}
- /**
- * Holds properties values, applied to current <tt>p</tt> element. Those
- * properties shall not be doubled in children <tt>span</tt> elements.
- */
- private static class BlockProperies
- {
- final String pFontName;
- final int pFontSize;
-
- public BlockProperies( String pFontName, int pFontSize )
- {
- this.pFontName = pFontName;
- this.pFontSize = pFontSize;
- }
- }
-
}