| 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; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.Location; |
| 11 | import net.sf.saxon.om.NodeInfo; |
| 12 | import net.sf.saxon.om.StructuredQName; |
| 13 | import net.sf.saxon.tree.util.Navigator; |
| 14 | |
| 15 | /** |
| 16 | * A Location corresponding to an attribute in a document (often a stylesheet) |
| 17 | */ |
| 18 | |
| 19 | public class AttributeLocation implements Location { |
| 20 | |
| 21 | private String systemId; |
| 22 | private int lineNumber; |
| 23 | private int columnNumber; |
| 24 | private StructuredQName elementName; |
| 25 | private StructuredQName attributeName; |
| 26 | private NodeInfo elementNode; |
| 27 | |
| 28 | public AttributeLocation(NodeInfo element, StructuredQName attributeName) { |
| 29 | this.systemId = element.getSystemId(); |
| 30 | this.lineNumber = element.getLineNumber(); |
| 31 | this.columnNumber = element.getColumnNumber(); |
| 32 | this.elementName = Navigator.getNodeName(element); |
| 33 | this.attributeName = attributeName; |
| 34 | } |
| 35 | |
| 36 | public AttributeLocation(StructuredQName elementName, StructuredQName attributeName, Location location) { |
| 37 | this.systemId = location.getSystemId(); |
| 38 | this.lineNumber = location.getLineNumber(); |
| 39 | this.columnNumber = location.getColumnNumber(); |
| 40 | this.elementName = elementName; |
| 41 | this.attributeName = attributeName; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Add a reference to the containing element node. This needs care because we don't want to retain |
| 46 | * links to the source stylesheet at run-time; it is therefore done only for static errors |
| 47 | */ |
| 48 | |
| 49 | public void setElementNode(NodeInfo node) { |
| 50 | elementNode = node; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get the reference to the containing element node, if available. |
| 55 | * @return the containing element node, or null if not available or not applicable. |
| 56 | */ |
| 57 | |
| 58 | public NodeInfo getElementNode() { |
| 59 | return elementNode; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the name of the containing element |
| 64 | * |
| 65 | * @return the name of the containing element in the stylesheet |
| 66 | */ |
| 67 | |
| 68 | public StructuredQName getElementName() { |
| 69 | return elementName; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get the name of the containing attribute |
| 74 | * |
| 75 | * @return the name of the containing attribute in the stylesheet. May be null |
| 76 | * if the XPath expression is contained in a text node |
| 77 | */ |
| 78 | |
| 79 | public StructuredQName getAttributeName() { |
| 80 | return attributeName; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get the column number. This column number is relative to the line identified by the line number. |
| 85 | * Column numbers start at 1. |
| 86 | * |
| 87 | * @return the column number, or -1 if the information is not available. |
| 88 | */ |
| 89 | public int getColumnNumber() { |
| 90 | return columnNumber; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the system ID. This should be the system identifier of an XML external entity; if a stylesheet module |
| 95 | * comprises multiple external entities, the distinction should be retained. This means, for example, that |
| 96 | * an instruction in a stylesheet can have a different system identifier from its parent instruction. However, |
| 97 | * SAX parsers only provide location information at the element level, so when part of an XPath expression |
| 98 | * is in a different external entity from other parts of the same expression, this distinction is lost. |
| 99 | * <p>The system identifier of a node is in general not the same as its base URI. The base URI is affected |
| 100 | * by xml:base attributes; the system identifier is not.</p> |
| 101 | * |
| 102 | * @return the system ID, or null if the information is not available. |
| 103 | */ |
| 104 | public String getSystemId() { |
| 105 | return systemId; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get the Public ID |
| 110 | * |
| 111 | * @return usually null |
| 112 | */ |
| 113 | public String getPublicId() { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get the line number. This line number is relative to the external entity identified by the system identifier. |
| 119 | * Line numbers start at 1. The value may be an approximation; SAX parsers only provide line number information |
| 120 | * at the level of element nodes. |
| 121 | * |
| 122 | * @return the line number, or -1 if the information is not available. |
| 123 | */ |
| 124 | public int getLineNumber() { |
| 125 | return lineNumber; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get an immutable copy of this Location object. By default Location objects may be mutable, so they |
| 130 | * should not be saved for later use. The result of this operation holds the same location information, |
| 131 | * but in an immutable form. |
| 132 | */ |
| 133 | public Location saveLocation() { |
| 134 | return this; |
| 135 | } |
| 136 | } |
| 137 |