| 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.StandardNames; |
| 11 | import net.sf.saxon.om.StructuredQName; |
| 12 | |
| 13 | /** |
| 14 | * The symbolic name of a component consists of the component kind (e.g. function, template, attribute set), |
| 15 | * the QName acting as the name of the template, function etc, and in the case of functions, the arity |
| 16 | */ |
| 17 | public class SymbolicName { |
| 18 | |
| 19 | private int kind; |
| 20 | private StructuredQName name; |
| 21 | |
| 22 | /** |
| 23 | * Create a symbolic name for a component other than a function. |
| 24 | * @param kind the component kind, for example {@link StandardNames#XSL_TEMPLATE} |
| 25 | * @param name the QName that is the "name" of the component |
| 26 | */ |
| 27 | |
| 28 | public SymbolicName(int kind, StructuredQName name) { |
| 29 | this.kind = kind; |
| 30 | this.name = name; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Returns a hash code value for the object. |
| 35 | */ |
| 36 | @Override |
| 37 | public int hashCode() { |
| 38 | return kind << 16 ^ name.hashCode(); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Indicates whether some other object is "equal to" this one. |
| 43 | */ |
| 44 | @Override |
| 45 | public boolean equals(Object obj) { |
| 46 | return obj instanceof SymbolicName |
| 47 | && ((SymbolicName) obj).kind == this.kind |
| 48 | && ((SymbolicName) obj).name.equals(this.name); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get the kind of component, for example {@link StandardNames#XSL_FUNCTION} or {@link StandardNames#XSL_VARIABLE} |
| 53 | * @return the kind of component identified by this symbolic name |
| 54 | */ |
| 55 | |
| 56 | public int getComponentKind() { |
| 57 | return kind; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get the QName part of the symbolic name of the component |
| 62 | * @return the QName part of the name |
| 63 | */ |
| 64 | |
| 65 | public StructuredQName getComponentName() { |
| 66 | return name; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the name as a string. |
| 71 | * @return a string typically in the form "template p:my-template" or "function f:my-function#2" |
| 72 | */ |
| 73 | |
| 74 | public String toString() { |
| 75 | //noinspection UnnecessaryParentheses |
| 76 | return StandardNames.getLocalName(kind) + " " + |
| 77 | name.getDisplayName(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get a short name suitable for use in messages |
| 82 | */ |
| 83 | |
| 84 | public String getShortName() { |
| 85 | return name.getDisplayName(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Subclass of SymbolicName used for function names (including the arity) |
| 90 | */ |
| 91 | |
| 92 | public static class F extends SymbolicName { |
| 93 | int arity; |
| 94 | |
| 95 | /** |
| 96 | * Create a symbolic name for a function. |
| 97 | * |
| 98 | * @param name the QName that is the "name" of the component |
| 99 | * @param arity the number of arguments |
| 100 | */ |
| 101 | |
| 102 | public F(StructuredQName name, int arity) { |
| 103 | super(StandardNames.XSL_FUNCTION, name); |
| 104 | this.arity = arity; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get the arity, in the case of function components |
| 109 | * |
| 110 | * @return in the case of a function, the arity, otherwise -1. |
| 111 | */ |
| 112 | |
| 113 | public int getArity() { |
| 114 | return arity; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns a hash code value for the object. |
| 119 | */ |
| 120 | @Override |
| 121 | public int hashCode() { |
| 122 | return super.hashCode() ^ arity; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Indicates whether some other object is "equal to" this one. |
| 127 | */ |
| 128 | @Override |
| 129 | public boolean equals(Object obj) { |
| 130 | return obj instanceof SymbolicName.F |
| 131 | && super.equals(obj) |
| 132 | && ((SymbolicName.F) obj).arity == this.arity; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Get the name as a string. |
| 137 | * |
| 138 | * @return a string typically in the form "template p:my-template" or "function f:my-function#2" |
| 139 | */ |
| 140 | |
| 141 | public String toString() { |
| 142 | //noinspection UnnecessaryParentheses |
| 143 | return super.toString() + "#" + arity; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get a short name suitable for use in messages |
| 148 | */ |
| 149 | @Override |
| 150 | public String getShortName() { |
| 151 | return super.getShortName() + "#" + arity; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 |