| 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.StructuredQName; |
| 11 | |
| 12 | import java.util.ArrayList; |
| 13 | import java.util.List; |
| 14 | |
| 15 | /** |
| 16 | * A set of xsl:key definitions in a stylesheet that share the same name |
| 17 | */ |
| 18 | |
| 19 | public class KeyDefinitionSet { |
| 20 | |
| 21 | private StructuredQName keyName; |
| 22 | private int keySetNumber; // unique among the KeyDefinitionSets within a KeyManager |
| 23 | private List<KeyDefinition> keyDefinitions; |
| 24 | private String collationName; |
| 25 | private boolean composite; |
| 26 | private boolean backwardsCompatible; // true if any of the keys is backwards compatible |
| 27 | private boolean rangeKey; // true if any of the keys is a range key |
| 28 | private boolean reusable = true; // true if indexes built for this key can be reused across transformations |
| 29 | |
| 30 | /** |
| 31 | * Create a key definition set for keys sharing a given name |
| 32 | * |
| 33 | * @param keyName the name of the key definitions in this set |
| 34 | * @param keySetNumber a unique number identifying this key definition set |
| 35 | */ |
| 36 | |
| 37 | public KeyDefinitionSet(StructuredQName keyName, int keySetNumber) { |
| 38 | this.keyName = keyName; |
| 39 | this.keySetNumber = keySetNumber; |
| 40 | keyDefinitions = new ArrayList<>(3); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Add a key definition to this set of key definitions. The caller is responsible for ensuring that |
| 45 | * all key definitions in a key definition set have the same name |
| 46 | * |
| 47 | * @param keyDef the key definition to be added |
| 48 | * @throws XPathException if the key definition uses a different collation from others in the set |
| 49 | */ |
| 50 | |
| 51 | public void addKeyDefinition(/*@NotNull*/ KeyDefinition keyDef) throws XPathException { |
| 52 | if (keyDefinitions.isEmpty()) { |
| 53 | collationName = keyDef.getCollationName(); |
| 54 | composite = keyDef.isComposite(); |
| 55 | } else { |
| 56 | if ((collationName == null && keyDef.getCollationName() != null) || |
| 57 | (collationName != null && !collationName.equals(keyDef.getCollationName()))) { |
| 58 | throw new XPathException("All keys with the same name must use the same collation", "XTSE1220"); |
| 59 | } |
| 60 | if (keyDef.isComposite() != composite) { |
| 61 | throw new XPathException("All keys with the same name must have the same value for @composite", "XTSE1222"); |
| 62 | } |
| 63 | // ignore this key definition if it is a duplicate of another already present. This can happen when including |
| 64 | // a stylesheet module more than once |
| 65 | List<KeyDefinition> v = getKeyDefinitions(); |
| 66 | for (KeyDefinition other : v) { |
| 67 | if (keyDef.getMatch().isEqual(other.getMatch()) && |
| 68 | keyDef.getBody().isEqual(other.getBody())) { |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | if (keyDef.isBackwardsCompatible()) { |
| 74 | backwardsCompatible = true; |
| 75 | } |
| 76 | if (keyDef.isRangeKey()) { |
| 77 | rangeKey = true; |
| 78 | } |
| 79 | keyDefinitions.add(keyDef); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get the name of the key definitions in this set (they all share the same name) |
| 84 | * |
| 85 | * @return the name of these key definitions |
| 86 | */ |
| 87 | |
| 88 | public StructuredQName getKeyName() { |
| 89 | return keyName; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the name of the collation used for this key |
| 94 | * |
| 95 | * @return the collation name (a URI) |
| 96 | */ |
| 97 | |
| 98 | public String getCollationName() { |
| 99 | return collationName; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Ask whether this is a composite key |
| 104 | * @return true if the key is composite |
| 105 | */ |
| 106 | |
| 107 | public boolean isComposite() { |
| 108 | return composite; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get the KeySet number. This uniquely identifies the KeyDefinitionSet within a KeyManager |
| 113 | * |
| 114 | * @return the unique number |
| 115 | */ |
| 116 | |
| 117 | public int getKeySetNumber() { |
| 118 | return keySetNumber; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get the key definitions in this set |
| 123 | * |
| 124 | * @return the key definitions in this set |
| 125 | */ |
| 126 | |
| 127 | public List<KeyDefinition> getKeyDefinitions() { |
| 128 | return keyDefinitions; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Ask if the keys are to be evaluated in backwards compatible mode |
| 133 | * |
| 134 | * @return true if backwards compatibility is in force for at least one of the keys in the set |
| 135 | */ |
| 136 | |
| 137 | public boolean isBackwardsCompatible() { |
| 138 | return backwardsCompatible; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Ask if this is a range key |
| 143 | * |
| 144 | * @return true if any of the keys in the set is defined as a range key |
| 145 | */ |
| 146 | |
| 147 | public boolean isRangeKey() { |
| 148 | return rangeKey; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Say whether this key definition set is reusable across transformations, that is, if |
| 153 | * the index built for a particular document is shareable across transformations. This will only |
| 154 | * be the case if the key has no dependencies on global variables or parameters in the stylesheet |
| 155 | * @param reusable set to true if the index can safely be shared and reused |
| 156 | */ |
| 157 | |
| 158 | public void setReusable(boolean reusable) { |
| 159 | this.reusable = reusable; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Ask whether this key definition set is reusable across transformations, that is, if |
| 164 | * the index built for a particular document is shareable across transformations. This will only |
| 165 | * be the case if the key has no dependencies on global variables or parameters in the stylesheet |
| 166 | * @return true if the index can safely be shared and reused |
| 167 | */ |
| 168 | |
| 169 | public boolean isReusable() { |
| 170 | return reusable; |
| 171 | } |
| 172 | } |
| 173 |