Skip to content

Commit 2ed62b5

Browse files
committed
Added documentation
1 parent 5b9de09 commit 2ed62b5

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,23 @@ This project provides just a convenient interface for evaluating scripts with ja
3131
Sample code :
3232

3333
```java
34-
EvalScript evalKts = new EvalScriptWithJsonDataModel( "kts" );
34+
// this will create a EvalScript instance for :
35+
// kotlin script engine (kts)
36+
// data model will be bound as "data" (if not provided this is the default binding name)
37+
EvalScript evalKts = new EvalScriptWithDataModel( "kts", "data" );
3538
try (Reader reader = [reader on kotlin script]) {
3639
Map<String, Object> dataModel = new HashMap<>();
3740
dataModel.put( "docTitle", "My custom title" );
3841
Object result = evalKts.evalKts( reader, dataModel );
3942
log.info( "my result : {}", result );
4043
}
41-
```
44+
```
45+
46+
It is possible to wrap the EvalScript with some decorators, for instance :
47+
48+
```java
49+
EvalScript evalKts = EvalScriptWithJsonDataModel( new EvalScriptWithDataModel( "kts", "data" ) );
50+
```
51+
52+
Will wraps the EvalScript with EvalScriptWithJsonDataModel decorator.
53+
EvalScriptWithJsonDataModel will transform a Map data model to a json data model style.

src/main/java/org/fugerit/java/script/helper/EvalScript.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,55 @@
55
import java.io.Reader;
66
import java.util.Map;
77

8+
/**
9+
* Simple interface for script handling.
10+
*/
811
public interface EvalScript {
912

13+
/**
14+
* It will handle a script and bind a data model.
15+
*
16+
* Any exception will be converted to {@link org.fugerit.java.core.cfg.ConfigRuntimeException}
17+
*
18+
* @param reader the reader over script
19+
* @param dataModel the data model to bind
20+
* @return the result of script processing
21+
*/
1022
default Object handle(Reader reader, Map<String, Object> dataModel) {
1123
return SafeFunction.get( () -> handleEx(reader, dataModel) );
1224
}
1325

26+
/**
27+
* It will handle a script with no data model
28+
*
29+
* Any exception will be converted to {@link org.fugerit.java.core.cfg.ConfigRuntimeException}
30+
*
31+
* @param reader the reader over script
32+
* @return the result of script processing
33+
*/
1434
default Object handle(Reader reader) {
1535
return handle(reader, null);
1636
}
1737

38+
/**
39+
* It will handle a script and bind a data model.
40+
*
41+
* Any class implementing EvalScript will need to provide at least this method.
42+
*
43+
* @param reader the reader over script
44+
* @param dataModel the data model to bind
45+
* @return the result of script processing
46+
* @throws ScriptException in case of script handling issues
47+
*/
1848
Object handleEx(Reader reader, Map<String, Object> dataModel) throws ScriptException;
1949

50+
/**
51+
* It will handle a script with no data model.
52+
*
53+
* @param reader the reader over script
54+
* @return the result of script processing
55+
* @throws ScriptException in case of script handling issues
56+
*/
2057
default Object handleEx(Reader reader) throws ScriptException {
2158
return handleEx(reader, null);
2259
}

src/main/java/org/fugerit/java/script/helper/EvalScriptWithDataModel.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
import java.util.LinkedHashMap;
1313
import java.util.Map;
1414

15+
/**
16+
* Simple implementation of EvalScript.
17+
*
18+
* It relies on extension based ScriptEngine (ScriptEngineManager.getEngineByExtension( this.scriptExtension ))
19+
*
20+
* Data model binding name can be customized, default is 'data'.
21+
*
22+
*/
1523
@Slf4j
1624
public class EvalScriptWithDataModel implements EvalScript {
1725

@@ -21,12 +29,23 @@ public class EvalScriptWithDataModel implements EvalScript {
2129

2230
private String dataModelBindingName;
2331

32+
/**
33+
* It will create a EvalScriptWithDataModel
34+
*
35+
* @param scriptExtension the extension to be used for the ScriptEngine
36+
* @param dataModelBindingName the data model binding name
37+
*/
2438
public EvalScriptWithDataModel(String scriptExtension, String dataModelBindingName) {
2539
this.scriptExtension = scriptExtension;
2640
this.dataModelBindingName = dataModelBindingName;
2741
log.debug( "use scriptExtension : [{}], use dataModelBindingName : [{}]s", scriptExtension, dataModelBindingName );
2842
}
2943

44+
/**
45+
* It will create a EvalScriptWithDataModel with the default data model binding name.
46+
*
47+
* @param scriptExtension the extension to be used for the ScriptEngine
48+
*/
3049
public EvalScriptWithDataModel(String scriptExtension) {
3150
this( scriptExtension, DEFAULT_DATA_MODEL_BINDING_NAME );
3251
}

src/main/java/org/fugerit/java/script/helper/EvalScriptWithJsonDataModel.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,23 @@
77
import java.util.LinkedHashMap;
88
import java.util.Map;
99

10+
/**
11+
* EvalScript decorator.
12+
*
13+
* The Map<String, Object> dataModel will be transformed into a json styled LinkedHashMap<String, Object>.
14+
*/
1015
@Slf4j
1116
public class EvalScriptWithJsonDataModel implements EvalScript {
1217

1318
private static final ObjectMapper MAPPER = new ObjectMapper();
1419

1520
private EvalScript evalScript;
1621

22+
/**
23+
* Add decoration to a give EvalScript
24+
*
25+
* @param evalScript the EvalScript instance to decorate
26+
*/
1727
public EvalScriptWithJsonDataModel(EvalScript evalScript) {
1828
this.evalScript = evalScript;
1929
}
@@ -28,10 +38,23 @@ public Object handleEx(Reader reader, Map<String, Object> dataModel) throws Scri
2838
}
2939
}
3040

41+
/**
42+
* It will decorate a EvalScriptWithDataModel instance.
43+
*
44+
* @param scriptExtension the extension to be used for the ScriptEngine
45+
* @param dataModelBindingName the data model binding name
46+
* @return the new EvalScript
47+
*/
3148
public static EvalScript newEvalScriptWithJsonDataModel( String scriptExtension, String dataModelBindingName ) {
3249
return new EvalScriptWithJsonDataModel( new EvalScriptWithDataModel( scriptExtension, dataModelBindingName ) );
3350
}
3451

52+
/**
53+
* It will decorate a EvalScriptWithDataModel instance with default data model binding name.
54+
*
55+
* @param scriptExtension the extension to be used for the ScriptEngine
56+
* @return the new EvalScript
57+
*/
3558
public static EvalScript newEvalScriptWithJsonDataModel( String scriptExtension ) {
3659
return new EvalScriptWithJsonDataModel( new EvalScriptWithDataModel( scriptExtension ) );
3760
}

0 commit comments

Comments
 (0)