diff --git a/pom.xml b/pom.xml index 6b129fe..2161c32 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,9 @@ 3.13.0 UTF-8 8.9.3 + 0.4.2 io.quarkus.platform + 2.0.21 @@ -102,6 +104,51 @@ org.fugerit.java fj-doc-mod-openrtf-ext + + org.fugerit.java + fj-doc-ext-kotlin + ${fj-doc-ext-kotlin-version} + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-scripting-jsr223 + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-script-runtime + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-scripting-common + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-scripting-jvm + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-scripting-jvm-host + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-compiler-embeddable + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-reflect + ${kotlin.version} + org.junit.jupiter junit-jupiter diff --git a/src/main/java/org/fugerit/java/demo/fjdocquarkustutorial/DocHelper.java b/src/main/java/org/fugerit/java/demo/fjdocquarkustutorial/DocHelper.java index eead616..73d9295 100644 --- a/src/main/java/org/fugerit/java/demo/fjdocquarkustutorial/DocHelper.java +++ b/src/main/java/org/fugerit/java/demo/fjdocquarkustutorial/DocHelper.java @@ -1,11 +1,13 @@ package org.fugerit.java.demo.fjdocquarkustutorial; +import jakarta.enterprise.context.ApplicationScoped; import org.fugerit.java.doc.freemarker.process.FreemarkerDocProcessConfig; import org.fugerit.java.doc.freemarker.process.FreemarkerDocProcessConfigFacade; /** * DocHelper, version : auto generated on 2024-10-16 23:44:16.802 */ +@ApplicationScoped public class DocHelper { private FreemarkerDocProcessConfig docProcessConfig = FreemarkerDocProcessConfigFacade.loadConfigSafe( "cl://fj-doc-quarkus-tutorial/fm-doc-process-config.xml" ); diff --git a/src/main/java/org/fugerit/java/demo/fjdocquarkustutorial/DocResource.java b/src/main/java/org/fugerit/java/demo/fjdocquarkustutorial/DocResource.java index f29247f..3ec4650 100644 --- a/src/main/java/org/fugerit/java/demo/fjdocquarkustutorial/DocResource.java +++ b/src/main/java/org/fugerit/java/demo/fjdocquarkustutorial/DocResource.java @@ -1,35 +1,88 @@ package org.fugerit.java.demo.fjdocquarkustutorial; +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.*; -import jakarta.ws.rs.core.MediaType; import lombok.extern.slf4j.Slf4j; +import org.fugerit.java.core.function.SafeFunction; +import org.fugerit.java.core.io.StreamIO; +import org.fugerit.java.core.lang.helpers.ClassHelper; import org.fugerit.java.core.util.ObjectUtils; +import org.fugerit.java.core.util.checkpoint.SimpleCheckpoint; import org.fugerit.java.doc.base.config.DocConfig; +import org.fugerit.java.doc.base.config.DocInput; +import org.fugerit.java.doc.base.config.DocOutput; +import org.fugerit.java.doc.base.config.DocTypeHandler; import org.fugerit.java.doc.base.facade.DocFacadeSource; import org.fugerit.java.doc.base.process.DocProcessContext; import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.StringReader; +import java.util.ArrayList; import java.util.Arrays; +import java.util.LinkedHashMap; import java.util.List; import org.eclipse.microprofile.openapi.annotations.Operation; import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; import org.eclipse.microprofile.openapi.annotations.tags.Tag; -import org.eclipse.microprofile.openapi.annotations.tags.Tags; + +import javax.script.*; @Slf4j @Path("/doc") @Tag( name = "XML SOURCE (DEFAULT)", description = "Add query param sourceType=1 or no value to use the XML source example") @Tag( name = "JSON SOURCE", description = "Add query param sourceType=2 to use the JSON source example") @Tag( name = "YAML SOURCE", description = "Add query param sourceType=3 to use the YAML source example") +@Tag( name = "KTS SOURCE", description = "Add query param sourceType=9 to use the Kotlin source example") +@ApplicationScoped public class DocResource { + + private DocHelper docHelper; + + public DocResource( DocHelper docHelper ) { + this.docHelper = docHelper; + } + + /* + * Kotlin support still experimental + */ + private void handleKts( List listPeople, String handlerId, ByteArrayOutputStream baos ) throws Exception { + SafeFunction.apply( () -> { + SimpleCheckpoint checkpoint = new SimpleCheckpoint(); + ScriptEngineManager manager = new ScriptEngineManager(); + log.info( "kts create script manager : {}", checkpoint.getFormatTimeDiffMillis() ); + ScriptEngine engine = manager.getEngineByExtension( "kts" ); + log.info( "kts create script engine : {}", checkpoint.getFormatTimeDiffMillis() ); + Bindings bindings = engine.createBindings(); + log.info( "kts create script bindings : {}", checkpoint.getFormatTimeDiffMillis() ); + LinkedHashMap data = new LinkedHashMap<>(); + ObjectMapper mapper = new ObjectMapper(); + data.put( "docTitle", "My sample title Source KTS" ); + data.put( "listPeople", mapper.readValue( mapper.writeValueAsString( listPeople ) , ArrayList.class ) ); + log.info( "kts read json data : {}", checkpoint.getFormatTimeDiffMillis() ); + bindings.put( "data", data ); + engine.setBindings( bindings, ScriptContext.ENGINE_SCOPE ); + try (InputStream is = ClassHelper.loadFromDefaultClassLoader( "fj-doc-quarkus-tutorial/kts/document.kts" )) { + log.info( "kts set bindings : {}", checkpoint.getFormatTimeDiffMillis() ); + Object obj = engine.eval( StreamIO.readString( is ) ); + log.info( "kts eval script : {}", checkpoint.getFormatTimeDiffMillis() ); + String xml = obj.toString(); + log.info( "kts toXml : {}", checkpoint.getFormatTimeDiffMillis() ); + try ( StringReader xmlReader = new StringReader( xml) ) { + DocTypeHandler handler = this.docHelper.getDocProcessConfig().getFacade().findHandler( handlerId ); + handler.handle( DocInput.newInput( handler.getType(), xmlReader ), DocOutput.newOutput( baos ) ); + } + } + } ); + } + byte[] processDocument(String handlerId, Integer sourceType) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { int realSourceType = ObjectUtils.objectWithDefault( sourceType, DocFacadeSource.SOURCE_TYPE_DEFAULT ); log.info( "using source type : {}", realSourceType ); - // creates the doc helper - DocHelper docHelper = new DocHelper(); // create custom data for the fremarker template 'document.ftl' List listPeople = Arrays.asList(new People("Luthien", "Tinuviel", "Queen"), new People("Thorin", "Oakshield", "King")); // output generation @@ -37,6 +90,7 @@ byte[] processDocument(String handlerId, Integer sourceType) { switch ( realSourceType ) { case DocFacadeSource.SOURCE_TYPE_JSON -> docHelper.getDocProcessConfig().fullProcess("document-json", context.withSourceType( realSourceType ), handlerId, baos); case DocFacadeSource.SOURCE_TYPE_YAML -> docHelper.getDocProcessConfig().fullProcess("document-yaml", context.withSourceType( realSourceType ), handlerId, baos); + case 9 -> handleKts( listPeople, handlerId, baos ); default -> docHelper.getDocProcessConfig().fullProcess("document", context, handlerId, baos); } // return the output diff --git a/src/main/resources/fj-doc-quarkus-tutorial/kts/document.kts b/src/main/resources/fj-doc-quarkus-tutorial/kts/document.kts new file mode 100644 index 0000000..cd202ac --- /dev/null +++ b/src/main/resources/fj-doc-quarkus-tutorial/kts/document.kts @@ -0,0 +1,33 @@ +import org.fugerit.java.doc.base.kotlin.dsl.dslDoc + +dslDoc { + val docTitle = attStr( data, "docTitle" ) + meta { + info( ( docTitle ) ).name( "doc-title" ) + info( ( "10;10;10;30" ) ).name( "margins" ) + info( ( "fj doc venus sample source Kotlin Template - kts" ) ).name( "doc-subject" ) + info( ( "fugerit79" ) ).name( "dock-author" ) + info( ( "en" ) ).name( "doc-language" ) + info( ( "TitilliumWeb" ) ).name( "default-font-name" ) + info( ( "data-table=print" ) ).name( "excel-table-id" ) + info( ( "data-table" ) ).name( "csv-table-id" ) + footerExt { + para( '$'+"{currentPage} / "+'$'+"{pageCount}" ).align( "right" ) + } + } + body { + h( docTitle ).headLevel( 1 ) + table { + row { + cell { para( "Name" ) }.align( "center" ) + cell { para( "Surname" ) }.align( "center" ) + cell { para( "Title" ) }.align( "center" ) + }.header( true ) + attListMap( data, "listPeople" ).forEach( { e -> row { + cell { para( attStr( e, "name" ) ) } + cell { para( attStr( e, "surname" ) ) } + cell { para( attStr( e, "title" ) ) } + } } ) + }.columns( 3 ).colwidths( "30;30;40" ).width( 100 ).id( "data-table" ).padding( 2 ) + } +} \ No newline at end of file