Java Doc for StrSubstitutor.java in  » Library » Apache-common-lang » org » apache » commons » lang » text » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache common lang » org.apache.commons.lang.text 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.apache.commons.lang.text.StrSubstitutor

StrSubstitutor
public class StrSubstitutor (Code)
Substitutes variables within a string by values.

This class takes a piece of text and substitutes all the variables within it. The default definition of a variable is ${variableName}. The prefix and suffix can be changed via constructors and set methods.

Variable values are typically resolved from a map, but could also be resolved from system properties, or by supplying a custom variable resolver.

The simplest example is to use this class to replace Java System properties. For example:

 StrSubstitutor.replaceSystemProperties(
 "You are running with java.version = ${java.version} and os.name = ${os.name}.");
 

Typical usage of this class follows the following pattern: First an instance is created and initialized with the map that contains the values for the available variables. If a prefix and/or suffix for variables should be used other than the default ones, the appropriate settings can be performed. After that the replace() method can be called passing in the source text for interpolation. In the returned text all variable references (as long as their values are known) will be resolved. The following example demonstrates this:

 Map valuesMap = HashMap();
 valuesMap.put("animal", "quick brown fox");
 valuesMap.put("target", "lazy dog");
 String templateString = "The ${animal} jumped over the ${target}.";
 StrSubstitutor sub = new StrSubstitutor(valuesMap);
 String resolvedString = sub.replace(templateString);
 
yielding:
 The quick brown fox jumped over the lazy dog.
 

In addition to this usage pattern there are some static convenience methods that cover the most common use cases. These methods can be used without the need of manually creating an instance. However if multiple replace operations are to be performed, creating and reusing an instance of this class will be more efficient.

Variable replacement works in a recursive way. Thus, if a variable value contains a variable then that variable will also be replaced. Cyclic replacements are detected and will cause an exception to be thrown.

Sometimes the interpolation's result must contain a variable prefix. As an example take the following source text:

 The variable ${${name}} must be used.
 
Here only the variable's name refered to in the text should be replaced resulting in the text (assuming that the value of the name variable is x):
 The variable ${x} must be used.
 
To achieve this effect there are two possibilities: Either set a different prefix and suffix for variables which do not conflict with the result text you want to produce. The other possibility is to use the escape character, by default '$'. If this character is placed before a variable reference, this reference is ignored and won't be replaced. For example:
 The variable $${${name}} must be used.
 

author:
   Oliver Heger
author:
   Stephen Colebourne
version:
   $Id: StrSubstitutor.java 437554 2006-08-28 06:21:41Z bayard $
since:
   2.2


Field Summary
final public static  charDEFAULT_ESCAPE
     Constant for the default escape character.
final public static  StrMatcherDEFAULT_PREFIX
     Constant for the default variable prefix.
final public static  StrMatcherDEFAULT_SUFFIX
     Constant for the default variable suffix.

Constructor Summary
public  StrSubstitutor()
     Creates a new instance with defaults for variable prefix and suffix and the escaping character.
public  StrSubstitutor(Map valueMap)
     Creates a new instance and initializes it.
public  StrSubstitutor(Map valueMap, String prefix, String suffix)
     Creates a new instance and initializes it.
public  StrSubstitutor(Map valueMap, String prefix, String suffix, char escape)
     Creates a new instance and initializes it.
public  StrSubstitutor(StrLookup variableResolver)
     Creates a new instance and initializes it.
public  StrSubstitutor(StrLookup variableResolver, String prefix, String suffix, char escape)
     Creates a new instance and initializes it.
public  StrSubstitutor(StrLookup variableResolver, StrMatcher prefixMatcher, StrMatcher suffixMatcher, char escape)
     Creates a new instance and initializes it.

Method Summary
public  chargetEscapeChar()
     Returns the escape character.
public  StrMatchergetVariablePrefixMatcher()
     Gets the variable prefix matcher currently in use.

The variable prefix is the characer or characters that identify the start of a variable.

public  StrLookupgetVariableResolver()
     Gets the VariableResolver that is used to lookup variables.
public  StrMatchergetVariableSuffixMatcher()
     Gets the variable suffix matcher currently in use.

The variable suffix is the characer or characters that identify the end of a variable.

