package org.displaytag.localization;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.MessageSource;
import org.springframework.web.servlet.support.RequestContextUtils;


/**
 * Spring implementation of a resource provider and locale resolver. Since Displaytag locale resolution is modelled on
 * the Spring one, it simply forward <code>resolveLocale</code> calls to the Spring-configured LocaleResolver.
 * @author Fabrizio Giustina
 * @version $Revision$ ($Author$)
 */
public class I18nSpringAdapter implements LocaleResolver, I18nResourceProvider
{

    /**
     * logger.
     */
    private static Log log = LogFactory.getLog(I18nSpringAdapter.class);

    /**
     * @see LocaleResolver#resolveLocale(HttpServletRequest)
     */
    public Locale resolveLocale(HttpServletRequest request)
    {
        return RequestContextUtils.getLocale(request);
    }

    /**
     * @see I18nResourceProvider#getResource(String, String, Tag, PageContext)
     */
    public String getResource(String resourceKey, String defaultValue, Tag tag, PageContext pageContext)
    {
        MessageSource messageSource = RequestContextUtils.getWebApplicationContext(pageContext.getRequest());
        if (messageSource == null)
        {
            return null;
            // @todo log a warn? thrown ax exception?
        }

        String message = null;
        if (resourceKey != null)
        {
            message = messageSource.getMessage(resourceKey, null, null, RequestContextUtils
                .getLocale((HttpServletRequest) pageContext.getRequest()));
            log.debug("Missing resource for key [" + resourceKey + "]");

            // if user explicitely added a resourceKey we guess this is an error
            if (resourceKey != null)
            {
                message = "??" + resourceKey + "??";
            }

        }
        else
        {
            message = messageSource.getMessage(defaultValue, null, null, RequestContextUtils
                .getLocale((HttpServletRequest) pageContext.getRequest()));
        }

        return message;
    }
}

@@ -767,16 +767,143 @@
 ider%0A%7B%0A%0A
+    /**%0A     * prefix/suffix for missing entries.%0A     */%0A    public static final String UNDEFINED_KEY = %22???%22; //$NON-NLS-1$%0A%0A
     /**%0A
@@ -1581,164 +1581,252 @@
     
-return null;%0A            // @todo log a warn? thrown ax exception?%0A        %7D%0A%0A        String message = null;%0A        if (resourceKey != null)%0A        %7B%0A    
+log.warn(%22messageSource not found%22);%0A            return null;%0A        %7D%0A%0A        // if resourceKey isn't defined either, use defaultValue%0A        String key = (resourceKey != null) ? resourceKey : defaultValue;%0A%0A        String message = null;%0A%0A
     
@@ -1864,25 +1864,17 @@
 Message(
-resourceK
+k
 ey, null
@@ -1893,36 +1893,32 @@
 estContextUtils%0A
-    
             .get
@@ -1977,86 +1977,9 @@
 ));%0A
-            log.debug(%22Missing resource for key %5B%22 + resourceKey + %22%5D%22);%0A%0A    
+%0A
     
@@ -2013,23 +2013,20 @@
 added a 
-resourc
+titl
 eKey we 
@@ -2056,24 +2056,39 @@
         
-    
 if (
+message == null && 
 resource
@@ -2104,28 +2104,24 @@
 ll)%0A        
-    
 %7B%0A          
@@ -2126,25 +2126,45 @@
     
-    message = %22??
+log.debug(%22Missing resource for key %5B
 %22 + 
@@ -2178,19 +2178,19 @@
 eKey + %22
-??%22
+%5D%22)
 ;%0A      
@@ -2199,211 +2199,61 @@
     
-%7D%0A%0A        %7D%0A        else%0A        %7B%0A            message = messageSource.getMessage(defaultValue, null, null, RequestContextUtils%0A                .getLocale((HttpServletRequest) pageContext.getRequest()))
+message = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY
 ;%0A  
@@ -2285,16 +2285,17 @@
 essage;%0A
+%0A
     %7D%0A%7D%0A

fix spring adapter to match other the behaviour of other adapters