| 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.tree.linked; |
| 9 | |
| 10 | import java.util.Arrays; |
| 11 | |
| 12 | /** |
| 13 | * System IDs are not held in nodes in the tree, because they are usually the same |
| 14 | * for a whole document. |
| 15 | * <p> |
| 16 | * This class provides a map from element sequence numbers to System IDs: it is |
| 17 | * linked to the root node of the tree.</p> |
| 18 | * <p> |
| 19 | * Note that the System ID is not necessarily the same as the Base URI. The System ID relates |
| 20 | * to the external entity in which a node was physically located; this provides a default for |
| 21 | * the Base URI, but this may be modified by specifying an xml:base attribute.</p> |
| 22 | * <p> |
| 23 | * The system IDs of nodes are assumed to be immutable; they can only be set sequentially, |
| 24 | * as the nodes are being created. </p> |
| 25 | * |
| 26 | * @author Michael H. Kay |
| 27 | */ |
| 28 | |
| 29 | public class SystemIdMap { |
| 30 | |
| 31 | private int[] sequenceNumbers; |
| 32 | private String[] uris; |
| 33 | private int allocated; |
| 34 | |
| 35 | public SystemIdMap() { |
| 36 | sequenceNumbers = new int[4]; |
| 37 | uris = new String[4]; |
| 38 | allocated = 0; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Set the system ID corresponding to a given sequence number |
| 43 | * @param sequence the sequence number (position in document order) of the |
| 44 | * node whose system ID is to be set. This must be one greater |
| 45 | * that the previous system ID. |
| 46 | */ |
| 47 | |
| 48 | public void setSystemId(int sequence, String uri) { |
| 49 | if (allocated > 0) { |
| 50 | // ignore it if same as previous |
| 51 | if (uri.equals(uris[allocated - 1])) { |
| 52 | return; |
| 53 | } |
| 54 | if (sequence <= sequenceNumbers[allocated - 1]) { |
| 55 | throw new IllegalArgumentException("System IDs of nodes are immutable"); |
| 56 | } |
| 57 | } |
| 58 | if (sequenceNumbers.length <= allocated + 1) { |
| 59 | sequenceNumbers = Arrays.copyOf(sequenceNumbers, allocated * 2); |
| 60 | uris = Arrays.copyOf(uris, allocated * 2); |
| 61 | } |
| 62 | sequenceNumbers[allocated] = sequence; |
| 63 | uris[allocated] = uri; |
| 64 | allocated++; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get the system ID corresponding to a given sequence number |
| 69 | */ |
| 70 | |
| 71 | /*@Nullable*/ |
| 72 | public String getSystemId(int sequence) { |
| 73 | if (allocated == 0) { |
| 74 | return null; |
| 75 | } |
| 76 | // could use a binary chop, but it's not important |
| 77 | for (int i = 1; i < allocated; i++) { |
| 78 | if (sequenceNumbers[i] > sequence) { |
| 79 | return uris[i - 1]; |
| 80 | } |
| 81 | } |
| 82 | return uris[allocated - 1]; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | } |