Scripturian
The Scalable Alternative
To JSR-223

Scripturian Tutorial

Go back to the tutorial's table of contents

Part 6: Extending Scripturian

Scriptlet Plugins

You can enhance the scriptlet parsing environment by creating scriptlet plugins. Let's install a "shout" tag that prints out the content of the tag with an exclamation point at the end:

class MyPlugin implements ScriptletPlugin {
	public String getScriptlet( String code, LanguageAdapter languageAdapter, String content ) {
		if( code.equals( "!!!" ) ) {
			return "print('" + content.trim().replace( "'", "\\'" ) + "' + '!')";
		}
		throw new RuntimeException( "We do not support tag: " + code );
	} 
}

parsingContext.getScriptletPlugins().put( "!!!", new MyPlugin() );
We would then be able to use the new "shout" scriptlet tag like so:

This is going to be <%!!! shouted out >

Our "shout" tag is "Not very useful, but of course you can do anything with the content, and you are not required to return any code. Further points to note:

  • Whatever "getScriptlet" returns is inserted into the executable during parsing only. Once the executable is ready, these plugins will never be called again for this document, so there is no performance loss for using them. It is simply a convenience for parsing.
  • You do not have to return scriptlet source code, but if you do, it's up to you to make sure that it fits the syntax of your language. In the example above we've made sure to escape the content's quotation marks so that it could be including in a literal string.
  • The languageAdapter value refers to the last language used (or the default): it's up to your to parse the 'code' for a language tag if you wish multiple languages.
  • You can use the same plugin to implement multiple tag codes. In this case, we only supported "!!!".

Supporting Other Language Engines

To add a new language to Scripturian, you will first need to create a language adapter for it. If the language supports the Java scripting standard (JSR-223), then you can use the JSR-223 adapter. However, our experience has shown that adherence to the Java scripting standard is inconsistent, and that it's often better to create your own adapter.


Go back to the tutorial's table of contents