1 24 25 package org.slf4j.helpers; 26 27 28 51 public class MessageFormatter { 52 static final char DELIM_START = '{'; 53 static final char DELIM_STOP = '}'; 54 55 66 public static String format(String messagePattern, Object arg) { 67 return arrayFormat(messagePattern, new Object [] {arg}); 68 } 69 70 84 public static String format(String messagePattern, Object arg1, Object arg2) { 85 return arrayFormat(messagePattern, new Object [] {arg1, arg2}); 86 } 87 88 97 public static String arrayFormat(String messagePattern, Object [] argArray) { 98 if(messagePattern == null) { 99 return null; 100 } 101 int i = 0; 102 int len = messagePattern.length(); 103 int j = messagePattern.indexOf(DELIM_START); 104 105 106 107 StringBuffer sbuf = new StringBuffer (messagePattern.length() + 50); 108 109 for (int L = 0; L < argArray.length; L++) { 110 111 char escape = 'x'; 112 113 j = messagePattern.indexOf(DELIM_START, i); 114 115 if (j == -1 || (j+1 == len)) { 116 if (i == 0) { return messagePattern; 119 } else { sbuf.append(messagePattern.substring(i, messagePattern.length())); 121 return sbuf.toString(); 122 } 123 } else { 124 char delimStop = messagePattern.charAt(j + 1); 125 if (j > 0) { 126 escape = messagePattern.charAt(j - 1); 127 } 128 129 if(escape == '\\') { 130 L--; sbuf.append(messagePattern.substring(i, j-1)); 132 sbuf.append(DELIM_START); 133 i = j + 1; 134 } else if ((delimStop != DELIM_STOP)) { 135 sbuf.append(messagePattern.substring(i, messagePattern.length())); 137 return sbuf.toString(); 138 } else { 139 sbuf.append(messagePattern.substring(i, j)); 141 sbuf.append(argArray[L]); 142 i = j + 2; 143 } 144 } 145 } 146 sbuf.append(messagePattern.substring(i, messagePattern.length())); 148 return sbuf.toString(); 149 } 150 } 151 | Popular Tags |