package de.hatoka.common.capi.resource;

import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import de.hatoka.common.capi.app.xslt.Localizer;

public class ResourceLocalizer implements Localizer
{
    private final LocalizationBundle bundle;
    private final String dateFormat;

    public ResourceLocalizer(LocalizationBundle bundle)
    {
        this(bundle, "yyyy/MM/dd");
    }

    public ResourceLocalizer(LocalizationBundle bundle, String dateFormat)
    {
        this.bundle = bundle;
        this.dateFormat = dateFormat;
    }

    @Override
    public String formatDate(String dateString)
    {
        // 2014-11-25T09:45:55.624+01:00
        Date date;
        try
        {
            date = new SimpleDateFormat(LocalizationConstants.XML_DATEFORMAT).parse(dateString);
        }
        catch(ParseException e)
        {
            return dateString;
        }
        return new SimpleDateFormat(dateFormat).format(date);
    }

    @Override
    public String getText(String key, String defaultText, Object... arguments)
    {
        String pattern = bundle.getText(key, null);
        if (pattern == null)
        {
            return defaultText;
        }
        MessageFormat messageFormat = new MessageFormat(pattern, bundle.getLocal());
        return messageFormat.format(arguments, new StringBuffer(), null).toString();
    }

}

@@ -137,31 +137,8 @@
 mat;
-%0Aimport java.util.Date;
 %0A%0Aim
@@ -663,10 +663,128 @@
     
-//
+if (dateString == null %7C%7C dateString.isEmpty())%0A        %7B%0A            return %22%22;%0A        %7D%0A        // convert xml format
  201
@@ -813,27 +813,18 @@
 1:00
-%0A        Date date;
+ to target
 %0A   
@@ -858,15 +858,55 @@
     
-date = 
+return new SimpleDateFormat(dateFormat).format(
 new 
@@ -977,16 +977,17 @@
 eString)
+)
 ;%0A      
@@ -1048,98 +1048,108 @@
     
-return
+throw new IllegalArgumentException(%22Illegal source
  date
-S
+ s
 tring
-;%0A        %7D%0A        return new SimpleDateFormat(dateFormat).format(date);
+: '%22 + dateString + %22'%22, e);%0A        %7D
 %0A   

throw exception in case of wrong format * return empty string for null date