scriptella.expression
Class PropertiesSubstitutor

java.lang.Object
  extended by scriptella.expression.PropertiesSubstitutor

public class PropertiesSubstitutor
extends java.lang.Object

Substitutes properties(or expressions) in strings.

$ symbol indicate property or expression to evaluate and substitute.

The following properties/expression syntax is used:

Property reference

References named property.
Examples:

 $foo
 

Expression

. Expression is wrapped by braces and evaluated by Expression engine. Examples:

 ${name+' '+surname} etc.
 

This class is not thread safe

Version:
1.0
Author:
Fyodor Kupolov

Field Summary
static java.util.regex.Pattern EXPR_PTR
          Expression pattern, e.g.
static java.util.regex.Pattern PROP_PTR
          Simple property patterns, e.g.
 
Constructor Summary
PropertiesSubstitutor()
          Creates a properties substitutor.
PropertiesSubstitutor(java.util.Map<java.lang.String,?> map)
          Creates a properties substitutor based on specified properties map.
PropertiesSubstitutor(ParametersCallback parameters)
          Creates a properties substitutor.
 
Method Summary
 java.lang.String getNullString()
          Returns string literal representing null value.
 ParametersCallback getParameters()
           
static boolean hasProperties(java.lang.String string)
          Tests if the given string contains properties/expressions.
 void setNullString(java.lang.String nullString)
          Sets string literal representing null.
 void setParameters(ParametersCallback parameters)
          Sets parameters callback used for substitution.
 java.lang.String substitute(java.io.Reader reader)
          Reads content from reader and expands properties.
 void substitute(java.io.Reader reader, java.io.Writer writer)
          Copies content from reader to writer and expands properties.
 java.lang.String substitute(java.lang.String s)
          Substitutes properties/expressions in s and returns the result string.
protected  java.lang.String toString(java.lang.Object o)
          Converts specified object to string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROP_PTR

public static final java.util.regex.Pattern PROP_PTR
Simple property patterns, e.g. $property


EXPR_PTR

public static final java.util.regex.Pattern EXPR_PTR
Expression pattern, e.g. ${property} etc.

Constructor Detail

PropertiesSubstitutor

public PropertiesSubstitutor()
Creates a properties substitutor.

This constructor is used for performance critical places where multiple instantiation via PropertiesSubstitutor(scriptella.spi.ParametersCallback) is expensive.

Note: setParameters(scriptella.spi.ParametersCallback) must be called before substitute(String).


PropertiesSubstitutor

public PropertiesSubstitutor(ParametersCallback parameters)
Creates a properties substitutor.

Parameters:
parameters - parameters callback to use for substitution.

PropertiesSubstitutor

public PropertiesSubstitutor(java.util.Map<java.lang.String,?> map)
Creates a properties substitutor based on specified properties map.

Parameters:
map - parameters to substitute.
Method Detail

substitute

public java.lang.String substitute(java.lang.String s)
Substitutes properties/expressions in s and returns the result string.

If result of evaluation is null or the property being substitued doesn't have value in callback - the whole expressions is copied into result string as is.

Parameters:
s - string to substitute. Null strings allowed.
Returns:
substituted string.

substitute

public void substitute(java.io.Reader reader,
                       java.io.Writer writer)
                throws java.io.IOException
Copies content from reader to writer and expands properties.

Parameters:
reader - reader to process.
writer - writer to output substituted content to.
Throws:
java.io.IOException - if I/O error occurs.

substitute

public java.lang.String substitute(java.io.Reader reader)
                            throws java.io.IOException
Reads content from reader and expands properties.

Note: For performance reasons use substitute(java.io.Reader,java.io.Writer) if possible.

Parameters:
reader - reader to process.
Returns:
reader's content with properties expanded.
Throws:
java.io.IOException - if I/O error occurs.
See Also:
substitute(java.io.Reader,java.io.Writer)

getParameters

public ParametersCallback getParameters()
Returns:
parameter callback used for substitution.

setParameters

public void setParameters(ParametersCallback parameters)
Sets parameters callback used for substitution.

Parameters:
parameters - not null parameters callback.

getNullString

public java.lang.String getNullString()
Returns string literal representing null value.

Used when converting objects toString(Object).

Returns:
string representing null value.

setNullString

public void setNullString(java.lang.String nullString)
Sets string literal representing null.

Used when converting objects toString(Object).

Parameters:
nullString - string literal representing null

toString

protected java.lang.String toString(java.lang.Object o)
Converts specified object to string.

getNullString() represents null values.

Subclasses may provide custom conversion strategy here.

Parameters:
o - object to convert to String.
Returns:
string representation of object.

hasProperties

public static boolean hasProperties(java.lang.String string)
Tests if the given string contains properties/expressions.

Parameters:
string - string to check.
Returns:
true if a given string contains properties/expressions.


Copyright © Copyright 2006-2009 The Scriptella Project Team.