Skip to content

Commit

Permalink
[0.1.0] - 2023-10-14
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Oct 14, 2023
1 parent 91c626c commit 6019d90
Show file tree
Hide file tree
Showing 17 changed files with 635 additions and 25 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.1.0] - 2023-10-14

### Added

- [Project history](src/docs/common/history.md) section
- [Doc Format Summary](src/docs/common/doc_format_summary.md) entry point section
- [Doc Format and Apache FreeMarker](src/docs/common/doc_format_freemarker.md) guidelines
- [From XML to JSON or YAML source](src/docs/common/doc_from_xml_to_json_or_yaml.md) section
- [Venus Guide E001 : Hello World PDF JSON Source](fj-doc-guides-E001-hello-world-pdf-json-source/README.md)
- Started guidelines section
- [Venus Guide A001 : Hello World PDF](fj-doc-guides-A001-hello-world-pdf/README.md)
- [Venus Guide A002 : Full document](fj-doc-guides-A002-full-document/README.md)
- [Venus Guide A003 : Apache FreeMarker Full document](fj-doc-guides-A003-full-document-freemarker/README.md)
- [Venus Guide A004 : Apache FreeMarker Hello World](fj-doc-guides-A004-freemarker-hello-world/README.md)
- badges to README.md
- Added [FreeMarker](fj-doc-guides-A003-full-document-freemarker/README.md)
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@ This projects provides simple guides to [Venus - Fugerit Document Generation Fra
## Index

### General topics
* [Venus Doc Format entry point](src/docs/common/doc_format_summary.md) - a starting point for writing the Venus XML meta model
* [From XML to JSON or YAML source](src/docs/common/doc_from_xml_to_json_or_yaml.md) - informations for using json or yaml as source instead of XML
* [Project history](src/docs/common/history.md) - some history of the project and the document format

- [Venus Doc Format entry point](src/docs/common/doc_format_summary.md) - a starting point for writing the Venus XML meta model
- [Venus Doc Format and freemarker](src/docs/common/doc_format_freemarker.md) - consideration on using the XML format with [Apache FreeMarker](https://freemarker.apache.org/)
- [From XML to JSON or YAML source](src/docs/common/doc_from_xml_to_json_or_yaml.md) - informations for using json or yaml as source instead of XML
- [Project history](src/docs/common/history.md) - some history of the project and the document format

### Guidelines
* ['Framework basics'](src/docs/guidelines/G001_basics.md) - This document explains the basic of the xml source format
* ['The source XML document format'](src/docs/guidelines/G002_xml_format.md) - This document explains the basics of the xml source format

- ['Framework basics'](src/docs/guidelines/G001_basics.md) - This document explains the basic of the xml source format
- ['The source XML document format'](src/docs/guidelines/G002_xml_format.md) - This document explains the basics of the xml source format

### Core functionalities practical guides
* [Venus Guide A001 : Hello World PDF](fj-doc-guides-A001-hello-world-pdf/README.md)

- [Venus Guide A001 : Hello World PDF](fj-doc-guides-A001-hello-world-pdf/README.md)
- [Venus Guide A002 : Full document](fj-doc-guides-A002-full-document/README.md)
- [Venus Guide A003 : Apache FreeMarker Full document](fj-doc-guides-A003-full-document-freemarker/README.md)
- [Venus Guide A004 : Apache FreeMarker Hello World](fj-doc-guides-A004-freemarker-hello-world/README.md)

### Extra functionalities practical guides
* [Venus Guide E001 : Hello World PDF JSON Source](fj-doc-guides-E001-hello-world-pdf-json-source/README.md)

- [Venus Guide E001 : Hello World PDF JSON Source](fj-doc-guides-E001-hello-world-pdf-json-source/README.md)

2 changes: 1 addition & 1 deletion fj-doc-guides-A001-hello-world-pdf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.fugerit.java.guide</groupId>
<artifactId>fj-doc-guides</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.0</version>
</parent>

<artifactId>fj-doc-guides-A001-hello-world-pdf</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions fj-doc-guides-A002-full-document/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Venus Guide A002 : Full document

[Index](../README.md)

*Abstract* :
Simple guide to a use [Venus XML Doc Format](../src/docs/common/doc_format_summary.md) to generate a document with most of the features of the format.
2 changes: 1 addition & 1 deletion fj-doc-guides-A002-full-document/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.fugerit.java.guide</groupId>
<artifactId>fj-doc-guides</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.0</version>
</parent>

<artifactId>fj-doc-guides-A002-full-document</artifactId>
Expand Down
128 changes: 128 additions & 0 deletions fj-doc-guides-A003-full-document-freemarker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Venus Guide A003 : Apache FreeMarker Full document

[Index](../README.md)

*Abstract* :
Simple guide to a use [Venus XML Doc Format](../src/docs/common/doc_format_summary.md) together with [Apache FreeMarker](../src/docs/common/doc_format_freemarker.md)

*Author* : Matteo Franci a.k.a. Fugerit (fugerit79)

*Version* : 001 (2023-10-14)

*Quickstart* :
Run the junit : [src/test/java/test/org/fugerit/java/doc/guides/fulldocument/fm/facade/TestFullDocumentFmFacade.java](src/test/java/test/org/fugerit/java/doc/guides/fulldocument/fm/facade/TestFullDocumentFmFacade.java)


*Guide* :

## 1. pom setup

For dependency handling we set the parent pom to [fj-doc](https://github.com/fugerit-org/fj-doc)

```
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc</artifactId>
<version>${fj-doc-version}</version>
<relativePath></relativePath>
</parent>
```

Minimum java 11 version (most modules works with java 8 too)

```
<properties>
<maven.compiler.release>11</maven.compiler.release>
<fj-doc-version>${project.parent.version}</fj-doc-version>
</properties>
```

And the module for rendering PDF through [FOP](https://github.com/fugerit-org/fj-doc/tree/main/fj-doc-mod-fop)
(It will all other modules as transient dependencies)

```
<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc-mod-fop</artifactId>
</dependency>
```

## 2. Create the ApacheFreemarker template

This will be the template [full-document.ftl](src/main/resources/fj-freemarker-sample-template/full-document.ftl)

It will generate a XML document which adheres to [doc xsd](https://www.fugerit.org/data/java/doc/xsd/doc-2-1.xsd)

## 3. Create the java cod for document generation

This is a simple facade [FullDocumentFmFacade](src/main/java/org/fugerit/java/doc/guides/fulldocument/fm/facade/FullDocumentFmFacade.java) which will just :

- create a simple FreemarkerDocProcessConfig (more complex examples will be available)
- add some ismple date
- implement the generateFullDocument() method which will do the job

```
private static final FreemarkerDocProcessConfig SIMPLE_CONFIG =
// SafeFunction.get() will execute the configuration code and re-throw a ConfigRuntimeException if any exception is thrown
SafeFunction.get( () -> FreemarkerDocProcessConfigFacade.newSimpleConfig( "A003-sample-config" , "/fj-freemarker-sample-template/" ) );
private static final List<MapEntry<String, String>> LIST_TESTS = Arrays.asList(
new MapEntry<>( "ul", "default unordered list"),
new MapEntry<>( "uld", "dotted unordered list"),
new MapEntry<>( "ulm", "square unordered list"),
new MapEntry<>( "ol", "default ordered list"),
new MapEntry<>( "oll", "letter ordered list"),
new MapEntry<>( "oln", "numbered ordered list")
);
public void generateFullDocument( OutputStream os, DocTypeHandler handler, List<UserData> listUsers ) throws DocException {
// wraps any exception as a DocException
DocException.apply( () -> {
DocProcessContext context = DocProcessContext.newContext( "listUsers", listUsers );
context.setAttribute( "listTests" , LIST_TESTS );
SIMPLE_CONFIG.fullProcess( "full-document", context, handler, DocOutput.newOutput(os) );
} );
}
```

## 4. Sample Junit to activate the code

This is a simple JUnit to activate the code [TestFullDocumentFmFacade](src/test/java/test/org/fugerit/java/doc/guides/fulldocument/fm/facade/TestFullDocumentFmFacade.java) , it will :

- define the handlers for rendering the desired output formats
- add some test data
- iterate all the handler and output them in `target/` directory

```
// three formats will be rendered in this example
private static final DocTypeHandler[] HANDLERS = {
PdfFopTypeHandler.HANDLER, // PDF through Apache FOP
FreeMarkerFopTypeHandler.HANDLER_UTF8, // XLS-FO format
FreeMarkerHtmlTypeHandlerEscapeUTF8.HANDLER, // HTML through Apache FreeMarker
DocTypeHandlerXMLUTF8.HANDLER }; // XML as source fugerit doc format XML
@Test
public void testFullDocument() throws IOException, DocException {
// custom data to be added to the template via Apache FreeMarker
List<UserData> listUsers = Arrays.asList(
new UserData( "Marie" , "Curie", ">> Checmist"),
new UserData( "Alan" , "Turing", "<IT/> Specialist")
);
for ( DocTypeHandler handler : HANDLERS ) {
File outputFile = new File( "target", "full-document."+handler.getType() ); // output file
log.info( "outputFile : {}", outputFile );
try ( OutputStream os = new FileOutputStream( outputFile ) ) {
FullDocumentFmFacade facade = new FullDocumentFmFacade(); // the facade to generate the document
facade.generateFullDocument(os, handler, listUsers); // actual document generation
log.info( "Generated full documenti pdf file in path : {}", outputFile.getCanonicalPath() );
Assert.assertTrue( outputFile.exists() );
}
}
}
```

If everything worked fine, in the path *target/full-document.pdf* it will be possible to find the generated document. (together with html, fo and xml format).

**Note about DocTypeHandlerXMLUTF8 handler** : this can be used to inspect the Venus XML Doc format after Apache FreeMarker substitution.

If you find any problem, you can submit an issue on the on this project github repository [https://github.com/fugerit-org/fj-doc-guides](https://github.com/fugerit-org/fj-doc-guides)
2 changes: 1 addition & 1 deletion fj-doc-guides-A003-full-document-freemarker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.fugerit.java.guide</groupId>
<artifactId>fj-doc-guides</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.0</version>
</parent>

<artifactId>fj-doc-guides-A003-full-document-freemarker</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<#ftl output_format="XML"><#-- Set the output format for XML, not mandatory but reccomended, otherwise escaping should be handled in a cusomt way, for example through CDATA sections -->
<?xml version="1.0" encoding="utf-8"?>
<doc
xmlns="http://javacoredoc.fugerit.org"
Expand Down Expand Up @@ -29,6 +30,8 @@

<para>This documents tries to show all the xml document format features.</para>

<para>Current Apache FreeMarker output format : ${.output_format}.</para>

<h head-level="2" id="sec_1" size="14">1. Sample tables</h>

<para>This sections contains some sample tables.</para>
Expand Down Expand Up @@ -60,11 +63,13 @@
<cell align="center"><para style="bold">Surname</para></cell>
<cell align="center"><para style="bold">Title</para></cell>
</row>
<#list listUsers as user>
<row>
<cell><para>${user.name}</para></cell>
<cell><para>${user.surname}</para></cell>
<cell><para>${user.title}</para></cell>
</row>
</#list>
</table>

<br/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,37 @@
import org.fugerit.java.doc.base.config.DocException;
import org.fugerit.java.doc.base.config.DocTypeHandler;
import org.fugerit.java.doc.base.config.DocTypeHandlerXMLUTF8;
import org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandlerUTF8;
import org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandlerEscapeUTF8;
import org.fugerit.java.doc.guides.fulldocument.fm.facade.FullDocumentFmFacade;
import org.fugerit.java.doc.guides.fulldocument.fm.facade.UserData;
import org.fugerit.java.doc.mod.fop.FreeMarkerFopTypeHandler;
import org.fugerit.java.doc.mod.fop.PdfFopTypeHandler;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestFullDocumentFmFacade {
import lombok.extern.slf4j.Slf4j;

private static final Logger log = LoggerFactory.getLogger( TestFullDocumentFmFacade.class );
@Slf4j
public class TestFullDocumentFmFacade {

// three formats will be rendered in this example
private static final DocTypeHandler[] HANDLERS = {
PdfFopTypeHandler.HANDLER, // PDF through Apache FOP
FreeMarkerHtmlTypeHandlerUTF8.HANDLER, // HTML through Apache FreeMarker
FreeMarkerFopTypeHandler.HANDLER_UTF8, // XLS-FO format
FreeMarkerHtmlTypeHandlerEscapeUTF8.HANDLER, // HTML through Apache FreeMarker
DocTypeHandlerXMLUTF8.HANDLER }; // XML as source fugerit doc format XML

@Test
public void testFullDocument() throws IOException, DocException {
// custom data to be added to the template via Apache FreeMarker
List<UserData> listUsers = Arrays.asList(
new UserData( "Marie" , "Curie", ">> Checmist"),
new UserData( "Alan" , "Turing", "<IT/> Specialist")
);
for ( DocTypeHandler handler : HANDLERS ) {
File outputFile = new File( "target", "full-document."+handler.getType() ); // output file
log.info( "outputFile : {}", outputFile );
try ( OutputStream os = new FileOutputStream( outputFile ) ) {
// custom data to be added to the template via Apache FreeMarker
List<UserData> listUsers = Arrays.asList(
new UserData( "Marie" , "Curie", "Checmist"),
new UserData( "Alan" , "Turing", "IT Specialist")
);
FullDocumentFmFacade facade = new FullDocumentFmFacade(); // the facade to generate the document
facade.generateFullDocument(os, handler, listUsers); // actual document generation
log.info( "Generated full documenti pdf file in path : {}", outputFile.getCanonicalPath() );
Expand Down
Loading

0 comments on commit 6019d90

Please sign in to comment.