1 16 17 package org.pentaho.core.util; 18 19 import java.util.ArrayList ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Properties ; 24 import java.util.Set ; 25 import java.util.StringTokenizer ; 26 import java.util.regex.Matcher ; 27 import java.util.regex.Pattern ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.pentaho.core.connection.IPentahoMetaData; 32 import org.pentaho.core.connection.IPentahoResultSet; 33 import org.pentaho.core.runtime.IRuntimeContext; 34 import org.pentaho.core.solution.IParameterProvider; 35 import org.pentaho.core.system.PentahoSystem; 36 import org.pentaho.messages.Messages; 37 import org.pentaho.util.DateMath; 38 39 public class TemplateUtil { 40 41 private final static String PARAMETER_PATTERN = "\\{([^\\}\\{\\s]*)\\}"; private final static String DATE_EXPR_PATTERN = "([\\+\\-]?(\\d*):(\\p{Alpha}{1,2}))([ \t]+[\\+\\-]?(\\d*):(\\p{Alpha}{1,2}))*([ \t]*;(.*))?"; private static final Pattern parameterExpressionPattern = Pattern.compile(PARAMETER_PATTERN); 44 private static final Pattern dateExpressionPattern = Pattern.compile(DATE_EXPR_PATTERN); 45 private static final List SystemInputs = new ArrayList (); 46 47 static { 48 SystemInputs.add("$user"); SystemInputs.add("$url"); SystemInputs.add("$solution"); } 52 53 public static String getSystemInput(String inputName, IRuntimeContext context) { 54 int i = SystemInputs.indexOf(inputName); 55 switch (i) { 56 case 0: { return context.getSession().getName(); 58 } 59 case 1: { return PentahoSystem.getApplicationContext().getBaseUrl(); 61 } 62 case 2: { return PentahoSystem.getApplicationContext().getSolutionPath(""); } 65 } 66 return null; 67 } 68 69 public static String applyTemplate(String template, IRuntimeContext context, IParameterResolver resolver) { 70 return applyTemplate(template, new InputProperties(context), resolver); 71 } 72 73 public static String applyTemplate(String template, IRuntimeContext context) { 74 return applyTemplate(template, new InputProperties(context), null); 75 } 76 77 public static String applyTemplate(String template, IRuntimeContext context, String parameterPatternStr) { 78 Pattern pattern = Pattern.compile( parameterPatternStr ); 79 return applyTemplate(template, new InputProperties(context), pattern, null); 80 } 81 82 public static String applyTemplate(String template, Properties inputs, IParameterResolver resolver) { 83 return applyTemplate(template, inputs, parameterExpressionPattern, resolver); 84 } 85 86 108 public static String applyTemplate(String template, Properties inputs, Pattern parameterPattern, IParameterResolver resolver) { 109 StringBuffer results = new StringBuffer (); 110 Matcher parameterMatcher = parameterPattern.matcher(template); 111 int copyStart = 0; 112 113 while (parameterMatcher.find()) { 114 int start = parameterMatcher.start(); 115 String parameter = parameterMatcher.group(1); 116 String value = null; 117 int pos1 = parameter.indexOf(':'); 118 if (pos1 > -1) { 119 if (resolver != null) { 122 int newCopyStart = resolver.resolveParameter(template, parameter, parameterMatcher, copyStart, results); 123 if (newCopyStart >=0 ) { 124 copyStart = newCopyStart; 125 continue; 126 } 127 } 128 StringTokenizer tokenizer = new StringTokenizer (parameter, ":"); if (tokenizer.countTokens() >= 5) { 130 parameter = tokenizer.nextToken(); 132 String keyColumn = tokenizer.nextToken(); 133 String keyValue = tokenizer.nextToken(); 134 String valueColumn = tokenizer.nextToken(); 135 StringBuffer defaultValue = new StringBuffer (); 136 defaultValue.append(tokenizer.nextToken()); 137 while (tokenizer.hasMoreTokens()) { 138 defaultValue.append(':').append(tokenizer.nextToken()); 139 } 140 if (inputs instanceof InputProperties) { 142 value = ((InputProperties) inputs).getProperty(parameter, keyColumn, keyValue, valueColumn, defaultValue.toString()); 143 } 144 } 145 } 146 if (value == null) { 147 value = inputs.getProperty(parameter); 149 } 150 151 results.append(template.substring(copyStart, start)); 152 copyStart = parameterMatcher.end(); 153 if (value == null) { 154 Matcher dateMatcher = dateExpressionPattern.matcher(parameter); 155 156 if (dateMatcher.matches()) { 157 value = DateMath.calculateDateString(null, parameter); 158 } 159 } 160 161 if (value == null) { 162 results.append(parameterMatcher.group()); 163 } else { 164 results.append(value); 165 } 166 } 167 168 if (copyStart < template.length()) { 169 results.append(template.substring(copyStart)); 170 } 171 172 return results.toString(); 173 } 174 175 public static String applyTemplate(String template, String name, String value) { 176 177 String result = template; 178 result = result.replaceAll("\\{" + name + "\\}", value); return result; 180 181 } 182 183 public static String applyTemplate(String template, String name, String [] value) { 184 if ( value == null ) { 185 return( template ); 186 } 187 188 if ( value.length == 1 ) { 189 return( applyTemplate( template, name, value[0] ) ); 190 } 191 192 int pos = template.indexOf( "{" + name + "}" ); if ( pos == -1 ) { 194 return( template ); 195 } 196 197 int startPos = template.substring(0, pos).lastIndexOf('&'); 198 if ( startPos < 0 ) { 199 startPos = template.substring(0, pos).lastIndexOf('?'); 200 } 201 if ( startPos < 0 ) { 202 startPos = 0; 203 } 204 else { 205 startPos += 1; 206 } 207 208 int endPos = template.substring(pos + name.length()+1).indexOf('&'); 209 if ( endPos < 0 ) { 210 endPos = template.substring(pos + name.length()+1).indexOf('#'); 211 } 212 if ( endPos < 0 ) { 213 endPos = template.length(); 214 } 215 else { 216 endPos += pos + name.length()+1; 217 } 218 219 String result = template.substring( 0, startPos ); 220 String replacePart = template.substring( startPos, endPos ); 221 222 result += replacePart.replaceAll("\\{" + name + "\\}", value[0]); for ( int i = 1; i < value.length; ++i ) { 224 result += "&" + replacePart.replaceAll("\\{" + name + "\\}", value[i]); } 226 227 result += template.substring( endPos ); 228 return result; 229 230 } 231 232 239 private static class InputProperties extends Properties { 240 private static final long serialVersionUID = 1L; 241 242 Pattern dateExpressionRegexPattern = Pattern.compile(DATE_EXPR_PATTERN); 243 244 private IRuntimeContext context; 245 246 private Set inputs; 247 248 private static final Log logger = LogFactory.getLog(InputProperties.class); 249 250 InputProperties(IRuntimeContext context) { 251 this.context = context; 252 inputs = new HashSet (); 253 inputs.addAll(context.getInputNames()); 254 inputs.add("$user"); inputs.add("$url"); inputs.add("$solution"); } 258 259 public int size() { 260 if (inputs == null) { 261 return 0; 262 } else { 263 return inputs.size(); 264 } 265 } 266 267 public String getProperty(String parameter, String keyColumn, String keyValue, String valueColumn, String defaultValue) { 268 if (!context.getInputNames().contains(parameter)) { 269 return "{" + parameter + ":" + keyColumn + ":" + keyValue + ":" + valueColumn + ":" + defaultValue + "}"; } 272 Object valueObj = context.getInputParameterValue(parameter); 273 if (valueObj instanceof IPentahoResultSet) { 274 IPentahoResultSet data = (IPentahoResultSet) valueObj; 275 int keyColumnNo = data.getMetaData().getColumnIndex(keyColumn); 278 if (keyValue.indexOf('_') > 0) { 279 keyValue = keyValue.replace('_', ' '); 280 } 281 int valueColumnNo = data.getMetaData().getColumnIndex(valueColumn); 282 if (keyColumnNo != -1 && valueColumnNo != -1) { 283 for (int row = 0; row < data.getRowCount(); row++) { 284 Object thisKey = data.getValueAt(row, keyColumnNo); 285 if (thisKey != null) { 286 if (keyValue.equals(thisKey.toString())) { 287 return data.getValueAt(row, valueColumnNo).toString(); 290 } 291 } 292 } 293 } 294 } 295 return defaultValue; 296 } 297 298 public String getProperty(String name) { 299 String value = null; 300 if (inputs == null) { 301 return null; 302 } 303 if (inputs.contains(name)) { 304 value = TemplateUtil.getSystemInput(name, context); 305 if (value != null) { 306 return value; 307 } 308 Object valueObj = context.getInputParameterValue(name); 309 if (valueObj instanceof String ) { 310 value = (String ) valueObj; 311 } else if (valueObj instanceof Object []) { 312 Object values[] = (Object []) valueObj; 313 StringBuffer valuesBuffer = new StringBuffer (); 314 for (int i = 0; i < values.length; i++) { 317 if (i == 0) { 318 valuesBuffer.append("'").append(values[i].toString()).append("'"); } else { 320 valuesBuffer.append(",'").append(values[i].toString()).append("'"); } 322 } 323 String valueStr = valuesBuffer.toString(); 324 value = valueStr.substring(1, valueStr.length() - 1); 325 } else if (valueObj instanceof IPentahoResultSet) { 326 IPentahoResultSet rs = (IPentahoResultSet) valueObj; 327 IPentahoMetaData md = rs.getMetaData(); 330 int columnIdx = -1; 331 if (md.getColumnCount() == 1) { 332 columnIdx = 0; 333 } else { 334 columnIdx = md.getColumnIndex(new String [] { name }); 335 } 336 if (columnIdx < 0) { 337 logger.error(Messages.getErrorString("Template.ERROR_0005_COULD_NOT_DETERMINE_COLUMN")); return null; 339 } 340 int rowCount = rs.getRowCount(); 341 Object valueCell = null; 342 StringBuffer valuesBuffer = new StringBuffer (); 343 for (int i = 0; i < rowCount; i++) { 345 valueCell = rs.getValueAt(i, columnIdx); 346 if (i == 0) { 347 valuesBuffer.append("'").append(valueCell.toString()).append("'"); } else { 349 valuesBuffer.append(",'").append(valueCell.toString()).append("'"); } 351 } 352 String valueStr = valuesBuffer.toString(); 353 value = valueStr.substring(1, valueStr.length() - 1); 356 } else if (valueObj != null) { 357 value = valueObj.toString(); 358 } 359 } else { 361 value = super.getProperty(name); 362 } 363 if (value == null) { 364 Matcher dateMatcher = dateExpressionRegexPattern.matcher(name); 365 if (dateMatcher.matches()) { 366 value = DateMath.calculateDateString(null, name); 367 } 368 } else { 369 Matcher dateMatcher = dateExpressionRegexPattern.matcher(value); 370 if (dateMatcher.matches()) { 371 value = DateMath.calculateDateString(null, value); 372 } 373 } 374 375 return value; 376 } 377 } 378 379 public static Properties parametersToProperties(IParameterProvider parameterProvider) { 380 Properties properties = new Properties (); 381 Iterator names = parameterProvider.getParameterNames(); 382 while (names.hasNext()) { 383 String name = (String ) names.next(); 384 String value = parameterProvider.getStringParameter(name, null); 385 if (value != null) { 386 properties.put(name, value); 387 } 388 } 389 return properties; 390 } 391 392 } 393 | Popular Tags |