Skip to content

dmeiners88/mapping-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Zero-Dependency Mapping Library

Simple Java Library to include scripting languages into your Java application.

API Module

Build Status SonarCloud Analysis Download License Semantic Versioning

FeaturesLimitationsInstallationUsage

Features

  • Zero dependencies
  • Alternative to Scripting for the Java Platform JSR-223
  • Different scripting language implementations
  • Compile scripts inline, load from the classpath or roll your own ScriptNameResolver
  • Expose your utility classes into the scripting language

Limitations

  • No handling of script engine/language mismatch
  • Only one script engine implementation from the classpath is picked up

Installation

Add the API Module

Add JCenter as a repository to your Maven POM:

<repositories>
    <repository>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <id>jcenter</id>
        <name>JCenter</name>
        <url>https://jcenter.bintray.com</url>
    </repository>
</repositories>

Add the following dependency:

<dependency>
    <groupId>de.dmeiners.mapping</groupId>
    <artifactId>mapping-api</artifactId>
    <version>1.0.0</version>
</dependency>

Important: Adding the Mapping API dependency alone will result in a no-op fallback implementation. See the next section for a list of implementation modules.

Add an Implementation Module

Usage

Simple Usage

PostProcessor processor = PostProcessorFactory.builder().build(); // <1>

String someObject = "Hello"; // <2>

Script script = processor.compileInline("target += ' World!'"); // <3>

String result = script.execute(someObject, Collections.emptyMap()); // <4>

assertThat(result).isEqualTo("Hello World!");
  1. Create a new post processor
  2. Create a very simple target object
  3. Compile a simple script (scripting language dependent on the implementation available on the classpath, here it is Apache Commons JEXL)
  4. Execute the script, result type is guaranteed to the original type

Context Usage

PostProcessor processor = PostProcessorFactory.builder().build();

Map<String, Object> user = new HashMap<>(); // <1>
user.put("firstName", "John");
user.put("lastName", "Doe");

Map<String, Object> context = new HashMap<>(); // <2>
context.put("user", user);

String someObject = "Hello";

String scriptText = "target += ` ${user.firstName} ${user.lastName}`"; // <3>

Script script = processor.compileInline(scriptText);

String result = script.execute(someObject, context);

assertThat(result).isEqualTo("Hello John Doe");
  1. Target object is a Map this time
  2. We create another map to use as script context
  3. Beware that the script text is dependent on the available implementation on the classpath, here it is Apache Commons JEXL

Expose Utility Class

PostProcessor processor = PostProcessorFactory.builder()
    .extension("StringUtils", StringUtils.class) // <1>
    .extension("tools", new Tools()) // <2>
    .build();

Map<String, Object> user = new HashMap<>();
user.put("firstName", "John");
user.put("lastName", "Doe");

Map<String, Object> context = new HashMap<>();
context.put("user", user);

String someObject = "Hello";

String scriptText = "if (StringUtils.isNotBlank(user.firstName)) { target = 'First name is not blank and reversed ' + tools.reverse(user.firstName); }"; // <2>

Script script = processor.compileInline(scriptText);

String result = script.execute(someObject, context);

assertThat(result).isEqualTo("First name is not blank and reversed nhoJ");
  1. We can register classes to expose static methods to the scripting language
  2. We can register objects to expose instance methods to the scripting language

About

A Zero-Dependency Mapping Library - API

Resources

License

Stars

Watchers

Forks

Packages

No packages published