| 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.event; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.ExplicitLocation; |
| 11 | import net.sf.saxon.expr.parser.Location; |
| 12 | import net.sf.saxon.om.NodeInfo; |
| 13 | |
| 14 | /** |
| 15 | * A Receiver that can be inserted into an event pipeline to copy location information. |
| 16 | * The class is used when it is necessary to copy a subtree along with its location information; |
| 17 | * for example, when copying an inline schema within a stylesheet to a separate schema document. |
| 18 | * <p><i>Note: prior to 9.2, the LocationCopier was a ProxyReceiver that passed all events on the |
| 19 | * pipeline unchanged. It no longer does this, instead it is found as the LocationProvider on a |
| 20 | * pipeline, but does not itself see the pipeline events.</i></p> |
| 21 | */ |
| 22 | |
| 23 | public class LocationCopier implements CopyInformee<Location> { |
| 24 | |
| 25 | private boolean wholeDocument; |
| 26 | |
| 27 | public LocationCopier(boolean wholeDocument) { |
| 28 | this.wholeDocument = wholeDocument; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Provide information about the node being copied. This method is called immediately before |
| 33 | * the startElement call for the element node in question. |
| 34 | * |
| 35 | * @param element the node being copied, which must be an element node |
| 36 | */ |
| 37 | |
| 38 | public Location notifyElementNode(NodeInfo element) { |
| 39 | String systemId = wholeDocument ? element.getSystemId() : element.getBaseURI(); |
| 40 | // The logic behind this is that if we are copying the whole document, we will be copying all |
| 41 | // the relevant xml:base attributes; so retaining the systemId values is sufficient to enable |
| 42 | // the base URIs of the nodes to be preserved. But if we only copy an element (for example |
| 43 | // an xsl:import-schema element - see test schema091 - then its base URI might be affected |
| 44 | // by xml:base attributes that aren't being copied. Ideally we would have two separate properties, |
| 45 | // but XDM doesn't work that way. |
| 46 | int lineNumber = element.getLineNumber(); |
| 47 | int columnNumber = element.getColumnNumber(); |
| 48 | return new ExplicitLocation(systemId, lineNumber, columnNumber); |
| 49 | } |
| 50 | } |
| 51 |