hivemind-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject cvs commit: jakarta-hivemind/framework/src/java/org/apache/hivemind/impl MessageFormatter.java
Date Wed, 09 Jun 2004 14:52:44 GMT
hlship      2004/06/09 07:52:44

  Added:       framework/src/java/org/apache/hivemind/impl
                        MessageFormatter.java
  Log:
  Add MessageFormatter class to make it easier for each package to have its own set of messages.
  
  Revision  Changes    Path
  1.1                  jakarta-hivemind/framework/src/java/org/apache/hivemind/impl/MessageFormatter.java
  
  Index: MessageFormatter.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  package org.apache.hivemind.impl;
  
  import java.text.MessageFormat;
  import java.util.MissingResourceException;
  import java.util.ResourceBundle;
  
  import org.apache.commons.logging.Log;
  
  /**
   * A wrapper around {@link java.util.ResourceBundle} that makes
   * it easier to access and format messages.
   *
   * @author Howard Lewis Ship
   */
  public class MessageFormatter
  {
      private Log _log;
      private ResourceBundle _bundle;
  
      public MessageFormatter(Log log, ResourceBundle bundle)
      {
          _log = log;
          _bundle = bundle;
      }
  
      public MessageFormatter(Log log, Class referenceClass, String name)
      {
          this(log, referenceClass.getPackage().getName() + "." + name);
      }
  
      public MessageFormatter(Log log, String bundleName)
      {
          this(log, ResourceBundle.getBundle(bundleName));
      }
  
      public String getString(String key)
      {
          try
          {
              return _bundle.getString(key);
          }
          catch (MissingResourceException ex)
          {
              _log.error("Missing resource key: " + key + ".");
              return "[" + key.toUpperCase() + "]";
          }
      }
  
      public String format(String key, Object arg)
      {
          return format(key, new Object[] { arg });
      }
  
      public String format(String key, Object arg1, Object arg2)
      {
          return format(key, new Object[] { arg1, arg2 });
      }
  
      public String format(String key, Object arg1, Object arg2, Object arg3)
      {
          return format(key, new Object[] { arg1, arg2, arg3 });
      }
  
      public String format(String key, Object[] args)
      {
          String pattern = getString(key);
  
          if (args == null)
              return pattern;
  
          try
          {
              return MessageFormat.format(pattern, args);
          }
          catch (Exception ex)
          {
              _log.error("Unable to format message: \"" + pattern + "\" from key " + key +
".", ex);
  
              return null;
          }
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-cvs-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-cvs-help@jakarta.apache.org


Mime
View raw message