Skip to content

Commit

Permalink
Code refactoring for sonar cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Aug 20, 2023
1 parent 9535ef9 commit 15ef6b7
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.fugerit.java.doc.base.config.DocTypeHandler;
import org.fugerit.java.doc.base.config.DocTypeHandlerDefault;
import org.fugerit.java.doc.base.model.DocBase;
import org.fugerit.java.doc.mod.openpdf.helpers.OpenPpfDocHandler;

import com.lowagie.text.Document;
import com.lowagie.text.html.HtmlWriter;
Expand All @@ -22,7 +23,7 @@ public class HtmlTypeHandler extends DocTypeHandlerDefault {
public static final DocTypeHandler HANDLER = new HtmlTypeHandler();

public HtmlTypeHandler() {
super( ITextDocHandler.DOC_OUTPUT_HTML, ITextDocHandler.MODULE );
super( OpenPpfDocHandler.DOC_OUTPUT_HTML, OpenPpfDocHandler.MODULE );
}

@Override
Expand All @@ -32,7 +33,7 @@ public void handle(DocInput docInput, DocOutput docOutput) throws Exception {
Document document = new Document( );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HtmlWriter.getInstance( document, baos );
ITextDocHandler handler = new ITextDocHandler( document, ITextDocHandler.DOC_OUTPUT_HTML );
OpenPpfDocHandler handler = new OpenPpfDocHandler( document, OpenPpfDocHandler.DOC_OUTPUT_HTML );
handler.handleDoc( docBase );
baos.writeTo( outputStream );
baos.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.fugerit.java.doc.base.config.DocTypeHandler;
import org.fugerit.java.doc.base.config.DocTypeHandlerDefault;
import org.fugerit.java.doc.base.model.DocBase;
import org.fugerit.java.doc.mod.openpdf.helpers.OpenPpfDocHandler;

import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
Expand All @@ -23,7 +24,7 @@ public class PdfTypeHandler extends DocTypeHandlerDefault {
public static DocTypeHandler HANDLER = new PdfTypeHandler();

public PdfTypeHandler() {
super( ITextDocHandler.DOC_OUTPUT_PDF, ITextDocHandler.MODULE );
super( OpenPpfDocHandler.DOC_OUTPUT_PDF, OpenPpfDocHandler.MODULE );
}

@Override
Expand All @@ -40,7 +41,7 @@ public void handle(DocInput docInput, DocOutput docOutput) throws Exception {
// create pdf writer
PdfWriter pdfWriter = PdfWriter.getInstance( document, baos );
// create doc handler
ITextDocHandler handler = new ITextDocHandler( document, pdfWriter );
OpenPpfDocHandler handler = new OpenPpfDocHandler( document, pdfWriter );
if ( "true".equalsIgnoreCase( docBase.getInfo().getProperty( "set-total-page" ) ) ) {
handler.handleDoc( docBase );
int totalPageCount = pdfWriter.getCurrentPageNumber()-1;
Expand All @@ -50,7 +51,7 @@ public void handle(DocInput docInput, DocOutput docOutput) throws Exception {
Integer.parseInt( margins[3] ) );
baos = new ByteArrayOutputStream();
pdfWriter = PdfWriter.getInstance( document, baos );
handler = new ITextDocHandler(document, pdfWriter, totalPageCount );
handler = new OpenPpfDocHandler(document, pdfWriter, totalPageCount );
}
handler.handleDoc( docBase );
baos.writeTo( outputStream );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.fugerit.java.doc.base.config.DocTypeHandler;
import org.fugerit.java.doc.base.config.DocTypeHandlerDefault;
import org.fugerit.java.doc.base.model.DocBase;
import org.fugerit.java.doc.mod.openpdf.helpers.OpenPpfDocHandler;

import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
Expand All @@ -23,7 +24,7 @@ public class RtfTypeHandler extends DocTypeHandlerDefault {
public static final DocTypeHandler HANDLER = new RtfTypeHandler();

public RtfTypeHandler() {
super( ITextDocHandler.DOC_OUTPUT_RTF, ITextDocHandler.MODULE );
super( OpenPpfDocHandler.DOC_OUTPUT_RTF, OpenPpfDocHandler.MODULE );
}

@Override
Expand All @@ -37,7 +38,7 @@ public void handle(DocInput docInput, DocOutput docOutput) throws Exception {
Integer.parseInt( margins[3] ) );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
RtfWriter2 rtfWriter2 = RtfWriter2.getInstance( document, baos );
ITextDocHandler handler = new ITextDocHandler( document, rtfWriter2 );
OpenPpfDocHandler handler = new OpenPpfDocHandler( document, rtfWriter2 );
handler.handleDoc( docBase );
baos.writeTo( outputStream );
baos.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package org.fugerit.java.doc.mod.openpdf.helpers;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import org.fugerit.java.core.log.LogFacade;
import org.fugerit.java.doc.base.model.DocBarcode;
import org.fugerit.java.doc.base.model.DocBorders;
import org.fugerit.java.doc.base.model.DocCell;
import org.fugerit.java.doc.base.model.DocElement;
import org.fugerit.java.doc.base.model.DocImage;
import org.fugerit.java.doc.base.model.DocPara;
import org.fugerit.java.doc.base.model.DocPhrase;
import org.fugerit.java.doc.base.model.DocRow;
import org.fugerit.java.doc.base.model.DocTable;
import org.fugerit.java.doc.base.xml.DocModelUtils;

import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.alignment.HorizontalAlignment;
import com.lowagie.text.alignment.VerticalAlignment;

public class OpenPdfDocTableHelper {

private static void handleBolders( DocCell docCell, Cell cell ) {
DocBorders docBorders = docCell.getDocBorders();
if ( docBorders != null ) {
if ( docBorders.getBorderColorBottom() != null ) {
cell.setBorderColorBottom( DocModelUtils.parseHtmlColor( docBorders.getBorderColorBottom() ) );
}
if ( docBorders.getBorderColorTop() != null ) {
cell.setBorderColorTop( DocModelUtils.parseHtmlColor( docBorders.getBorderColorTop() ) );
}
if ( docBorders.getBorderColorLeft() != null ) {
cell.setBorderColorLeft( DocModelUtils.parseHtmlColor( docBorders.getBorderColorLeft() ) );
}
if ( docBorders.getBorderColorRight() != null ) {
cell.setBorderColorRight( DocModelUtils.parseHtmlColor( docBorders.getBorderColorRight() ) );
}
if ( docBorders.getBorderWidthBottom() != -1 ) {
cell.setBorderWidthBottom( docBorders.getBorderWidthBottom() );
}
if ( docBorders.getBorderWidthTop() != -1 ) {
cell.setBorderWidthTop( docBorders.getBorderWidthTop() );
}
if ( docBorders.getBorderWidthLeft() != -1 ) {
cell.setBorderWidthLeft( docBorders.getBorderWidthLeft() );
}
if ( docBorders.getBorderWidthRight() != -1 ) {
cell.setBorderWidthRight( docBorders.getBorderWidthRight() );
}
}
}

private static void handleAligns( DocCell docCell, Cell cell ) {
if ( docCell.getAlign() != DocPara.ALIGN_UNSET ) {
Optional<HorizontalAlignment> ha = HorizontalAlignment.of( docCell.getAlign() );
if ( ha.isPresent() ) {
cell.setHorizontalAlignment( ha.get() );
}
}
if ( docCell.getValign() != DocPara.ALIGN_UNSET ) {
Optional<VerticalAlignment> va = VerticalAlignment.of( docCell.getAlign() );
if ( va.isPresent() ) {
cell.setVerticalAlignment( va.get() );
}
}
}

private static List<Font> handleContent( Table table, CellParent cellParent, DocCell docCell, Cell cell, OpenPdfHelper docHelper ) throws Exception {
List<Font> fontList = new ArrayList<>();
Iterator<DocElement> itCurrent = docCell.docElements();
while ( itCurrent.hasNext() ) {
DocElement docElement = (DocElement) itCurrent.next();
if ( docElement instanceof DocPara ) {
DocPara docPara = (DocPara)docElement;
OpenPpfDocHandler.setStyle( docCell , docPara );
Paragraph paragraph = OpenPpfDocHandler.createPara( docPara, docHelper, fontList );
cellParent.add( paragraph );
} else if ( docElement instanceof DocPhrase ) {
DocPhrase docPhrase = (DocPhrase)docElement;
//setStyle( docCell , docPara );
cellParent.add( OpenPpfDocHandler.createPhrase( docPhrase, docHelper, fontList ) );
} else if ( docElement instanceof DocTable ) {
LogFacade.getLog().debug( "nested table" );
table.insertTable( createTable( (DocTable)docElement, docHelper ) );
} else if ( docElement instanceof DocImage ) {
LogFacade.getLog().debug( "cell DocImage : "+docElement );
cellParent.add( OpenPpfDocHandler.createImage( (DocImage)docElement ) );
} else if ( docElement instanceof DocBarcode ) {
LogFacade.getLog().info( "cell DocBarcode : "+docElement );
cellParent.add( OpenPpfDocHandler.createBarcode( (DocBarcode)docElement, docHelper ) );
}
}
return fontList;
}

private static boolean handleCell( Table table, DocCell docCell, boolean startHeader, DocTable docTable, OpenPdfHelper docHelper ) throws Exception {
OpenPpfDocHandler.setStyle( docTable, docCell );
Cell cell = new Cell();
if ( docCell.isHeader() ) {
cell.setHeader( true );
startHeader = true;
} else {
if ( startHeader ) {
startHeader = false;
table.endHeaders();
}
}
cell.setColspan( docCell.getCSpan() );
cell.setRowspan( docCell.getRSpan() );

handleBolders(docCell, cell);

if ( docCell.getBackColor() != null ) {
cell.setBackgroundColor( DocModelUtils.parseHtmlColor( docCell.getBackColor() ) );
}

handleAligns(docCell, cell);

CellParent cellParent = new CellParent( cell );

List<Font> fontList = handleContent(table, cellParent, docCell, cell, docHelper);

table.addCell( cell );
List<Element> listChunk = cell.getChunks();
if ( listChunk.size() == fontList.size() ) {
for ( int k=0; k<listChunk.size(); k++ ) {
Chunk c = (Chunk)listChunk.get( k );
Font f = (Font) fontList.get( k );
c.setFont( f );
}
}
if ( docHelper.getPdfWriter() != null ) {
docHelper.getPdfWriter().flush();
}
return startHeader;
}

protected static Table createTable( DocTable docTable, OpenPdfHelper docHelper ) throws Exception {

boolean startHeader = false;
Table table = new Table( docTable.getColumns() );
table.setBorderWidth(0);
table.setWidth( docTable.getWidth() );
table.setBorderColor( Color.black );
table.setPadding( docTable.getPadding() );
table.setSpacing( docTable.getSpacing() );
table.setCellsFitPage( true );

if ( docTable.getSpaceBefore() != null ) {
table.setSpacing( docTable.getSpaceBefore().floatValue() );
}
if ( docTable.getSpaceAfter() != null ) {
table.setSpacing( docTable.getSpaceAfter().floatValue() );
}

int[] cw = docTable.getColWithds();
if ( cw != null ) {
float[] w = new float[ cw.length ];
for ( int k=0; k<w.length; k++ ) {
w[k] = (float)((float)cw[k]/(float)100);
}
table.setWidths( w );
}
Iterator<DocElement> itRow = docTable.docElements();
while ( itRow.hasNext() ) {
DocRow docRow = (DocRow)itRow.next();
Iterator<DocElement> itCell = docRow.docElements();
while ( itCell.hasNext() ) {
DocCell docCell = (DocCell)itCell.next();
startHeader = handleCell(table, docCell, startHeader, docTable, docHelper);
}
}

return table;
}


}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.fugerit.java.doc.mod.openpdf;
package org.fugerit.java.doc.mod.openpdf.helpers;

import java.util.Properties;

import com.lowagie.text.pdf.PdfWriter;

public class ITextHelper {
public class OpenPdfHelper {

public ITextHelper() {
public OpenPdfHelper() {
this.params = new Properties();
}

Expand Down
Loading

0 comments on commit 15ef6b7

Please sign in to comment.