View Javadoc

1   /*
2    * Copyright 1999,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.log4j.helpers;
18  
19  
20  /***
21   * Formats messages according to very simple rules.
22   * See {@link #format(String,Object)} and
23   * {@link #format(String,Object,Object)} for more details.
24   *
25   * @author Ceki Gülcü
26   */
27  public final class MessageFormatter {
28      /***
29       * Private formatter since all methods and members are static.
30       */
31      private MessageFormatter() {
32          super();
33      }
34  
35      /***
36       * Start of replacement block.
37       */
38      private static final char DELIM_START = '{';
39      /***
40       * End of replacement block.
41       */
42      private static final char DELIM_STOP = '}';
43  
44      /***
45       * Performs single argument substitution for the 'messagePattern' passed as
46       * parameter.
47       * <p/>
48       * For example, <code>MessageFormatter.format("Hi {}.", "there");</code>
49       * will return the string "Hi there.".
50       * <p/>
51       * The {} pair is called the formatting element. It serves to designate the
52       * location where the argument needs to be inserted within the pattern.
53       *
54       * @param messagePattern
55       *     The message pattern which will be parsed and formatted
56       * @param argument
57       *     The argument to be inserted instead of the formatting element
58       * @return The formatted message
59       */
60      public static String format(final String messagePattern,
61                                  final Object argument) {
62          int j = messagePattern.indexOf(DELIM_START);
63          int len = messagePattern.length();
64          char escape = 'x';
65  
66          // if there are no { characters or { is the last character
67          // then we just return messagePattern
68          if (j == -1 || (j + 1 == len)) {
69              return messagePattern;
70          } else {
71              char delimStop = messagePattern.charAt(j + 1);
72              if (j > 0) {
73                  escape = messagePattern.charAt(j - 1);
74              }
75              if ((delimStop != DELIM_STOP) || (escape == '//')) {
76                  // invalid DELIM_START/DELIM_STOP pair or espace character is
77                  // present
78                  return messagePattern;
79              } else {
80                  StringBuffer sbuf = new StringBuffer(len + 20);
81                  sbuf.append(messagePattern.substring(0, j));
82                  sbuf.append(argument);
83                  sbuf.append(messagePattern.substring(j + 2));
84                  return sbuf.toString();
85              }
86          }
87      }
88  
89      /***
90       * /**
91       * Performs a two argument substitution for the 'messagePattern' passed as
92       * parameter.
93       * <p/>
94       * For example, <code>MessageFormatter.format("Hi {}. My name is {}.",
95       * "there", "David");</code> will return the string
96       * "Hi there. My name is David.".
97       * <p/>
98       * The '{}' pair is called a formatting element. It serves to designate the
99       * location where the arguments need to be inserted within
100      * the message pattern.
101      *
102      * @param messagePattern
103      *     The message pattern which will be parsed and formatted
104      * @param arg1
105      *     The first argument to replace the first formatting element
106      * @param arg2
107      *     The second argument to replace the second formatting element
108      * @return The formatted message
109      */
110     public static String format(final String messagePattern,
111                                 final Object arg1,
112                                 final Object arg2) {
113         int i = 0;
114         int len = messagePattern.length();
115 
116         StringBuffer sbuf = new StringBuffer(messagePattern.length() + 50);
117 
118         for (int l = 0; l < 2; l++) {
119             int j = messagePattern.indexOf(DELIM_START, i);
120 
121             if (j == -1 || (j + 1 == len)) {
122                 // no more variables
123                 if (i == 0) { // this is a simple string
124                     return messagePattern;
125                 } else {
126                     // add the tail string which contains no variables
127                     // and return the result.
128                     sbuf.append(messagePattern.substring(i,
129                                     messagePattern.length()));
130                     return sbuf.toString();
131                 }
132             } else {
133                 char delimStop = messagePattern.charAt(j + 1);
134                 if ((delimStop != DELIM_STOP)) {
135                     // invalid DELIM_START/DELIM_STOP pair
136                     sbuf.append(messagePattern.substring(i,
137                             messagePattern.length()));
138                     return sbuf.toString();
139                 }
140                 sbuf.append(messagePattern.substring(i, j));
141                 if (l == 0) {
142                     sbuf.append(arg1);
143                 } else {
144                     sbuf.append(arg2);
145                 }
146                 i = j + 2;
147             }
148         }
149         // append the characters following the second {} pair.
150         sbuf.append(messagePattern.substring(i, messagePattern.length()));
151         return sbuf.toString();
152     }
153 }