Skip to content

Commit

Permalink
Added kotlin source type (9)
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Oct 17, 2024
1 parent 71d87a1 commit 8156f1f
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 4 deletions.
47 changes: 47 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
<compiler-plugin.version>3.13.0</compiler-plugin.version>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<fj-doc-version>8.9.3</fj-doc-version>
<fj-doc-ext-kotlin-version>0.4.2</fj-doc-ext-kotlin-version>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<kotlin.version>2.0.21</kotlin.version>
</properties>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -102,6 +104,51 @@
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc-mod-openrtf-ext</artifactId>
</dependency>
<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc-ext-kotlin</artifactId>
<version>${fj-doc-ext-kotlin-version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-jsr223</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-script-runtime</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-common</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-jvm</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-jvm-host</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler-embeddable</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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" );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,96 @@
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<People> 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<String, Object> 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<People> listPeople = Arrays.asList(new People("Luthien", "Tinuviel", "Queen"), new People("Thorin", "Oakshield", "King"));
// output generation
DocProcessContext context = DocProcessContext.newContext("listPeople", listPeople);
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
Expand Down
33 changes: 33 additions & 0 deletions src/main/resources/fj-doc-quarkus-tutorial/kts/document.kts
Original file line number Diff line number Diff line change
@@ -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 )
}
}

0 comments on commit 8156f1f

Please sign in to comment.