Skip to content

Commit

Permalink
Fix to code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Sep 6, 2023
1 parent 2e649a1 commit 2c5d551
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public CellParent( Cell cell ) {
/* (non-Javadoc)
* @see org.fugerit.java.doc.mod.itext.ParentElement#add(com.lowagie.text.Element)
*/
public void add(Element element) throws Exception {
public void add(Element element) {
this.cell.addElement( element );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public DocumentParent( Document document) {
/* (non-Javadoc)
* @see org.fugerit.java.doc.mod.itext.ParentElement#add(com.lowagie.text.Element)
*/
public void add(Element element) throws Exception {
public void add(Element element) {
this.document.add( element );
}

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

import java.awt.Color;
import java.io.IOException;
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;
Expand All @@ -20,13 +20,17 @@

import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.DocumentException;
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;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class OpenPdfDocTableHelper {

private OpenPdfDocTableHelper() {}
Expand Down Expand Up @@ -84,7 +88,7 @@ private static void handleAligns( DocCell docCell, Cell cell ) {
}
}

private static List<Font> handleContent( Table table, CellParent cellParent, DocCell docCell, Cell cell, OpenPdfHelper docHelper ) throws Exception {
private static List<Font> handleContent( Table table, CellParent cellParent, DocCell docCell, Cell cell, OpenPdfHelper docHelper ) throws DocumentException, IOException {
List<Font> fontList = new ArrayList<>();
Iterator<DocElement> itCurrent = docCell.docElements();
while ( itCurrent.hasNext() ) {
Expand All @@ -96,23 +100,20 @@ private static List<Font> handleContent( Table table, CellParent cellParent, Doc
cellParent.add( paragraph );
} else if ( docElement instanceof DocPhrase ) {
DocPhrase docPhrase = (DocPhrase)docElement;
//setStyle( docCell , docPara );
log.trace( "docCell -> {}, docPara : {}", docCell, cell );
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 {
private static boolean handleCell( Table table, DocCell docCell, boolean startHeader, DocTable docTable, OpenPdfHelper docHelper ) throws DocumentException, IOException {
OpenPpfDocHandler.setStyle( docTable, docCell );
Cell cell = new Cell();
if ( docCell.isHeader() ) {
Expand Down Expand Up @@ -144,7 +145,7 @@ private static boolean handleCell( Table table, DocCell docCell, boolean startHe
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 );
Font f = fontList.get( k );
c.setFont( f );
}
}
Expand All @@ -154,7 +155,7 @@ private static boolean handleCell( Table table, DocCell docCell, boolean startHe
return startHeader;
}

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

boolean startHeader = false;
Table table = new Table( docTable.getColumns() );
Expand All @@ -176,7 +177,7 @@ protected static Table createTable( DocTable docTable, OpenPdfHelper docHelper )
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);
w[k] = ((float)cw[k]/(float)100);
}
table.setWidths( w );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static int handleFontStyle( int style, int fontStyle ) {
return style;
}

protected static Font createFont( String fontName, String fontPath, int fontSize, int fontStyle, OpenPdfHelper docHelper, String color ) throws Exception {
protected static Font createFont( String fontName, String fontPath, int fontSize, int fontStyle, OpenPdfHelper docHelper, String color ) throws DocumentException, IOException {
Font font = null;
int size = fontSize;
int style = Font.NORMAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -30,8 +31,10 @@
import org.fugerit.java.doc.base.xml.DocModelUtils;

import com.lowagie.text.Anchor;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Header;
Expand All @@ -53,6 +56,9 @@
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class OpenPpfDocHandler {

private static final ParamFinder PARAM_FINDER = ParamFinder.newFinder();
Expand All @@ -65,7 +71,7 @@ public class OpenPpfDocHandler {

private static HashMap<String, BaseFont> fonts = new HashMap<>();

public static void registerFont( String name, String path ) throws Exception {
public static void registerFont( String name, String path ) throws DocumentException, IOException {
BaseFont font = BaseFont.createFont( path, BaseFont.CP1252, true );
registerFont( name, font );
}
Expand All @@ -75,7 +81,7 @@ public static void registerFont( String name, BaseFont font ) {
}

public static BaseFont findFont( String name ) {
BaseFont res = (BaseFont)fonts.get( name );
BaseFont res = fonts.get( name );
return res;
}

Expand All @@ -90,29 +96,27 @@ protected static void setStyle( DocStyle parent, DocStyle current ) {

private PdfWriter pdfWriter;

//private RtfWriter2 rtfWriter2;

private Document document;

private String docType;

public final static String MODULE = "itext";
public static final String MODULE = "itext";

public final static String DOC_OUTPUT_HTML = DocConfig.TYPE_HTML;
public static final String DOC_OUTPUT_HTML = DocConfig.TYPE_HTML;

public final static String DOC_OUTPUT_PDF = DocConfig.TYPE_PDF;
public static final String DOC_OUTPUT_PDF = DocConfig.TYPE_PDF;

public final static String DOC_OUTPUT_RTF = DocConfig.TYPE_RTF;
public static final String DOC_OUTPUT_RTF = DocConfig.TYPE_RTF;

public final static String DOC_DEFAULT_FONT_NAME = "default-font-name";
public final static String DOC_DEFAULT_FONT_SIZE = "default-font-size";
public final static String DOC_DEFAULT_FONT_STYLE = "default-font-style";
public static final String DOC_DEFAULT_FONT_NAME = "default-font-name";
public static final String DOC_DEFAULT_FONT_SIZE = "default-font-size";
public static final String DOC_DEFAULT_FONT_STYLE = "default-font-style";

private int totalPageCount;

public OpenPpfDocHandler( Document document, RtfWriter2 rtfWriter2 ) {
this( document, DOC_OUTPUT_RTF );
//this.rtfWriter2 = rtfWriter2;
log.trace( "currently unused parameter rtfWriter2 {}", rtfWriter2 );
}

public OpenPpfDocHandler( Document document, PdfWriter pdfWriter ) {
Expand Down Expand Up @@ -145,18 +149,18 @@ private static int getAlign( int align ) {
}


protected static Image createImage( DocImage docImage ) throws Exception {
protected static Image createImage( DocImage docImage ) {
Image image = null;
String url = docImage.getUrl();
log.trace( "currently unsupported image param url {}", url );
try {
byte[] data = SourceResolverHelper.resolveImage( docImage );
image = Image.getInstance( data );
if ( docImage.getScaling() != null ) {
image.scalePercent( docImage.getScaling().floatValue() );
}
} catch (Exception e) {
LogFacade.getLog().error( "ITextDocHandler.createImage() Error loading image url : "+url, e );
throw e;
throw new ConfigRuntimeException( e );
}
return image;
}
Expand All @@ -166,16 +170,15 @@ public static String createText( Properties params, String text ) {
return PARAM_FINDER.substitute( text , params );
}

protected static Chunk createChunk( DocPhrase docPhrase, OpenPdfHelper docHelper ) throws Exception {
protected static Chunk createChunk( DocPhrase docPhrase, OpenPdfHelper docHelper ) throws DocumentException, IOException {
String text = createText( docHelper.getParams(), docPhrase.getText() );
int style = docPhrase.getStyle();
String fontName = docPhrase.getFontName();
Font f = createFont(fontName, docPhrase.getSize(), style, docHelper, docPhrase.getForeColor() );
Chunk p = new Chunk( text, f );
return p;
return new Chunk( text, f );
}

protected static Phrase createPhrase( DocPhrase docPhrase, OpenPdfHelper docHelper, List<Font> fontMap ) throws Exception {
protected static Phrase createPhrase( DocPhrase docPhrase, OpenPdfHelper docHelper, List<Font> fontMap ) throws DocumentException, IOException {
String text = createText( docHelper.getParams(), docPhrase.getText() );
int style = docPhrase.getStyle();
String fontName = docPhrase.getFontName();
Expand All @@ -198,26 +201,17 @@ protected static Phrase createPhrase( DocPhrase docPhrase, OpenPdfHelper docHelp
return p;
}

protected static Phrase createPhrase( DocPhrase docPhrase, OpenPdfHelper docHelper ) throws Exception {
protected static Phrase createPhrase( DocPhrase docPhrase, OpenPdfHelper docHelper ) throws DocumentException, IOException {
return createPhrase(docPhrase, docHelper, null);
}

protected static Paragraph createPara( DocPara docPara, OpenPdfHelper docHelper ) throws Exception {
protected static Paragraph createPara( DocPara docPara, OpenPdfHelper docHelper ) throws DocumentException, IOException {
return createPara(docPara, docHelper, null);
}

protected static Paragraph createPara( DocPara docPara, OpenPdfHelper docHelper, List<Font> fontMap ) throws Exception {
protected static Paragraph createPara( DocPara docPara, OpenPdfHelper docHelper, List<Font> fontMap ) throws DocumentException, IOException {
int style = docPara.getStyle();
String text = createText( docHelper.getParams(), docPara.getText() );
// if ( DOC_OUTPUT_HTML.equals( this.docType ) ) {
// int count = 0;
// StringBuffer buffer = new StringBuffer();
// while ( count < text.length() && text.indexOf( " " )==count ) {
// count++;
// }
// buffer.append( text.substring( count ) );
// text = buffer.toString();
// }
String fontName = docPara.getFontName();
Font f = createFont(fontName, docPara.getSize(), style, docHelper, docPara.getForeColor() );
Phrase phrase = new Phrase( text, f );
Expand All @@ -226,7 +220,6 @@ protected static Paragraph createPara( DocPara docPara, OpenPdfHelper docHelper,
Color c = DocModelUtils.parseHtmlColor( docPara.getForeColor() );
Font f1 = new Font( f.getFamily(), f.getSize(), f.getStyle(), c );
p = new Paragraph( new Phrase( text, f1 ) );
//f = f1;
}
if ( docPara.getAlign() != DocPara.ALIGN_UNSET ) {
p.setAlignment( getAlign( docPara.getAlign() ) );
Expand All @@ -248,7 +241,8 @@ protected static Paragraph createPara( DocPara docPara, OpenPdfHelper docHelper,
return p;
}

protected static Image createBarcode( DocBarcode docBarcode, OpenPdfHelper helper ) throws Exception {
protected static Image createBarcode( DocBarcode docBarcode, OpenPdfHelper helper ) throws BadElementException, IOException {
log.trace( "currently unused parameter helper : {}", helper );
Barcode barcode = null;
if ( "128".equalsIgnoreCase( docBarcode.getType() ) ) {
barcode = new Barcode128();
Expand All @@ -261,19 +255,18 @@ protected static Image createBarcode( DocBarcode docBarcode, OpenPdfHelper helpe
barcode.setCode( docBarcode.getText() );
barcode.setAltText( docBarcode.getText() );
java.awt.Image awtImage = barcode.createAwtImage( Color.white, Color.black );
Image img = Image.getInstance( awtImage, null );
return img;
return Image.getInstance( awtImage, null );
}

private static RtfHeaderFooter createRtfHeaderFooter( DocHeaderFooter docHeaderFooter, Document document, boolean header, OpenPdfHelper docHelper ) throws Exception {
private static RtfHeaderFooter createRtfHeaderFooter( DocHeaderFooter docHeaderFooter, Document document, boolean header, OpenPdfHelper docHelper ) throws DocumentException, IOException {
List<DocElement> list = new ArrayList<>();
Iterator<DocElement> itDoc = docHeaderFooter.docElements();
while ( itDoc.hasNext() ) {
list.add( itDoc.next() );
}
Element[] e = new Element[ list.size() ];
for ( int k=0; k<list.size(); k++ ) {
e[k] = (Element)getElement( document, (DocElement)list.get( k ) , false, docHelper );
e[k] = getElement( document, list.get( k ) , false, docHelper );
}
RtfHeaderFooter rtfHeaderFooter = new RtfHeaderFooter( e );
rtfHeaderFooter.setDisplayAt( RtfHeaderFooter.DISPLAY_ALL_PAGES );
Expand All @@ -285,7 +278,7 @@ private static RtfHeaderFooter createRtfHeaderFooter( DocHeaderFooter docHeaderF
return rtfHeaderFooter;
}

public static Font createFont( String fontName, int fontSize, int fontStyle, OpenPdfHelper docHelper, String color ) throws Exception {
public static Font createFont( String fontName, int fontSize, int fontStyle, OpenPdfHelper docHelper, String color ) throws DocumentException, IOException {
return OpenPdfFontHelper.createFont(fontName, fontName, fontSize, fontStyle, docHelper, color);
}

Expand All @@ -301,13 +294,10 @@ private void handleTypeSpecific( Properties info ) {
if ( DOC_OUTPUT_PDF.equalsIgnoreCase( this.docType ) || DOC_OUTPUT_RTF.equalsIgnoreCase( this.docType ) ) {
Rectangle size = this.document.getPageSize();
String pageOrient = info.getProperty( DocInfo.INFO_NAME_PAGE_ORIENT );
if ( pageOrient != null ) {
if ( "horizontal".equalsIgnoreCase( pageOrient ) ) {
size = new Rectangle( size.getHeight(), size.getWidth() );
this.document.setPageSize( size );
}
if ( "horizontal".equalsIgnoreCase( pageOrient ) ) {
size = new Rectangle( size.getHeight(), size.getWidth() );
this.document.setPageSize( size );
}

if ( DOC_OUTPUT_PDF.equalsIgnoreCase( this.docType ) ) {
String pdfFormat = info.getProperty( DocInfo.INFO_NAME_PDF_FORMAT );
if ( "pdf-a".equalsIgnoreCase( pdfFormat ) ) {
Expand All @@ -316,8 +306,6 @@ private void handleTypeSpecific( Properties info ) {
outi.put( PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("sRGB IEC61966-2.1") );
outi.put( PdfName.INFO, new PdfString("sRGB IEC61966-2.1") );
outi.put( PdfName.S, PdfName.GTS_PDFA1 );
// FontFactory.
// BaseFont bf = BaseFont.createFont( Font.HELVETICA, BaseFont.WINANSI, true );
this.pdfWriter.getExtraCatalog().put( PdfName.OUTPUTINTENTS, new PdfArray( outi ) );
}

Expand Down Expand Up @@ -407,14 +395,14 @@ public static void handleElementsSafe( Document document, Iterator<DocElement> i
}
}

public static void handleElements( Document document, Iterator<DocElement> itDoc, OpenPdfHelper docHelper ) throws Exception {
public static void handleElements( Document document, Iterator<DocElement> itDoc, OpenPdfHelper docHelper ) throws DocumentException, IOException {
while ( itDoc.hasNext() ) {
DocElement docElement = (DocElement)itDoc.next();
DocElement docElement = itDoc.next();
getElement(document, docElement, true, docHelper );
}
}

public static Element getElement( Document document, DocElement docElement, boolean addElement, OpenPdfHelper docHelper ) throws Exception {
public static Element getElement( Document document, DocElement docElement, boolean addElement, OpenPdfHelper docHelper ) throws DocumentException, IOException {
Element result = null;
DocumentParent documentParent = new DocumentParent( document );
if ( docElement instanceof DocPhrase ) {
Expand Down Expand Up @@ -443,7 +431,7 @@ public static Element getElement( Document document, DocElement docElement, bool
return result;
}

private void handleHeaderFooterElement( DocElement docElement, float leading, OpenPdfHelper docHelper , Phrase phrase ) throws Exception {
private void handleHeaderFooterElement( DocElement docElement, float leading, OpenPdfHelper docHelper , Phrase phrase ) throws DocumentException, IOException {
if ( docElement instanceof DocPhrase ) {
DocPhrase docPhrase = (DocPhrase) docElement;
Chunk ck = createChunk( docPhrase, docHelper );
Expand Down Expand Up @@ -475,7 +463,7 @@ private void handleHeaderFooterElement( DocElement docElement, float leading, Op
}
}

private HeaderFooter createHeaderFooter( DocHeaderFooter container, int align, OpenPdfHelper docHelper ) throws Exception {
private HeaderFooter createHeaderFooter( DocHeaderFooter container, int align, OpenPdfHelper docHelper ) throws DocumentException, IOException {
Iterator<DocElement> it = container.docElements();
Phrase phrase = new Phrase();
float leading = (float)-1.0;
Expand All @@ -489,7 +477,6 @@ private HeaderFooter createHeaderFooter( DocHeaderFooter container, int align, O
}
headerFooter.setAlignment( getAlign( align ) );
headerFooter.setBorder( container.getBorderWidth() );
//headerFooter.setUseVariableBorders( true );
return headerFooter;
}

Expand Down
Loading

0 comments on commit 2c5d551

Please sign in to comment.