<commit_before>package org.codehaus.groovy.grails.plugins.web.taglib

import org.springframework.web.servlet.support.RequestContextUtils as RCU;
import java.text.*

/* Copyright 2004-2005 the original author or authors.
 *
 * 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 c;pWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.springframework.web.servlet.support.RequestContextUtils as RCU;

 /**
 * The base application tag library for Grails many of which take inspiration from Rails helpers (thanks guys! :)
 * This tag library tends to get extended by others as tags within here can be re-used in said libraries
 *
 * @author <NAME>
 * @since 0.6
 *
 * Created:17-Jan-2006
 */
class FormatTagLib {

    /**
      * Outputs the given boolean as the specified text label. If the
      * <code>true</code> and <code>false</code> option are not given,
      * then the boolean is output using the default label.
      *
      * Attributes:
      *
      * boolean - the boolean to output
      * true (optional) - text label for boolean true value
      * false (optional) - text label for boolean false value
      *
      * Examples:
      *
      * <g:formatBoolean boolean="${myBoolean}" />
      * <g:formatBoolean boolean="${myBoolean}" true="True!" false="False!" />
      */
     def formatBoolean = { attrs ->
         if (!attrs.containsKey("boolean"))
             throwTagError("Tag [formatBoolean] is missing required attribute [boolean]")

         def b = attrs.get("boolean")
         if (b == null) {
             return
         }
         else if (!(b instanceof Boolean)) {
             b = Boolean.valueOf(b)
         }

         if (b) {
             out << (attrs["true"] ?: message(code:"boolean.true", default: message(code:"default.boolean.true", default:"True")))
         }
         else {
             out << (attrs["false"] ?: message(code:"boolean.false", default: message(code:"default.boolean.false", default:"False")))
         }
     }

    /**
     * Outputs the given <code>Date</code> object in the specified format.  If
     * the <code>date</code> is not given, then the current date/time is used.
     * If the <code>format</code> option is not given, then the date is output
     * using the default format.
     *
     * e.g., <g:formatDate date="${myDate}" format="yyyy-MM-dd HH:mm" />
     *
     * @see java.text.SimpleDateFormat
     */
    def formatDate = { attrs ->
    
    	def date
    	if (attrs.containsKey('date')) {
        	date = attrs.get('date')
        	if(date == null) return
    	}
    	else {
            date = new Date()
    	}

        def formatName = attrs.get('formatName')
        def format = attrs.get('format')
        
        if(!format && formatName) {
            format = message(code:formatName)
            if(!format) throwTagError("Attribute [formatName] of Tag [formatDate] specifies a format key [$formatName] that does not exist within a message bundle!")
        }
        else if (!format) {
            format = message(code: "date.format", default: message(code: "default.date.format", default: "yyyy-MM-dd HH:mm:ss z"))
        }

        def locale = RCU.getLocale(request)
        def simpleDateFormat = locale ?
        new SimpleDateFormat(format, locale) :
        new SimpleDateFormat(format)
        
        out << simpleDateFormat.format(date)
    }

    /**
     * Outputs the given number in the specified format.  If the
     * <code>format</code> option is not given, then the number is output
     * using the default format.
     *
     * e.g., <g:formatNumber number="${myNumber}" format="###,##0" />
     *
     * @see java.text.DecimalFormat
     */
    def formatNumber = { attrs ->
<commit_msg>fix for GRAILS-2631<commit_after>package org.codehaus.groovy.grails.plugins.web.taglib

import org.springframework.web.servlet.support.RequestContextUtils as RCU;
import java.text.*

/* Copyright 2004-2005 the original author or authors.
 *
 * 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 c;pWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.springframework.web.servlet.support.RequestContextUtils as RCU;

 /**
 * The base application tag library for Grails many of which take inspiration from Rails helpers (thanks guys! :)
 * This tag library tends to get extended by others as tags within here can be re-used in said libraries
 *
 * @author <NAME>
 * @since 0.6
 *
 * Created:17-Jan-2006
 */
class FormatTagLib {

    /**
      * Outputs the given boolean as the specified text label. If the
      * <code>true</code> and <code>false</code> option are not given,
      * then the boolean is output using the default label.
      *
      * Attributes:
      *
      * boolean - the boolean to output
      * true (optional) - text label for boolean true value
      * false (optional) - text label for boolean false value
      *
      * Examples:
      *
      * <g:formatBoolean boolean="${myBoolean}" />
      * <g:formatBoolean boolean="${myBoolean}" true="True!" false="False!" />
      */
     def formatBoolean = { attrs ->
         if (!attrs.containsKey("boolean"))
             throwTagError("Tag [formatBoolean] is missing required attribute [boolean]")

         def b = attrs.get("boolean")
         if (b == null) {
             return
         }
         else if (!(b instanceof Boolean)) {
             b = Boolean.valueOf(b)
         }

         if (b) {
             out << (attrs["true"] ?: message(code:"boolean.true", default: message(code:"default.boolean.true", default:"True")))
         }
         else {
             out << (attrs["false"] ?: message(code:"boolean.false", default: message(code:"default.boolean.false", default:"False")))
         }
     }

    /**
     * Outputs the given <code>Date</code> object in the specified format.  If
     * the <code>date</code> is not given, then the current date/time is used.
     * If the <code>format</code> option is not given, then the date is output
     * using the default format.
     *
     * e.g., <g:formatDate date="${myDate}" format="yyyy-MM-dd HH:mm" />
     *
     * @see java.text.SimpleDateFormat
     */
    def formatDate = { attrs ->
    
    	def date
    	if (attrs.containsKey('date')) {
        	date = attrs.get('date')
        	if(date == null) return
    	}
    	else {
            date = new Date()
    	}

        def formatName = attrs.get('formatName')
        def format = attrs.get('format')
        
        if(!format && formatName) {
            format = message(code:formatName, default:'')
            if(!format) throwTagError("Attribute [formatName] of Tag [formatDate] specifies a format key [$formatName] that does not exist within a message bundle!")
        }
        else if (!format) {
            format = message(code: "date.format", default: message(code: "default.date.format", default: "yyyy-MM-dd HH:mm:ss z"))
        }

        def locale = RCU.getLocale(request)
        def simpleDateFormat = locale ?
        new SimpleDateFormat(format, locale) :
        new SimpleDateFormat(format)
        
        out << simpleDateFormat.format(date)
    }

    /**
     * Outputs the given number in the specified format.  If the
     * <code>format</code> option is not given, then the number is output
     * using the default format.
     *
     * e.g., <g:formatNumber number="${myNumber}" format="###,##0" />
     *
     * @see java.text.DecimalFormat
     */
    def formatNumber = { attrs ->

{'fraction_non_alphanumeric': 0.11714831621686171, 'fraction_numerical': 0.00549319321710055, 'mean_word_length': 2.4679089026915113, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5046547', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2311, 'n_words': 773}