Skip to content

Commit

Permalink
Fix to hedaer footer handling cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Aug 20, 2023
1 parent 80711f5 commit acbd40c
Showing 1 changed file with 69 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,37 +288,14 @@ public static Font createFont( String fontName, int fontSize, int fontStyle, Ope
return OpenPdfFontHelper.createFont(fontName, fontName, fontSize, fontStyle, docHelper, color);
}

/* (non-Javadoc)
* @see org.fugerit.java.doc.base.DocHandler#handleDoc(org.fugerit.java.doc.base.DocBase)
*/
public void handleDoc(DocBase docBase) throws Exception {
Properties info = docBase.getInfo();

String defaultFontName = info.getProperty( DOC_DEFAULT_FONT_NAME, "helvetica" );
String defaultFontSize = info.getProperty( DOC_DEFAULT_FONT_SIZE, "10" );
String defaultFontStyle = info.getProperty( DOC_DEFAULT_FONT_STYLE, "normal" );
OpenPdfHelper docHelper = new OpenPdfHelper();

if ( this.pdfWriter != null ) {
docHelper.setPdfWriter( this.pdfWriter );
}

docHelper.setDefFontName( defaultFontName );
docHelper.setDefFontStyle( defaultFontStyle );
docHelper.setDefFontSize( defaultFontSize );

if ( this.totalPageCount != -1 ) {
docHelper.getParams().setProperty( PARAM_PAGE_TOTAL , String.valueOf( this.totalPageCount ) );
}

private void handleTypeSpecific( Properties info ) {
// per documenti tipo HTML
if ( DOC_OUTPUT_HTML.equalsIgnoreCase( this.docType ) ) {
String cssLink = info.getProperty( DocInfo.INFO_NAME_CSS_LINK );
if ( cssLink != null ) {
this.document.add( new Header( HtmlTags.STYLESHEET, cssLink ) );
}
}

// per documenti tipo word o pdf
if ( DOC_OUTPUT_PDF.equalsIgnoreCase( this.docType ) || DOC_OUTPUT_RTF.equalsIgnoreCase( this.docType ) ) {
Rectangle size = this.document.getPageSize();
Expand All @@ -331,8 +308,6 @@ public void handleDoc(DocBase docBase) throws Exception {
}

if ( DOC_OUTPUT_PDF.equalsIgnoreCase( this.docType ) ) {


String pdfFormat = info.getProperty( DocInfo.INFO_NAME_PDF_FORMAT );
if ( "pdf-a".equalsIgnoreCase( pdfFormat ) ) {
this.pdfWriter.setPDFXConformance(PdfWriter.PDFA1B);
Expand All @@ -346,14 +321,10 @@ public void handleDoc(DocBase docBase) throws Exception {
}

}




}
// header / footer section
PdfHelper pdfHelper = new PdfHelper( docHelper );
}

private void handleHeaderFooter( DocBase docBase, PdfHelper pdfHelper, OpenPdfHelper docHelper) throws Exception {
DocHeader docHeader = docBase.getDocHeader();
if ( docHeader != null && docHeader.isUseHeader() ) {
if ( docHeader.isBasic() ) {
Expand All @@ -380,7 +351,37 @@ public void handleDoc(DocBase docBase) throws Exception {
}
}

}
}
}

/* (non-Javadoc)
* @see org.fugerit.java.doc.base.DocHandler#handleDoc(org.fugerit.java.doc.base.DocBase)
*/
public void handleDoc(DocBase docBase) throws Exception {
Properties info = docBase.getInfo();

String defaultFontName = info.getProperty( DOC_DEFAULT_FONT_NAME, "helvetica" );
String defaultFontSize = info.getProperty( DOC_DEFAULT_FONT_SIZE, "10" );
String defaultFontStyle = info.getProperty( DOC_DEFAULT_FONT_STYLE, "normal" );
OpenPdfHelper docHelper = new OpenPdfHelper();

if ( this.pdfWriter != null ) {
docHelper.setPdfWriter( this.pdfWriter );
}

docHelper.setDefFontName( defaultFontName );
docHelper.setDefFontStyle( defaultFontStyle );
docHelper.setDefFontSize( defaultFontSize );

if ( this.totalPageCount != -1 ) {
docHelper.getParams().setProperty( PARAM_PAGE_TOTAL , String.valueOf( this.totalPageCount ) );
}

this.handleTypeSpecific(info);

// header / footer section
PdfHelper pdfHelper = new PdfHelper( docHelper );
this.handleHeaderFooter(docBase, pdfHelper, docHelper);
if ( DOC_OUTPUT_PDF.equals( this.docType ) ) {
this.pdfWriter.setPageEvent( pdfHelper );
}
Expand Down Expand Up @@ -429,44 +430,47 @@ public static Element getElement( Document document, DocElement docElement, bool
return result;
}

private void handleHeaderFooterElement( DocElement docElement, float leading, OpenPdfHelper docHelper , Phrase phrase ) throws Exception {
if ( docElement instanceof DocPhrase ) {
DocPhrase docPhrase = (DocPhrase) docElement;
Chunk ck = createChunk( docPhrase, docHelper );
if( docPhrase.getLeading() != null && docPhrase.getLeading().floatValue() != leading ) {
leading = docPhrase.getLeading().floatValue();
phrase.setLeading( leading );
}
phrase.add( ck );
} else if ( docElement instanceof DocPara ) {
DocPara docPara = (DocPara) docElement;
if ( docPara.getLeading() != null ) {
phrase.setLeading( docPara.getLeading().floatValue() );
}
Font f = new Font( Font.HELVETICA, docPara.getSize() );
if ( docPara.getForeColor() != null ) {
try {
f.setColor( DocModelUtils.parseHtmlColor( docPara.getForeColor() ) );
} catch (Exception fe) {
LogFacade.getLog().warn( "Error setting fore color on footer : "+docPara.getForeColor(), fe );
}
}
Chunk ck = new Chunk( docPara.getText(), f );
phrase.add( ck );
} else if ( docElement instanceof DocImage ) {
DocImage docImage = (DocImage)docElement;
Image img = createImage( docImage );
Chunk ck = new Chunk( img, 0, 0, true );
phrase.add( ck );
}
}

private HeaderFooter createHeaderFooter( DocHeaderFooter container, int align, OpenPdfHelper docHelper ) throws Exception {
Iterator<DocElement> it = container.docElements();
Phrase phrase = new Phrase();
float leading = (float)-1.0;
while ( it.hasNext() ) {
DocElement docElement = (DocElement)it.next();
if ( docElement instanceof DocPhrase ) {
DocPhrase docPhrase = (DocPhrase) docElement;
Chunk ck = createChunk( docPhrase, docHelper );
if( docPhrase.getLeading() != null && docPhrase.getLeading().floatValue() != leading ) {
leading = docPhrase.getLeading().floatValue();
phrase.setLeading( leading );
}
phrase.add( ck );
} else if ( docElement instanceof DocPara ) {
DocPara docPara = (DocPara) docElement;
if ( docPara.getLeading() != null ) {
phrase.setLeading( docPara.getLeading().floatValue() );
}
Font f = new Font( Font.HELVETICA, docPara.getSize() );
if ( docPara.getForeColor() != null ) {
try {
f.setColor( DocModelUtils.parseHtmlColor( docPara.getForeColor() ) );
} catch (Exception fe) {
LogFacade.getLog().warn( "Error setting fore color on footer : "+docPara.getForeColor(), fe );
}
}
Chunk ck = new Chunk( docPara.getText(), f );
phrase.add( ck );
} else if ( docElement instanceof DocImage ) {
DocImage docImage = (DocImage)docElement;
Image img = createImage( docImage );
Chunk ck = new Chunk( img, 0, 0, true );
phrase.add( ck );
}
DocElement docElement = it.next();
this.handleHeaderFooterElement(docElement, leading, docHelper, phrase);
}
HeaderFooter headerFooter = new HeaderFooter( phrase, container.isNumbered() );

if ( align == DocPara.ALIGN_UNSET ) {
align = DocPara.ALIGN_CENTER;
}
Expand Down

0 comments on commit acbd40c

Please sign in to comment.