com.neeve.util
Class UtlTailoring

java.lang.Object
  extended by com.neeve.util.UtlTailoring

public class UtlTailoring
extends Object

Utility class facilitating tailoring of textual content from provided properties.

This class IS NOT intended to be used by end users.


Nested Class Summary
static interface UtlTailoring.PropertySource
          Allows callers to plug in different property sources.
static class UtlTailoring.STYLE
          Enumerates possible tailoring styles.
static class UtlTailoring.TailoringException
          Indicates that an error occured tailoring String content
 
Field Summary
static boolean ENABLE_NEW_SPRING_STYLE_SUBSTITUTION_PARSER
           
static String ENABLE_NEW_SPRING_STYLE_SUBSTITUTION_PARSER_PROP
          Defines the property name used to control whether or not the new 3.5 spring-style parser is enabled when calling substitute(String, PropertySource)
static String ENABLE_SPRING_STYLE_SUBSTITUTION_PROP
          Defines the property name used to control whether or not UtlTailoring.STYLE.SPRING style substitution is enabled when calling substitute(String, PropertySource)
static UtlTailoring.PropertySource ENV_SUBSTITUTION_RESOLVER
          The environment based substitution resolver.
 
Constructor Summary
UtlTailoring()
           
 
Method Summary
 boolean isAppendUnreplacedProperties()
           
 boolean isLeaveReplacementComments()
           
static boolean isPropFileComment(String line)
          Tests if the line is a comment line from a prop file.
static void main(String[] args)
           
static String propFileScanAndReplace(File target, Properties props, boolean appendUreplacedValues, boolean leaveReplaceComment)
          Convenience method equivalent to propFileScanAndReplace(String, Properties, boolean, boolean) except that the File is read in as a string and tailored.
static String propFileScanAndReplace(String propFileContent, Properties replacementProps, boolean appendUreplacedValues, boolean leaveReplaceComment)
          Scans the provided String as a properties file looking for lines of the form
propName=propVal
replacing them with the value in the provided properties if such a property exists.
 void setAppendUnreplacedProperties(boolean appendUnreplacedProperties)
          Indicates that unreplaced properties should be appended at the end of the file being tailored.
 void setLeaveReplacementComments(boolean leaveReplacementComments)
          Indicates that when a property line is replaced a comment containing the original line should be left in the file.
static String springScanAndReplace(File f, Properties props)
          Same as springScanAndReplace(String, Properties) except that the given File is read in as a string and tailored.
static String springScanAndReplace(String string, Properties props)
          Scans the provided String looking for replacements sequences of the form
${toReplace}
replacing them with the value specified by "toReplace" in the provided properties if such a property exists.
static String springScanAndReplace(String string, Properties props, boolean preserveDefaults)
          Scans the provided String looking for replacements sequences of the form
${toReplace}
replacing them with the value specified by "toReplace" in the provided properties if such a property exists.
static String springScanAndReplace(String string, UtlTailoring.PropertySource props)
          Scans the provided String looking for replacements sequences of the form
${toReplace}
replacing them with the value specified by "toReplace" in the provided properties if such a property exists.
static String springScanAndReplace(String string, UtlTailoring.PropertySource props, boolean preserveDefaults)
          Scans the provided String looking for replacements sequences of the form
${toReplace}
replacing them with the value specified by "toReplace" in the provided properties if such a property exists.
static String substitute(String str, UtlTailoring.PropertySource props)
          Checks if a string contains subst(...).
static String substituteFromEnv(String str)
          Performs substitute(String, PropertySource) using the UtlProps.getEnvProps() as the property source.
static String substScanAndReplace(File f, Properties props)
          Same as substScanAndReplace(String, Properties) except that the given File is read in as a string and tailored.
static String substScanAndReplace(String string, Properties props)
          Same as substScanAndReplace(String, PropertySource) using the given Properties as the UtlTailoring.PropertySource
static String substScanAndReplace(String str, UtlTailoring.PropertySource props)
          Scans the provided String looking for replacements sequences of the form:
$subst(%toReplace::defaultValue%)
replacing them with the value specified by "toReplace" in the provided properties if such a property exists or the defaultValue if the property does not exist.
static void tailorFile(File f, Properties props, UtlTailoring.STYLE style)
          Tailors the given file with the specified properties.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENABLE_SPRING_STYLE_SUBSTITUTION_PROP

