private static <T> String applyTemplate(T t, String template) { List<TemplateVariable> vars = TemplateUtil.getVariables(template); Map<String, String> map = genItemFieldMap(t); for (TemplateVariable var : vars) { String name = var.getName(); if (map.containsKey(name)) { var.setValue(map.get(name)); } } return TemplateUtil.getResult(vars, template); }
private static <T> String applyTextPager(long offset, long size, String template, String text) { List<TemplateVariable> vars = TemplateUtil.getVariables(template); for (TemplateVariable var : vars) { String name = var.getName(); if (name.equals("page")) { var.setValue(text); } else if (name.equals("offset")) { var.setValue(String.valueOf(offset)); } else if (name.equals("size")) { var.setValue(String.valueOf(size)); } } return TemplateUtil.getResult(vars, template); }
/** * This method is called when TemplateUtil.applyTemplate() encounters a parameter. * * @param template the source string * @param parameter the parameter value * @param parameterMatcher the regex parameter matcher * @param copyStart the start of the copy * @param results the output result * @return the next copystart */ public int resolveParameter( final String template, final String parameter, final Matcher parameterMatcher, int copyStart, final StringBuffer results) { StringTokenizer tokenizer = new StringTokenizer(parameter, ":"); // $NON-NLS-1$ if (tokenizer.countTokens() == 2) { // Currently, the component only handles one bit of metadata String parameterPrefix = tokenizer.nextToken(); String inputName = tokenizer.nextToken(); if (parameterPrefix.equals(prefix)) { // We know this parameter is for us. // First, is this a special input Object parameterValue = TemplateUtil.getSystemInput(inputName, runtimecontext); if ((parameterValue == null) && lookupMap.containsKey(inputName)) { parameterValue = lookupMap.get(inputName); } if (parameterValue != null) { // We have a parameter value - now, it's time to create a parameter and build up the // parameter string int start = parameterMatcher.start(); int end = parameterMatcher.end(); // We now have a valid start and end. It's time to see whether we're dealing // with an array, a result set, or a scalar. StringBuffer parameterBuffer = new StringBuffer(); // find and remove the next placeholder, to be replaced by the new value if (parameterValue instanceof String) { parameterBuffer.append( ((String) parameterValue).replaceAll("'", "\\'")); // $NON-NLS-1$ //$NON-NLS-2$ } else if (parameterValue instanceof Object[]) { Object[] pObj = (Object[]) parameterValue; for (Object element : pObj) { // TODO: escape quotes! parameterBuffer.append( (parameterBuffer.length() == 0) ? "'" + element + "'" : ",'" + element + "'"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ } } else if (parameterValue instanceof IPentahoResultSet) { IPentahoResultSet rs = (IPentahoResultSet) parameterValue; // See if we can find a column in the metadata with the same // name as the input IPentahoMetaData md = rs.getMetaData(); int columnIdx = -1; if (md.getColumnCount() == 1) { columnIdx = 0; } else { columnIdx = md.getColumnIndex(new String[] {parameter}); } if (columnIdx < 0) { error( Messages.getErrorString( "Template.ERROR_0005_COULD_NOT_DETERMINE_COLUMN")); //$NON-NLS-1$ return -1; } int rowCount = rs.getRowCount(); Object valueCell = null; // TODO support non-string columns for (int i = 0; i < rowCount; i++) { valueCell = rs.getValueAt(i, columnIdx); // TODO: escape quotes! parameterBuffer.append( (parameterBuffer.length() == 0) ? "'" + valueCell + "'" : ",'" + valueCell + "'"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ } } else if (parameterValue instanceof List) { List pObj = (List) parameterValue; for (int i = 0; i < pObj.size(); i++) { parameterBuffer.append( (parameterBuffer.length() == 0) ? "'" + pObj.get(i) + "'" : ",'" + pObj.get(i) + "'"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ } } else { // If we're here, we know parameterValue is not null and not a string parameterBuffer.append( parameterValue.toString().replaceAll("'", "\\'")); // $NON-NLS-1$ //$NON-NLS-2$ } // OK - We have a parameterBuffer and have filled out the preparedParameters // list. It's time to change the SQL to insert our parameter marker and tell // the caller we've done our job. results.append(template.substring(copyStart, start)); copyStart = end; results.append(parameterBuffer); return copyStart; } } } return -1; // Nothing here for us - let default behavior through }