compact HTML output of WordToHtmlConverter

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1148269 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-07-19 10:51:23 +00:00
parent 2b7cbb8a82
commit 67710377a8
3 changed files with 89 additions and 72 deletions

View File

@ -160,6 +160,7 @@ public class WordToHtmlConverter extends AbstractWordConverter
public Document getDocument() public Document getDocument()
{ {
htmlDocumentFacade.updateStylesheet();
return htmlDocumentFacade.getDocument(); return htmlDocumentFacade.getDocument();
} }
@ -182,7 +183,7 @@ public class WordToHtmlConverter extends AbstractWordConverter
} }
if ( characterRun.getFontSize() / 2 != blockProperies.pFontSize ) if ( characterRun.getFontSize() / 2 != blockProperies.pFontSize )
{ {
style.append( "font-size: " + characterRun.getFontSize() / 2 + "; " ); style.append( "font-size:" + characterRun.getFontSize() / 2 + "pt;" );
} }
if ( triplet.bold ) if ( triplet.bold )
{ {
@ -195,7 +196,8 @@ public class WordToHtmlConverter extends AbstractWordConverter
WordToHtmlUtils.addCharactersProperties( characterRun, style ); WordToHtmlUtils.addCharactersProperties( characterRun, style );
if ( style.length() != 0 ) if ( style.length() != 0 )
span.setAttribute( "style", style.toString() ); span.setAttribute( "class", htmlDocumentFacade.getOrCreateCssClass(
span.getTagName(), "s", style.toString() ) );
Text textNode = htmlDocumentFacade.createText( text ); Text textNode = htmlDocumentFacade.createText( text );
span.appendChild( textNode ); span.appendChild( textNode );
@ -312,22 +314,28 @@ public class WordToHtmlConverter extends AbstractWordConverter
float visibleHeight = Math.max( 0, imageHeight - cropTop float visibleHeight = Math.max( 0, imageHeight - cropTop
- cropBottom ); - cropBottom );
root = htmlDocumentFacade.document.createElement( "div" ); root = htmlDocumentFacade.createBlock();
root.setAttribute( "style", "vertical-align:text-bottom;width:" root.setAttribute( "class", htmlDocumentFacade.getOrCreateCssClass(
+ visibleWidth + "in;height:" + visibleHeight + "in;" ); root.getTagName(), "d", "vertical-align:text-bottom;width:"
+ visibleWidth + "in;height:" + visibleHeight
+ "in;" ) );
// complex // complex
Element inner = htmlDocumentFacade.document.createElement( "div" ); Element inner = htmlDocumentFacade.createBlock();
inner.setAttribute( "style", "position:relative;width:" inner.setAttribute( "class", htmlDocumentFacade
+ visibleWidth + "in;height:" + visibleHeight .getOrCreateCssClass( inner.getTagName(), "d",
+ "in;overflow:hidden;" ); "position:relative;width:" + visibleWidth
+ "in;height:" + visibleHeight
+ "in;overflow:hidden;" ) );
root.appendChild( inner ); root.appendChild( inner );
Element image = htmlDocumentFacade.document.createElement( "img" ); Element image = htmlDocumentFacade.document.createElement( "img" );
image.setAttribute( "src", imageSourcePath ); image.setAttribute( "src", imageSourcePath );
image.setAttribute( "style", "position:absolute;left:-" + cropLeft image.setAttribute( "class", htmlDocumentFacade
+ ";top:-" + cropTop + ";width:" + imageWidth .getOrCreateCssClass( image.getTagName(), "i",
+ "in;height:" + imageHeight + "in;" ); "position:absolute;left:-" + cropLeft + ";top:-"
+ cropTop + ";width:" + imageWidth
+ "in;height:" + imageHeight + "in;" ) );
inner.appendChild( image ); inner.appendChild( image );
style.append( "overflow:hidden;" ); style.append( "overflow:hidden;" );
@ -414,7 +422,10 @@ public class WordToHtmlConverter extends AbstractWordConverter
} }
if ( style.length() > 0 ) if ( style.length() > 0 )
pElement.setAttribute( "style", style.toString() ); pElement.setAttribute(
"class",
htmlDocumentFacade.getOrCreateCssClass(
pElement.getTagName(), "p", style.toString() ) );
return; return;
} }
@ -422,8 +433,9 @@ public class WordToHtmlConverter extends AbstractWordConverter
protected void processSection( HWPFDocumentCore wordDocument, protected void processSection( HWPFDocumentCore wordDocument,
Section section, int sectionCounter ) Section section, int sectionCounter )
{ {
Element div = htmlDocumentFacade.document.createElement( "div" ); Element div = htmlDocumentFacade.createBlock();
div.setAttribute( "style", getSectionStyle( section ) ); div.setAttribute( "class", htmlDocumentFacade.getOrCreateCssClass(
div.getTagName(), "d", getSectionStyle( section ) ) );
htmlDocumentFacade.body.appendChild( div ); htmlDocumentFacade.body.appendChild( div );
processSectionParagraphes( wordDocument, div, section, processSectionParagraphes( wordDocument, div, section,
@ -434,8 +446,9 @@ public class WordToHtmlConverter extends AbstractWordConverter
protected void processSingleSection( HWPFDocumentCore wordDocument, protected void processSingleSection( HWPFDocumentCore wordDocument,
Section section ) Section section )
{ {
htmlDocumentFacade.body.setAttribute( "style", htmlDocumentFacade.body
getSectionStyle( section ) ); .setAttribute( "class", htmlDocumentFacade.getOrCreateCssClass(
"body", "b", getSectionStyle( section ) ) );
processSectionParagraphes( wordDocument, htmlDocumentFacade.body, processSectionParagraphes( wordDocument, htmlDocumentFacade.body,
section, Integer.MIN_VALUE ); section, Integer.MIN_VALUE );
@ -538,15 +551,19 @@ public class WordToHtmlConverter extends AbstractWordConverter
.createParagraph() ); .createParagraph() );
} }
if ( tableCellStyle.length() > 0 ) if ( tableCellStyle.length() > 0 )
tableCellElement.setAttribute( "style", tableCellElement.setAttribute( "class", htmlDocumentFacade
tableCellStyle.toString() ); .getOrCreateCssClass(
tableCellElement.getTagName(),
tableCellElement.getTagName(),
tableCellStyle.toString() ) );
tableRowElement.appendChild( tableCellElement ); tableRowElement.appendChild( tableCellElement );
} }
if ( tableRowStyle.length() > 0 ) if ( tableRowStyle.length() > 0 )
tableRowElement tableRowElement.setAttribute( "class", htmlDocumentFacade
.setAttribute( "style", tableRowStyle.toString() ); .getOrCreateCssClass( "tr", "r",
tableRowStyle.toString() ) );
if ( tableRow.isTableHeader() ) if ( tableRow.isTableHeader() )
{ {

View File

@ -37,21 +37,21 @@ public class WordToHtmlUtils extends AbstractWordUtils
if ( isEmpty( where ) ) if ( isEmpty( where ) )
{ {
style.append( "border-style: " + getBorderType( borderCode ) + "; " ); style.append( "border:" );
style.append( "border-color: " + getColor( borderCode.getColor() )
+ "; " );
style.append( "border-width: " + getBorderWidth( borderCode )
+ "; " );
} }
else else
{ {
style.append( "border-" + where + "-style: " style.append( "border-" );
+ getBorderType( borderCode ) + "; " ); style.append( where );
style.append( "border-" + where + "-color: "
+ getColor( borderCode.getColor() ) + "; " );
style.append( "border-" + where + "-width: "
+ getBorderWidth( borderCode ) + "; " );
} }
style.append( ":" );
style.append( getBorderWidth( borderCode ) );
style.append( ' ' );
style.append( getBorderType( borderCode ) );
style.append( ' ' );
style.append( getColor( borderCode.getColor() ) );
style.append( ';' );
} }
public static void addCharactersProperties( public static void addCharactersProperties(

View File

@ -92,7 +92,7 @@ public class TestWordToHtmlConverter extends TestCase
public void testAIOOBTap() throws Exception public void testAIOOBTap() throws Exception
{ {
String result = getHtmlText( "AIOOB-Tap.doc" ); String result = getHtmlText( "AIOOB-Tap.doc" );
assertContains( result.substring( 0, 2000 ), "<table>" ); assertContains( result.substring( 0, 6000 ), "<table>" );
} }
public void testBug33519() throws Exception public void testBug33519() throws Exception