1 20 21 package net.innig.macker.rule; 22 23 import java.util.*; 24 25 import org.apache.regexp.RE; 26 import org.apache.regexp.RESyntaxException; 27 28 public final class VariableParser 29 { 30 public static String parse(EvaluationContext context, String inS) 31 throws UndeclaredVariableException 32 { 33 StringBuffer outS = new StringBuffer (); 34 for(int pos = 0; pos >= 0; ) 35 { 36 boolean hasAnotherVar = var.match(inS, pos); 37 int expEnd = hasAnotherVar ? var.getParenStart(0) : inS.length(); 38 39 if(pos < expEnd) 40 outS.append(inS.substring(pos, expEnd)); 41 if(hasAnotherVar) 42 outS.append(context.getVariableValue(var.getParen(1))); 43 44 pos = hasAnotherVar ? var.getParenEnd(0) : -1; 45 } 46 return outS.toString(); 47 } 48 49 static private RE var; 50 static 51 { 52 try 53 { var = new RE("\\$\\{([A-Za-z0-9_\\.\\-]+)\\}"); } 54 catch(RESyntaxException rese) 55 { 56 rese.printStackTrace(System.out); 57 throw new RuntimeException ("Can't initialize VariableParser: " + rese); 58 } 59 } 60 61 private VariableParser() { } 62 } 63 64 | Popular Tags |