Spring String Bean with Context Variables for Java Multi-line Text or Script Template


GitHub: https://github.com/SylkeWay/synthful

There are two String beans in the context configuration file /durian/src/main/resources/spring/ScriptTemplates-context.xml .

Each of them
  • is a java.lang.String bean
  • specifying constructor argument:
    <constructor-arg><value> script body </value></constructor-arg>
  • making use of the CDATA enclosure, within which all characters are not interpreted
    <![CDATA[ script body ]]> , except for recognising ${context variable} substitution.
CDATA escape enclosure is useful for String beans to be used as scripting template, HTML template or XML template.

The Java class injected with the String beans is src/main/java/com/synthful/angst/app/durian/ScriptTemplates.java
@Service
public class ScriptTemplates
extends AAngsta {

    @Inject private String inquireFruitSQL;
    @Inject private String fruitySASMacro;
    
    @Value(value="${fruit.name}")
    public void setNothing(String nothing) {
        logger.info("fruit.name:{}", nothing);
        logger.info("inquireFruitSQL:\n{}", inquireFruitSQL);
        logger.info("fruitySASMacro:\n{}", fruitySASMacro);
    }
}

Where the context variable substitution is due to the property file appbase/properties/a.properties.
maxUsers = ${durian.maxUsers}
fruit.data.filepath = /u/reporter/jobs/data/fruits.txt
fruit.type = Berry
fruit.name = Rambutan
fruit.sortBy = Name


SQL script template as multi-line text inSpring String Bean

The String bean inquireFruitSQL in ScriptTemplates-context.xml
  <bean id="inquireFruitSQL" class="java.lang.String" scope="prototype">
    <constructor-arg><value><![CDATA[
      SELECT f.name, f.genus
      FROM fruits f
      WHERE f.type = '${fruit.type}'
    ]]></value></constructor-arg>
  </bean>

The logger output shows result of context variable substitution:
INFO  ScriptTemplates - inquireFruitSQL:

      SELECT f.name, f.genus
      FROM fruits f
      WHERE f.type = 'Berry'
The text of the String bean could then be sent into Spring JDBC template, or any SQL query connector.

SAS macro template as multi-line text in Spring String Bean

The String bean inquireFruitSQL in ScriptTemplates-context.xml
  <bean id="fruitySASMacro" class="java.lang.String" scope="prototype">
    <constructor-arg><value><![CDATA[
    %macro durian;
      %let fruitname = ${fruit.name};
      
      DATA ${fruit.name};
        INFILE '${fruit.data.filepath}';
        INPUT Name $ 1-15 Genus $ Family $ 
      RUN;
      
      PROC SORT DATA = ${fruit.name};
      BY ${fruit.sortBy};
      
      PROC PRINT DATA = ${fruit.name} NOOBS;
        TITLE &fruitname;
        VAR Name Genus Family;
      RUN;
    %mend durian;
    ]]></value></constructor-arg>
  </bean>


The logger output shows result of context variable substitution:
INFO  ScriptTemplates - fruitySASMacro:

    %macro durian;
      %let fruitname = Rambutan;
      
      DATA Rambutan;
        INFILE '/u/reporter/jobs/data/fruits.txt';
        INPUT Name $ 1-15 Genus $ Family $ 
      RUN;
      
      PROC SORT DATA = Rambutan;
      BY Name;
      
      PROC PRINT DATA = Rambutan NOOBS;
        TITLE &fruitname;
        VAR Name Genus Family;
      RUN;
    %mend durian;
The text resolved from the String bean could then be submitted to an established SAS session thro SAS/Connect.


No comments:

Post a Comment