public static final String ENABLE_SPRING_STYLE_SUBSTITUTION_PROP
Defines the property name used to control whether or not UtlTailoring.STYLE.SPRING style substitution is enabled when calling substitute(String, PropertySource)

See Also:
Constant Field Values

ENABLE_NEW_SPRING_STYLE_SUBSTITUTION_PARSER_PROP

public static final String ENABLE_NEW_SPRING_STYLE_SUBSTITUTION_PARSER_PROP
Defines the property name used to control whether or not the new 3.5 spring-style parser is enabled when calling substitute(String, PropertySource)

See Also:
Constant Field Values

ENABLE_NEW_SPRING_STYLE_SUBSTITUTION_PARSER

public static final boolean ENABLE_NEW_SPRING_STYLE_SUBSTITUTION_PARSER

ENV_SUBSTITUTION_RESOLVER

public static final UtlTailoring.PropertySource ENV_SUBSTITUTION_RESOLVER
The environment based substitution resolver.

Constructor Detail

UtlTailoring

public UtlTailoring()
Method Detail

tailorFile

public static final void tailorFile(File f,
                                    Properties props,
                                    UtlTailoring.STYLE style)
                             throws IOException
Tailors the given file with the specified properties. If the file is a directory then this method will recursively tailor all files in the directory.

Parameters:
f - The file or directory.
props - The replacement properties.
Throws:
IOException - If there is an error tailoring.

springScanAndReplace

public static String springScanAndReplace(File f,
                                          Properties props)
                                   throws IOException
Same as springScanAndReplace(String, Properties) except that the given File is read in as a string and tailored. If substitutions are made then the file is rewritten with the new content.

Returns:
If no replacements are performed the same String object will be returned.
Throws:
IOException - if there is an error reading the file.

springScanAndReplace

public static String springScanAndReplace(String string,
                                          Properties props)
Scans the provided String looking for replacements sequences of the form
${toReplace}
replacing them with the value specified by "toReplace" in the provided properties if such a property exists. This replacement method also allows for default values to be specified in the form:
${toReplace,defaultValue}
if no property called "toReplace" exists then ${toReplace,defaultValue} will be replaced with defaultValue".

Parameters:
string - The string to tailor
props - The properties to used for replacements.
Returns:
If no replacements are performed the same String object will be returned.

springScanAndReplace

public static String springScanAndReplace(String string,
                                          Properties props,
                                          boolean preserveDefaults)
Scans the provided String looking for replacements sequences of the form
${toReplace}
replacing them with the value specified by "toReplace" in the provided properties if such a property exists. This replacement method also allows for default values to be specified in the form:
${toReplace,defaultValue}
if no property called "toReplace" exists then ${toReplace,defaultValue} will be replaced with defaultValue".

Parameters:
string - The string to tailor
props - The properties to used for replacements.
preserveDefaults - When true if a value is not specified in the properties the default will be left in place.
Returns:
If no replacements are performed the same String object will be returned.

springScanAndReplace

public static String springScanAndReplace(String string,
                                          UtlTailoring.PropertySource props)
                                   throws UtlTailoring.TailoringException
Scans the provided String looking for replacements sequences of the form
${toReplace}
replacing them with the value specified by "toReplace" in the provided properties if such a property exists. This replacement method also allows for default values to be specified in the form:
${toReplace::defaultValue}
if no property called "toReplace" exists then ${toReplace,defaultValue} will be replaced with defaultValue".

Parameters:
string - The string to tailor
props - The property source to used for replacements.
Returns:
If no replacements are performed the same String object will be returned.
Throws:
UtlTailoring.TailoringException

springScanAndReplace

public static String springScanAndReplace(String string,
                                          UtlTailoring.PropertySource props,
                                          boolean preserveDefaults)
                                   throws UtlTailoring.TailoringException
Scans the provided String looking for replacements sequences of the form
${toReplace}
replacing them with the value specified by "toReplace" in the provided properties if such a property exists. This replacement method also allows for default values to be specified in the form:
${toReplace::defaultValue}
if no property called "toReplace" exists then ${toReplace,defaultValue} will be replaced with defaultValue".

Parameters:
string - The string to tailor
props - The property source to used for replacements.
preserveDefaults - When true if a value is not specified in the properties the default will be left in place.
Returns:
If no replacements are performed the same String object will be returned.
Throws:
UtlTailoring.TailoringException

substScanAndReplace

public static String substScanAndReplace(File f,
                                         Properties props)
                                  throws IOException
Same as substScanAndReplace(String, Properties) except that the given File is read in as a string and tailored. If substitutions are made then the file is rewritten with the new content.

