View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.util;
20  
21  import org.apache.myfaces.trinidad.logging.TrinidadLogger;
22  
23  /**
24   * The FastMessageFormat class is a greatly reduced version
25   * of the java.text.MessageFormat class.  It's also much faster
26   * and much less expensive to create, which is especially
27   * valuable when it is created and thrown away many times - 
28   * a common use case in web applications.
29   * <p>
30   * The only syntax supported by this class is simple index-based
31   * replacement, namely:
32   * <pre>
33   *     some{1}text{0}here{2}andthere
34   * </pre>
35   * Unlike MessageFormat, single quotes are NOT used for escaping.  
36   * So, the following pattern could be used to include a left bracket:
37   * <pre>
38   *     some{text{0}
39   * </pre>
40   * <p>
41   * @version $Name:  $ ($Revision: adfrt/faces/adf-faces-api/src/main/java/oracle/adf/view/faces/util/FastMessageFormat.java#0 $) $Date: 10-nov-2005.19:08:37 $
42   */
43  public class FastMessageFormat
44  {
45    /**
46     * Creates a FastMessageFormat based on the given format string.
47     */
48    public FastMessageFormat(String formatString)
49    {
50      if (formatString == null)
51        throw new NullPointerException();
52  
53      _formatText = formatString.toCharArray();
54    }
55  
56  
57    /**
58     * This formatter will only replace patterns of the type "{[0-9]}"
59     * for which there is an associated token.
60     * Any other use of '{}' will be interpreted as literal text.
61     * This aims to have the same behavior as TrFastMessageFormatUtils.format
62     * on the client.
63     * <p>
64     * @param source an array of strings (tokens)
65     */
66    public String format(Object[] source)
67    {
68      int formatLength = _formatText.length;
69      int length = 0;
70      int tokenCount = source.length;
71      for (int i = 0; i < tokenCount; i++)
72      {
73        Object sourceString = source[i];
74        if (sourceString != null)
75        {
76          length += sourceString.toString().length();
77        }
78      }
79  
80      // The following buffer size is just an initial estimate. It is legal for
81      // any given pattern, such as {0}, to occur more than once, in which case
82      // the buffer size will expand automatically if need be.
83      StringBuilder buffer = new StringBuilder(length + formatLength);
84  
85      int lastStart = 0;
86      for (int i = 0; i < formatLength; i++)
87      {
88        char ch = _formatText[i];
89        if (ch == '{')
90        {
91          // Only check for single digit patterns that have an associated token.
92          if (i + 2 < formatLength && _formatText[i + 2] == '}')
93          {
94            int tokenIndex = _formatText[i + 1] - '0';
95            if (tokenIndex >= 0 && tokenIndex < tokenCount)
96            {
97              buffer.append(_formatText, lastStart, i - lastStart);
98              Object sourceString = source[tokenIndex];
99              if (sourceString != null)
100               buffer.append(sourceString.toString());
101 
102             i += 2;
103             lastStart = i + 1;
104           }
105         }
106       }
107       // ELSE: Do nothing. The character will be added in later.
108     }
109 
110     buffer.append(_formatText, lastStart, formatLength - lastStart);
111 
112     return new String(buffer);
113   }
114 
115   private final char[] _formatText;
116   private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
117     FastMessageFormat.class);
118 }
119 
120 
121 
122