Skip to content

Commit

Permalink
Basic list implementation (#268)
Browse files Browse the repository at this point in the history
* Basic list implementation

* Fixed test for new implementation

* Fix sonar issues

* Code coverage

* basic list implementation

info suppress-wrong-type-error, some type error will be ignored if set to true and '1'
  • Loading branch information
fugerit79 authored Dec 6, 2024
1 parent 14986b4 commit f32b904
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 50 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- [fj-mod-doc-openpdf-ext] basic list implementation
- info suppress-wrong-type-error, some type error will be ignored if set to true and '1'

### Changed

- quarkus-version set to 3.17.2 across al the modules
Expand Down
18 changes: 18 additions & 0 deletions docs/html/doc_meta_info.html
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,24 @@ <h2 style="font-weight: bold;">Properties for generic metadata</h2>
<td id="cell_12_4" style=" width: 5%; border-top: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 2px;">
<p>1.4.5</p>
</td>
</tr>
<tr>
<td id="cell_13_0" style=" width: 20%; border-top: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 2px;">
<span id="suppress-wrong-type-error"></span>
<p style="font-style: italic;">suppress-wrong-type-error</p>
</td>
<td id="cell_13_1" style=" width: 40%; border-top: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 2px;">
<p>If set to 'a' or 'true' it will set suppress some type error.</p>
</td>
<td id="cell_13_2" style=" width: 25%; border-top: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 2px;">
<p>fj-doc-mod-openpdf-ext(pdf), fj-doc-mod-openrtf-ext(rtf), </p>
</td>
<td id="cell_13_3" style=" width: 10%; border-top: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 2px;">
<p>false</p>
</td>
<td id="cell_13_4" style=" width: 5%; border-top: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 2px;">
<p>8.11.5</p>
</td>
</tr>
</tbody>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ private GenericConsts() {}
* <a href="https://venusdocs.fugerit.org/docs/html/doc_meta_info.html#default-font-style">See 'default-font-style' documentation.</a>
*/
public static final String DOC_DEFAULT_FONT_STYLE = "default-font-style";

/**
* <a href="https://venusdocs.fugerit.org/docs/html/doc_meta_info.html#suppress-wrong-type-error">See 'suppress-wrong-type-error' documentation.</a>
*/
public static final String DOC_SUPPRESS_WRONG_TYPE_ERROR = "suppress-wrong-type-error";


}
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@
<admeta:supportedHandler>fj-doc-mod-openpdf-ext(pdf)</admeta:supportedHandler>
<admeta:supportedHandler>fj-doc-mod-openrtf-ext(rtf)</admeta:supportedHandler>
</admeta:admMetaInfo>
<admeta:admMetaInfo>
<admeta:name>suppress-wrong-type-error</admeta:name>
<admeta:description>If set to 'a' or 'true' it will set suppress some type error.</admeta:description>
<admeta:defaultValue>false</admeta:defaultValue>
<admeta:since>8.11.5</admeta:since>
<admeta:supportedType>pdf</admeta:supportedType>
<admeta:supportedType>rtf</admeta:supportedType>
<admeta:supportedHandler>fj-doc-mod-openpdf-ext(pdf)</admeta:supportedHandler>
<admeta:supportedHandler>fj-doc-mod-openrtf-ext(rtf)</admeta:supportedHandler>
</admeta:admMetaInfo>
</admeta:admSection>

<admeta:admSection>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package org.fugerit.java.doc.mod.openpdf.ext.helpers;

import java.io.IOException;
import java.util.Properties;

import com.lowagie.text.pdf.PdfWriter;
import lombok.extern.slf4j.Slf4j;
import org.fugerit.java.core.lang.helpers.BooleanUtils;
import org.fugerit.java.doc.base.typehelper.generic.GenericConsts;

