Class NumberConverter
java.lang.Object
org.apache.fop.complexscripts.util.NumberConverter
Implementation of Number to String Conversion algorithm specified by XSL Transformations (XSLT) Version 2.0, W3C Recommendation, 23 January 2007.
This algorithm differs from that specified in XSLT 1.0 in the following ways:
- input numbers are greater than or equal to zero rather than greater than zero;
- introduces format tokens { w, W, Ww };
- introduces ordinal parameter to generate ordinal numbers;
Implementation Defaults and Limitations
- If language parameter is unspecified (null or empty string), then the value of DEFAULT_LANGUAGE is used, which is defined below as "eng" (English).
- Only English, French, and Spanish word numerals are supported, and only if less than one trillion (1,000,000,000,000).
- Ordinal word numerals are supported for French and Spanish only when less than or equal to ten (10).
Implementation Notes
- In order to handle format tokens outside the Unicode BMP, all processing is done in Unicode Scalar Values represented with Integer and Integer[] types. Without affecting behavior, this may be subsequently optimized to use int and int[] types.
- In order to communicate various sub-parameters, including ordinalization, a features is employed, which consists of comma separated name and optional value tokens, where name and value are separated by an equals '=' sign.
- Ordinal numbers are selected by specifying a word based format token in combination with a 'ordinal' feature with no value, in which case the features 'male' and 'female' may be used to specify gender for gender sensitive languages. For example, the feature string "ordinal,female" selects female ordinals.
This work was originally authored by Glenn Adams (gadams@apache.org).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classprivate static classprivate static classprivate classprivate static classIsopsephry (Greek) Numeralsprivate classprivate static classprivate static class(package private) static interfaceSpecial number formatter.private class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static int[]Arabic Numeralsprivate static int[]private Stringcountryprivate static final Stringdefault languageprivate static final Integer[]default separatorprivate static final Integer[]default tokenprivate static String[]English Word Numeralsprivate static String[]private static String[]private static String[]private static String[]private static String[]private static String[]private static String[]private static String[][]private Stringletter value systemprivate static String[]French Word Numeralsprivate static String[]private static String[]private static String[]private static String[]private static String[]private intgrouping separatorprivate intgrouping sizeprivate static int[]Gematria (Hebrew) Numeralsprivate static int[]Kana (Japanese) Numeralsprivate static int[]private Stringlanguagestatic final intalphabeticalstatic final inttraditionalprivate intletter valueprivate Integer[]prefix tokenprivate static String[]private static int[]Roman (Latin) Numeralsprivate static String[]private static String[]private Integer[][]sequence of separators, as parsed from formatprivate static String[]private static String[]Spanish Word Numeralsprivate static String[]private static String[]private static String[]private static String[]private static String[]private static String[]private Integer[]suffix tokenprivate static int[][]private static int[][]private static int[]Thai Numeralsprivate static final intalhphanumeric token typeprivate static final intnonalphanumeric token typeprivate static final intno token typeprivate Integer[][]sequence of tokens, as parsed from format -
Constructor Summary
ConstructorsConstructorDescriptionNumberConverter(String format, int groupingSeparator, int groupingSize, int letterValue, String features, String language, String country) Construct parameterized number converter. -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidappendScalars(List<Integer> scalars, Integer[] sa) convert(long number) Convert a number to string according to conversion parameters.Convert list of numbers to string according to conversion parameters.private Integer[]convertNumber(long number, Integer[] separator, Integer[] token) private voidconvertNumbers(List<Integer> scalars, List<Long> numbers) private static StringconvertWordCase(String word, int caseType) convertWordCase(List<String> words, int caseType) private Integer[]formatNumber(long number, Integer[] token) private Integer[]formatNumberAsDecimal(long number, int one, int width) Format NUMBER as decimal using characters denoting digits that start at ONE, adding one or more (zero) padding characters as needed to fill out field WIDTH.private Integer[]formatNumberAsSequence(long number, int one, int base, int[] map) Format NUMBER as using sequence of characters that start at ONE, and having BASE radix.private Integer[]formatNumberAsSpecial(long number, int one) Format NUMBER as using special system that starts at ONE.private Integer[]formatNumberAsWord(long number, int caseType) Format NUMBER as word according to TYPE, which must be either Character.UPPERCASE_LETTER, Character.LOWERCASE_LETTER, or Character.TITLECASE_LETTER.private static intgetDecimalValue(Integer scalar) private static intgetSequenceBase(int s) getSpecialFormatter(int one, int letterValue, String features, String language, String country) private static booleanhasFeature(String features, String feature) private static booleanisAlphaNumeric(int c) private booleanisLanguage(String iso3Code) private static booleanisPaddedOne(Integer[] token) private static booleanisSameLanguage(String i3c, String lc) private static booleanisStartOfAlphabeticSequence(int s) private static booleanisStartOfDecimalSequence(int s) private static booleanisStartOfNumericSpecial(int s) private static Stringprivate voidparseFormatTokens(String format) performGrouping(List<Integer> sl, int groupingSize, int groupingSeparator) private static StringscalarsToString(List<Integer> scalars) private static Integer[]toLowerCase(Integer[] sa) private static Integer[]toUpperCase(Integer[] sa)
-
Field Details
-
LETTER_VALUE_ALPHABETIC
public static final int LETTER_VALUE_ALPHABETICalphabetical- See Also:
-
LETTER_VALUE_TRADITIONAL
public static final int LETTER_VALUE_TRADITIONALtraditional- See Also:
-
TOKEN_NONE
private static final int TOKEN_NONEno token type- See Also:
-
TOKEN_ALPHANUMERIC
private static final int TOKEN_ALPHANUMERICalhphanumeric token type- See Also:
-
TOKEN_NONALPHANUMERIC
private static final int TOKEN_NONALPHANUMERICnonalphanumeric token type- See Also:
-
DEFAULT_TOKEN
default token -
DEFAULT_SEPARATOR
default separator -
DEFAULT_LANGUAGE
-
prefix
prefix token -
suffix
suffix token -
tokens
sequence of tokens, as parsed from format -
separators
sequence of separators, as parsed from format -
groupingSeparator
private int groupingSeparatorgrouping separator -
groupingSize
private int groupingSizegrouping size -
letterValue
private int letterValueletter value -
features
letter value system -
language
language -
country
country -
equivalentLanguages
-
supportedAlphabeticSequences
private static int[][] supportedAlphabeticSequences -
supportedSpecials
private static int[][] supportedSpecials -
englishWordOnes
English Word Numerals -
englishWordTeens
-
englishWordTens
-
englishWordOthers
-
englishWordOnesOrd
-
englishWordTeensOrd
-
englishWordTensOrd
-
englishWordOthersOrd
-
frenchWordOnes
French Word Numerals -
frenchWordTeens
-
frenchWordTens
-
frenchWordOthers
-
frenchWordOnesOrdMale
-
frenchWordOnesOrdFemale
-
spanishWordOnes
Spanish Word Numerals -
spanishWordTeens
-
spanishWordTweens
-
spanishWordTens
-
spanishWordHundreds
-
spanishWordOthers
-
spanishWordOnesOrdMale
-
spanishWordOnesOrdFemale
-
romanMapping
private static int[] romanMappingRoman (Latin) Numerals -
romanStandardForms
-
romanLargeForms
-
romanNumberForms
-
hebrewGematriaAlphabeticMap
private static int[] hebrewGematriaAlphabeticMapGematria (Hebrew) Numerals -
arabicAbjadiAlphabeticMap
private static int[] arabicAbjadiAlphabeticMapArabic Numerals -
arabicHijaiAlphabeticMap
private static int[] arabicHijaiAlphabeticMap -
hiraganaGojuonAlphabeticMap
private static int[] hiraganaGojuonAlphabeticMapKana (Japanese) Numerals -
katakanaGojuonAlphabeticMap
private static int[] katakanaGojuonAlphabeticMap -
thaiAlphabeticMap
private static int[] thaiAlphabeticMapThai Numerals
-
-
Constructor Details
-
NumberConverter
public NumberConverter(String format, int groupingSeparator, int groupingSize, int letterValue, String features, String language, String country) throws IllegalArgumentException Construct parameterized number converter.- Parameters:
format- format for the page number (may be null or empty, which is treated as null)groupingSeparator- grouping separator (if zero, then no grouping separator applies)groupingSize- grouping size (if zero or negative, then no grouping size applies)letterValue- letter value (must be one of the above letter value enumeration values)features- features (feature sub-parameters)language- (may be null or empty, which is treated as null)country- (may be null or empty, which is treated as null)- Throws:
IllegalArgumentException- if format is not a valid UTF-16 string (e.g., has unpaired surrogate)
-
-
Method Details
-
convert
Convert a number to string according to conversion parameters.- Parameters:
number- number to conver- Returns:
- string representing converted number
-
convert
-
parseFormatTokens
- Throws:
IllegalArgumentException
-
isAlphaNumeric
private static boolean isAlphaNumeric(int c) -
convertNumbers
-
convertNumber
-
formatNumber
-
formatNumberAsDecimal
Format NUMBER as decimal using characters denoting digits that start at ONE, adding one or more (zero) padding characters as needed to fill out field WIDTH.- Parameters:
number- to be formattedone- unicode scalar value denoting numeric value 1width- non-negative integer denoting field width of number, possible including padding- Returns:
- formatted number as array of unicode scalars
-
performGrouping
-
formatNumberAsSequence
Format NUMBER as using sequence of characters that start at ONE, and having BASE radix.- Parameters:
number- to be formattedone- unicode scalar value denoting start of sequence (numeric value 1)base- number of elements in sequencemap- if non-null, then maps sequences indices to unicode scalars- Returns:
- formatted number as array of unicode scalars
-
formatNumberAsSpecial
Format NUMBER as using special system that starts at ONE.- Parameters:
number- to be formattedone- unicode scalar value denoting start of system (numeric value 1)- Returns:
- formatted number as array of unicode scalars
-
formatNumberAsWord
Format NUMBER as word according to TYPE, which must be either Character.UPPERCASE_LETTER, Character.LOWERCASE_LETTER, or Character.TITLECASE_LETTER. Makes use of this.language to determine language of word.- Parameters:
number- to be formattedcaseType- unicode character type for case conversion- Returns:
- formatted number as array of unicode scalars
-
isLanguage
-
isSameLanguage
-
hasFeature
-
appendScalars
-
scalarsToString
-
isPaddedOne
-
getDecimalValue
-
isStartOfDecimalSequence
private static boolean isStartOfDecimalSequence(int s) -
isStartOfAlphabeticSequence
private static boolean isStartOfAlphabeticSequence(int s) -
getSequenceBase
private static int getSequenceBase(int s) -
isStartOfNumericSpecial
private static boolean isStartOfNumericSpecial(int s) -
getSpecialFormatter
private NumberConverter.SpecialNumberFormatter getSpecialFormatter(int one, int letterValue, String features, String language, String country) -
toUpperCase
-
toLowerCase
-
convertWordCase
-
convertWordCase
-
joinWords
-