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 64c4eab
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 55 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 @@ -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 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 @@ -90,8 +96,6 @@ protected static void setStyle( DocStyle parent, DocStyle current ) {

private PdfWriter pdfWriter;

//private RtfWriter2 rtfWriter2;

private Document document;

private String docType;
Expand All @@ -112,7 +116,7 @@ protected static void setStyle( DocStyle parent, DocStyle current ) {

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,7 +170,7 @@ 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();
Expand All @@ -175,7 +179,7 @@ protected static Chunk createChunk( DocPhrase docPhrase, OpenPdfHelper docHelper
return p;
}

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 +202,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 +221,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 +242,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 @@ -265,7 +260,7 @@ protected static Image createBarcode( DocBarcode docBarcode, OpenPdfHelper helpe
return img;
}

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() ) {
Expand All @@ -285,7 +280,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 +296,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 +308,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 +397,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();
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 +433,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 @@ -489,7 +479,6 @@ private HeaderFooter createHeaderFooter( DocHeaderFooter container, int align, O
}
headerFooter.setAlignment( getAlign( align ) );
headerFooter.setBorder( container.getBorderWidth() );
//headerFooter.setUseVariableBorders( true );
return headerFooter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public PdfHelper( OpenPdfHelper docHelper ) {

private DocFooter docFooter;

@Override
public void onStartPage(PdfWriter writer, Document document) {
this.currentPageNumber = writer.getPageNumber();
this.docHelper.getParams().setProperty( OpenPpfDocHandler.PARAM_PAGE_CURRENT , String.valueOf( writer.getPageNumber() ) );
Expand All @@ -52,12 +53,14 @@ public void onStartPage(PdfWriter writer, Document document) {
}
}

@Override
public void onOpenDocument(PdfWriter writer, Document document) {
totalPages = writer.getDirectContent().createTemplate(100, 100);
totalPages.setBoundingBox( new Rectangle(-20, -20, 100, 100) );
this.baseFont = OpenPdfFontHelper.createBaseFontSafe(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
}

@Override
public void onEndPage(PdfWriter writer, Document document) {
if ( this.getDocFooter() != null && !this.getDocFooter().isBasic() ) {
// allocate direct writer
Expand All @@ -78,17 +81,14 @@ public void onEndPage(PdfWriter writer, Document document) {
int rowOffset = 10;
if( para.getAlign() == DocPara.ALIGN_CENTER ) {
cb.setTextMatrix((document.right() / 2), textBase);
cb.showText(text);
// cb.addTemplate(totalPages, (document.right() / 2) + textSize, textBase);
cb.showText(text);
} else if( para.getAlign() == DocPara.ALIGN_LEFT ) {
cb.setTextMatrix(document.left(), textBase);
cb.showText(text);
// cb.addTemplate(totalPages, document.left() + textSize, textBase);
} else {
float adjust = baseFont.getWidthPoint("0", footerTextSize);
cb.setTextMatrix(document.right() - textSize - adjust, textBase);
cb.showText(text);
// cb.addTemplate(totalPages, document.right() - adjust, textBase);
}

totalOffset+= rowOffset;
Expand All @@ -101,9 +101,9 @@ public void onEndPage(PdfWriter writer, Document document) {
// restore writer state
cb.restoreState();
}

}

@Override
public void onCloseDocument(PdfWriter writer, Document document) {
totalPages.beginText();
totalPages.setFontAndSize(baseFont, footerTextSize);
Expand All @@ -116,8 +116,6 @@ public void setPageNumberAlignment(int pageNumberAlignment) {
this.pageNumberAlignment = pageNumberAlignment;
}



public DocHeader getDocHeader() {
return docHeader;
}
Expand Down

0 comments on commit 64c4eab

Please sign in to comment.