SonarQube fixes and removed redundant logger level checks, for simple String messages

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1782548 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2017-02-11 00:57:39 +00:00
parent ea2eb34f46
commit db3000b142
2 changed files with 408 additions and 848 deletions

File diff suppressed because it is too large Load Diff

View File

@ -19,11 +19,15 @@ package org.apache.poi.hwpf.converter;
import static org.apache.poi.hwpf.converter.AbstractWordUtils.TWIPS_PER_INCH; import static org.apache.poi.hwpf.converter.AbstractWordUtils.TWIPS_PER_INCH;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
@ -56,8 +60,6 @@ import org.w3c.dom.Text;
* This implementation doesn't create images or links to them. This can be * This implementation doesn't create images or links to them. This can be
* changed by overriding {@link #processImage(Element, boolean, Picture)} * changed by overriding {@link #processImage(Element, boolean, Picture)}
* method. * method.
*
* @author Sergey Vladimirov (vlsergey {at} gmail {dot} com)
*/ */
@Beta @Beta
public class WordToHtmlConverter extends AbstractWordConverter public class WordToHtmlConverter extends AbstractWordConverter
@ -78,9 +80,27 @@ public class WordToHtmlConverter extends AbstractWordConverter
} }
} }
private static final POILogger logger = POILogFactory private static final POILogger logger = POILogFactory.getLogger( WordToHtmlConverter.class );
.getLogger( WordToHtmlConverter.class );
private final Deque<BlockProperies> blocksProperies = new LinkedList<BlockProperies>();
private final HtmlDocumentFacade htmlDocumentFacade;
private Element notes;
/**
* Creates new instance of {@link WordToHtmlConverter}. Can be used for
* output several {@link HWPFDocument}s into single HTML document.
*
* @param document XML DOM Document used as HTML document
*/
public WordToHtmlConverter( Document document ) {
this.htmlDocumentFacade = new HtmlDocumentFacade( document );
}
public WordToHtmlConverter( HtmlDocumentFacade htmlDocumentFacade ) {
this.htmlDocumentFacade = htmlDocumentFacade;
}
private static String getSectionStyle( Section section ) private static String getSectionStyle( Section section )
{ {
@ -110,20 +130,17 @@ public class WordToHtmlConverter extends AbstractWordConverter
} }
/** /**
* Java main() interface to interact with {@link WordToHtmlConverter} * Java main() interface to interact with {@link WordToHtmlConverter}<p>
*
* Usage: WordToHtmlConverter infile outfile<p>
* *
* <p>
* Usage: WordToHtmlConverter infile outfile
* </p>
* Where infile is an input .doc file ( Word 95-2007) which will be rendered * Where infile is an input .doc file ( Word 95-2007) which will be rendered
* as HTML into outfile * as HTML into outfile
*/ */
public static void main( String[] args ) throws Exception public static void main( String[] args )
{ throws IOException, ParserConfigurationException, TransformerException {
if ( args.length < 2 ) if ( args.length < 2 ) {
{ System.err.println( "Usage: WordToHtmlConverter <inputFile.doc> <saveTo.html>" );
System.err
.println( "Usage: WordToHtmlConverter <inputFile.doc> <saveTo.html>" );
return; return;
} }
@ -144,9 +161,9 @@ public class WordToHtmlConverter extends AbstractWordConverter
serializer.transform( domSource, streamResult ); serializer.transform( domSource, streamResult );
} }
static Document process( File docFile ) throws Exception static Document process( File docFile ) throws IOException, ParserConfigurationException
{ {
final HWPFDocumentCore wordDocument = WordToHtmlUtils.loadDoc( docFile ); final HWPFDocumentCore wordDocument = AbstractWordUtils.loadDoc( docFile );
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter( WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
XMLHelper.getDocumentBuilderFactory().newDocumentBuilder() XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
.newDocument() ); .newDocument() );
@ -154,38 +171,17 @@ public class WordToHtmlConverter extends AbstractWordConverter
return wordToHtmlConverter.getDocument(); return wordToHtmlConverter.getDocument();
} }
private final Stack<BlockProperies> blocksProperies = new Stack<BlockProperies>();
private final HtmlDocumentFacade htmlDocumentFacade;
private Element notes;
/**
* Creates new instance of {@link WordToHtmlConverter}. Can be used for
* output several {@link HWPFDocument}s into single HTML document.
*
* @param document
* XML DOM Document used as HTML document
*/
public WordToHtmlConverter( Document document )
{
this.htmlDocumentFacade = new HtmlDocumentFacade( document );
}
public WordToHtmlConverter( HtmlDocumentFacade htmlDocumentFacade )
{
this.htmlDocumentFacade = htmlDocumentFacade;
}
@Override @Override
protected void afterProcess() protected void afterProcess()
{ {
if ( notes != null ) if ( notes != null ) {
htmlDocumentFacade.getBody().appendChild( notes ); htmlDocumentFacade.getBody().appendChild( notes );
}
htmlDocumentFacade.updateStylesheet(); htmlDocumentFacade.updateStylesheet();
} }
@Override
public Document getDocument() public Document getDocument()
{ {
return htmlDocumentFacade.getDocument(); return htmlDocumentFacade.getDocument();
@ -202,8 +198,8 @@ public class WordToHtmlConverter extends AbstractWordConverter
BlockProperies blockProperies = this.blocksProperies.peek(); BlockProperies blockProperies = this.blocksProperies.peek();
Triplet triplet = getCharacterRunTriplet( characterRun ); Triplet triplet = getCharacterRunTriplet( characterRun );
if ( WordToHtmlUtils.isNotEmpty( triplet.fontName ) if ( AbstractWordUtils.isNotEmpty( triplet.fontName )
&& !WordToHtmlUtils.equals( triplet.fontName, && !AbstractWordUtils.equals( triplet.fontName,
blockProperies.pFontName ) ) blockProperies.pFontName ) )
{ {
style.append( "font-family:" + triplet.fontName + ";" ); style.append( "font-family:" + triplet.fontName + ";" );
@ -222,8 +218,9 @@ public class WordToHtmlConverter extends AbstractWordConverter
} }
WordToHtmlUtils.addCharactersProperties( characterRun, style ); WordToHtmlUtils.addCharactersProperties( characterRun, style );
if ( style.length() != 0 ) if ( style.length() != 0 ) {
htmlDocumentFacade.addStyleClass( span, "s", style.toString() ); htmlDocumentFacade.addStyleClass( span, "s", style.toString() );
}
Text textNode = htmlDocumentFacade.createText( text ); Text textNode = htmlDocumentFacade.createText( text );
span.appendChild( textNode ); span.appendChild( textNode );
@ -243,26 +240,31 @@ public class WordToHtmlConverter extends AbstractWordConverter
parent = bookmarkElement; parent = bookmarkElement;
} }
if ( range != null ) if ( range != null ) {
processCharacters( wordDocument, currentTableLevel, range, parent ); processCharacters( wordDocument, currentTableLevel, range, parent );
}
} }
@Override @Override
protected void processDocumentInformation( protected void processDocumentInformation(
SummaryInformation summaryInformation ) SummaryInformation summaryInformation )
{ {
if ( WordToHtmlUtils.isNotEmpty( summaryInformation.getTitle() ) ) if ( AbstractWordUtils.isNotEmpty( summaryInformation.getTitle() ) ) {
htmlDocumentFacade.setTitle( summaryInformation.getTitle() ); htmlDocumentFacade.setTitle( summaryInformation.getTitle() );
}
if ( WordToHtmlUtils.isNotEmpty( summaryInformation.getAuthor() ) ) if ( AbstractWordUtils.isNotEmpty( summaryInformation.getAuthor() ) ) {
htmlDocumentFacade.addAuthor( summaryInformation.getAuthor() ); htmlDocumentFacade.addAuthor( summaryInformation.getAuthor() );
}
if ( WordToHtmlUtils.isNotEmpty( summaryInformation.getKeywords() ) ) if ( AbstractWordUtils.isNotEmpty( summaryInformation.getKeywords() ) ) {
htmlDocumentFacade.addKeywords( summaryInformation.getKeywords() ); htmlDocumentFacade.addKeywords( summaryInformation.getKeywords() );
}
if ( WordToHtmlUtils.isNotEmpty( summaryInformation.getComments() ) ) if ( AbstractWordUtils.isNotEmpty( summaryInformation.getComments() ) ) {
htmlDocumentFacade htmlDocumentFacade
.addDescription( summaryInformation.getComments() ); .addDescription( summaryInformation.getComments() );
}
} }
@Override @Override
@ -318,11 +320,13 @@ public class WordToHtmlConverter extends AbstractWordConverter
Element basicLink = htmlDocumentFacade.createHyperlink( hyperlink ); Element basicLink = htmlDocumentFacade.createHyperlink( hyperlink );
currentBlock.appendChild( basicLink ); currentBlock.appendChild( basicLink );
if ( textRange != null ) if ( textRange != null ) {
processCharacters( wordDocument, currentTableLevel, textRange, processCharacters( wordDocument, currentTableLevel, textRange,
basicLink ); basicLink );
}
} }
@Override
protected void processImage( Element currentBlock, boolean inlined, protected void processImage( Element currentBlock, boolean inlined,
Picture picture, String imageSourcePath ) Picture picture, String imageSourcePath )
{ {
@ -372,7 +376,7 @@ public class WordToHtmlConverter extends AbstractWordConverter
} }
Element root; Element root;
if ( cropTop != 0 || cropRight != 0 || cropBottom != 0 || cropLeft != 0 ) if ( Math.abs(cropTop)+Math.abs(cropRight)+Math.abs(cropBottom)+Math.abs(cropLeft) > 0 )
{ {
float visibleWidth = Math float visibleWidth = Math
.max( 0, imageWidth - cropLeft - cropRight ); .max( 0, imageWidth - cropLeft - cropRight );
@ -482,6 +486,7 @@ public class WordToHtmlConverter extends AbstractWordConverter
flow.appendChild( htmlDocumentFacade.createLineBreak() ); flow.appendChild( htmlDocumentFacade.createLineBreak() );
} }
@Override
protected void processPageref( HWPFDocumentCore hwpfDocument, protected void processPageref( HWPFDocumentCore hwpfDocument,
Element currentBlock, Range textRange, int currentTableLevel, Element currentBlock, Range textRange, int currentTableLevel,
String pageref ) String pageref )
@ -489,11 +494,13 @@ public class WordToHtmlConverter extends AbstractWordConverter
Element basicLink = htmlDocumentFacade.createHyperlink( "#" + pageref ); Element basicLink = htmlDocumentFacade.createHyperlink( "#" + pageref );
currentBlock.appendChild( basicLink ); currentBlock.appendChild( basicLink );
if ( textRange != null ) if ( textRange != null ) {
processCharacters( hwpfDocument, currentTableLevel, textRange, processCharacters( hwpfDocument, currentTableLevel, textRange,
basicLink ); basicLink );
}
} }
@Override
protected void processParagraph( HWPFDocumentCore hwpfDocument, protected void processParagraph( HWPFDocumentCore hwpfDocument,
Element parentElement, int currentTableLevel, Paragraph paragraph, Element parentElement, int currentTableLevel, Paragraph paragraph,
String bulletText ) String bulletText )
@ -526,13 +533,13 @@ public class WordToHtmlConverter extends AbstractWordConverter
else else
{ {
pFontSize = -1; pFontSize = -1;
pFontName = WordToHtmlUtils.EMPTY; pFontName = AbstractWordUtils.EMPTY;
} }
blocksProperies.push( new BlockProperies( pFontName, pFontSize ) ); blocksProperies.push( new BlockProperies( pFontName, pFontSize ) );
} }
try try
{ {
if ( WordToHtmlUtils.isNotEmpty( bulletText ) ) if ( AbstractWordUtils.isNotEmpty( bulletText ) )
{ {
if ( bulletText.endsWith( "\t" ) ) if ( bulletText.endsWith( "\t" ) )
{ {
@ -581,13 +588,15 @@ public class WordToHtmlConverter extends AbstractWordConverter
blocksProperies.pop(); blocksProperies.pop();
} }
if ( style.length() > 0 ) if ( style.length() > 0 ) {
htmlDocumentFacade.addStyleClass( pElement, "p", style.toString() ); htmlDocumentFacade.addStyleClass( pElement, "p", style.toString() );
}
WordToHtmlUtils.compactSpans( pElement ); WordToHtmlUtils.compactSpans( pElement );
return; return;
} }
@Override
protected void processSection( HWPFDocumentCore wordDocument, protected void processSection( HWPFDocumentCore wordDocument,
Section section, int sectionCounter ) Section section, int sectionCounter )
{ {
@ -609,13 +618,14 @@ public class WordToHtmlConverter extends AbstractWordConverter
Integer.MIN_VALUE ); Integer.MIN_VALUE );
} }
@Override
protected void processTable( HWPFDocumentCore hwpfDocument, Element flow, protected void processTable( HWPFDocumentCore hwpfDocument, Element flow,
Table table ) Table table )
{ {
Element tableHeader = htmlDocumentFacade.createTableHeader(); Element tableHeader = htmlDocumentFacade.createTableHeader();
Element tableBody = htmlDocumentFacade.createTableBody(); Element tableBody = htmlDocumentFacade.createTableBody();
final int[] tableCellEdges = WordToHtmlUtils final int[] tableCellEdges = AbstractWordUtils
.buildTableCellEdgesArray( table ); .buildTableCellEdgesArray( table );
final int tableRows = table.numRows(); final int tableRows = table.numRows();
@ -667,18 +677,21 @@ public class WordToHtmlConverter extends AbstractWordConverter
currentEdgeIndex, tableCell ); currentEdgeIndex, tableCell );
currentEdgeIndex += colSpan; currentEdgeIndex += colSpan;
if ( colSpan == 0 ) if ( colSpan == 0 ) {
continue; continue;
}
if ( colSpan != 1 ) if ( colSpan != 1 ) {
tableCellElement.setAttribute( "colspan", tableCellElement.setAttribute( "colspan",
String.valueOf( colSpan ) ); String.valueOf( colSpan ) );
}
final int rowSpan = getNumberRowsSpanned( table, final int rowSpan = getNumberRowsSpanned( table,
tableCellEdges, r, c, tableCell ); tableCellEdges, r, c, tableCell );
if ( rowSpan > 1 ) if ( rowSpan > 1 ) {
tableCellElement.setAttribute( "rowspan", tableCellElement.setAttribute( "rowspan",
String.valueOf( rowSpan ) ); String.valueOf( rowSpan ) );
}
processParagraphes( hwpfDocument, tableCellElement, tableCell, processParagraphes( hwpfDocument, tableCellElement, tableCell,
table.getTableLevel() ); table.getTableLevel() );
@ -688,17 +701,19 @@ public class WordToHtmlConverter extends AbstractWordConverter
tableCellElement.appendChild( htmlDocumentFacade tableCellElement.appendChild( htmlDocumentFacade
.createParagraph() ); .createParagraph() );
} }
if ( tableCellStyle.length() > 0 ) if ( tableCellStyle.length() > 0 ) {
htmlDocumentFacade.addStyleClass( tableCellElement, htmlDocumentFacade.addStyleClass( tableCellElement,
tableCellElement.getTagName(), tableCellElement.getTagName(),
tableCellStyle.toString() ); tableCellStyle.toString() );
}
tableRowElement.appendChild( tableCellElement ); tableRowElement.appendChild( tableCellElement );
} }
if ( tableRowStyle.length() > 0 ) if ( tableRowStyle.length() > 0 ) {
tableRowElement.setAttribute( "class", htmlDocumentFacade tableRowElement.setAttribute( "class", htmlDocumentFacade
.getOrCreateCssClass( "r", tableRowStyle.toString() ) ); .getOrCreateCssClass( "r", tableRowStyle.toString() ) );
}
if ( tableRow.isTableHeader() ) if ( tableRow.isTableHeader() )
{ {