| 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.trans; |
| 9 | |
| 10 | import net.sf.saxon.om.NodeInfo; |
| 11 | import net.sf.saxon.s9api.XsltExecutable; |
| 12 | |
| 13 | import java.util.Map; |
| 14 | import java.util.concurrent.ConcurrentHashMap; |
| 15 | |
| 16 | /** |
| 17 | * A cache of the stylesheets (as XsltExecutables) used in calls to the fn:transform function, in a stylesheet or query. |
| 18 | */ |
| 19 | |
| 20 | public class StylesheetCache { |
| 21 | |
| 22 | private Map<String, XsltExecutable> cacheByText = new ConcurrentHashMap<String, XsltExecutable>(); |
| 23 | private Map<String, XsltExecutable> cacheByLocation = new ConcurrentHashMap<String, XsltExecutable>(); |
| 24 | private Map<NodeInfo, XsltExecutable> cacheByNode = new ConcurrentHashMap<NodeInfo, XsltExecutable>(); |
| 25 | |
| 26 | /** |
| 27 | * Get the stylesheet (XsltExecutable) in the cache associated with the supplied stylesheet text string. |
| 28 | * @param style the stylesheet text as a string (as supplied in the stylesheet-text option of fn:transform()) |
| 29 | * @return the XsltExecutable corresponding to this stylesheet-text, if already cached. |
| 30 | */ |
| 31 | |
| 32 | public XsltExecutable getStylesheetByText(String style) { |
| 33 | return cacheByText.get(style); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Get the stylesheet (XsltExecutable) in the cache associated with the supplied stylesheet location string. |
| 38 | * @param style the URI for the stylesheet location (as supplied in the stylesheet-location option of fn:transform()) |
| 39 | * @return the XsltExecutable corresponding to this stylesheet-location, if already cached. |
| 40 | */ |
| 41 | |
| 42 | public XsltExecutable getStylesheetByLocation(String style) { |
| 43 | return cacheByLocation.get(style); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Get the stylesheet (XsltExecutable) in the cache associated with the supplied stylesheet node. |
| 48 | * @param style the root node of the stylesheet (as supplied in the stylesheet-node option of fn:transform()) |
| 49 | * @return the XsltExecutable corresponding to this stylesheet-node, if already cached. |
| 50 | */ |
| 51 | |
| 52 | public XsltExecutable getStylesheetByNode(NodeInfo style) { |
| 53 | return cacheByNode.get(style); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Set a key-value pair in the cache for stylesheets referenced by stylesheet text. |
| 58 | * @param style the stylesheet text as a string (as supplied in the stylesheet-text option of fn:transform()) |
| 59 | * @param xsltExecutable the compiled stylesheet |
| 60 | */ |
| 61 | |
| 62 | public void setStylesheetByText(String style, XsltExecutable xsltExecutable) { |
| 63 | cacheByText.put(style, xsltExecutable); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Set a key-value pair in the cache for stylesheets referenced by stylesheet location. |
| 68 | * @param style the URI for the stylesheet location (as supplied in the stylesheet-location option of fn:transform()) |
| 69 | * @param xsltExecutable the compiled stylesheet |
| 70 | */ |
| 71 | |
| 72 | public void setStylesheetByLocation(String style, XsltExecutable xsltExecutable) { |
| 73 | cacheByLocation.put(style, xsltExecutable); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Set a key-value pair in the cache for stylesheets referenced by stylesheet root node. |
| 78 | * @param style the root node of the stylesheet (as supplied in the stylesheet-node option of fn:transform()) |
| 79 | * @param xsltExecutable the compiled stylesheet |
| 80 | */ |
| 81 | |
| 82 | public void setStylesheetByNode(NodeInfo style, XsltExecutable xsltExecutable) { |
| 83 | cacheByNode.put(style, xsltExecutable); |
| 84 | } |
| 85 | } |