| 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.om; |
| 9 | |
| 10 | import java.io.File; |
| 11 | |
| 12 | /** |
| 13 | * This class encapsulates a string used as the value of the document-uri() property of a document, |
| 14 | * together with a normalized representation of the string used for equality comparisons. The idea |
| 15 | * is that on Windows systems, document URIs are compared using case-blind comparison, but the original |
| 16 | * case is retained for display purposes. |
| 17 | */ |
| 18 | public class DocumentURI { |
| 19 | |
| 20 | public final static boolean CASE_BLIND_FILES = new File("a").equals(new File("A")); |
| 21 | |
| 22 | private String displayValue; |
| 23 | private String normalizedValue; |
| 24 | |
| 25 | /** |
| 26 | * Create a DocumentURI object that wraps a given URI |
| 27 | * |
| 28 | * @param uri the URI to be wrapped. Must not be null |
| 29 | * @throws NullPointerException if uri is null |
| 30 | */ |
| 31 | |
| 32 | public DocumentURI(/*@Nullable*/ String uri) { |
| 33 | if (uri == null) { |
| 34 | throw new NullPointerException("uri"); |
| 35 | } |
| 36 | this.displayValue = uri; |
| 37 | this.normalizedValue = normalizeURI(uri); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public String toString() { |
| 42 | return displayValue; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public boolean equals(Object obj) { |
| 47 | return obj instanceof DocumentURI && normalizedValue.equals(((DocumentURI) obj).normalizedValue); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public int hashCode() { |
| 52 | return normalizedValue.hashCode(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Normalize the representation of file: URIs to give better equality matching than straight |
| 57 | * string comparison. The main purpose is (a) to eliminate the distinction between "file:/" and |
| 58 | * "file:///", and (b) to normalize case in the case of Windows filenames: especially the distinction |
| 59 | * between "file:/C:" and "file:/c:". |
| 60 | * |
| 61 | * @param uri the URI to be normalized |
| 62 | * @return the normalized URI. |
| 63 | */ |
| 64 | |
| 65 | public static String normalizeURI(String uri) { |
| 66 | if (uri == null) { |
| 67 | return null; |
| 68 | } |
| 69 | if (uri.startsWith("FILE:")) { |
| 70 | uri = "file:" + uri.substring(5); |
| 71 | } |
| 72 | if (uri.startsWith("file:")) { |
| 73 | if (uri.startsWith("file:///")) { |
| 74 | uri = "file:/" + uri.substring(8); |
| 75 | } |
| 76 | if (CASE_BLIND_FILES) { |
| 77 | uri = uri.toLowerCase(); |
| 78 | } |
| 79 | } |
| 80 | return uri; |
| 81 | } |
| 82 | } |
| 83 |