| 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 org.xml.sax.Locator; |
| 11 | |
| 12 | import java.util.Stack; |
| 13 | |
| 14 | /** |
| 15 | * Implementation of the SAX Locator interface, used to supply location information to the ContentHandler. |
| 16 | * <p>When the ContentHandler is used to receive the results of a query or stylesheet, |
| 17 | * the information supplied by the standard methods such as {@link #getSystemId} and |
| 18 | * {@link #getLineNumber} relates to the position of the expression/instruction in the stylesheet |
| 19 | * or query that caused the relevant nodes to be output.</p> |
| 20 | * <p>If the ContentHandler is used in other contexts, for example as the destination of an |
| 21 | * IdentityTransformer, the information reflects the position in the source document.</p> |
| 22 | * <p>If the output property <code>saxon:supply-source-locator</code> was set to the |
| 23 | * value "yes" (which in turn requires that tracing was enabled at compile time), the Locator will |
| 24 | * also contain information about the current location in the source document (specifically, the position |
| 25 | * of the context node). This will not always be 100% accurate, since there is some buffering of events |
| 26 | * in the output pipeline: it reflects the context node at the time the event reaches the ContentHandler, |
| 27 | * which may not be the same as the context node at the time the relevant instruction was executed; |
| 28 | * however, it still provides some information that may be useful for diagnostics.</p> |
| 29 | */ |
| 30 | |
| 31 | public class ContentHandlerProxyLocator implements Locator { |
| 32 | |
| 33 | private ContentHandlerProxy parent = null; |
| 34 | |
| 35 | /** |
| 36 | * Create the Locator for a ContentHandlerProxy |
| 37 | * |
| 38 | * @param parent the ContentHandlerProxy |
| 39 | */ |
| 40 | |
| 41 | public ContentHandlerProxyLocator(ContentHandlerProxy parent) { |
| 42 | this.parent = parent; |
| 43 | } |
| 44 | |
| 45 | // previously ContentHandlerProxy implemented Locator directly. However, this caused a clash |
| 46 | // over the semantics of getSystemId(), which is inherited from both Receiver and Locator. |
| 47 | // The getSystemId() method in the Receiver interface identifies the URI of the document represented |
| 48 | // by the event stream. The getSystemId() method in this class typically returns the URI of the stylesheet |
| 49 | // or query module that generated the event. |
| 50 | |
| 51 | /** |
| 52 | * Get the Public ID |
| 53 | * |
| 54 | * @return null (always) |
| 55 | */ |
| 56 | |
| 57 | public String getPublicId() { |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get the System ID |
| 63 | * |
| 64 | * @return the system ID giving the location in the query or stylesheet of the most recent event notified |
| 65 | */ |
| 66 | |
| 67 | public String getSystemId() { |
| 68 | return parent.getCurrentLocation().getSystemId(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the line number |
| 73 | * |
| 74 | * @return the line number giving the location of the most recent event notified |
| 75 | */ |
| 76 | |
| 77 | public int getLineNumber() { |
| 78 | return parent.getCurrentLocation().getLineNumber(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get the column number |
| 83 | * |
| 84 | * @return -1 (always) |
| 85 | */ |
| 86 | |
| 87 | public int getColumnNumber() { |
| 88 | return parent.getCurrentLocation().getColumnNumber(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get the current item stack. This is a Stack, whose members are objects of class |
| 93 | * {@link net.sf.saxon.om.Item}. The top item in the stack is the context node or atomic value; items |
| 94 | * further down the stack represent previous context node or atomic value |
| 95 | * |
| 96 | * @return the stack of context items |
| 97 | */ |
| 98 | |
| 99 | /*@Nullable*/ |
| 100 | public Stack getContextItemStack() { |
| 101 | final ContentHandlerProxy.ContentHandlerProxyTraceListener traceListener = parent.getTraceListener(); |
| 102 | if (traceListener == null) { |
| 103 | return null; |
| 104 | } else { |
| 105 | return traceListener.getContextItemStack(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | } |
| 110 |