| 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 net.sf.saxon.trans.KeyManager; |
| 11 | |
| 12 | import java.util.HashMap; |
| 13 | import java.util.HashSet; |
| 14 | import java.util.Map; |
| 15 | import java.util.Set; |
| 16 | |
| 17 | /** |
| 18 | * An object representing the collection of documents handled during |
| 19 | * a single transformation. |
| 20 | * <p>The function of allocating document numbers is performed |
| 21 | * by the DocumentNumberAllocator in the Configuration, not by the DocumentPool. This has a |
| 22 | * number of effects: in particular it allows operations involving multiple |
| 23 | * documents (such as generateId() and document()) to occur in a free-standing |
| 24 | * XPath environment.</p> |
| 25 | */ |
| 26 | |
| 27 | public final class DocumentPool { |
| 28 | |
| 29 | // The document pool ensures that the document() |
| 30 | // function, when called twice with the same URI, returns the same document |
| 31 | // each time. For this purpose we use a hashtable from |
| 32 | // URI to DocumentInfo object. |
| 33 | |
| 34 | private Map<DocumentURI, TreeInfo> documentNameMap = new HashMap<DocumentURI, TreeInfo>(10); |
| 35 | |
| 36 | |
| 37 | // The set of documents known to be unavailable. These documents must remain |
| 38 | // unavailable for the duration of a transformation or query! |
| 39 | |
| 40 | private Set<DocumentURI> unavailableDocuments = new HashSet<DocumentURI>(10); |
| 41 | |
| 42 | /** |
| 43 | * Add a document to the pool |
| 44 | * |
| 45 | * @param doc The DocumentInfo for the document in question |
| 46 | * @param uri The document-uri property of the document. |
| 47 | */ |
| 48 | |
| 49 | public synchronized void add(TreeInfo doc, /*@Nullable*/ String uri) { |
| 50 | if (uri != null) { |
| 51 | documentNameMap.put(new DocumentURI(uri), doc); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Add a document to the pool |
| 57 | * |
| 58 | * @param doc The DocumentInfo for the document in question |
| 59 | * @param uri The document-uri property of the document. |
| 60 | */ |
| 61 | |
| 62 | public synchronized void add(TreeInfo doc, /*@Nullable*/ DocumentURI uri) { |
| 63 | if (uri != null) { |
| 64 | documentNameMap.put(uri, doc); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the document with a given document-uri |
| 70 | * |
| 71 | * @param uri The document-uri property of the document. |
| 72 | * @return the DocumentInfo with the given document-uri property if it exists, |
| 73 | * or null if it is not found. |
| 74 | */ |
| 75 | |
| 76 | public synchronized TreeInfo find(String uri) { |
| 77 | return documentNameMap.get(new DocumentURI(uri)); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the document with a given document-uri |
| 82 | * |
| 83 | * @param uri The document-uri property of the document. |
| 84 | * @return the DocumentInfo with the given document-uri property if it exists, |
| 85 | * or null if it is not found. |
| 86 | */ |
| 87 | |
| 88 | public synchronized TreeInfo find(DocumentURI uri) { |
| 89 | return documentNameMap.get(uri); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /** |
| 94 | * Get the URI for a given document node, if it is present in the pool. This supports the |
| 95 | * document-uri() function. |
| 96 | * |
| 97 | * @param doc The document node |
| 98 | * @return The uri of the document node, if present in the pool, or the systemId of the document node otherwise |
| 99 | */ |
| 100 | |
| 101 | /*@Nullable*/ |
| 102 | public synchronized String getDocumentURI(NodeInfo doc) { |
| 103 | for (DocumentURI uri : documentNameMap.keySet()) { |
| 104 | TreeInfo found = find(uri); |
| 105 | if (found == null) { |
| 106 | continue; // can happen when discard-document() is used concurrently |
| 107 | } |
| 108 | if (found.getRootNode().equals(doc)) { |
| 109 | return uri.toString(); |
| 110 | } |
| 111 | } |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Determine whether a given document is present in the pool |
| 117 | * |
| 118 | * @param doc the document being sought |
| 119 | * @return true if the document is present, false otherwise |
| 120 | */ |
| 121 | |
| 122 | public synchronized boolean contains(TreeInfo doc) { |
| 123 | // relies on "equals" for nodes comparing node identity |
| 124 | return documentNameMap.values().contains(doc); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Release a document from the document pool. This means that if the same document is |
| 129 | * loaded again later, the source will need to be re-parsed, and nodes will get new identities. |
| 130 | * |
| 131 | * @param doc the document to be discarded from the pool |
| 132 | * @return the document supplied in the doc parameter |
| 133 | */ |
| 134 | |
| 135 | public synchronized TreeInfo discard(TreeInfo doc) { |
| 136 | for (Map.Entry<DocumentURI, TreeInfo> e : documentNameMap.entrySet()) { |
| 137 | DocumentURI name = e.getKey(); |
| 138 | TreeInfo entry = e.getValue(); |
| 139 | if (entry.equals(doc)) { |
| 140 | documentNameMap.remove(name); |
| 141 | return doc; |
| 142 | } |
| 143 | } |
| 144 | return doc; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Release all indexs for documents in this pool held by the KeyManager |
| 149 | * |
| 150 | * @param keyManager the keymanager from which indexes are to be released |
| 151 | */ |
| 152 | |
| 153 | public void discardIndexes(/*@NotNull*/ KeyManager keyManager) { |
| 154 | for (TreeInfo doc : documentNameMap.values()) { |
| 155 | keyManager.clearDocumentIndexes(doc); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Add a document URI to the set of URIs known to be unavailable (because doc-available() has returned |
| 161 | * false |
| 162 | * |
| 163 | * @param uri the URI of the unavailable document |
| 164 | */ |
| 165 | |
| 166 | public void markUnavailable(DocumentURI uri) { |
| 167 | unavailableDocuments.add(uri); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Ask whether a document URI is in the set of URIs known to be unavailable, because doc-available() |
| 172 | * has been previously called and has returned false |
| 173 | */ |
| 174 | |
| 175 | public boolean isMarkedUnavailable(DocumentURI uri) { |
| 176 | return unavailableDocuments.contains(uri); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | } |
| 181 |