/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 groovy.text;

import java.util.ResourceBundle;
import java.util.MissingResourceException;

import java.text.MessageFormat;

import groovy.lang.GroovyObjectSupport;

/**
 * Message source backed up by one or more {@link java.util.ResourceBundle}
 * instances for simple i18n support.
 *
 * @version $Id$
 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
 */
public class MessageSource
    extends GroovyObjectSupport
{
    private final ResourceBundle[] bundles;
    
    //
    // TODO: Re-add the String and Object? constructor helpers
    //
    
    public MessageSource(final Class type) {
        this(new Class[] { type });
    }
    
    public MessageSource(final Class[] types) {
        assert types != null;
        assert types.length != 0;
        
        bundles = new ResourceBundle[types.length];
        
        for (int i=0; i<types.length; i++) {
            assert types[i] != null;
            
            bundles[i] = ResourceBundle.getBundle(types[i].getName());
        }
    }
    
    /**
     * Get a raw message from the resource bundles using the given code.
     */
    public String getMessage(final String code) {
        assert code != null;
        
        MissingResourceException error = null;
        
        for (int i=0; i<bundles.length; i++) {
            try {
                return bundles[i].getString(code);
            }
            catch (MissingResourceException e) {
                //
                // FIXME: For now just save the first error, should really roll a new message with all of the details
                //
                
                if (error != null) {
                    error = e;
                }
            }
        }
        
        assert error != null;
        
        throw error;
    }

    /**
     * Format a message (based on {@link MessageFormat} using the message
     * from the resource bundles using the given code as a pattern and the
     * given objects as arguments.
     */
    public String format(final String code, final Object[] args) {
        assert args != null;
        
        String pattern = getMessage(code);
        
        return MessageFormat.format(pattern, args);
    }
    
    /**
     * @see #getMessage(String)
     */
    public Object getProperty(final String name) {
        return getMessage(name);
    }
}

@@ -1298,80 +1298,358 @@
     
-//%0A    // TODO: Re-add the String and Object? constructor helpers%0A    //
+public MessageSource(final String%5B%5D names) %7B%0A        assert names != null;%0A        assert names.length != 0;%0A        %0A        bundles = new ResourceBundle%5Bnames.length%5D;%0A        %0A        for (int i=0; i%3Cnames.length; i++) %7B%0A            assert names%5Bi%5D != null;%0A            %0A            bundles%5Bi%5D = ResourceBundle.getBundle(names%5Bi%5D);%0A        %7D%0A    %7D
 %0A   
@@ -1685,17 +1685,18 @@
 nal 
-Class typ
+String nam
 e) %7B
@@ -1717,21 +1717,22 @@
 new 
-Class
+String
 %5B%5D %7B 
-typ
+nam
 e %7D)
@@ -1749,35 +1749,49 @@
   %0A    p
-ublic MessageSource
+rivate static String%5B%5D classNames
 (final C
@@ -1891,36 +1891,35 @@
     
-bundles = new ResourceBundle
+String%5B%5D names = new String
 %5Btyp
@@ -2042,37 +2042,35 @@
             
-bundl
+nam
 es%5Bi%5D = 
 ResourceBund
@@ -2061,58 +2061,165 @@
 %5D = 
-ResourceBundle.getBundle(types%5Bi%5D.getName(
+types%5Bi%5D.getName();%0A        %7D%0A        %0A        return names;%0A    %7D%0A    %0A    public MessageSource(final Class%5B%5D types) %7B%0A        this(classNames(types
 ));%0A    
     
@@ -2210,29 +2210,122 @@
 ypes));%0A    
+%7D%0A
     
-%7D
+%0A    public MessageSource(final Class type) %7B%0A        this(new String%5B%5D %7B type.getName() %7D);
 %0A    %7D%0A    %0A
@@ -3094,16 +3094,20 @@
 ;%0A    %7D%0A
+    
 %0A    /**
@@ -3657,10 +3657,632 @@
 ;%0A    %7D%0A
+    %0A    /**%0A     * @see #format(String,Object%5B%5D)%0A     * @deprecated%0A     */%0A    public String getMessage(final String code, final Object%5B%5D args) %7B%0A        return format(code, args);%0A    %7D%0A    %0A    /**%0A     * @see #format(String,Object%5B%5D)%0A     * @deprecated%0A     */%0A    public String getMessage(final String code, final Object arg1) %7B%0A        return format(code, new Object%5B%5D %7B arg1 %7D);%0A    %7D%0A    %0A    /**%0A     * @see #format(String,Object%5B%5D)%0A     * @deprecated%0A     */%0A    public String getMessage(final String code, final Object arg1, final Object arg2) %7B%0A        return format(code, new Object%5B%5D %7B arg1, arg2 %7D);%0A    %7D%0A
 %7D%0A

Add helper methods for java access