@Slf4j
public class OpenPdfHelper {

public OpenPdfHelper() {
Expand Down Expand Up @@ -59,5 +64,13 @@ public PdfWriter getPdfWriter() {
public void setPdfWriter(PdfWriter pdfWriter) {
this.pdfWriter = pdfWriter;
}

public void handelError( String message ) throws IOException {
if (BooleanUtils.isTrue( this.getParams().getProperty(GenericConsts.DOC_SUPPRESS_WRONG_TYPE_ERROR ) ) ) {
log.warn( "Suppressed type error : {}", message );
} else {
throw new IOException(message);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,16 @@
import java.util.List;
import java.util.Properties;

import org.fugerit.java.core.cfg.ConfigRuntimeException;
import com.lowagie.text.*;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.core.util.regex.ParamFinder;
import org.fugerit.java.doc.base.config.DocConfig;
import org.fugerit.java.doc.base.helper.SourceResolverHelper;
import org.fugerit.java.doc.base.model.DocBarcode;
import org.fugerit.java.doc.base.model.DocBase;
import org.fugerit.java.doc.base.model.DocElement;
import org.fugerit.java.doc.base.model.DocFooter;
import org.fugerit.java.doc.base.model.DocHeader;
import org.fugerit.java.doc.base.model.DocHeaderFooter;
import org.fugerit.java.doc.base.model.DocImage;
import org.fugerit.java.doc.base.model.DocInfo;
import org.fugerit.java.doc.base.model.DocPageBreak;
import org.fugerit.java.doc.base.model.DocPara;
import org.fugerit.java.doc.base.model.DocPhrase;
import org.fugerit.java.doc.base.model.DocStyle;
import org.fugerit.java.doc.base.model.DocTable;
import org.fugerit.java.doc.base.model.*;
import org.fugerit.java.doc.base.typehelper.generic.GenericConsts;
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;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.html.HtmlTags;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.Barcode128;
Expand Down Expand Up @@ -277,12 +252,12 @@ private void handleTypeSpecific( Properties info ) {
}

protected void handleHeaderExt( DocHeader docHeader, PdfHelper pdfHelper, OpenPdfHelper docHelper ) throws DocumentException {
log.trace( "docHelper : {}", docHelper );
log.trace( "handleHeaderExt docHelper : {}", docHelper );
pdfHelper.setDocHeader( docHeader );
}

protected void handleFooterExt( DocFooter docFooter, PdfHelper pdfHelper, OpenPdfHelper docHelper ) throws DocumentException {
log.trace( "docHelper : {}", docHelper );
log.trace( "handleFooterExt docHelper : {}", docHelper );
pdfHelper.setDocFooter( docFooter );
}

Expand Down Expand Up @@ -352,11 +327,7 @@ public void handleDoc(DocBase docBase) throws DocumentException, IOException {
}

public static void handleElementsSafe( Document document, Iterator<DocElement> itDoc, OpenPdfHelper docHelper ) {
try {
handleElements(document, itDoc, docHelper);
} catch (Exception e) {
throw new ConfigRuntimeException( e );
}
SafeFunction.apply( () -> handleElements(document, itDoc, docHelper) );
}

public static void handleElements( Document document, Iterator<DocElement> itDoc, OpenPdfHelper docHelper ) throws DocumentException, IOException {
Expand All @@ -365,35 +336,53 @@ public static void handleElements( Document document, Iterator<DocElement> itDoc
getElement(document, docElement, true, docHelper );
}
}


public static void checkAddElement( DocumentParent documentParent, boolean addElement, Element result ) {
if ( addElement ) {
documentParent.add( result );
}
}

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 ) {
result = createPhrase( (DocPhrase)docElement, docHelper );
if ( addElement ) {
documentParent.add( result );
}
checkAddElement( documentParent, addElement, result );
} else if ( docElement instanceof DocPara ) {
result = createPara( (DocPara)docElement, docHelper );
if ( addElement ) {
documentParent.add( result );
}
checkAddElement( documentParent, addElement, result );
} else if ( docElement instanceof DocTable ) {
result = OpenPdfDocTableHelper.createTable( (DocTable)docElement, docHelper );
if ( addElement ) {
document.add( result );
}
checkAddElement( documentParent, addElement, result );
} else if ( docElement instanceof DocImage ) {
result = createImage( (DocImage)docElement );
if ( addElement ) {
documentParent.add( result );
}
checkAddElement( documentParent, addElement, result );
} else if ( docElement instanceof DocList) {
result = createList( (DocList)docElement, docHelper );
checkAddElement( documentParent, addElement, result );
} else if ( docElement instanceof DocPageBreak ) {
document.newPage();
}
return result;
}

private static com.lowagie.text.List createList( DocList docList, OpenPdfHelper docHelper ) throws IOException {
com.lowagie.text.List list = new com.lowagie.text.List( docList.isOrdered() );
for ( DocElement element : docList.getElementList() ) {
if ( element instanceof DocLi ) {
DocLi li = (DocLi) element;
if ( li.getContent() instanceof DocPara ) {
list.add( new ListItem( createPara( (DocPara)li.getContent(), docHelper ) ) );
} else {
docHelper.handelError( String.format( "unsupported type %s", li.getContent().getClass().getSimpleName() ) );
}
} else {
docHelper.handelError( String.format( "wrong children%s", element.getClass().getSimpleName() ) );
}
}
return list;
}

private void handleHeaderFooterElement( DocElement docElement, float inputLeading, OpenPdfHelper docHelper , Phrase phrase ) throws DocumentException, IOException {
float leading = inputLeading;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
package test.org.fugerit.java.doc.mod.openpdf.ext;

import com.lowagie.text.Document;
import lombok.experimental.Helper;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.lang.helpers.BooleanUtils;
import org.fugerit.java.doc.base.model.DocLi;
import org.fugerit.java.doc.base.model.DocList;
import org.fugerit.java.doc.base.model.DocTable;
import org.fugerit.java.doc.base.typehelper.generic.GenericConsts;
import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPdfHelper;
import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPpfDocHandler;
import org.fugerit.java.doc.mod.openpdf.ext.helpers.PhraseParent;
import org.junit.Assert;
import org.junit.Test;

import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;

import java.io.IOException;

public class TestOpenPdfHelper {

@Test
Expand All @@ -15,5 +27,23 @@ public void test001() throws Exception {
parent.add( new Paragraph() );
Assert.assertNotNull( parent );
}


@Test
public void testList() {
OpenPdfHelper helper = new OpenPdfHelper();
try ( Document document = new Document() ) {
DocList list1 = new DocList();
list1.addElement( new DocTable() );
list1.setListType(DocList.LIST_TYPE_OL);
Assert.assertThrows( IOException.class, () -> OpenPpfDocHandler.getElement( document, list1, false, helper ) );
DocList list2 = new DocList();
list2.setListType(DocList.LIST_TYPE_UL);
DocLi li2 = new DocLi();
li2.addElement( new DocTable() );
list2.addElement( li2 );
helper.getParams().setProperty(GenericConsts.DOC_SUPPRESS_WRONG_TYPE_ERROR, BooleanUtils.BOOLEAN_1 );
SafeFunction.apply( () -> OpenPpfDocHandler.getElement( document, list2, false, helper ) );
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void testSetGet() {

@Test
public void testTandleElementSafe() {
Assert.assertThrows( ConfigRuntimeException.class , () -> OpenPpfDocHandler.handleElementsSafe(null, null, null) );
Assert.assertThrows( NullPointerException.class , () -> OpenPpfDocHandler.handleElementsSafe(null, null, null) );
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@
<para font-name="courier" align="justifyall" style="bolditalic">Courier</para>
<para space-before="10" font-name="symbol" align="right" style="italic">Symbol</para>
<para leading="10" font-name="helvetica" align="center" style="underline">Symbol</para>
<para size="-1" align="left" fore-color="#dddddd">Test default font</para>
<para size="-1" align="left" fore-color="#dddddd">Test default font 1</para>
<list>
<li><para>list a</para></li>
<li><para>list b</para></li>
<li><para>list c</para></li>
</list>
<table columns="3" colwidths="30;30;40" width="100" id="excel-table" padding="2" space-before="10" space-after="10">
<row>
<cell align="center" border-color="#000000" border-width="1"><para style="bold">Name</para></cell>
Expand Down

0 comments on commit f32b904

Please sign in to comment.