Returns:
If no replacements are performed the same String object will be returned.
Throws:
IOException - if there is an error reading the file.

substScanAndReplace

public static String substScanAndReplace(String string,
                                         Properties props)
                                  throws Exception
Same as substScanAndReplace(String, PropertySource) using the given Properties as the UtlTailoring.PropertySource

Returns:
If no replacements are performed the same String object will be returned.
Throws:
Exception

substScanAndReplace

public static String substScanAndReplace(String str,
                                         UtlTailoring.PropertySource props)
                                  throws Exception
Scans the provided String looking for replacements sequences of the form:
$subst(%toReplace::defaultValue%)
replacing them with the value specified by "toReplace" in the provided properties if such a property exists or the defaultValue if the property does not exist. This replacement method requires that a default value be supplied.

This substitution method requires supports escaping of special characters inside the $subst(...)

Returns:
If no replacements are performed the same String object will be returned.
Throws:
Exception

propFileScanAndReplace

public static String propFileScanAndReplace(File target,
                                            Properties props,
                                            boolean appendUreplacedValues,
                                            boolean leaveReplaceComment)
                                     throws IOException
Convenience method equivalent to propFileScanAndReplace(String, Properties, boolean, boolean) except that the File is read in as a string and tailored. If substitutions are made then the file is rewritten with the new content.

Parameters:
target - The property file content
props - The replacementProps properties
appendUreplacedValues - if True values that are not replaced are appended to the end of the file.
leaveReplaceComment - if True values that are not replaced are appended to the end of the file.
Returns:
If no replacements are performed the same String object will be returned.
Throws:
IOException - if there is an error reading the file.

propFileScanAndReplace

public static String propFileScanAndReplace(String propFileContent,
                                            Properties replacementProps,
                                            boolean appendUreplacedValues,
                                            boolean leaveReplaceComment)
Scans the provided String as a properties file looking for lines of the form
propName=propVal
replacing them with the value in the provided properties if such a property exists. Lines starting with '#' or ';' are treated as comments and not tailored.
This method preserves the original order of the properties file and comments.

Parameters:
propFileContent - The propety file content
replacementProps - The replacementProps properties
appendUreplacedValues - if True values that are not replaced are appended to the end of the file.
leaveReplaceComment - Leaves a comment indicating what was replaced
Returns:
If no replacements are performed the same String object will be returned.

isPropFileComment

public static final boolean isPropFileComment(String line)
Tests if the line is a comment line from a prop file.

Parameters:
line - The line to check
Returns:
True if the line should be considered a comment

setAppendUnreplacedProperties

public void setAppendUnreplacedProperties(boolean appendUnreplacedProperties)
Indicates that unreplaced properties should be appended at the end of the file being tailored.

Applies to UtlTailoring.STYLE.PROPFILE tailoring.

Parameters:
appendUnreplacedProperties - true to if unreplaced properties should be appended to the end of the file

isAppendUnreplacedProperties

public boolean isAppendUnreplacedProperties()
Returns:
Whether unreplaced properties should be appended at the end of the file being tailored.

setLeaveReplacementComments

public void setLeaveReplacementComments(boolean leaveReplacementComments)
Indicates that when a property line is replaced a comment containing the original line should be left in the file.

Applies to UtlTailoring.STYLE.PROPFILE tailoring.

Parameters:
leaveReplacementComments - Whether or not to leave comments about replacements

isLeaveReplacementComments

public boolean isLeaveReplacementComments()
Returns:
Whether when a property line is replaced a comment containing the original line should be left in the file.

substitute

public static final String substitute(String str,
                                      UtlTailoring.PropertySource props)
                               throws Exception
Checks if a string contains subst(...). If so, it parses the line through UtlStr.subst(). Following $subst(...) substitution Spring style substitution is perform unless disabled by the ENABLE_SPRING_STYLE_SUBSTITUTION_PROP property.

Throws:
Exception

substituteFromEnv

public static final String substituteFromEnv(String str)
                                      throws Exception
Performs substitute(String, PropertySource) using the UtlProps.getEnvProps() as the property source.

Parameters:
str - The String on which to perform substitution.
Returns:
The substituted string.
Throws:
UtlTailoring.TailoringException - If there is an error making substitutions.
Exception

main

public static final void main(String[] args)
                       throws Exception
Throws:
Exception


Copyright © 2016 Neeve Research, LLC. All Rights Reserved.