public static List<Fragment> parsePlaceholder(String parseString) throws PlaceholderParseException {
  List<Fragment> result=new ArrayList<Fragment>();
  int currOutputIndex=0;
  int currSearchIndex=0;
  while (true) {
    if (currSearchIndex == parseString.length()) {
      break;
    }
    int startIndex=parseString.indexOf("${",currSearchIndex);
    if (startIndex == -1) {
      if (currOutputIndex < parseString.length()) {
        String endString=parseString.substring(currOutputIndex,parseString.length());
        TextFragment textFragment=new TextFragment(endString);
        result.add(textFragment);
      }
      break;
    }
    if (startIndex > 0) {
      String textSoFar=parseString.substring(currOutputIndex,startIndex);
      if (textSoFar.length() != 0) {
        result.add(new TextFragment(textSoFar));
      }
    }
    if ((startIndex > 0) && (parseString.charAt(startIndex - 1) == '$')) {
      currOutputIndex=startIndex + 1;
      currSearchIndex=startIndex + 1;
      continue;
    }
    int endIndex=parseString.indexOf('}',startIndex);
    if (endIndex == -1) {
      throw new PlaceholderParseException("Syntax error in property or variable: '" + parseString.substring(startIndex,parseString.length()) + "'");
    }
    String between=parseString.substring(startIndex + 2,endIndex);
    ParameterFragment parameterFragment=new ParameterFragment(between);
    result.add(parameterFragment);
    currOutputIndex=endIndex + 1;
    currSearchIndex=endIndex;
  }
  LinkedList<Fragment> fragments=new LinkedList<Fragment>();
  fragments.add(result.get(0));
  for (int i=1; i < result.size(); i++) {
    Fragment fragment=result.get(i);
    if (!(result.get(i) instanceof TextFragment)) {
      fragments.add(fragment);
      continue;
    }
    if (!(fragments.getLast() instanceof TextFragment)) {
      fragments.add(fragment);
      continue;
    }
    TextFragment textFragment=(TextFragment)fragments.getLast();
    fragments.removeLast();
    fragments.add(new TextFragment(textFragment.getValue() + fragment.getValue()));
  }
  return fragments;
}

public static List < Fragment > parsePlaceholder ( String parseString ) throws PlaceholderParseException { List < Fragment > result = new ArrayList < Fragment > ( ) ; int currOutputIndex = 0 ; int currSearchIndex = 0 ; while ( true ) { if ( currSearchIndex == parseString . length ( ) ) { break ; } int startIndex = parseString . indexOf ( "${" , currSearchIndex ) ; if ( startIndex == - 1 ) { if ( currOutputIndex < parseString . length ( ) ) { String endString = parseString . substring ( currOutputIndex , parseString . length ( ) ) ; TextFragment textFragment = new TextFragment ( endString ) ; result . add ( textFragment ) ; } break ; } if ( startIndex > 0 ) { String textSoFar = parseString . substring ( currOutputIndex , startIndex ) ; if ( textSoFar . length ( ) != 0 ) { result . add ( new TextFragment ( textSoFar ) ) ; } } if ( ( startIndex > 0 ) && ( parseString . charAt ( startIndex - 1 ) == '$' ) ) { currOutputIndex = startIndex + 1 ; currSearchIndex = startIndex + 1 ; continue ; } int endIndex = parseString . indexOf ( '}' , startIndex ) ; if ( endIndex == - 1 ) { throw new PlaceholderParseException ( "Syntax error in property or variable: '" + parseString . substring ( startIndex , parseString . length ( ) ) + "'" ) ; } String between = parseString . substring ( startIndex + 2 , endIndex ) ; ParameterFragment parameterFragment = new ParameterFragment ( between ) ; result . add ( parameterFragment ) ; currOutputIndex = endIndex + 1 ; currSearchIndex = endIndex ; } LinkedList < Fragment > fragments = new LinkedList < Fragment > ( ) ; fragments . add ( result . get ( 0 ) ) ; for ( int i = 1 ; i < result . size ( ) ; i ++ ) { Fragment fragment = result . get ( i ) ; if ( ! ( result . get ( i ) instanceof TextFragment ) ) { fragments . add ( fragment ) ; continue ; } if ( ! ( fragments . getLast ( ) instanceof TextFragment ) ) { fragments . add ( fragment ) ; continue ; } TextFragment textFragment = ( TextFragment ) fragments . getLast ( ) ; fragments . removeLast ( ) ; fragments . add ( new TextFragment ( textFragment . getValue ( ) + fragment . getValue ( ) ) ) ; } return fragments ; }
parses a string to find placeholders of format $ { placeholder } . < p > example : " my $ { thing } is $ { color } " < p > the example above parses into 4 fragements : a text fragment of value " my " , a parameter fragment " thing " , a text fragement " is " and a parameter fragment " color " .
