xmlBlaster 2.2.0 API

org.xmlBlaster.util
Class ReplaceVariable

java.lang.Object
  extended by org.xmlBlaster.util.ReplaceVariable

public final class ReplaceVariable
extends java.lang.Object

Author:
xmlBlaster@marcelruff.info
See Also:
org.xmlBlaster.test.classtest.ReplaceVariableTest, The util.property.env requirement

Field Summary
private  java.lang.String endToken
           
private  int maxNest
           
private  java.lang.String startToken
           
private  boolean throwException
           
 
Constructor Summary
ReplaceVariable()
           
ReplaceVariable(java.lang.String startToken, java.lang.String endToken)
           
 
Method Summary
 java.lang.String charChain(char oneChar, int count)
          returns a String that contains count time the passed character
static java.lang.String extract(java.lang.String xml, java.lang.String tag)
          Find the given tag from the given xml string and return its value.
static java.lang.String extract(java.lang.String xml, java.lang.String tag, java.lang.String attributeName)
          Find the given attribute from the given tag from the given xml string and return its value.
static java.lang.String extractIgnoreAttr(java.lang.String xml, java.lang.String tag)
          If the tag can have attributes
static java.lang.String extractWithMatchingAttrs(java.lang.String xml, java.lang.String tag, java.lang.String attrString)
          Find the given tag from the given xml string and return its value.
static void main(java.lang.String[] args)
          Method for testing only.
 java.lang.String replace(java.lang.String text, I_ReplaceVariable cb)
          Replace dynamic variables, e.g.
static java.lang.String replaceAll(java.lang.String str, int fromIndex, java.lang.String from, java.lang.String to)
          Replace all occurrences of "from" with to "to".
static java.lang.String replaceAll(java.lang.String str, java.lang.String from, java.lang.String to)
          Replace all occurrences of "from" with to "to".
static java.lang.String replaceFirst(java.lang.String str, java.lang.String from, java.lang.String to)
          Replace exactly one occurrence of "from" with to "to"
 void setMaxNest(int maxNest)
           
 void setThrowException(boolean throwException)
           
static java.lang.String[] toArray(java.lang.String str, java.lang.String separator)
          Convert a separator based string to an array of strings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

maxNest

private int maxNest

startToken

private java.lang.String startToken

endToken

private java.lang.String endToken

throwException

private boolean throwException
Constructor Detail

ReplaceVariable

public ReplaceVariable()

ReplaceVariable

public ReplaceVariable(java.lang.String startToken,
                       java.lang.String endToken)
Method Detail

setMaxNest

public void setMaxNest(int maxNest)

setThrowException

public void setThrowException(boolean throwException)

charChain

public java.lang.String charChain(char oneChar,
                                  int count)
returns a String that contains count time the passed character

Parameters:
oneChar - char
count - int
Returns:
java.lang.String

replace

public final java.lang.String replace(java.lang.String text,
                                      I_ReplaceVariable cb)
Replace dynamic variables, e.g. ${XY} with their values.

The maximum replacement (nesting) depth is 50.

Parameters:
text - The value string which may contain zero to many ${...} variables
cb - The callback supplied by you which replaces the found keys (from ${key})
Returns:
The new value where all resolvable ${} are replaced.
Throws:
java.lang.IllegalArgumentException - if matching "}" is missing

replaceAll

public static final java.lang.String replaceAll(java.lang.String str,
                                                int fromIndex,
                                                java.lang.String from,
                                                java.lang.String to)
Replace all occurrences of "from" with to "to".


replaceAll

public static final java.lang.String replaceAll(java.lang.String str,
                                                java.lang.String from,
                                                java.lang.String to)
Replace all occurrences of "from" with to "to".


replaceFirst

public static final java.lang.String replaceFirst(java.lang.String str,
                                                  java.lang.String from,
                                                  java.lang.String to)
Replace exactly one occurrence of "from" with to "to"


toArray

public static final java.lang.String[] toArray(java.lang.String str,
                                               java.lang.String separator)
Convert a separator based string to an array of strings.

Example:
NameList=Josua,David,Ken,Abel
Will return each name separately in the array.

Parameters:
key - the key to look for
defaultVal - The default value to return if key is not found
separator - The separator, typically ","
Returns:
The String array for the given key, the elements are trimmed (no leading/following white spaces), is never null

extract

public static java.lang.String extract(java.lang.String xml,
                                       java.lang.String tag)
Find the given tag from the given xml string and return its value. Does not work if the tag exists multiple time or occures somewhere else in the text

Parameters:
tag - For example "nodeId" for a tag <nodeId>value</nodeId>
Returns:
null if none is found

extractIgnoreAttr

public static java.lang.String extractIgnoreAttr(java.lang.String xml,
                                                 java.lang.String tag)
If the tag can have attributes

Parameters:
xml -
tag - For example "nodeId" for a tag <nodeId something='ignored'>value</nodeId>
Returns:
null if none is found, else 'value'

extractWithMatchingAttrs

public static java.lang.String extractWithMatchingAttrs(java.lang.String xml,
                                                        java.lang.String tag,
                                                        java.lang.String attrString)
Find the given tag from the given xml string and return its value. Does not work if the tag exists multiple time or occures somewhere else in the text

Parameters:
tag - For example "nodeId" for a tag <nodeId>value</nodeId>
attrToMatch. - If you want to parse <nodeId name='dummy' x='3'>value</nodeId> then you need to pass " name='dummy' x='3'" here.
Returns:
null if none is found

extract

public static java.lang.String extract(java.lang.String xml,
                                       java.lang.String tag,
                                       java.lang.String attributeName)
Find the given attribute from the given tag from the given xml string and return its value. Does not work if the tag exists multiple time or occures somewhere else in the text

Parameters:
xml - The xml string to parse
tag - For example "node" for a tag <node id='heron'>
attributeName - "id"
Returns:
null if none is found

main

public static void main(java.lang.String[] args)
Method for testing only.
Invoke: java org.xmlBlaster.util.ReplaceVariable -template 'Hello ${A} and ${B}, ${A${B}}' -A ZZXX -B OO -AOO OK java org.xmlBlaster.util.ReplaceVariable -template 'Hello ${A} and ${B}, ${A${B}}' -A ZZXX -B OO -AOO OK java org.xmlBlaster.util.ReplaceVariable -startToken @ -endToken @ -template '@A@' -A aa -B bb -Abb WRONG -aabb OK


xmlBlaster 2.2.0 API

Copyright © 1999-2014 The xmlBlaster.org contributers.