public static  Stringreplace(Object source, Map valueMap)
     Replaces all the occurrences of variables in the given source object with their matching values from the map.
public static  Stringreplace(Object source, Map valueMap, String prefix, String suffix)
     Replaces all the occurrences of variables in the given source object with their matching values from the map.
public  Stringreplace(String source)
     Replaces all the occurrences of variables with their matching values from the resolver using the given source string as a template.
public  Stringreplace(String source, int offset, int length)
     Replaces all the occurrences of variables with their matching values from the resolver using the given source string as a template.
public  Stringreplace(char[] source)
     Replaces all the occurrences of variables with their matching values from the resolver using the given source array as a template.
public  Stringreplace(char[] source, int offset, int length)
     Replaces all the occurrences of variables with their matching values from the resolver using the given source array as a template.
public  Stringreplace(StringBuffer source)
     Replaces all the occurrences of variables with their matching values from the resolver using the given source buffer as a template.
public  Stringreplace(StringBuffer source, int offset, int length)
     Replaces all the occurrences of variables with their matching values from the resolver using the given source buffer as a template.
public  Stringreplace(StrBuilder source)
     Replaces all the occurrences of variables with their matching values from the resolver using the given source builder as a template.
public  Stringreplace(StrBuilder source, int offset, int length)
     Replaces all the occurrences of variables with their matching values from the resolver using the given source builder as a template.
public  Stringreplace(Object source)
     Replaces all the occurrences of variables in the given source object with their matching values from the resolver.
public  booleanreplaceIn(StringBuffer source)
     Replaces all the occurrences of variables within the given source buffer with their matching values from the resolver.
public  booleanreplaceIn(StringBuffer source, int offset, int length)
     Replaces all the occurrences of variables within the given source buffer with their matching values from the resolver.
public  booleanreplaceIn(StrBuilder source)
     Replaces all the occurrences of variables within the given source builder with their matching values from the resolver.
public  booleanreplaceIn(StrBuilder source, int offset, int length)
     Replaces all the occurrences of variables within the given source builder with their matching values from the resolver.
public static  StringreplaceSystemProperties(Object source)
     Replaces all the occurrences of variables in the given source object with their matching values from the system properties.
protected  StringresolveVariable(String variableName, StrBuilder buf, int startPos, int endPos)
     Internal method that resolves the value of a variable.

Most users of this class do not need to call this method.

public  voidsetEscapeChar(char escapeCharacter)
     Sets the escape character.
public  StrSubstitutorsetVariablePrefix(char prefix)
     Sets the variable prefix to use.

The variable prefix is the characer or characters that identify the start of a variable.

public  StrSubstitutorsetVariablePrefix(String prefix)
     Sets the variable prefix to use.

The variable prefix is the characer or characters that identify the start of a variable.

public  StrSubstitutorsetVariablePrefixMatcher(StrMatcher prefixMatcher)
     Sets the variable prefix matcher currently in use.

The variable prefix is the characer or characters that identify the start of a variable.

public  voidsetVariableResolver(StrLookup variableResolver)
     Sets the VariableResolver that is used to lookup variables.
public  StrSubstitutorsetVariableSuffix(char suffix)
     Sets the variable suffix to use.

The variable suffix is the characer or characters that identify the end of a variable.

public  StrSubstitutorsetVariableSuffix(String suffix)
     Sets the variable suffix to use.

The variable suffix is the characer or characters that identify the end of a variable.

public  StrSubstitutorsetVariableSuffixMatcher(StrMatcher suffixMatcher)
     Sets the variable suffix matcher currently in use.

The variable suffix is the characer or characters that identify the end of a variable.

protected  booleansubstitute(StrBuilder buf, int offset, int length)
     Internal method that substitutes the variables.

Most users of this class do not need to call this method.


Field Detail
DEFAULT_ESCAPE
final public static char DEFAULT_ESCAPE(Code)
Constant for the default escape character.



DEFAULT_PREFIX
final public static StrMatcher DEFAULT_PREFIX(Code)
Constant for the default variable prefix.



DEFAULT_SUFFIX
final public static StrMatcher DEFAULT_SUFFIX(Code)
Constant for the default variable suffix.




