generate CSS without tag names

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1170116 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-09-13 11:10:23 +00:00
parent 7b475e643d
commit 57f40b0e3f
3 changed files with 16 additions and 26 deletions

View File

@ -136,8 +136,7 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
public ExcelToHtmlConverter( Document doc ) public ExcelToHtmlConverter( Document doc )
{ {
htmlDocumentFacade = new HtmlDocumentFacade( doc ); htmlDocumentFacade = new HtmlDocumentFacade( doc );
cssClassTable = htmlDocumentFacade.getOrCreateCssClass( "table", "t", cssClassTable = htmlDocumentFacade.getOrCreateCssClass( "t", "border-collapse:collapse;border-spacing:0;" );
"border-collapse:collapse;border-spacing:0;" );
} }
protected String buildStyle( HSSFWorkbook workbook, HSSFCellStyle cellStyle ) protected String buildStyle( HSSFWorkbook workbook, HSSFCellStyle cellStyle )
@ -249,8 +248,7 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
return knownClass; return knownClass;
String cssStyle = buildStyle( workbook, cellStyle ); String cssStyle = buildStyle( workbook, cellStyle );
String cssClass = htmlDocumentFacade.getOrCreateCssClass( "td", "c", String cssClass = htmlDocumentFacade.getOrCreateCssClass( "c", cssStyle );
cssStyle );
excelStyleToClass.put( cellStyleKey, cssClass ); excelStyleToClass.put( cellStyleKey, cssClass );
return cssClass; return cssClass;
} }
@ -693,10 +691,9 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
{ {
// prepare CSS classes for later usage // prepare CSS classes for later usage
this.cssClassContainerCell = htmlDocumentFacade this.cssClassContainerCell = htmlDocumentFacade
.getOrCreateCssClass( "td", "c", .getOrCreateCssClass( "c", "padding:0;margin:0;align:left;vertical-align:top;" );
"padding:0;margin:0;align:left;vertical-align:top;" );
this.cssClassContainerDiv = htmlDocumentFacade.getOrCreateCssClass( this.cssClassContainerDiv = htmlDocumentFacade.getOrCreateCssClass(
"div", "d", "position:relative;" ); "d", "position:relative;" );
} }
for ( int s = 0; s < workbook.getNumberOfSheets(); s++ ) for ( int s = 0; s < workbook.getNumberOfSheets(); s++ )

View File

@ -87,8 +87,7 @@ public class HtmlDocumentFacade
String style ) String style )
{ {
String exising = element.getAttribute( "class" ); String exising = element.getAttribute( "class" );
String addition = getOrCreateCssClass( element.getTagName(), String addition = getOrCreateCssClass( classNamePrefix, style );
classNamePrefix, style );
String newClassValue = WordToHtmlUtils.isEmpty( exising ) ? addition String newClassValue = WordToHtmlUtils.isEmpty( exising ) ? addition
: ( exising + " " + addition ); : ( exising + " " + addition );
element.setAttribute( "class", newClassValue ); element.setAttribute( "class", newClassValue );
@ -210,13 +209,13 @@ public class HtmlDocumentFacade
return head; return head;
} }
public String getOrCreateCssClass( String tagName, String classNamePrefix, public String getOrCreateCssClass( String classNamePrefix, String style )
String style )
{ {
if ( !stylesheet.containsKey( tagName ) ) if ( !stylesheet.containsKey( classNamePrefix ) )
stylesheet.put( tagName, new LinkedHashMap<String, String>( 1 ) ); stylesheet.put( classNamePrefix, new LinkedHashMap<String, String>(
1 ) );
Map<String, String> styleToClassName = stylesheet.get( tagName ); Map<String, String> styleToClassName = stylesheet.get( classNamePrefix );
String knownClass = styleToClassName.get( style ); String knownClass = styleToClassName.get( style );
if ( knownClass != null ) if ( knownClass != null )
return knownClass; return knownClass;
@ -257,18 +256,14 @@ public class HtmlDocumentFacade
public void updateStylesheet() public void updateStylesheet()
{ {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
for ( Map.Entry<String, Map<String, String>> byTag : stylesheet for ( Map<String, String> byPrefix : stylesheet.values() )
.entrySet() )
{ {
String tagName = byTag.getKey(); for ( Map.Entry<String, String> byStyle : byPrefix.entrySet() )
for ( Map.Entry<String, String> byStyle : byTag.getValue()
.entrySet() )
{ {
String style = byStyle.getKey(); String style = byStyle.getKey();
String className = byStyle.getValue(); String className = byStyle.getValue();
stringBuilder.append( tagName + "." + className + "{" + style stringBuilder.append( "." + className + "{" + style + "}\n" );
+ "}\n" );
} }
} }
stylesheetElement.setTextContent( stringBuilder.toString() ); stylesheetElement.setTextContent( stringBuilder.toString() );

View File

@ -450,7 +450,7 @@ public class WordToHtmlConverter extends AbstractWordConverter
{ {
final String textIndex = String.valueOf( noteIndex + 1 ); final String textIndex = String.valueOf( noteIndex + 1 );
final String textIndexClass = htmlDocumentFacade.getOrCreateCssClass( final String textIndexClass = htmlDocumentFacade.getOrCreateCssClass(
"a", "a", "vertical-align:super;font-size:smaller;" ); "a", "vertical-align:super;font-size:smaller;" );
final String forwardNoteLink = type + "note_" + textIndex; final String forwardNoteLink = type + "note_" + textIndex;
final String backwardNoteLink = type + "note_back_" + textIndex; final String backwardNoteLink = type + "note_back_" + textIndex;
@ -682,8 +682,7 @@ public class WordToHtmlConverter extends AbstractWordConverter
if ( tableRowStyle.length() > 0 ) if ( tableRowStyle.length() > 0 )
tableRowElement.setAttribute( "class", htmlDocumentFacade tableRowElement.setAttribute( "class", htmlDocumentFacade
.getOrCreateCssClass( "tr", "r", .getOrCreateCssClass( "r", tableRowStyle.toString() ) );
tableRowStyle.toString() ) );
if ( tableRow.isTableHeader() ) if ( tableRow.isTableHeader() )
{ {
@ -700,8 +699,7 @@ public class WordToHtmlConverter extends AbstractWordConverter
.setAttribute( .setAttribute(
"class", "class",
htmlDocumentFacade.getOrCreateCssClass( htmlDocumentFacade.getOrCreateCssClass(
tableElement.getTagName(), "t", "t", "table-layout:fixed;border-collapse:collapse;border-spacing:0;" ) );
"table-layout:fixed;border-collapse:collapse;border-spacing:0;" ) );
if ( tableHeader.hasChildNodes() ) if ( tableHeader.hasChildNodes() )
{ {
tableElement.appendChild( tableHeader ); tableElement.appendChild( tableHeader );