ReplacerUtils { public static String jprintf(String template, Map<String, Object> env) { logger.debug("Running jprintf on template: {}", template); Map<String, Object> envVars = new HashMap<>(); Matcher matcher = pattern.matcher(template); String[] variables = matcher.results().map(m -> m.group(1)).toArray(String[]::new); logger.debug("We found {} variables", variables.length); for (String variable : variables) { logger.debug("Handling variable {}", variable); String currentValue; if (variable.indexOf(',') != -1) { String[] varSplitter = variable.split(","); if (varSplitter.length != 2) { logger.warn("Variable format anomaly detected for variable {} in pattern {}", variable, pattern); continue; } String varName = varSplitter[0]; String options = varSplitter[1]; Object currentData = env.get(varName); currentValue = currentData != null ? currentData.toString() : "[Unknown]"; boolean alignLeft = false; if (options.charAt(0) == '-') { alignLeft = true; options = options.substring(1); } int fieldSize; int maxSize = -1; int valueSize = currentValue.length(); if (options.indexOf('.') != -1) { String[] optionsSplit = options.split("\\."); if (optionsSplit.length != 2) { logger.warn("Variable format anomaly detected for variable {} in pattern {}", variable, pattern); continue; } fieldSize = Integer.parseInt(optionsSplit[0]); maxSize = Integer.parseInt(optionsSplit[1]); } else { fieldSize = Integer.parseInt(options); } logger.debug("valueSize: {}, fieldSize: {}, maxSize: {}", valueSize, fieldSize, maxSize); if (maxSize != -1 && valueSize > maxSize) { logger.warn("Value {} has a bigger size than the variable {} should hold (value size: {}, variable max size: {}. We are cutting of the end to make it fit", currentValue, variable, valueSize, maxSize); currentValue = currentValue.substring(0, maxSize); } else { if (fieldSize > valueSize) { if (alignLeft) { currentValue = StringUtils.rightPad(currentValue, fieldSize); } else { currentValue = StringUtils.leftPad(currentValue, fieldSize); } } } } else { Object currentData = env.get(variable); currentValue = currentData != null ? currentData.toString() : "[Unknown]"; } logger.debug("Inserting variable {} with value {} into map", variable, currentValue); envVars.put(variable, currentValue); } StringSubstitutor sub = new StringSubstitutor(envVars); String finalResult = sub.replace(template); logger.debug("Final result for template [{}] is [{}]", template, finalResult); return finalResult; } private  ReplacerUtils(); static String jprintf(String template, Map<String, Object> env); static String jprintf(String key, Map<String, Object> env, ResourceBundle bundle);  }
@Test public void testVarReplacement() { Map<String, Object> env = new HashMap<>(); env.put("position", 3); env.put("user", "test"); env.put("group", "sysop"); env.put("files", 11); env.put("bytes", "10To"); env.put("speed", "27.4MB/s"); env.put("percent", "45"); String template = "#${position,3.3} ${user,-13} @ ${group,-11} - ${files,7}F - ${bytes,7} - ${speed,10} - ${percent,6}"; assertEquals("# 3 test @ sysop - 11F - 10To - 27.4MB/s - 45", jprintf(template, env)); template = "#${position} ${user} @ ${group} - ${files}F - ${bytes} - ${speed} - ${percent}"; assertEquals("#3 test @ sysop - 11F - 10To - 27.4MB/s - 45", jprintf(template, env)); }