Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion main/SS/Format/CellDateFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
using System.Text;
using NPOI.SS.Util;
using System.Globalization;
using NPOI.Util;

namespace NPOI.SS.Format
{
Expand Down Expand Up @@ -159,6 +160,18 @@ public void Finish(StringBuilder toAppendTo)
* @param format The format.
*/
public CellDateFormatter(String format)
: this(LocaleUtil.GetUserLocale(), format)
{

}

/**
* Creates a new date formatter with the given specification.
*
* @param locale The locale.
* @param format The format.
*/
public CellDateFormatter(CultureInfo locale, String format)
: base(format)
{
DatePartHandler partHandler = new DatePartHandler(this);
Expand All @@ -171,7 +184,7 @@ public CellDateFormatter(String format)
// See https://issues.apache.org/bugzilla/show_bug.cgi?id=53369

String ptrn = Regex.Replace(descBuf.ToString(), "((y)(?!y))(?<!yy)", "yy");
dateFmt = new SimpleDateFormat(ptrn);
dateFmt = new SimpleDateFormat(ptrn, locale);
}

/** {@inheritDoc} */
Expand Down
74 changes: 48 additions & 26 deletions main/SS/Format/CellFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ limitations under the License.

namespace NPOI.SS.Format
{
using System;

using NPOI.SS.UserModel;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using NPOI.Util;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;

/**
* Format a value according to the standard Excel behavior. This "standard" is
Expand Down Expand Up @@ -72,6 +72,7 @@ namespace NPOI.SS.Format
*/
public class CellFormat
{
private CultureInfo locale;
private readonly String format;
private readonly CellFormatPart posNumFmt;
private readonly CellFormatPart zeroNumFmt;
Expand All @@ -81,9 +82,6 @@ public class CellFormat

private static readonly Regex ONE_PART = new Regex(CellFormatPart.FORMAT_PAT.ToString() + "(;|$)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);

private static readonly CellFormatPart DEFAULT_TEXT_FORMAT =
new CellFormatPart("@");

/*
* Cells that cannot be formatted, e.g. cells that have a date or time
* format and have an invalid date or time value, are displayed as 255
Expand All @@ -97,28 +95,32 @@ public class CellFormat
"###################################################";

private const string QUOTE = "\"";

private static readonly CellFormat GENERAL_FORMAT = new GeneralCellFormat();
[Obsolete("use {@link #getInstance(Locale, \"General\")} instead")]
private static readonly CellFormat GENERAL_FORMAT = CreateGeneralFormat(LocaleUtil.GetUserLocale());
/**
* Format a value as it would be were no format specified. This is also
* used when the format specified is <tt>General</tt>.
*/
public class GeneralCellFormat : CellFormat
{
public GeneralCellFormat()
: base("General")
public GeneralCellFormat(CultureInfo locale)
: base(locale, "General")
{
}
public override CellFormatResult Apply(Object value)
{
String text = (new CellGeneralFormatter()).Format(value);
String text = (new CellGeneralFormatter(locale)).Format(value);
return new CellFormatResult(true, text, POIUtils.Color_Empty);
}
}

private static GeneralCellFormat CreateGeneralFormat(CultureInfo locale)
{
return new GeneralCellFormat(locale);
}
/** Maps a format string to its Parsed version for efficiencies sake. */
private static readonly Dictionary<String, CellFormat> formatCache =
new Dictionary<String, CellFormat>();
private static Dictionary<CultureInfo, Dictionary<String, CellFormat>> formatCache =
new Dictionary<CultureInfo, Dictionary<String, CellFormat>>();

/**
* Returns a {@link CellFormat} that applies the given format. Two calls
Expand All @@ -130,28 +132,48 @@ public override CellFormatResult Apply(Object value)
*/
public static CellFormat GetInstance(String format)
{
return GetInstance(LocaleUtil.GetUserLocale(), format);
}
/**
* Returns a {@link CellFormat} that applies the given format. Two calls
* with the same format may or may not return the same object.
*
* @param locale The locale.
* @param format The format.
*
* @return A {@link CellFormat} that applies the given format.
*/
public static CellFormat GetInstance(CultureInfo locale, String format)
{
Dictionary<String, CellFormat> formatMap = formatCache.TryGetValue(locale, out Dictionary<string, CellFormat> value) ? value : null;
if (formatMap == null)
{
formatMap = new Dictionary<String, CellFormat>();
formatCache[locale] = formatMap;
}
CellFormat fmt = null;
if (formatCache.TryGetValue(format, out CellFormat value))
fmt = value;
if (formatMap.TryGetValue(format, out CellFormat value1))
fmt = value1;
if (fmt == null)
{
if (format.Equals("General") || format.Equals("@"))
fmt = GENERAL_FORMAT;
fmt = CreateGeneralFormat(locale);
else
fmt = new CellFormat(format);
formatCache.Add(format, fmt);
fmt = new CellFormat(locale, format);
formatMap.Add(format, fmt);
}
return fmt;
}

/**
* Creates a new object.
*
* @param format The format.
*/
private CellFormat(String format)
private CellFormat(CultureInfo locale, String format)
{
this.locale = locale;
this.format = format;
CellFormatPart defaultTextFormat = new CellFormatPart(locale, "@");
MatchCollection mc = ONE_PART.Matches(format);
List<CellFormatPart> parts = new List<CellFormatPart>();

Expand All @@ -166,7 +188,7 @@ private CellFormat(String format)
if (valueDesc.EndsWith(';'))
valueDesc = valueDesc.Substring(0, valueDesc.Length - 1);

parts.Add(new CellFormatPart(valueDesc));
parts.Add(new CellFormatPart(locale, valueDesc));
}
catch
{
Expand All @@ -182,19 +204,19 @@ private CellFormat(String format)
posNumFmt = parts[(0)];
negNumFmt = null;
zeroNumFmt = null;
textFmt = DEFAULT_TEXT_FORMAT;
textFmt = defaultTextFormat;
break;
case 2:
posNumFmt = parts[0];
negNumFmt = parts[1];
zeroNumFmt = null;
textFmt = DEFAULT_TEXT_FORMAT;
textFmt = defaultTextFormat;
break;
case 3:
posNumFmt = parts[0];
negNumFmt = parts[1];
zeroNumFmt = parts[2];
textFmt = DEFAULT_TEXT_FORMAT;
textFmt = defaultTextFormat;
break;
case 4:
default:
Expand Down Expand Up @@ -335,7 +357,7 @@ private CellFormatPart GetApplicableFormatPart(Object value)
}
else
{
return new CellFormatPart("General");
return new CellFormatPart(locale, "General");
}
}
else if (formatPartCount == 2)
Expand Down
20 changes: 15 additions & 5 deletions main/SS/Format/CellFormatPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace NPOI.SS.Format
using System.Collections.Generic;
using System.Collections;
using System.Text.RegularExpressions;
using System.Text;
using Cysharp.Text;
using System.Text;using Cysharp.Text;
using System.Globalization;
using SixLabors.ImageSharp;
using NPOI.Util;

Expand Down Expand Up @@ -196,6 +196,16 @@ String HandlePart(Match m, String part, CellFormatType type,
* @param desc The string to Parse.
*/
public CellFormatPart(String desc)
: this(LocaleUtil.GetUserLocale(), desc)
{
}
/**
* Create an object to represent a format part.
*
* @param locale The locale to use.
* @param desc The string to parse.
*/
public CellFormatPart(CultureInfo locale, String desc)
{
Match m = FORMAT_PAT.Match(desc);
if (!m.Success)
Expand All @@ -205,7 +215,7 @@ public CellFormatPart(String desc)
color = GetColor(m);
condition = GetCondition(m);
type = GetCellFormatType(m);
format = GetFormatter(m);
format = GetFormatter(locale, m);
}

/**
Expand Down Expand Up @@ -322,7 +332,7 @@ private static CellFormatType GetCellFormatType(Match matcher)
*
* @return The formatter.
*/
private CellFormatter GetFormatter(Match matcher)
private CellFormatter GetFormatter(CultureInfo locale, Match matcher)
{
String fdesc = matcher.Groups[(SPECIFICATION_GROUP)].Value;
// For now, we don't support localised currencies, so simplify if there
Expand All @@ -344,7 +354,7 @@ private CellFormatter GetFormatter(Match matcher)
}

// Build a formatter for this simplified string
return type.Formatter(fdesc);
return type.Formatter(locale, fdesc);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions main/SS/Format/CellFormatType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
==================================================================== */

using System;
using System.Globalization;

namespace NPOI.SS.Format
{
Expand All @@ -29,6 +30,10 @@ public override bool IsSpecial(char ch)
{
return false;
}
public override CellFormatter Formatter(CultureInfo locale, String pattern)
{
return new CellGeneralFormatter(locale);
}
}

internal sealed class NumberCellFormatType : CellFormatType
Expand All @@ -41,6 +46,10 @@ public override bool IsSpecial(char ch)
{
return false;
}
public override CellFormatter Formatter(CultureInfo locale, String pattern)
{
return new CellNumberFormatter(locale, pattern);
}
}

internal sealed class DateCellFormatType : CellFormatType
Expand All @@ -53,6 +62,10 @@ public override CellFormatter Formatter(String pattern)
{
return new CellDateFormatter(pattern);
}
public override CellFormatter Formatter(CultureInfo locale, String pattern)
{
return new CellDateFormatter(locale, pattern);
}
}

internal sealed class ElapsedCellFormatType : CellFormatType
Expand All @@ -65,6 +78,10 @@ public override CellFormatter Formatter(String pattern)
{
return new CellElapsedFormatter(pattern);
}
public override CellFormatter Formatter(CultureInfo locale, String pattern)
{
return new CellElapsedFormatter(pattern);
}
}

internal sealed class TextCellFormatType : CellFormatType
Expand All @@ -77,6 +94,10 @@ public override CellFormatter Formatter(String pattern)
{
return new CellTextFormatter(pattern);
}
public override CellFormatter Formatter(CultureInfo locale, String pattern)
{
return new CellTextFormatter(pattern);
}
}

