1 7 package com.inversoft.util.variable; 8 9 10 31 public final class VariableExpander { 32 33 36 public final static String START = "${"; 37 38 41 public final static char END = '}'; 42 43 44 63 public static String expand(String str, ExpanderStrategy strategy) 64 throws ExpanderException { 65 assert (strategy != null) : "strategy == null"; 66 67 if (str == null) { 68 return null; 69 } 70 71 int startIndex = str.indexOf(START); 72 int endIndex = -1; 73 if (startIndex != -1) { 74 endIndex = str.indexOf(END, startIndex + 2); 75 } 76 77 if (startIndex == -1 || endIndex == -1) { 78 return str; 79 } 80 81 int length = str.length(); 82 int curPos = 0; 83 String value; 84 StringBuffer buf = new StringBuffer (length); 85 while (startIndex != -1 && endIndex != -1) { 86 value = strategy.expand(str.substring(startIndex + 2, endIndex)); 87 88 buf.append(str.substring(curPos, startIndex)); 89 buf.append(value); 90 91 curPos = endIndex + 1; 92 93 startIndex = str.indexOf(START, curPos); 94 if (startIndex != -1) { 95 endIndex = str.indexOf(END, startIndex + 2); 96 } 97 } 98 99 if (curPos < length) { 100 buf.append(str.substring(curPos)); 101 } 102 103 return buf.toString(); 104 } 105 106 129 public static String expand(StringBuffer buf, ExpanderStrategy strategy) 130 throws ExpanderException { 131 return expand(buf.toString(), strategy); 132 } 133 } 134 135 | Popular Tags |