Constructor Detail
StrSubstitutor
public StrSubstitutor()(Code)
Creates a new instance with defaults for variable prefix and suffix and the escaping character.



StrSubstitutor
public StrSubstitutor(Map valueMap)(Code)
Creates a new instance and initializes it. Uses defaults for variable prefix and suffix and the escaping character.
Parameters:
  valueMap - the map with the variables' values, may be null



StrSubstitutor
public StrSubstitutor(Map valueMap, String prefix, String suffix)(Code)
Creates a new instance and initializes it. Uses a default escaping character.
Parameters:
  valueMap - the map with the variables' values, may be null
Parameters:
  prefix - the prefix for variables, not null
Parameters:
  suffix - the suffix for variables, not null
throws:
  IllegalArgumentException - if the prefix or suffix is null



StrSubstitutor
public StrSubstitutor(Map valueMap, String prefix, String suffix, char escape)(Code)
Creates a new instance and initializes it.
Parameters:
  valueMap - the map with the variables' values, may be null
Parameters:
  prefix - the prefix for variables, not null
Parameters:
  suffix - the suffix for variables, not null
Parameters:
  escape - the escape character
throws:
  IllegalArgumentException - if the prefix or suffix is null



StrSubstitutor
public StrSubstitutor(StrLookup variableResolver)(Code)
Creates a new instance and initializes it.
Parameters:
  variableResolver - the variable resolver, may be null



StrSubstitutor
public StrSubstitutor(StrLookup variableResolver, String prefix, String suffix, char escape)(Code)
Creates a new instance and initializes it.
Parameters:
  variableResolver - the variable resolver, may be null
Parameters:
  prefix - the prefix for variables, not null
Parameters:
  suffix - the suffix for variables, not null
Parameters:
  escape - the escape character
throws:
  IllegalArgumentException - if the prefix or suffix is null



StrSubstitutor
public StrSubstitutor(StrLookup variableResolver, StrMatcher prefixMatcher, StrMatcher suffixMatcher, char escape)(Code)
Creates a new instance and initializes it.
Parameters:
  variableResolver - the variable resolver, may be null
Parameters:
  prefixMatcher - the prefix for variables, not null
Parameters:
  suffixMatcher - the suffix for variables, not null
Parameters:
  escape - the escape character
throws:
  IllegalArgumentException - if the prefix or suffix is null




Method Detail
getEscapeChar
public char getEscapeChar()(Code)
Returns the escape character. the character used for escaping variable references



getVariablePrefixMatcher
public StrMatcher getVariablePrefixMatcher()(Code)
Gets the variable prefix matcher currently in use.

The variable prefix is the characer or characters that identify the start of a variable. This prefix is expressed in terms of a matcher allowing advanced prefix matches. the prefix matcher in use




getVariableResolver
public StrLookup getVariableResolver()(Code)
Gets the VariableResolver that is used to lookup variables. the VariableResolver



getVariableSuffixMatcher
public StrMatcher getVariableSuffixMatcher()(Code)
Gets the variable suffix matcher currently in use.

The variable suffix is the characer or characters that identify the end of a variable. This suffix is expressed in terms of a matcher allowing advanced suffix matches. the suffix matcher in use




replace
public static String replace(Object source, Map valueMap)(Code)
Replaces all the occurrences of variables in the given source object with their matching values from the map.
Parameters:
  source - the source text containing the variables to substitute, null returns null
Parameters:
  valueMap - the map with the values, may be null the result of the replace operation



replace
public static String replace(Object source, Map valueMap, String prefix, String suffix)(Code)
Replaces all the occurrences of variables in the given source object with their matching values from the map. This method allows to specifiy a custom variable prefix and suffix
Parameters:
  source - the source text containing the variables to substitute, null returns null
Parameters:
  valueMap - the map with the values, may be null
Parameters:
  prefix - the prefix of variables, not null
Parameters:
  suffix - the suffix of variables, not null the result of the replace operation
throws:
  IllegalArgumentException - if the prefix or suffix is null



replace
public String replace(String source)(Code)
Replaces all the occurrences of variables with their matching values from the resolver using the given source string as a template.
Parameters:
  source - the string to replace in, null returns null the result of the replace operation



