| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.value; |
| 9 | |
| 10 | import net.sf.saxon.expr.sort.AtomicMatchKey; |
| 11 | import net.sf.saxon.lib.StringCollator; |
| 12 | import net.sf.saxon.regex.*; |
| 13 | import net.sf.saxon.tree.iter.UnfailingIterator; |
| 14 | import net.sf.saxon.tree.util.FastStringBuffer; |
| 15 | import net.sf.saxon.type.AtomicType; |
| 16 | import net.sf.saxon.type.BuiltInAtomicType; |
| 17 | |
| 18 | |
| 19 | /** |
| 20 | * An atomic value of type xs:string. This class is also used for types derived from xs:string. |
| 21 | * Subclasses of StringValue are used for xs:untypedAtomic and xs:anyURI values. |
| 22 | */ |
| 23 | |
| 24 | public class StringValue extends AtomicValue { |
| 25 | |
| 26 | public static final StringValue EMPTY_STRING = new StringValue(EmptyString.THE_INSTANCE); |
| 27 | public static final StringValue SINGLE_SPACE = new StringValue(LatinString.SINGLE_SPACE); |
| 28 | public static final StringValue TRUE = new StringValue(new LatinString("true")); |
| 29 | public static final StringValue FALSE = new StringValue(new LatinString("false")); |
| 30 | |
| 31 | // We hold the value as a CharSequence (it may be a StringBuffer rather than a string) |
| 32 | // But the first time this is converted to a string, we keep it as a string |
| 33 | |
| 34 | protected CharSequence value; // may be zero-length, will never be null |
| 35 | |
| 36 | /** |
| 37 | * Protected constructor for use by subtypes |
| 38 | */ |
| 39 | |
| 40 | protected StringValue() { |
| 41 | value = ""; |
| 42 | typeLabel = BuiltInAtomicType.STRING; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Constructor. Note that although a StringValue may wrap any kind of CharSequence |
| 47 | * (usually a String, but it can also be, for example, a StringBuffer), the caller |
| 48 | * is responsible for ensuring that the value is immutable. |
| 49 | * |
| 50 | * @param value the String value. Null is taken as equivalent to "". |
| 51 | */ |
| 52 | |
| 53 | public StringValue(/*@Nullable*/ CharSequence value) { |
| 54 | this.value = value == null ? "" : value; |
| 55 | typeLabel = BuiltInAtomicType.STRING; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Constructor. Note that although a StringValue may wrap any kind of CharSequence |
| 60 | * (usually a String, but it can also be, for example, a StringBuffer), the caller |
| 61 | * is responsible for ensuring that the value is immutable. |
| 62 | * |
| 63 | * @param value the String value. |
| 64 | * @param typeLabel the type of the value to be created. The caller must ensure that this is |
| 65 | * a type derived from string and that the string is valid against this type. |
| 66 | */ |
| 67 | |
| 68 | public StringValue(CharSequence value, AtomicType typeLabel) { |
| 69 | this.value = value; |
| 70 | this.typeLabel = typeLabel; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * Assert that the string is known to contain no surrogate pairs |
| 76 | */ |
| 77 | |
| 78 | public synchronized void setContainsNoSurrogates() { |
| 79 | if (!(value instanceof BMPString || value instanceof LatinString || value instanceof EmptyString)) { |
| 80 | value = new BMPString(value); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Create a copy of this atomic value, with a different type label |
| 86 | * |
| 87 | * @param typeLabel the type label of the new copy. The caller is responsible for checking that |
| 88 | * the value actually conforms to this type. |
| 89 | */ |
| 90 | |
| 91 | public AtomicValue copyAsSubType(AtomicType typeLabel) { |
| 92 | StringValue v = new StringValue(value); |
| 93 | v.typeLabel = typeLabel; |
| 94 | return v; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Determine the primitive type of the value. This delivers the same answer as |
| 99 | * getItemType().getPrimitiveItemType(). The primitive types are |
| 100 | * the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration, |
| 101 | * and xs:untypedAtomic. For external objects, the result is AnyAtomicType. |
| 102 | */ |
| 103 | |
| 104 | public BuiltInAtomicType getPrimitiveType() { |
| 105 | return BuiltInAtomicType.STRING; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Factory method. Unlike the constructor, this avoids creating a new StringValue in the case |
| 110 | * of a zero-length string (and potentially other strings, in future) |
| 111 | * |
| 112 | * @param value the String value. Null is taken as equivalent to "". |
| 113 | * @return the corresponding StringValue |
| 114 | */ |
| 115 | |
| 116 | /*@NotNull*/ |
| 117 | public static StringValue makeStringValue(/*@Nullable*/ CharSequence value) { |
| 118 | if (value == null || value.length() == 0) { |
| 119 | return StringValue.EMPTY_STRING; |
| 120 | } else { |
| 121 | return new StringValue(value); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Utility method to test whether a CharSequence is empty |
| 127 | * @param string the input CharSequence |
| 128 | * @return true if the CharSequence is empty |
| 129 | */ |
| 130 | |
| 131 | public static boolean isEmpty(CharSequence string) { |
| 132 | if (string instanceof String) { |
| 133 | return ((String) string).isEmpty(); |
| 134 | } else if (string instanceof UnicodeString) { |
| 135 | return ((UnicodeString)string).uLength() == 0; |
| 136 | } else { |
| 137 | return string.length() == 0; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // public static StringValue makeStringValue(UnicodeString value) { |
| 142 | // if (value == null || value.uLength() == 0) { |
| 143 | // return StringValue.EMPTY_STRING; |
| 144 | // } else { |
| 145 | // StringValue sv = new StringValue(value.getCharSequence()); |
| 146 | // sv.unicodeString = value; |
| 147 | // return sv; |
| 148 | // } |
| 149 | // } |
| 150 | |
| 151 | /** |
| 152 | * Get the string value as a CharSequence |
| 153 | */ |
| 154 | |
| 155 | public final CharSequence getPrimitiveStringValue() { |
| 156 | return value; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Set the value of the item as a CharSequence. |
| 161 | * <p><b>For system use only. In principle, a StringValue is immutable. However, in special circumstances, |
| 162 | * if it is newly constructed, the content can be changed to reflect the effect of the whiteSpace facet.</b></p> |
| 163 | * |
| 164 | * @param value the value of the string |
| 165 | */ |
| 166 | |
| 167 | public final void setStringValueCS(CharSequence value) { |
| 168 | this.value = value; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get the length of this string, as defined in XPath. This is not the same as the Java length, |
| 173 | * as a Unicode surrogate pair counts as a single character |
| 174 | * |
| 175 | * @return the length of the string in Unicode code points |
| 176 | */ |
| 177 | |
| 178 | public synchronized int getStringLength() { |
| 179 | if (!(value instanceof UnicodeString)) { |
| 180 | makeUnicodeString(); |
| 181 | } |
| 182 | return ((UnicodeString)value).uLength(); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Get an upper bound on the length of the string in Unicode codepoints. |
| 187 | * @return a value N such that getStringLength <= N. In practice, if the string is held |
| 188 | * as UTF16 codepoints this will be the length in UTF16 codepoints; if it is held in Unicode |
| 189 | * codepoints, it will be the length in Unicode codepoints |
| 190 | */ |
| 191 | |
| 192 | public synchronized int getStringLengthUpperBound() { |
| 193 | if (value instanceof UnicodeString) { |
| 194 | return ((UnicodeString) value).uLength(); |
| 195 | } else { |
| 196 | return value.length(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Get a UnicodeString value representing the same characters as this string. This is a memo-function; |
| 202 | * the value is computed the first time it is needed, and is cached for subsequent reuse |
| 203 | * |
| 204 | * @return the corresponding UnicodeString |
| 205 | */ |
| 206 | |
| 207 | public synchronized UnicodeString getUnicodeString() { |
| 208 | if (!(value instanceof UnicodeString)) { |
| 209 | makeUnicodeString(); |
| 210 | } |
| 211 | return (UnicodeString)value; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Construct a Unicode representation of this string in which each character occupies a fixed amount of space, |
| 216 | * allowing direct addressing and counting of Unicode characters |
| 217 | */ |
| 218 | |
| 219 | private void makeUnicodeString() { |
| 220 | value = UnicodeString.makeUnicodeString(value); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Get the length of a string, as defined in XPath. This is not the same as the Java length, |
| 225 | * as a Unicode surrogate pair counts as a single character. |
| 226 | * |
| 227 | * @param s The string whose length is required |
| 228 | * @return the length of the string in Unicode code points |
| 229 | */ |
| 230 | |
| 231 | public static int getStringLength(/*@NotNull*/ CharSequence s) { |
| 232 | if (s instanceof UnicodeString) { |
| 233 | return ((UnicodeString) s).uLength(); |
| 234 | } |
| 235 | int n = 0; |
| 236 | for (int i = 0; i < s.length(); i++) { |
| 237 | int c = (int) s.charAt(i); |
| 238 | if (c < 55296 || c > 56319) { |
| 239 | n++; // don't count high surrogates, i.e. D800 to DBFF |
| 240 | } |
| 241 | } |
| 242 | return n; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | /** |
| 247 | * Determine whether the string is a zero-length string. This may |
| 248 | * be more efficient than testing whether the length is equal to zero |
| 249 | * |
| 250 | * @return true if the string is zero length |
| 251 | */ |
| 252 | |
| 253 | public boolean isZeroLength() { |
| 254 | return value.length() == 0; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Determine whether the string contains surrogate pairs |
| 259 | * |
| 260 | * @return true if the string contains any non-BMP characters |
| 261 | */ |
| 262 | |
| 263 | public boolean containsSurrogatePairs() { |
| 264 | return UnicodeString.containsSurrogatePairs(value); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Ask whether the string is known to contain no surrogate pairs. |
| 269 | * |
| 270 | * @return true if it is known to contain no surrogates, false if the answer is not known |
| 271 | */ |
| 272 | |
| 273 | public boolean isKnownToContainNoSurrogates() { |
| 274 | return value instanceof BMPString || value instanceof LatinString || value instanceof EmptyString; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Iterate over a string, returning a sequence of integers representing the Unicode code-point values |
| 279 | * |
| 280 | * @return an iterator over the characters (Unicode code points) in the string |
| 281 | */ |
| 282 | |
| 283 | /*@NotNull*/ |
| 284 | public synchronized UnfailingIterator<Int64Value> iterateCharacters() { |
| 285 | if (value instanceof UnicodeString) { |
| 286 | return new UnicodeCharacterIterator((UnicodeString)value); |
| 287 | } else { |
| 288 | return new CharacterIterator(value); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Expand a string containing surrogate pairs into an array of 32-bit characters |
| 294 | * |
| 295 | * @param s the string to be expanded |
| 296 | * @return an array of integers representing the Unicode code points |
| 297 | */ |
| 298 | |
| 299 | /*@NotNull*/ |
| 300 | public static int[] expand(/*@NotNull*/ CharSequence s) { |
| 301 | int[] array = new int[getStringLength(s)]; |
| 302 | int o = 0; |
| 303 | for (int i = 0; i < s.length(); i++) { |
| 304 | int charval; |
| 305 | int c = s.charAt(i); |
| 306 | if (c >= 55296 && c <= 56319) { |
| 307 | // we'll trust the data to be sound |
| 308 | charval = ((c - 55296) * 1024) + ((int) s.charAt(i + 1) - 56320) + 65536; |
| 309 | i++; |
| 310 | } else { |
| 311 | charval = c; |
| 312 | } |
| 313 | array[o++] = charval; |
| 314 | } |
| 315 | return array; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Contract an array of integers containing Unicode codepoints into a Java string |
| 320 | * |
| 321 | * @param codes an array of integers representing the Unicode code points |
| 322 | * @param used the number of items in the array that are actually used |
| 323 | * @return the constructed string |
| 324 | */ |
| 325 | |
| 326 | /*@NotNull*/ |
| 327 | public static CharSequence contract(/*@NotNull*/ int[] codes, int used) { |
| 328 | FastStringBuffer sb = new FastStringBuffer(codes.length); |
| 329 | for (int i = 0; i < used; i++) { |
| 330 | sb.appendWideChar(codes[i]); |
| 331 | } |
| 332 | return sb; |
| 333 | } |
| 334 | |
| 335 | |
| 336 | /** |
| 337 | * Get an object value that implements the XPath equality and ordering comparison semantics for this value. |
| 338 | * If the ordered parameter is set to true, the result will be a Comparable and will support a compareTo() |
| 339 | * method with the semantics of the XPath lt/gt operator, provided that the other operand is also obtained |
| 340 | * using the getXPathComparable() method. In all cases the result will support equals() and hashCode() methods |
| 341 | * that support the semantics of the XPath eq operator, again provided that the other operand is also obtained |
| 342 | * using the getXPathComparable() method. A context argument is supplied for use in cases where the comparison |
| 343 | * semantics are context-sensitive, for example where they depend on the implicit timezone or the default |
| 344 | * collation. |
| 345 | * |
| 346 | * |
| 347 | * @param ordered true if an ordered comparison is required. In this case the result is null if the |
| 348 | * type is unordered; in other cases the returned value will be a Comparable. |
| 349 | * @param collator Collation to be used for comparing strings |
| 350 | * @param implicitTimezone the XPath dynamic evaluation context, used in cases where the comparison is context |
| 351 | * sensitive |
| 352 | * @return an Object whose equals() and hashCode() methods implement the XPath comparison semantics |
| 353 | * with respect to this atomic value. If ordered is specified, the result will either be null if |
| 354 | * no ordering is defined, or will be a Comparable |
| 355 | */ |
| 356 | |
| 357 | public AtomicMatchKey getXPathComparable(boolean ordered, /*@NotNull*/ StringCollator collator, int implicitTimezone) { |
| 358 | return collator.getCollationKey(value); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Determine if two AtomicValues are equal, according to XPath rules. (This method |
| 363 | * is not used for string comparisons, which are always under the control of a collation. |
| 364 | * If we get here, it's because there's a type error in the comparison.) |
| 365 | * |
| 366 | * @throws ClassCastException always |
| 367 | */ |
| 368 | |
| 369 | @SuppressWarnings({"EqualsWhichDoesntCheckParameterClass", "Contract"}) |
| 370 | public boolean equals(Object other) { |
| 371 | throw new ClassCastException("equals on StringValue is not allowed"); |
| 372 | } |
| 373 | |
| 374 | public int hashCode() { |
| 375 | return value.hashCode(); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Test whether this StringValue is equal to another under the rules of the codepoint collation |
| 380 | * |
| 381 | * @param other the value to be compared with this value |
| 382 | * @return true if the strings are equal on a codepoint-by-codepoint basis |
| 383 | */ |
| 384 | |
| 385 | public boolean codepointEquals(/*@NotNull*/ StringValue other) { |
| 386 | if (value instanceof String) { |
| 387 | return ((String) value).contentEquals(other.value); |
| 388 | } else if (other.value instanceof String) { |
| 389 | return ((String)other.value).contentEquals(value); |
| 390 | } else if (value instanceof UnicodeString) { |
| 391 | if (!(other.value instanceof UnicodeString)) { |
| 392 | other.makeUnicodeString(); |
| 393 | } |
| 394 | return value.equals(other.value); |
| 395 | } else { |
| 396 | // Avoid conversion to String unless the lengths are equal |
| 397 | return value.length() == other.value.length() && value.toString().equals(other.value.toString()); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Get the effective boolean value of a string |
| 403 | * |
| 404 | * @return true if the string has length greater than zero |
| 405 | */ |
| 406 | |
| 407 | public boolean effectiveBooleanValue() { |
| 408 | return value.length() > 0; |
| 409 | } |
| 410 | |
| 411 | |
| 412 | /*@NotNull*/ |
| 413 | public String toString() { |
| 414 | return "\"" + value + '\"'; |
| 415 | } |
| 416 | |
| 417 | public String toShortString() { |
| 418 | String s = value.toString(); |
| 419 | if (s.length() > 40) { |
| 420 | s = s.substring(0,35) + "..."; |
| 421 | } |
| 422 | return "\"" + s + '\"'; |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Get a Comparable value that implements the XML Schema comparison semantics for this value. |
| 427 | * Returns null if the value is not comparable according to XML Schema rules. This implementation |
| 428 | * returns the underlying Java string, which works because strings will only be compared for |
| 429 | * equality, not for ordering, and the equality rules for strings in XML schema are the same as in Java. |
| 430 | */ |
| 431 | |
| 432 | public Comparable getSchemaComparable() { |
| 433 | return getStringValue(); |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Determine whether two atomic values are identical, as determined by XML Schema rules. This is a stronger |
| 438 | * test than equality (even schema-equality); for example two dateTime values are not identical unless |
| 439 | * they are in the same timezone. |
| 440 | * <p>Note that even this check ignores the type annotation of the value. The integer 3 and the short 3 |
| 441 | * are considered identical, even though they are not fully interchangeable. "Identical" means the |
| 442 | * same point in the value space, regardless of type annotation.</p> |
| 443 | * <p>NaN is identical to itself.</p> |
| 444 | * |
| 445 | * @param v the other value to be compared with this one |
| 446 | * @return true if the two values are identical, false otherwise. |
| 447 | */ |
| 448 | |
| 449 | public boolean isIdentical(/*@NotNull*/ AtomicValue v) { |
| 450 | return v instanceof StringValue && |
| 451 | (this instanceof AnyURIValue == v instanceof AnyURIValue) && |
| 452 | (this instanceof UntypedAtomicValue == v instanceof UntypedAtomicValue) && |
| 453 | codepointEquals((StringValue) v); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Produce a diagnostic representation of the contents of the string |
| 458 | * |
| 459 | * @param s the string |
| 460 | * @return a string in which non-Ascii-printable characters are replaced by \ uXXXX escapes |
| 461 | */ |
| 462 | |
| 463 | /*@NotNull*/ |
| 464 | public static String diagnosticDisplay(/*@NotNull*/ String s) { |
| 465 | FastStringBuffer fsb = new FastStringBuffer(s.length()); |
| 466 | for (int i = 0, len = s.length(); i < len; i++) { |
| 467 | char c = s.charAt(i); |
| 468 | if (c >= 0x20 && c <= 0x7e) { |
| 469 | fsb.append(c); |
| 470 | } else { |
| 471 | fsb.append("\\u"); |
| 472 | for (int shift = 12; shift >= 0; shift -= 4) { |
| 473 | fsb.append("0123456789ABCDEF".charAt((c >> shift) & 0xF)); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | return fsb.toString(); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * CharacterIterator is used to iterate over the characters in a string, |
| 482 | * returning them as integers representing the Unicode code-point. |
| 483 | */ |
| 484 | |
| 485 | |
| 486 | public final static class CharacterIterator implements UnfailingIterator<Int64Value> { |
| 487 | |
| 488 | int inpos = 0; // 0-based index of the current Java char |
| 489 | private CharSequence value; |
| 490 | |
| 491 | /** |
| 492 | * Create an iterator over a string |
| 493 | */ |
| 494 | |
| 495 | public CharacterIterator(CharSequence value) { |
| 496 | this.value = value; |
| 497 | } |
| 498 | |
| 499 | /*@Nullable*/ |
| 500 | public Int64Value next() { |
| 501 | if (inpos < value.length()) { |
| 502 | int c = value.charAt(inpos++); |
| 503 | int current; |
| 504 | if (c >= 55296 && c <= 56319) { |
| 505 | // we'll trust the data to be sound |
| 506 | try { |
| 507 | current = ((c - 55296) * 1024) + ((int) value.charAt(inpos++) - 56320) + 65536; |
| 508 | } catch (StringIndexOutOfBoundsException e) { |
| 509 | System.err.println("Invalid surrogate at end of string"); |
| 510 | System.err.println(diagnosticDisplay(value.toString())); |
| 511 | e.printStackTrace(); |
| 512 | throw e; |
| 513 | } |
| 514 | } else { |
| 515 | current = c; |
| 516 | } |
| 517 | return new Int64Value(current); |
| 518 | } else { |
| 519 | return null; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | } |
| 524 | |
| 525 | public final static class UnicodeCharacterIterator implements UnfailingIterator<Int64Value> { |
| 526 | |
| 527 | UnicodeString uValue; |
| 528 | int inpos = 0; // 0-based index of the current Java char |
| 529 | |
| 530 | /** |
| 531 | * Create an iterator over a string |
| 532 | */ |
| 533 | |
| 534 | public UnicodeCharacterIterator(UnicodeString value) { |
| 535 | this.uValue = value; |
| 536 | } |
| 537 | |
| 538 | /*@Nullable*/ |
| 539 | public Int64Value next() { |
| 540 | if (inpos < uValue.uLength()) { |
| 541 | return new Int64Value(uValue.uCharAt(inpos++)); |
| 542 | } else { |
| 543 | return null; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | public void close() { |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Get properties of this iterator, as a bit-significant integer. |
| 552 | * |
| 553 | * @return the properties of this iterator. This will be some combination of |
| 554 | * properties such as {@link #GROUNDED} and {@link #LAST_POSITION_FINDER}. It is always |
| 555 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 556 | */ |
| 557 | |
| 558 | public int getProperties() { |
| 559 | return 0; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | } |
| 564 |