<commit_before>package com.hectorlopezfernandez.pebblejodatime;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.coverity.security.Escape;
import com.mitchellbosecke.pebble.extension.Filter;

public class JodaFilter implements Filter {

    private final List<String> argumentNames = new ArrayList<>();

    public JodaFilter() {
        argumentNames.add("strategy");
    }

    public List<String> getArgumentNames() {
        return argumentNames;
    }

    public Object apply(Object inputObject, Map<String, Object> args) {
        return inputObject;
    }

}<commit_msg>Implement JodaFilter to format Joda DateTimes<commit_after>package com.hectorlopezfernandez.pebblejodatime;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.joda.time.DateTimeZone;
import org.joda.time.ReadableInstant;
import org.joda.time.ReadablePartial;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import com.mitchellbosecke.pebble.error.AttributeNotFoundException;
import com.mitchellbosecke.pebble.extension.Filter;
import com.mitchellbosecke.pebble.template.EvaluationContext;

public class JodaFilter implements Filter {

    private final List<String> argumentNames = new ArrayList<>(4);

    public JodaFilter() {
        argumentNames.add("pattern");
        argumentNames.add("style");
        argumentNames.add("locale");
        argumentNames.add("timezone");
    }

    public List<String> getArgumentNames() {
        return argumentNames;
    }

    public Object apply(Object inputObject, Map<String, Object> args) {
        if (inputObject == null || !(inputObject instanceof ReadableInstant || inputObject instanceof ReadablePartial)) {
            return inputObject;
        }
        
        EvaluationContext context = (EvaluationContext) args.get("_context");

        // pattern param
        String pattern = (String) args.get("pattern");
        // style param
        String style = (String) args.get("style");
        // pattern is preferred to style, but only if they are both specified
        // style, if specified, is preferred to default joda pattern
        if (pattern == null && style == null) {
        	try {
				String defaultPattern = (String) context.get(JodaExtension.PATTERN_REQUEST_ATTRIBUTE);
				if (defaultPattern != null) pattern = defaultPattern;
			} catch (AttributeNotFoundException e) {}
        }

        // create formatter
        DateTimeFormatter formatter;
        if (pattern != null) {
            formatter = DateTimeFormat.forPattern(pattern);
        } else if (style != null) {
            formatter = DateTimeFormat.forStyle(style);
        } else {
            // use a medium date (no time) style by default; same as jstl
            formatter = DateTimeFormat.mediumDate();
        }

        // locale param
        Object localeParam = args.get("locale");
        Locale locale = null;
        if (localeParam == null) {
        	// try first the default joda locale and then resort to EvaluationContext's locale
        	try {
				Locale defaultLocale = (Locale) context.get(JodaExtension.LOCALE_REQUEST_ATTRIBUTE);
				if (defaultLocale != null) locale = defaultLocale;
				else locale = context.getLocale();
			} catch (AttributeNotFoundException e) {}
        } else if (localeParam instanceof String) {
            locale = Locale.forLanguageTag((String) localeParam);
        } else if (localeParam instanceof Locale) {
        	locale = (Locale) localeParam;
        } else {
        	throw new IllegalArgumentException("JodaFilter only supports String and Locale locales. Actual argument was: " + localeParam.getClass().getName());
        }
        formatter = formatter.withLocale(locale);
        
        // shortcircuit if no timezone can be involved
        if (inputObject instanceof ReadablePartial) {
        	return formatter.print((ReadablePartial) inputObject);
        }

        // timezone param
        Object timezoneParam = args.get("timezone");
        DateTimeZone timezone = null;
        if (timezoneParam == null) {
        	// try default joda timezone
        	try {
        		DateTimeZone defaultTimezone = (DateTimeZone) context.get(JodaExtension.TIMEZONE_REQUEST_ATTRIBUTE);
        		if (defaultTimezone != null) timezone = defaultTimezone;
			} catch (AttributeNotFoundException e) {}
        } else if (timezoneParam instanceof String) {
        	timezone = DateTimeZone.forID((String) timezoneParam);
        } else if (timezoneParam instanceof DateTimeZone) {
        	timezone = (DateTimeZone) timezoneParam;
        } else {
        	throw new IllegalArgumentException("JodaFilter only supports String and DateTimeZone timezones. Actual argument was: " + timezoneParam.getClass().getName());
        }
        if (timezone != null) {
        	formatter.withZone(timezone);
        }

        return formatter.print((ReadableInstant) inputObject);
    }

}
{'fraction_non_alphanumeric': 0.09483793517406963, 'fraction_numerical': 0.00020008003201280514, 'mean_word_length': 2.936220472440945, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7590978', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1319, 'n_words': 346}