replace
public String replace(String source, int offset, int length)(Code)
Replaces all the occurrences of variables with their matching values from the resolver using the given source string as a template.

Only the specified portion of the string will be processed. The rest of the string is not processed, and is not returned.
Parameters:
  source - the string to replace in, null returns null
Parameters:
  offset - the start offset within the array, must be valid
Parameters:
  length - the length within the array to be processed, must be valid the result of the replace operation




replace
public String replace(char[] source)(Code)
Replaces all the occurrences of variables with their matching values from the resolver using the given source array as a template. The array is not altered by this method.
Parameters:
  source - the character array to replace in, not altered, null returns null the result of the replace operation



replace
public String replace(char[] source, int offset, int length)(Code)
Replaces all the occurrences of variables with their matching values from the resolver using the given source array as a template. The array is not altered by this method.

Only the specified portion of the array will be processed. The rest of the array is not processed, and is not returned.
Parameters:
  source - the character array to replace in, not altered, null returns null
Parameters:
  offset - the start offset within the array, must be valid
Parameters:
  length - the length within the array to be processed, must be valid the result of the replace operation




replace
public String replace(StringBuffer source)(Code)
Replaces all the occurrences of variables with their matching values from the resolver using the given source buffer as a template. The buffer is not altered by this method.
Parameters:
  source - the buffer to use as a template, not changed, null returns null the result of the replace operation



replace
public String replace(StringBuffer source, int offset, int length)(Code)
Replaces all the occurrences of variables with their matching values from the resolver using the given source buffer as a template. The buffer is not altered by this method.

Only the specified portion of the buffer will be processed. The rest of the buffer is not processed, and is not returned.
Parameters:
  source - the buffer to use as a template, not changed, null returns null
Parameters:
  offset - the start offset within the array, must be valid
Parameters:
  length - the length within the array to be processed, must be valid the result of the replace operation




replace
public String replace(StrBuilder source)(Code)
Replaces all the occurrences of variables with their matching values from the resolver using the given source builder as a template. The builder is not altered by this method.
Parameters:
  source - the builder to use as a template, not changed, null returns null the result of the replace operation



replace
public String replace(StrBuilder source, int offset, int length)(Code)
Replaces all the occurrences of variables with their matching values from the resolver using the given source builder as a template. The builder is not altered by this method.

Only the specified portion of the builder will be processed. The rest of the builder is not processed, and is not returned.
Parameters:
  source - the builder to use as a template, not changed, null returns null
Parameters:
  offset - the start offset within the array, must be valid
Parameters:
  length - the length within the array to be processed, must be valid the result of the replace operation




replace
public String replace(Object source)(Code)
Replaces all the occurrences of variables in the given source object with their matching values from the resolver. The input source object is converted to a string using toString and is not altered.
Parameters:
  source - the source to replace in, null returns null the result of the replace operation



replaceIn
public boolean replaceIn(StringBuffer source)(Code)
Replaces all the occurrences of variables within the given source buffer with their matching values from the resolver. The buffer is updated with the result.
Parameters:
  source - the buffer to replace in, updated, null returns zero true if altered



replaceIn
public boolean replaceIn(StringBuffer source, int offset, int length)(Code)
Replaces all the occurrences of variables within the given source buffer with their matching values from the resolver. The buffer is updated with the result.

Only the specified portion of the buffer will be processed. The rest of the buffer is not processed, but it is not deleted.
Parameters:
  source - the buffer to replace in, updated, null returns zero
Parameters:
  offset - the start offset within the array, must be valid
Parameters:
  length - the length within the buffer to be processed, must be valid true if altered




replaceIn
public boolean replaceIn(StrBuilder source)(Code)
Replaces all the occurrences of variables within the given source builder with their matching values from the resolver.
Parameters:
  source - the builder to replace in, updated, null returns zero true if altered



replaceIn
public boolean replaceIn(StrBuilder source, int offset, int length)(Code)
Replaces all the occurrences of variables within the given source builder with their matching values from the resolver.

Only the specified portion of the builder will be processed. The rest of the builder is not processed, but it is not deleted.
Parameters:
  source - the builder to replace in, null returns zero
Parameters:
  offset - the start offset within the array, must be valid
