All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.jasperreports.functions.standard.TextFunctions Maven / Gradle / Ivy

There is a newer version: 7.0.1
Show newest version
/*
 * JasperReports - Free Java Reporting Library.
 * Copyright (C) 2001 - 2023 Cloud Software Group, Inc. All rights reserved.
 * http://www.jaspersoft.com
 *
 * Unless you have purchased a commercial license agreement from Jaspersoft,
 * the following license terms apply:
 *
 * This program is part of JasperReports.
 *
 * JasperReports is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JasperReports is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JasperReports. If not, see .
 */
package net.sf.jasperreports.functions.standard;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.functions.AbstractFunctionSupport;
import net.sf.jasperreports.functions.annotations.Function;
import net.sf.jasperreports.functions.annotations.FunctionCategories;
import net.sf.jasperreports.functions.annotations.FunctionParameter;
import net.sf.jasperreports.functions.annotations.FunctionParameters;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This class should maintain all function methods that belongs to the category {@link #TEXT}.
 * 
 * @author Massimo Rabbi ([email protected])
 */
@FunctionCategories({TextCategory.class})
public final class TextFunctions  extends AbstractFunctionSupport
{
	private static final Log log = LogFactory.getLog(TextFunctions.class);	
	private static final int BASE_MIN_RADIX=2;
	private static final int BASE_MAX_RADIX=36;

	// ===================== BASE function ===================== //
	/**
	 * Returns a text representation of a number, in a specified base radix.
	 */
	@Function("BASE")
	@FunctionParameters({
		@FunctionParameter("number"),
		@FunctionParameter("radix"),
		@FunctionParameter("minLength")})
	public static String BASE(Integer number, Integer radix){
		// java.lang.Character.MIN_RADIX and java.lang.Character.MAX_RADIX are already 2 and 36 respectively
		// However we should check the parameter specified because the method we rely on uses 10 radix
		// as fallback when a smaller/greater radix is specified.
		if(number==null){
			if(log.isDebugEnabled()){
				log.debug("The number can not be null.");
			}
			return null;
		}
		if(radix==null || radix>BASE_MAX_RADIX || radix255)){
			if(log.isDebugEnabled()) {
				log.debug("The number must be an integer number between 1 and 255.");
			}
			return null;
		}
		return Character.toString((char)number.intValue());
	}
	
	// ===================== CLEAN function ===================== //
	/**
	 * Returns a new text string without non-printable characters.
	 */
	@Function("CLEAN")
	@FunctionParameters({
			@FunctionParameter("text")})
	public static String CLEAN(String text){
		if(text==null){
			logNullTextString();
			return null;
		}
		String cleanedString = text.replaceAll("\\p{Cntrl}", "");
		return cleanedString;
	}

	// ===================== CODE function ===================== //
	/**
	 * Returns the numeric code (0-255) for the first character in a string.
	 */
	@Function("CODE")
	@FunctionParameters({
			@FunctionParameter("textString")})
	public static Integer CODE(String textString){
		if(textString==null){
			logNullTextString();
			return null;
		}
		int firstCharAsNum = textString.charAt(0);
		if(firstCharAsNum<0 || firstCharAsNum>255){
			throw new JRRuntimeException("The first character of the text can not be converted to a valid numeric ASCII code.");
		}
		return firstCharAsNum;
	}
	
	// ===================== CONCATENATE function ===================== //
	/**
	 * Combines a list of strings into a single one.
	 */
	@Function("CONCATENATE")
	@FunctionParameters({
			@FunctionParameter("strings")})
	public static String CONCATENATE(String ...strings){
		if(strings.length==0) {
			if(log.isDebugEnabled()) {
				log.debug("No arguments were specified.");
			}
			return null;
		}
		StringBuilder sb=new StringBuilder();
		for (int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy