| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
|
| 11 |
import net.sf.saxon.Configuration; |
| 12 |
import net.sf.saxon.expr.Expression; |
| 13 |
import net.sf.saxon.expr.Literal; |
| 14 |
import net.sf.saxon.expr.XPathContext; |
| 15 |
import net.sf.saxon.expr.number.*; |
| 16 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 17 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 18 |
import net.sf.saxon.lib.Numberer; |
| 19 |
import net.sf.saxon.om.Sequence; |
| 20 |
import net.sf.saxon.regex.ARegularExpression; |
| 21 |
import net.sf.saxon.regex.RegularExpression; |
| 22 |
import net.sf.saxon.regex.UnicodeString; |
| 23 |
import net.sf.saxon.regex.charclass.Categories; |
| 24 |
import net.sf.saxon.trans.UncheckedXPathException; |
| 25 |
import net.sf.saxon.trans.XPathException; |
| 26 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 27 |
import net.sf.saxon.value.IntegerValue; |
| 28 |
import net.sf.saxon.value.StringValue; |
| 29 |
import net.sf.saxon.z.IntHashSet; |
| 30 |
import net.sf.saxon.z.IntSet; |
| 31 |
|
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.List; |
| 34 |
import java.util.function.Function; |
| 35 |
import java.util.function.IntPredicate; |
| 36 |
|
| 37 |
public class FormatInteger extends SystemFunction implements StatefulSystemFunction { |
| 38 |
|
| 39 |
private static final RegularExpression badHashPattern; |
| 40 |
private static final RegularExpression modifierPattern; |
| 41 |
private static final RegularExpression decimalDigitPattern; |
| 42 |
|
| 43 |
public static final String preface = "In the picture string for format-integer, "; |
| 44 |
|
| 45 |
static { |
| 46 |
try { |
| 47 |
badHashPattern = new ARegularExpression("((\\d+|\\w+)#+.*)|(#+[^\\d]+)", "", "XP20", null, null); |
| 48 |
modifierPattern = new ARegularExpression("([co](\\(.*\\))?)?[at]?", "", "XP20", null, null); |
| 49 |
decimalDigitPattern = new ARegularExpression("^((\\p{Nd}|#|[^\\p{N}\\p{L}])+?)$", "", "XP20", null, null); |
| 50 |
} catch (Exception e) { |
| 51 |
throw new AssertionError(e); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
private Function<IntegerValue, String> formatter = null; |
| 56 |
|
| 57 |
@Override |
| 58 |
public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException { |
| 59 |
boolean opt = true; |
| 60 |
if (!(arguments[1] instanceof Literal)) { |
| 61 |
opt = false; |
| 62 |
} |
| 63 |
if (arguments.length == 3 && !(arguments[2] instanceof Literal)) { |
| 64 |
opt = false; |
| 65 |
} |
| 66 |
if (!opt) { |
| 67 |
return super.makeOptimizedFunctionCall(visitor, contextInfo, arguments); |
| 68 |
} |
| 69 |
Configuration config = visitor.getConfiguration(); |
| 70 |
|
| 71 |
String language = arguments.length == 3 ? ((Literal)arguments[2]).getValue().getStringValue() : config.getDefaultLanguage(); |
| 72 |
Numberer numb = config.makeNumberer(language, null); |
| 73 |
|
| 74 |
formatter = makeFormatter(numb, ((Literal)arguments[1]).getValue().getStringValue()); |
| 75 |
return super.makeOptimizedFunctionCall(visitor, contextInfo, arguments); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public StringValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 88 |
return formatInteger( |
| 89 |
(IntegerValue) arguments[0].head(), |
| 90 |
(StringValue) arguments[1].head(), |
| 91 |
arguments.length == 2 ? null : (StringValue) arguments[2].head(), |
| 92 |
context); |
| 93 |
} |
| 94 |
|
| 95 |
private StringValue formatInteger(IntegerValue num, StringValue picture, StringValue language, XPathContext context) throws XPathException { |
| 96 |
Configuration config = context.getConfiguration(); |
| 97 |
|
| 98 |
if (num == null) { |
| 99 |
return StringValue.EMPTY_STRING; |
| 100 |
} |
| 101 |
|
| 102 |
Function<IntegerValue, String> localFormatter = formatter; |
| 103 |
|
| 104 |
if (localFormatter == null) { |
| 105 |
String languageVal; |
| 106 |
if (language != null) { |
| 107 |
languageVal = language.getStringValue(); |
| 108 |
} else { |
| 109 |
|
| 110 |
languageVal = config.getDefaultLanguage(); |
| 111 |
} |
| 112 |
Numberer numb = config.makeNumberer(languageVal, null); |
| 113 |
localFormatter = makeFormatter(numb, picture.getStringValue()); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
try { |
| 118 |
return new StringValue(localFormatter.apply(num)); |
| 119 |
} catch (UncheckedXPathException e) { |
| 120 |
throw e.getXPathException(); |
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
private Function<IntegerValue, String> makeFormatter(Numberer numb, String pic) throws XPathException { |
| 126 |
if (pic.isEmpty()) { |
| 127 |
throw new XPathException(preface + "the picture cannot be empty", "FODF1310"); |
| 128 |
} |
| 129 |
|
| 130 |
String primaryToken; |
| 131 |
String modifier; |
| 132 |
String parenthetical; |
| 133 |
|
| 134 |
int lastSemicolon = pic.lastIndexOf(';'); |
| 135 |
if (lastSemicolon >= 0) { |
| 136 |
primaryToken = pic.substring(0, lastSemicolon); |
| 137 |
if (primaryToken.isEmpty()) { |
| 138 |
throw new XPathException(preface + "the primary format token cannot be empty", "FODF1310"); |
| 139 |
} |
| 140 |
modifier = lastSemicolon < pic.length() - 1 ? pic.substring(lastSemicolon + 1) : ""; |
| 141 |
if (!modifierPattern.matches(modifier)) { |
| 142 |
throw new XPathException(preface + "the modifier is invalid", "FODF1310"); |
| 143 |
} |
| 144 |
} else { |
| 145 |
primaryToken = pic; |
| 146 |
modifier = ""; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
boolean ordinal = modifier.startsWith("o"); |
| 151 |
|
| 152 |
boolean alphabetic = modifier.endsWith("a"); |
| 153 |
|
| 154 |
int leftParen = modifier.indexOf('('); |
| 155 |
int rightParen = modifier.lastIndexOf(')'); |
| 156 |
|
| 157 |
parenthetical = leftParen < 0 ? "" : modifier.substring(leftParen + 1, rightParen); |
| 158 |
|
| 159 |
String letterValue = alphabetic ? "alphabetic" : "traditional"; |
| 160 |
String ordinalValue = ordinal ? "".equals(parenthetical) ? "yes" : parenthetical : ""; |
| 161 |
|
| 162 |
|
| 163 |
UnicodeString primary = UnicodeString.makeUnicodeString(primaryToken); |
| 164 |
IntPredicate isDecimalDigit = Categories.getCategory("Nd"); |
| 165 |
boolean isDecimalDigitPattern = false; |
| 166 |
for (int i = 0; i < primary.uLength(); i++) { |
| 167 |
if (isDecimalDigit.test(primary.uCharAt(i))) { |
| 168 |
isDecimalDigitPattern = true; |
| 169 |
break; |
| 170 |
} |
| 171 |
} |
| 172 |
if (isDecimalDigitPattern) { |
| 173 |
if (!decimalDigitPattern.matches(primaryToken)) { |
| 174 |
throw new XPathException( |
| 175 |
preface + "the primary format token contains a decimal digit but does not " + |
| 176 |
"meet the rules for a decimal digit pattern", "FODF1310"); |
| 177 |
} |
| 178 |
NumericGroupFormatter picGroupFormat = getPicSeparators(primaryToken); |
| 179 |
UnicodeString adjustedPicture = picGroupFormat.getAdjustedPicture(); |
| 180 |
return num -> { |
| 181 |
try { |
| 182 |
String s = numb.format(num.abs().longValue(), adjustedPicture, picGroupFormat, letterValue, ordinalValue); |
| 183 |
return num.signum() < 0 ? ("-" + s) : s; |
| 184 |
} catch (XPathException e) { |
| 185 |
throw new UncheckedXPathException(e); |
| 186 |
} |
| 187 |
}; |
| 188 |
} else { |
| 189 |
UnicodeString token = UnicodeString.makeUnicodeString(primaryToken); |
| 190 |
return num -> { |
| 191 |
try { |
| 192 |
String s = numb.format(num.abs().longValue(), token, null, letterValue, ordinalValue); |
| 193 |
return num.signum() < 0 ? ("-" + s) : s; |
| 194 |
} catch (XPathException e) { |
| 195 |
throw new UncheckedXPathException(e); |
| 196 |
} |
| 197 |
}; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public static NumericGroupFormatter getPicSeparators(String pic) throws XPathException { |
| 212 |
|
| 213 |
UnicodeString picExpanded = UnicodeString.makeUnicodeString(pic); |
| 214 |
IntSet groupingPositions = new IntHashSet(5); |
| 215 |
List<Integer> separatorList = new ArrayList<>(); |
| 216 |
int groupingPosition = 0; |
| 217 |
int firstGroupingPos = 0; |
| 218 |
int lastGroupingPos = 0; |
| 219 |
boolean regularCheck = true; |
| 220 |
int zeroDigit = -1; |
| 221 |
|
| 222 |
if (badHashPattern.matches(pic)) { |
| 223 |
throw new XPathException(preface + "the picture is not valid (it uses '#' where disallowed)", "FODF1310"); |
| 224 |
} |
| 225 |
|
| 226 |
for (int i = picExpanded.uLength() - 1; i >= 0; i--) { |
| 227 |
|
| 228 |
final int codePoint = picExpanded.uCharAt(i); |
| 229 |
switch (Character.getType(codePoint)) { |
| 230 |
|
| 231 |
case Character.DECIMAL_DIGIT_NUMBER: |
| 232 |
|
| 233 |
if (zeroDigit == -1) { |
| 234 |
zeroDigit = Alphanumeric.getDigitFamily(codePoint); |
| 235 |
} else { |
| 236 |
if (zeroDigit != Alphanumeric.getDigitFamily(codePoint)) { |
| 237 |
throw new XPathException( |
| 238 |
preface + "the picture mixes digits from different digit families", "FODF1310"); |
| 239 |
} |
| 240 |
} |
| 241 |
groupingPosition++; |
| 242 |
break; |
| 243 |
|
| 244 |
case Character.LETTER_NUMBER: |
| 245 |
case Character.OTHER_NUMBER: |
| 246 |
case Character.UPPERCASE_LETTER: |
| 247 |
case Character.LOWERCASE_LETTER: |
| 248 |
case Character.MODIFIER_LETTER: |
| 249 |
case Character.OTHER_LETTER: |
| 250 |
break; |
| 251 |
|
| 252 |
default: |
| 253 |
if (i == picExpanded.uLength() - 1) { |
| 254 |
throw new XPathException(preface + "the picture cannot end with a separator", "FODF1310"); |
| 255 |
} |
| 256 |
if (codePoint == '#') { |
| 257 |
groupingPosition++; |
| 258 |
if (i != 0) { |
| 259 |
switch (Character.getType(picExpanded.uCharAt(i - 1))) { |
| 260 |
case Character.DECIMAL_DIGIT_NUMBER: |
| 261 |
case Character.LETTER_NUMBER: |
| 262 |
case Character.OTHER_NUMBER: |
| 263 |
case Character.UPPERCASE_LETTER: |
| 264 |
case Character.LOWERCASE_LETTER: |
| 265 |
case Character.MODIFIER_LETTER: |
| 266 |
case Character.OTHER_LETTER: |
| 267 |
throw new XPathException( |
| 268 |
preface + "the picture cannot contain alphanumeric character(s) before character '#'", "FODF1310"); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
break; |
| 273 |
} else { |
| 274 |
boolean added = groupingPositions.add(groupingPosition); |
| 275 |
if (!added) { |
| 276 |
throw new XPathException(preface + "the picture contains consecutive separators", "FODF1310"); |
| 277 |
} |
| 278 |
separatorList.add(codePoint); |
| 279 |
|
| 280 |
if (groupingPositions.size() == 1) { |
| 281 |
firstGroupingPos = groupingPosition; |
| 282 |
} else { |
| 283 |
|
| 284 |
if (groupingPosition != firstGroupingPos * groupingPositions.size()) { |
| 285 |
regularCheck = false; |
| 286 |
} |
| 287 |
if (separatorList.get(0) != codePoint) { |
| 288 |
regularCheck = false; |
| 289 |
} |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
if (i == 0) { |
| 294 |
throw new XPathException(preface + "the picture cannot begin with a separator", "FODF1310"); |
| 295 |
} |
| 296 |
lastGroupingPos = groupingPosition; |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
if (regularCheck && groupingPositions.size() >= 1) { |
| 301 |
if (picExpanded.uLength() - lastGroupingPos - groupingPositions.size() > firstGroupingPos) { |
| 302 |
regularCheck = false; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
UnicodeString adjustedPic = extractSeparators(picExpanded, groupingPositions); |
| 307 |
if (groupingPositions.isEmpty()) { |
| 308 |
return new RegularGroupFormatter(0, "", adjustedPic); |
| 309 |
} |
| 310 |
|
| 311 |
if (regularCheck) { |
| 312 |
if (separatorList.isEmpty()) { |
| 313 |
return new RegularGroupFormatter(0, "", adjustedPic); |
| 314 |
} else { |
| 315 |
FastStringBuffer sb = new FastStringBuffer(4); |
| 316 |
sb.appendWideChar(separatorList.get(0)); |
| 317 |
return new RegularGroupFormatter(firstGroupingPos, sb.toString(), adjustedPic); |
| 318 |
} |
| 319 |
} else { |
| 320 |
return new IrregularGroupFormatter(groupingPositions, separatorList, adjustedPic); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
private static UnicodeString extractSeparators(UnicodeString arr, IntSet excludePositions) { |
| 332 |
|
| 333 |
FastStringBuffer fsb = new FastStringBuffer(arr.uLength()); |
| 334 |
for (int i = 0; i < arr.uLength(); i++) { |
| 335 |
if (NumberFormatter.isLetterOrDigit(arr.uCharAt(i))) { |
| 336 |
fsb.appendWideChar(arr.uCharAt(i)); |
| 337 |
} |
| 338 |
} |
| 339 |
return UnicodeString.makeUnicodeString(fsb); |
| 340 |
} |
| 341 |
|
| 342 |
@Override |
| 343 |
public SystemFunction copy() { |
| 344 |
FormatInteger fi2 = (FormatInteger) SystemFunction.makeFunction(getFunctionName().getLocalPart(), getRetainedStaticContext(), getArity()); |
| 345 |
fi2.formatter = formatter; |
| 346 |
return fi2; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|