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 af53d68
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 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 Down Expand Up @@ -65,7 +68,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 +93,6 @@ protected static void setStyle( DocStyle parent, DocStyle current ) {

private PdfWriter pdfWriter;

//private RtfWriter2 rtfWriter2;

private Document document;

private String docType;
Expand Down Expand Up @@ -145,18 +146,17 @@ 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();
//String url = docImage.getUrl();
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 @@ -175,7 +175,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,15 +198,15 @@ 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 ) ) {
Expand Down Expand Up @@ -248,7 +248,7 @@ 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 {
Barcode barcode = null;
if ( "128".equalsIgnoreCase( docBarcode.getType() ) ) {
barcode = new Barcode128();
Expand Down Expand Up @@ -285,7 +285,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 Down Expand Up @@ -407,14 +407,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

0 comments on commit af53d68

Please sign in to comment.