Parameters:
  length - the length within the builder to be processed, must be valid true if altered




replaceSystemProperties
public static String replaceSystemProperties(Object source)(Code)
Replaces all the occurrences of variables in the given source object with their matching values from the system properties.
Parameters:
  source - the source text containing the variables to substitute, null returns null the result of the replace operation



resolveVariable
protected String resolveVariable(String variableName, StrBuilder buf, int startPos, int endPos)(Code)
Internal method that resolves the value of a variable.

Most users of this class do not need to call this method. This method is called automatically by the substitution process.

Writers of subclasses can override this method if they need to alter how each substitution occurs. The method is passed the variable's name and must return the corresponding value. This implementation uses the StrSubstitutor.getVariableResolver() with the variable's name as the key.
Parameters:
  variableName - the name of the variable, not null
Parameters:
  buf - the buffer where the substitution is occurring, not null
Parameters:
  startPos - the start position of the variable including the prefix, valid
Parameters:
  endPos - the end position of the variable including the suffix, valid the variable's value or null if the variable is unknown




setEscapeChar
public void setEscapeChar(char escapeCharacter)(Code)
Sets the escape character. If this character is placed before a variable reference in the source text, this variable will be ignored.
Parameters:
  escapeCharacter - the escape character (0 for disabling escaping)



setVariablePrefix
public StrSubstitutor setVariablePrefix(char prefix)(Code)
Sets the variable prefix to use.

The variable prefix is the characer or characters that identify the start of a variable. This method allows a single character prefix to be easily set.
Parameters:
  prefix - the prefix character to use this, to enable chaining




setVariablePrefix
public StrSubstitutor setVariablePrefix(String prefix)(Code)
Sets the variable prefix to use.

The variable prefix is the characer or characters that identify the start of a variable. This method allows a string prefix to be easily set.
Parameters:
  prefix - the prefix for variables, not null this, to enable chaining
throws:
  IllegalArgumentException - if the prefix is null




setVariablePrefixMatcher
public StrSubstitutor setVariablePrefixMatcher(StrMatcher prefixMatcher)(Code)
Sets the variable prefix matcher currently in use.

The variable prefix is the characer or characters that identify the start of a variable. This prefix is expressed in terms of a matcher allowing advanced prefix matches.
Parameters:
  prefixMatcher - the prefix matcher to use, null ignored this, to enable chaining
throws:
  IllegalArgumentException - if the prefix matcher is null




setVariableResolver
public void setVariableResolver(StrLookup variableResolver)(Code)
Sets the VariableResolver that is used to lookup variables.
Parameters:
  variableResolver - the VariableResolver



setVariableSuffix
public StrSubstitutor setVariableSuffix(char suffix)(Code)
Sets the variable suffix to use.

The variable suffix is the characer or characters that identify the end of a variable. This method allows a single character suffix to be easily set.
Parameters:
  suffix - the suffix character to use this, to enable chaining




setVariableSuffix
public StrSubstitutor setVariableSuffix(String suffix)(Code)
Sets the variable suffix to use.

The variable suffix is the characer or characters that identify the end of a variable. This method allows a string suffix to be easily set.
Parameters:
  suffix - the suffix for variables, not null this, to enable chaining
throws:
  IllegalArgumentException - if the suffix is null




setVariableSuffixMatcher
public StrSubstitutor setVariableSuffixMatcher(StrMatcher suffixMatcher)(Code)
Sets the variable suffix matcher currently in use.

The variable suffix is the characer or characters that identify the end of a variable. This suffix is expressed in terms of a matcher allowing advanced suffix matches.
Parameters:
  suffixMatcher - the suffix matcher to use, null ignored this, to enable chaining
throws:
  IllegalArgumentException - if the suffix matcher is null




substitute
protected boolean substitute(StrBuilder buf, int offset, int length)(Code)
Internal method that substitutes the variables.

Most users of this class do not need to call this method. This method will be called automatically by another (public) method.

Writers of subclasses can override this method if they need access to the substitution process at the start or end.
Parameters:
  buf - the string builder to substitute into, not null
Parameters:
  offset - the start offset within the builder, must be valid
Parameters:
  length - the length within the builder to be processed, must be valid true if altered




Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.