| 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.wrapper; |
| 9 | |
| 10 | import net.sf.saxon.om.GenericTreeInfo; |
| 11 | import net.sf.saxon.om.NodeInfo; |
| 12 | import net.sf.saxon.om.TreeInfo; |
| 13 | |
| 14 | import java.util.function.Function; |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * A <tt>RebasedDocument</tt> represents a view of a real Document in which all nodes are mapped to a different |
| 19 | * base URI and/or system ID using supplied mapping functions. |
| 20 | * |
| 21 | * <p>It is possible to map either base URIs or system IDs or both.</p> |
| 22 | * |
| 23 | * <p>All properties of the nodes other than the base URI and system ID are unchanged.</p> |
| 24 | * |
| 25 | * <p>The user-supplied functions supplied to compute the base URI and system ID will be applied |
| 26 | * to the underlying node in the "real" document. It is of course possible to supply a function that |
| 27 | * ignores the supplied input. |
| 28 | * |
| 29 | * @since 9.9.0.2 |
| 30 | */ |
| 31 | |
| 32 | public class RebasedDocument extends GenericTreeInfo { |
| 33 | |
| 34 | private TreeInfo underlyingTree; |
| 35 | private Function<NodeInfo, String> baseUriMapper; |
| 36 | private Function<NodeInfo, String> systemIdMapper; |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * Create a rebased view of a document |
| 41 | * @param doc the underlying document |
| 42 | * @param baseUriMapper a function that is applied to a node in the original document |
| 43 | * to deliver the base URI of the corresponding node in the rebased document |
| 44 | * @param systemIdMapper a function that is applied to a node in the original document |
| 45 | * to deliver the system ID of the corresponding node in the rebased document |
| 46 | */ |
| 47 | |
| 48 | public RebasedDocument(TreeInfo doc, Function<NodeInfo, String> baseUriMapper, Function<NodeInfo, String> systemIdMapper) { |
| 49 | super(doc.getConfiguration()); |
| 50 | this.baseUriMapper = baseUriMapper; |
| 51 | this.systemIdMapper = systemIdMapper; |
| 52 | setRootNode(wrap(doc.getRootNode())); |
| 53 | this.underlyingTree = doc; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Create a wrapped node within this document |
| 58 | * @param node the node to be wrapped - must be a node in the base document |
| 59 | */ |
| 60 | |
| 61 | public RebasedNode wrap(NodeInfo node) { |
| 62 | return RebasedNode.makeWrapper(node, this, null); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Ask whether the document contains any nodes whose type annotation is anything other than |
| 67 | * UNTYPED. (This will be true if and only if the underlying document is untyped). |
| 68 | * |
| 69 | * <p>Note: in XSD 1.1 it is possible to define assertions such that the validity of a node |
| 70 | * depends on its base URI. This class assumes that no-one would be quite so perverse. The validity |
| 71 | * and type annotation of a virtual node are therefore the same as the validity and type annotation |
| 72 | * of its underlying node.</p> |
| 73 | * |
| 74 | * @return true if the document contains elements whose type is other than UNTYPED |
| 75 | */ |
| 76 | public boolean isTyped() { |
| 77 | return underlyingTree.isTyped(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the element with a given ID, if any |
| 82 | * |
| 83 | * @param id the required ID value |
| 84 | * @param getParent true if the required element is the parent of an element annotated as xs:ID, false |
| 85 | * if the required node is the element itself |
| 86 | * @return the element with the given ID value, or null if there is none. |
| 87 | */ |
| 88 | |
| 89 | /*@Nullable*/ |
| 90 | public NodeInfo selectID(String id, boolean getParent) { |
| 91 | NodeInfo n = underlyingTree.selectID(id, false); |
| 92 | if (n == null) { |
| 93 | return null; |
| 94 | } else { |
| 95 | return wrap(n); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the underlying tree (the one whose nodes are being mapped to a new base URI and system ID) |
| 101 | * @return the underlying tree |
| 102 | */ |
| 103 | public TreeInfo getUnderlyingTree() { |
| 104 | return underlyingTree; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get the function that is used to map nodes in the underlying tree to a new base URI |
| 109 | * @return the base URI mapping function |
| 110 | */ |
| 111 | |
| 112 | public Function<NodeInfo, String> getBaseUriMapper() { |
| 113 | return baseUriMapper; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get the function that is used to map nodes in the underlying tree to a new system ID |
| 118 | * |
| 119 | * @return the system ID mapping function |
| 120 | */ |
| 121 | |
| 122 | public Function<NodeInfo, String> getSystemIdMapper() { |
| 123 | return systemIdMapper; |
| 124 | } |
| 125 | |
| 126 | } |
| 127 |