/**
Expand Down Expand Up @@ -115,5 +136,16 @@ public abstract class CellFormatType
* @return A new formatter of the appropriate type, for the given pattern.
*/
public abstract CellFormatter Formatter(String pattern);

/**
* Returns a new formatter of the appropriate type, for the given pattern.
* The pattern must be appropriate for the type.
*
* @param locale The locale to use.
* @param pattern The pattern to use.
*
* @return A new formatter of the appropriate type, for the given pattern.
*/
public abstract CellFormatter Formatter(CultureInfo locale, String pattern);
}
}
15 changes: 14 additions & 1 deletion main/SS/Format/CellFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace NPOI.SS.Format
using System;
using System.Text;
using System.Globalization;
using NPOI.Util;



Expand All @@ -32,7 +33,7 @@ public abstract class CellFormatter
{
/** The original specified format. */
protected String format;

protected CultureInfo locale;
/**
* This is the locale used to Get a consistent format result from which to
* work.
Expand All @@ -45,7 +46,19 @@ public abstract class CellFormatter
* @param format The format.
*/
public CellFormatter(String format)
: this(LocaleUtil.GetUserLocale(), format)
{
}

/**
* Creates a new formatter object, storing the format in {@link #format}.
*
* @param locale The locale.
* @param format The format.
*/
public CellFormatter(CultureInfo locale, String format)
{
this.locale = locale;
this.format = format;
}

Expand Down
Loading
Loading