| 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.expr; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.expr.accum.AccumulatorRegistry; |
| 12 | import net.sf.saxon.expr.instruct.GlobalVariable; |
| 13 | import net.sf.saxon.expr.instruct.SlotManager; |
| 14 | import net.sf.saxon.trans.DecimalFormatManager; |
| 15 | import net.sf.saxon.trans.KeyManager; |
| 16 | import net.sf.saxon.trans.TypeAliasManager; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
| 20 | |
| 21 | /** |
| 22 | * Information about a unit of compilation: in XSLT, a package; in XQuery, a module. May also be a single |
| 23 | * free-standing XPath expression. |
| 24 | */ |
| 25 | public class PackageData { |
| 26 | |
| 27 | protected Configuration config; |
| 28 | private int hostLanguage; |
| 29 | private int xPathVersion; |
| 30 | private boolean isSchemaAware; |
| 31 | private DecimalFormatManager decimalFormatManager = null; |
| 32 | protected KeyManager keyManager = null; |
| 33 | private AccumulatorRegistry accumulatorRegistry = null; |
| 34 | private List<GlobalVariable> globalVariables = new ArrayList<GlobalVariable>(); |
| 35 | private SlotManager globalSlotManager; |
| 36 | private int localLicenseId = -1; |
| 37 | private String targetEdition; |
| 38 | private boolean relocatable; |
| 39 | private TypeAliasManager typeAliasManager; |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Create a PackageData object |
| 44 | * @param config the Saxon configuration |
| 45 | */ |
| 46 | |
| 47 | public PackageData(Configuration config) { |
| 48 | if (config == null) { |
| 49 | throw new NullPointerException(); |
| 50 | } |
| 51 | this.config = config; |
| 52 | targetEdition = config.getEditionCode(); |
| 53 | globalSlotManager = config.makeSlotManager(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the Configuration to which this package belongs |
| 58 | * @return the Saxon configuration |
| 59 | */ |
| 60 | |
| 61 | public Configuration getConfiguration() { |
| 62 | return config; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Set the Configuration to which this package belongs |
| 67 | * @param configuration the Saxon configuration |
| 68 | */ |
| 69 | |
| 70 | public void setConfiguration(Configuration configuration) { |
| 71 | this.config = configuration; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get the language in which this package is written |
| 76 | * @return typically {@link Configuration#XSLT}, {@link Configuration#XQUERY}, or {@link Configuration#XPATH} |
| 77 | */ |
| 78 | |
| 79 | public int getHostLanguage() { |
| 80 | return hostLanguage; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set the language in which this package is written |
| 85 | * @param hostLanguage typically {@link Configuration#XSLT}, {@link Configuration#XQUERY}, or {@link Configuration#XPATH} |
| 86 | */ |
| 87 | |
| 88 | public void setHostLanguage(int hostLanguage) { |
| 89 | this.hostLanguage = hostLanguage; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the XPath version in use (as the version number times ten, e.g. version 3.0 => 30) |
| 94 | * |
| 95 | * @return the version number times ten |
| 96 | */ |
| 97 | |
| 98 | public int getXPathVersion() { |
| 99 | return xPathVersion; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Set the local license id, identifying any local license in the case |
| 104 | * where this package was loaded from a compiled package that contained its own |
| 105 | * embedded license |
| 106 | * @param id identifier used to distinguish this local license |
| 107 | */ |
| 108 | public void setLocalLicenseId(int id) { |
| 109 | localLicenseId = id; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get the local license id, identifying any local license in the case |
| 114 | * where this package was loaded from a compiled package that contained its own |
| 115 | * embedded license |
| 116 | * @return integer identifying the local license, or -1 if there is none |
| 117 | */ |
| 118 | |
| 119 | public int getLocalLicenseId() { |
| 120 | return localLicenseId; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Set the target Saxon edition under which this package will execute |
| 125 | * |
| 126 | * @param edition the Saxon edition e.g. "HE" or "JS" |
| 127 | */ |
| 128 | |
| 129 | public void setTargetEdition(String edition) { |
| 130 | this.targetEdition = edition; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get the target Saxon edition under which this package will execute |
| 135 | * |
| 136 | * @return the Saxon edition e.g. "HE" or "JS" |
| 137 | */ |
| 138 | |
| 139 | public String getTargetEdition() { |
| 140 | return this.targetEdition; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Ask whether the package can be deployed to a different location, with a different base URI |
| 145 | * |
| 146 | * @return if true then static-base-uri() represents the deployed location of the package, |
| 147 | * rather than its compile time location |
| 148 | */ |
| 149 | |
| 150 | public boolean isRelocatable() { |
| 151 | return relocatable; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Say whether the package can be deployed to a different location, with a different base URI |
| 156 | * |
| 157 | * @param relocatable if true then static-base-uri() represents the deployed location of the package, |
| 158 | * rather than its compile time location |
| 159 | */ |
| 160 | |
| 161 | public void setRelocatable(boolean relocatable) { |
| 162 | this.relocatable = relocatable; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * Ask whether the package is schema-aware |
| 169 | * @return true if the package is schema-aware |
| 170 | */ |
| 171 | |
| 172 | public boolean isSchemaAware() { |
| 173 | return isSchemaAware; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Say whether the package is schema-aware |
| 178 | * @param schemaAware set to true if the package is schema-aware |
| 179 | */ |
| 180 | |
| 181 | public void setSchemaAware(boolean schemaAware) { |
| 182 | isSchemaAware = schemaAware; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Get a DecimalFormatManager to resolve the names of decimal formats used in calls |
| 187 | * to the format-number() function. |
| 188 | * |
| 189 | * @return the decimal format manager for this static context; a newly created empty |
| 190 | * DecimalFormatManager if none has been supplied |
| 191 | * @since 9.2 |
| 192 | */ |
| 193 | |
| 194 | public DecimalFormatManager getDecimalFormatManager() { |
| 195 | if (decimalFormatManager == null) { |
| 196 | decimalFormatManager = new DecimalFormatManager(hostLanguage, xPathVersion); |
| 197 | } |
| 198 | return decimalFormatManager; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Set a DecimalFormatManager to resolve the names of decimal formats used in calls |
| 203 | * to the format-number() function. |
| 204 | * |
| 205 | * @param manager the decimal format manager for this static context |
| 206 | */ |
| 207 | |
| 208 | public void setDecimalFormatManager(DecimalFormatManager manager) { |
| 209 | decimalFormatManager = manager; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Get the KeyManager, containing definitions of keys available for use. |
| 214 | * |
| 215 | * @return the KeyManager. This is used to resolve key names, both explicit calls |
| 216 | * on key() used in XSLT, and system-generated calls on key() which may |
| 217 | * also appear in XQuery and XPath |
| 218 | */ |
| 219 | public KeyManager getKeyManager() { |
| 220 | if (keyManager == null) { |
| 221 | keyManager = new KeyManager(getConfiguration(), this); |
| 222 | } |
| 223 | return keyManager; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Set the KeyManager, containing definitions of keys available for use. |
| 228 | * |
| 229 | * @param manager the KeyManager. This is used to resolve key names, both explicit calls |
| 230 | * on key() used in XSLT, and system-generated calls on key() which may |
| 231 | * also appear in XQuery and XPath |
| 232 | */ |
| 233 | public void setKeyManager(KeyManager manager) { |
| 234 | keyManager = manager; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Get the object that manages accumulator functions |
| 239 | * |
| 240 | * @return the class that manages accumulators. |
| 241 | */ |
| 242 | |
| 243 | public AccumulatorRegistry getAccumulatorRegistry() { |
| 244 | return accumulatorRegistry; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Set the object that manages accumulator functions |
| 249 | * |
| 250 | * @param accumulatorRegistry the manager of accumulator functions |
| 251 | */ |
| 252 | |
| 253 | public void setAccumulatorRegistry(AccumulatorRegistry accumulatorRegistry) { |
| 254 | this.accumulatorRegistry = accumulatorRegistry; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Get the SlotManager used to record the names and slot numbers of all global variables |
| 259 | * @return the SlotManager used for global variables contained in this package |
| 260 | */ |
| 261 | |
| 262 | public SlotManager getGlobalSlotManager() { |
| 263 | return globalSlotManager; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Set the SlotManager to be used to record the names and slot numbers of all global variables |
| 268 | * @param manager the SlotManager used for global variables contained in this package |
| 269 | */ |
| 270 | |
| 271 | public void setGlobalSlotManager(SlotManager manager) { |
| 272 | this.globalSlotManager = manager; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Add a global variable to the list of global variables contained in this package |
| 277 | * @param variable the global variable to be added |
| 278 | */ |
| 279 | |
| 280 | public void addGlobalVariable(GlobalVariable variable) { |
| 281 | globalVariables.add(variable); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Get the list of global variables contained in this package |
| 286 | * @return the list of global variables |
| 287 | */ |
| 288 | |
| 289 | public List<GlobalVariable> getGlobalVariableList() { |
| 290 | return globalVariables; |
| 291 | } |
| 292 | |
| 293 | public void setTypeAliasManager(TypeAliasManager manager) { |
| 294 | this.typeAliasManager = manager; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Get the type alias manager if one already exists, creating it if it does not already exist |
| 299 | * @return a type alias manager, newly created if necessary |
| 300 | */ |
| 301 | |
| 302 | public TypeAliasManager obtainTypeAliasManager() { |
| 303 | if (typeAliasManager == null) { |
| 304 | typeAliasManager = config.makeTypeAliasManager(); |
| 305 | } |
| 306 | return typeAliasManager; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | } |
| 311 | |
| 312 |