1 package org.apache.velocity.runtime.parser.node; 2 17 18 import org.apache.velocity.context.Context; 19 import org.apache.velocity.runtime.parser.*; 20 21 28 public class NodeUtils 29 { 30 39 public static String specialText(Token t) 40 { 41 String specialText = ""; 42 43 if (t.specialToken == null || t.specialToken.image.startsWith("##") ) 44 return specialText; 45 46 Token tmp_t = t.specialToken; 47 48 while (tmp_t.specialToken != null) 49 { 50 tmp_t = tmp_t.specialToken; 51 } 52 53 while (tmp_t != null) 54 { 55 String st = tmp_t.image; 56 57 StringBuffer sb = new StringBuffer (); 58 59 for(int i = 0; i < st.length(); i++) 60 { 61 char c = st.charAt(i); 62 63 if ( c == '#' || c == '$' ) 64 { 65 sb.append( c ); 66 } 67 68 73 74 if ( c == '\\') 75 { 76 boolean ok = true; 77 boolean term = false; 78 79 int j = i; 80 for( ok = true; ok && j < st.length(); j++) 81 { 82 char cc = st.charAt( j ); 83 84 if (cc == '\\') 85 { 86 89 continue; 90 } 91 else if( cc == '$' ) 92 { 93 96 term = true; 97 ok = false; 98 } 99 else 100 { 101 104 ok = false; 105 } 106 } 107 108 if (term) 109 { 110 String foo = st.substring( i, j ); 111 sb.append( foo ); 112 i = j; 113 } 114 } 115 } 116 117 specialText += sb.toString(); 118 119 tmp_t = tmp_t.next; 120 } 121 122 return specialText; 123 } 124 125 129 public static String tokenLiteral( Token t ) 130 { 131 return specialText( t ) + t.image; 132 } 133 134 146 public static String interpolate(String argStr, Context vars) 147 { 148 StringBuffer argBuf = new StringBuffer (); 149 150 for (int cIdx = 0 ; cIdx < argStr.length();) 151 { 152 char ch = argStr.charAt(cIdx); 153 154 switch (ch) 155 { 156 case '$': 157 StringBuffer nameBuf = new StringBuffer (); 158 for (++cIdx ; cIdx < argStr.length(); ++cIdx) 159 { 160 ch = argStr.charAt(cIdx); 161 if (ch == '_' || ch == '-' 162 || Character.isLetterOrDigit(ch)) 163 nameBuf.append(ch); 164 else if (ch == '{' || ch == '}') 165 continue; 166 else 167 break; 168 } 169 170 if (nameBuf.length() > 0) 171 { 172 Object value = vars.get(nameBuf.toString()); 173 174 if (value == null) 175 argBuf.append("$").append(nameBuf.toString()); 176 else 177 argBuf.append(value.toString()); 178 } 179 break; 180 181 default: 182 argBuf.append(ch); 183 ++cIdx; 184 break; 185 } 186 } 187 188 return argBuf.toString(); 189 } 190 } 191 | Popular Tags |