Class StringMerger

java.lang.Object
net.solarnetwork.util.StringMerger

public final class StringMerger extends Object
Utility class for performing a simple mail-merge.

This class parses a String source and substitutes variables in the form of ${name} for a corresponding property in a specified data object. The data object can be either a java.util.Map or an arbitrary JavaBean. If the data object is a Map, the variable names will be treated as keys in that map, the the corresponding value will be substituted in the output String. Otherwise, reflection will be used to access JavaBean getter methods, using the Struts naming conventions for bean property names and nested names.

Version:
1.1
Author:
matt
  • Method Details

    • mergeResource

      public static String mergeResource(org.springframework.core.io.Resource resource, Object data) throws IOException
      Merge from a Resource, and return the merged output as a String.

      This method calls the mergeResource(Resource, Object, String) method, passing an empty string for nullValue.

      Parameters:
      resource - the resource
      data - the data to merge with
      Returns:
      merged string
      Throws:
      IOException - if an error occurs
    • mergeResource

      public static String mergeResource(org.springframework.core.io.Resource resource, Object data, String nullValue) throws IOException
      Merge from a Resource, and return the merged output as a String.

      This method will read the Resource as character data line by line, merging each line as it goes.

      Parameters:
      resource - the resource
      data - the data to merge with
      nullValue - the value to substitute for null data elements
      Returns:
      merged string
      Throws:
      IOException - if an error occurs
    • merge

      public static String merge(String filePath, Object data) throws IOException
      Merge from a file source, and return the merged output as a String.
      Parameters:
      filePath - path to the source file
      data - the Map or JavaBean with merge data
      Returns:
      the merged output
      Throws:
      IOException - if an exception occurs
    • mergeString

      public static String mergeString(String src, String nullValue, Object data)
      Merge from a String source and return the result.
      Parameters:
      src - java.lang.String
      nullValue - the value to substitute for null data
      data - java.lang.Object
      Returns:
      java.lang.String
    • mergeString

      public static void mergeString(String src, Object data, String nullValue, StringBuilder buf)
      Merge from a String source into a StringBuilder.
      Parameters:
      src - the source String to substitute into
      data - the data object to substitute with
      nullValue - the value to substitute for null data
      buf - the StringBuilder to append the output to