| 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.s9api; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.Version; |
| 12 | import net.sf.saxon.event.NamespaceReducer; |
| 13 | import net.sf.saxon.event.Receiver; |
| 14 | import net.sf.saxon.event.ReceiverOptions; |
| 15 | import net.sf.saxon.event.TreeReceiver; |
| 16 | import net.sf.saxon.expr.XPathContext; |
| 17 | import net.sf.saxon.expr.parser.ExplicitLocation; |
| 18 | import net.sf.saxon.expr.sort.RuleBasedSubstringMatcher; |
| 19 | import net.sf.saxon.expr.sort.SimpleCollation; |
| 20 | import net.sf.saxon.lib.*; |
| 21 | import net.sf.saxon.om.*; |
| 22 | import net.sf.saxon.trans.XPathException; |
| 23 | import net.sf.saxon.value.SequenceType; |
| 24 | |
| 25 | import javax.xml.transform.Source; |
| 26 | import java.io.File; |
| 27 | import java.io.OutputStream; |
| 28 | import java.io.Writer; |
| 29 | import java.text.RuleBasedCollator; |
| 30 | import java.util.Comparator; |
| 31 | import java.util.Objects; |
| 32 | |
| 33 | /** |
| 34 | * The <code>Processor</code> class serves three purposes: it allows global Saxon configuration options to be set; |
| 35 | * it acts as a factory for generating XQuery, XPath, and XSLT compilers; and it owns certain shared |
| 36 | * resources such as the Saxon NamePool and compiled schemas. This is the first object that a |
| 37 | * Saxon application should create. Once established, a Processor may be used in multiple threads. |
| 38 | * <p>It is possible to run more than one Saxon Processor concurrently, but only when running completely |
| 39 | * independent workloads. Nothing can be shared between Processor instances. Within a query or transformation, |
| 40 | * all source documents and schemas must be built using the same Processor, which must also be used to |
| 41 | * compile the query or stylesheet.</p> |
| 42 | */ |
| 43 | |
| 44 | public class Processor implements Configuration.ApiProvider { |
| 45 | |
| 46 | private Configuration config; |
| 47 | private SchemaManager schemaManager; |
| 48 | |
| 49 | /** |
| 50 | * Create a Processor |
| 51 | * |
| 52 | * @param licensedEdition indicates whether the Processor requires features of Saxon that need a license |
| 53 | * file (that is, features not available in Saxon HE (Home Edition). If true, the method will create |
| 54 | * a Configuration appropriate to the version of the software that is running: for example, if running |
| 55 | * Saxon-EE, it will create an EnterpriseConfiguration. The method does not at this stage check that a license |
| 56 | * is available, and in the absence of a license, it should run successfully provided no features that |
| 57 | * require licensing are actually used. If the argument is set to false, a plain Home Edition Configuration |
| 58 | * is created unconditionally. |
| 59 | */ |
| 60 | |
| 61 | public Processor(boolean licensedEdition) { |
| 62 | if (licensedEdition) { |
| 63 | config = Configuration.newConfiguration(); |
| 64 | if (config.getEditionCode().equals("EE")) { |
| 65 | schemaManager = makeSchemaManager(); |
| 66 | } |
| 67 | } else { |
| 68 | config = new Configuration(); |
| 69 | } |
| 70 | config.setProcessor(this); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Create a Processor based on an existing Configuration. This constructor is useful for transition, |
| 75 | * when new components of an application are to use s9api interfaces but existing components use lower-level |
| 76 | * interfaces. |
| 77 | * |
| 78 | * @param config the Configuration to be used by this processor |
| 79 | * @since 9.3 |
| 80 | */ |
| 81 | |
| 82 | public Processor(/*@NotNull*/ Configuration config) { |
| 83 | this.config = config; |
| 84 | if (config.getEditionCode().equals("EE")) { |
| 85 | schemaManager = makeSchemaManager(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Create a Processor configured according to the settings in a supplied configuration file. |
| 91 | * |
| 92 | * @param source the Source of the configuration file |
| 93 | * @throws SaxonApiException if the configuration file cannot be read, or its contents are invalid |
| 94 | * @since 9.2 |
| 95 | */ |
| 96 | |
| 97 | public Processor(Source source) throws SaxonApiException { |
| 98 | try { |
| 99 | config = Configuration.readConfiguration(source); |
| 100 | schemaManager = makeSchemaManager(); |
| 101 | } catch (XPathException e) { |
| 102 | throw new SaxonApiException(e); |
| 103 | } |
| 104 | config.setProcessor(this); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Create a DocumentBuilder. A DocumentBuilder is used to load source XML documents. |
| 109 | * |
| 110 | * @return a newly created DocumentBuilder |
| 111 | */ |
| 112 | |
| 113 | /*@NotNull*/ |
| 114 | public DocumentBuilder newDocumentBuilder() { |
| 115 | return new DocumentBuilder(config); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Create an XPathCompiler. An XPathCompiler is used to compile XPath expressions. |
| 120 | * |
| 121 | * @return a newly created XPathCompiler |
| 122 | */ |
| 123 | |
| 124 | /*@NotNull*/ |
| 125 | public XPathCompiler newXPathCompiler() { |
| 126 | return new XPathCompiler(this); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Create an XsltCompiler. An XsltCompiler is used to compile XSLT stylesheets. |
| 131 | * |
| 132 | * @return a newly created XsltCompiler |
| 133 | * @throws UnsupportedOperationException if this version of the Saxon product does not support XSLT processing |
| 134 | */ |
| 135 | |
| 136 | /*@NotNull*/ |
| 137 | public XsltCompiler newXsltCompiler() { |
| 138 | return new XsltCompiler(this); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Create an XQueryCompiler. An XQueryCompiler is used to compile XQuery queries. |
| 143 | * |
| 144 | * @return a newly created XQueryCompiler |
| 145 | * @throws UnsupportedOperationException if this version of the Saxon product does not support XQuery processing |
| 146 | */ |
| 147 | |
| 148 | /*@NotNull*/ |
| 149 | public XQueryCompiler newXQueryCompiler() { |
| 150 | return new XQueryCompiler(this); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Create a Serializer |
| 155 | * |
| 156 | * @return a new Serializer |
| 157 | * @since 9.3 |
| 158 | */ |
| 159 | |
| 160 | /*@NotNull*/ |
| 161 | public Serializer newSerializer() { |
| 162 | return new Serializer(this); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Create a Serializer initialized to write to a given OutputStream. |
| 167 | * <p>Closing the output stream after use is the responsibility of the caller.</p> |
| 168 | * |
| 169 | * @param stream The OutputStream to which the Serializer will write |
| 170 | * @return a new Serializer |
| 171 | * @since 9.3 |
| 172 | */ |
| 173 | |
| 174 | /*@NotNull*/ |
| 175 | public Serializer newSerializer(OutputStream stream) { |
| 176 | Serializer s = new Serializer(this); |
| 177 | s.setOutputStream(stream); |
| 178 | return s; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Create a Serializer initialized to write to a given Writer. |
| 183 | * <p>Closing the writer after use is the responsibility of the caller.</p> |
| 184 | * |
| 185 | * @param writer The Writer to which the Serializer will write |
| 186 | * @return a new Serializer |
| 187 | * @since 9.3 |
| 188 | */ |
| 189 | |
| 190 | /*@NotNull*/ |
| 191 | public Serializer newSerializer(Writer writer) { |
| 192 | Serializer s = new Serializer(this); |
| 193 | s.setOutputWriter(writer); |
| 194 | return s; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Create a Serializer initialized to write to a given File. |
| 199 | * |
| 200 | * @param file The File to which the Serializer will write |
| 201 | * @return a new Serializer |
| 202 | * @since 9.3 |
| 203 | */ |
| 204 | |
| 205 | /*@NotNull*/ |
| 206 | public Serializer newSerializer(File file) { |
| 207 | Serializer s = new Serializer(this); |
| 208 | s.setOutputFile(file); |
| 209 | return s; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Register a simple external/extension function that is to be made available within any stylesheet, query, |
| 214 | * or XPath expression compiled under the control of this processor. |
| 215 | * <p>This interface provides only for simple extension functions that have no side-effects and no dependencies |
| 216 | * on the static or dynamic context.</p> |
| 217 | * |
| 218 | * @param function the implementation of the extension function. |
| 219 | * @since 9.4 |
| 220 | */ |
| 221 | |
| 222 | public void registerExtensionFunction(ExtensionFunction function) { |
| 223 | ExtensionFunctionDefinitionWrapper wrapper = new ExtensionFunctionDefinitionWrapper(function); |
| 224 | registerExtensionFunction(wrapper); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Register an extension function that is to be made available within any stylesheet, query, |
| 229 | * or XPath expression compiled under the control of this processor. This method |
| 230 | * registers an extension function implemented as an instance of |
| 231 | * {@link net.sf.saxon.lib.ExtensionFunctionDefinition}, using an arbitrary name and namespace. |
| 232 | * <p>This interface allows extension functions that have dependencies on the static or dynamic |
| 233 | * context. It also allows an extension function to declare that it has side-effects, in which |
| 234 | * case calls to the function will be optimized less aggressively than usual, although the semantics |
| 235 | * are still to some degree unpredictable.</p> |
| 236 | * |
| 237 | * @param function the implementation of the extension function. |
| 238 | * @since 9.2 |
| 239 | */ |
| 240 | |
| 241 | public void registerExtensionFunction(ExtensionFunctionDefinition function) { |
| 242 | try { |
| 243 | config.registerExtensionFunction(function); |
| 244 | } catch (Exception err) { |
| 245 | throw new IllegalArgumentException(err); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Get the associated SchemaManager. The SchemaManager provides capabilities to load and cache |
| 251 | * XML schema definitions. There is exactly one SchemaManager in a schema-aware Processor, and none |
| 252 | * in a Processor that is not schema-aware. The SchemaManager is created automatically by the system. |
| 253 | * |
| 254 | * @return the associated SchemaManager, or null if the Processor is not schema-aware. |
| 255 | */ |
| 256 | |
| 257 | public SchemaManager getSchemaManager() { |
| 258 | return schemaManager; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Test whether this processor is schema-aware |
| 263 | * |
| 264 | * @return true if this this processor is licensed for schema processing, false otherwise |
| 265 | */ |
| 266 | |
| 267 | public boolean isSchemaAware() { |
| 268 | return config.isLicensedFeature(Configuration.LicenseFeature.SCHEMA_VALIDATION); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Get the user-visible Saxon product version, for example "9.0.0.1" |
| 273 | * |
| 274 | * @return the Saxon product version, as a string |
| 275 | */ |
| 276 | |
| 277 | public String getSaxonProductVersion() { |
| 278 | return Version.getProductVersion(); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Get the short name of the Saxon product edition, for example "EE". This represents the kind of configuration |
| 283 | * that has been created, rather than the software that has been installed; for example it is possible to |
| 284 | * instantiate an "HE" configuration even when using the "PE" or "EE" software. |
| 285 | * @return the Saxon edition code: "EE", "PE", or "HE" |
| 286 | */ |
| 287 | |
| 288 | public String getSaxonEdition() { |
| 289 | return config.getEditionCode(); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Set the version of XML used by this Processor. If the value is set to "1.0", then |
| 294 | * output documents will be serialized as XML 1.0. This option also affects |
| 295 | * the characters permitted to appear in queries and stylesheets, and the characters that can appear |
| 296 | * in names (for example, in path expressions). |
| 297 | * <p>Note that source documents specifying xml version="1.0" or "1.1" are accepted |
| 298 | * regardless of this setting.</p> |
| 299 | * |
| 300 | * @param version must be one of the strings "1.0" or "1.1" |
| 301 | * @throws IllegalArgumentException if any string other than "1.0" or "1.1" is supplied |
| 302 | */ |
| 303 | |
| 304 | public void setXmlVersion(/*@NotNull*/ String version) { |
| 305 | switch (version) { |
| 306 | case "1.0": |
| 307 | config.setXMLVersion(Configuration.XML10); |
| 308 | break; |
| 309 | case "1.1": |
| 310 | config.setXMLVersion(Configuration.XML11); |
| 311 | break; |
| 312 | default: |
| 313 | throw new IllegalArgumentException("XmlVersion"); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Get the version of XML used by this Processor. If the value is "1.0", then input documents |
| 319 | * must be XML 1.0 documents, and output documents will be serialized as XML 1.0. This option also affects |
| 320 | * the characters permitted to appear in queries and stylesheets, and the characters that can appear |
| 321 | * in names (for example, in path expressions). |
| 322 | * |
| 323 | * @return one of the strings "1.0" or "1.1" |
| 324 | */ |
| 325 | |
| 326 | /*@NotNull*/ |
| 327 | public String getXmlVersion() { |
| 328 | if (config.getXMLVersion() == Configuration.XML10) { |
| 329 | return "1.0"; |
| 330 | } else { |
| 331 | return "1.1"; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Set a configuration property |
| 337 | * |
| 338 | * @param name the name of the option to be set. The names of the options available are listed |
| 339 | * as constants in class {@link net.sf.saxon.lib.FeatureKeys}. |
| 340 | * @param value the value of the option to be set. |
| 341 | * @throws IllegalArgumentException if the property name is not recognized or if the supplied value |
| 342 | * is not a valid value for the named property. |
| 343 | * @deprecated since 9.9 - use {@link #setConfigurationProperty(Feature, Object)} |
| 344 | */ |
| 345 | |
| 346 | public void setConfigurationProperty(/*@NotNull*/ String name, /*@NotNull*/ Object value) { |
| 347 | if (name.equals(FeatureKeys.CONFIGURATION)) { |
| 348 | config = (Configuration) value; |
| 349 | } else { |
| 350 | config.setConfigurationProperty(name, value); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Get the value of a configuration property |
| 356 | * |
| 357 | * @param name the name of the option required. The names of the properties available are listed |
| 358 | * as constants in class {@link net.sf.saxon.lib.FeatureKeys}. |
| 359 | * @return the value of the property, if one is set; or null if the property is unset and there is |
| 360 | * no default. |
| 361 | * @throws IllegalArgumentException if the property name is not recognized |
| 362 | * @deprecated since 9.9 - use {@link #getConfigurationProperty(Feature)} |
| 363 | */ |
| 364 | |
| 365 | |
| 366 | /*@Nullable*/ |
| 367 | public Object getConfigurationProperty(/*@NotNull*/ String name) { |
| 368 | return config.getConfigurationProperty(name); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Set a configuration property |
| 373 | * |
| 374 | * @param feature the option to be set. The names of the options available are listed |
| 375 | * as constants in class {@link net.sf.saxon.lib.Feature}. |
| 376 | * @param value the value of the option to be set (which must be of the appropriate type for the |
| 377 | * particular feature. |
| 378 | * @throws IllegalArgumentException if the supplied value is not a valid value for the selected feature. |
| 379 | * @since 9.9 introduced to give a faster and type-safe alternative to |
| 380 | * {@link #setConfigurationProperty(String, Object)} |
| 381 | */ |
| 382 | |
| 383 | public <T> void setConfigurationProperty(Feature<T> feature, T value) { |
| 384 | if (feature == Feature.CONFIGURATION) { |
| 385 | config = (Configuration) value; |
| 386 | } else { |
| 387 | config.setConfigurationProperty(feature, value); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Get the value of a configuration property |
| 393 | * |
| 394 | * @param feature the option required. The names of the properties available are listed |
| 395 | * as constants in class {@link net.sf.saxon.lib.Feature}. |
| 396 | * @return the value of the property, if one is set; or null if the property is unset and there is |
| 397 | * no default. |
| 398 | * @since 9.9 introduced to give a faster and type-safe alternative to |
| 399 | * {@link #getConfigurationProperty(String)} |
| 400 | */ |
| 401 | |
| 402 | |
| 403 | /*@Nullable*/ |
| 404 | public <T> T getConfigurationProperty(Feature<T> feature) { |
| 405 | return config.getConfigurationProperty(feature); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Bind a collation URI to a collation |
| 410 | * |
| 411 | * @param uri the absolute collation URI |
| 412 | * @param collation a {@link Comparator} object that implements the required collation |
| 413 | * @throws IllegalArgumentException if an attempt is made to rebind the standard URI |
| 414 | * for the Unicode codepoint collation or the HTML5 case-blind |
| 415 | * collation |
| 416 | * @since 9.6. Changed in 9.8 to allow any Comparator to be supplied as a collation |
| 417 | */ |
| 418 | |
| 419 | public void declareCollation(String uri, final Comparator collation) { |
| 420 | if (uri.equals(NamespaceConstant.CODEPOINT_COLLATION_URI)) { |
| 421 | throw new IllegalArgumentException("Cannot redeclare the Unicode codepoint collation URI"); |
| 422 | } |
| 423 | if (uri.equals(NamespaceConstant.HTML5_CASE_BLIND_COLLATION_URI)) { |
| 424 | throw new IllegalArgumentException("Cannot redeclare the HTML5 caseblind collation URI"); |
| 425 | } |
| 426 | StringCollator saxonCollation; |
| 427 | if (collation instanceof RuleBasedCollator) { |
| 428 | saxonCollation = new RuleBasedSubstringMatcher(uri, (RuleBasedCollator) collation); |
| 429 | } else { |
| 430 | saxonCollation = new SimpleCollation(uri, collation); |
| 431 | } |
| 432 | config.registerCollation(uri, saxonCollation); |
| 433 | } |
| 434 | |
| 435 | |
| 436 | /** |
| 437 | * Get the underlying {@link Configuration} object that underpins this Processor. This method |
| 438 | * provides an escape hatch to internal Saxon implementation objects that offer a finer and lower-level |
| 439 | * degree of control than the s9api classes and methods. Some of these classes and methods may change |
| 440 | * from release to release. |
| 441 | * |
| 442 | * @return the underlying Configuration object |
| 443 | */ |
| 444 | |
| 445 | public Configuration getUnderlyingConfiguration() { |
| 446 | return config; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Write an XdmValue to a given destination. |
| 451 | * |
| 452 | * <p>If the destination is a {@link Serializer} then the method <code>processor.writeXdmValue(V, S)</code> |
| 453 | * is equivalent to calling <code>S.serializeXdmValue(V)</code>.</p> |
| 454 | * |
| 455 | * <p>In other cases, the sequence represented by the XdmValue is "normalized" |
| 456 | * as defined in the serialization specification (this is equivalent to constructing a document node |
| 457 | * in XSLT or XQuery with this sequence as the content expression), and the resulting document is |
| 458 | * then copied to the destination. Note that the construction of a document tree will fail if |
| 459 | * the sequence contains items such as maps and arrays.</p> |
| 460 | * |
| 461 | * @param value the value to be written |
| 462 | * @param destination the destination to which the value is to be written |
| 463 | * @throws SaxonApiException if any failure occurs, for example a serialization error |
| 464 | */ |
| 465 | |
| 466 | public void writeXdmValue(XdmValue value, Destination destination) throws SaxonApiException { |
| 467 | Objects.requireNonNull(value); |
| 468 | Objects.requireNonNull(destination); |
| 469 | try { |
| 470 | if (destination instanceof Serializer) { |
| 471 | ((Serializer)destination).serializeXdmValue(value); |
| 472 | } else { |
| 473 | Receiver out = destination.getReceiver(config.makePipelineConfiguration(), config.obtainDefaultSerializationProperties()); |
| 474 | out = new NamespaceReducer(out); |
| 475 | TreeReceiver tree = new TreeReceiver(out); |
| 476 | tree.open(); |
| 477 | tree.startDocument(0); |
| 478 | for (XdmItem item : value) { |
| 479 | tree.append(item.getUnderlyingValue(), ExplicitLocation.UNKNOWN_LOCATION, ReceiverOptions.ALL_NAMESPACES); |
| 480 | } |
| 481 | tree.endDocument(); |
| 482 | tree.close(); |
| 483 | destination.closeAndNotify(); |
| 484 | } |
| 485 | } catch (XPathException err) { |
| 486 | throw new SaxonApiException(err); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | |
| 491 | private static class ExtensionFunctionDefinitionWrapper extends ExtensionFunctionDefinition { |
| 492 | |
| 493 | private ExtensionFunction function; |
| 494 | |
| 495 | public ExtensionFunctionDefinitionWrapper(ExtensionFunction function) { |
| 496 | this.function = function; |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Get the name of the function, as a QName. |
| 501 | * <p>This method must be implemented in all subclasses</p> |
| 502 | * |
| 503 | * @return the function name |
| 504 | */ |
| 505 | @Override |
| 506 | public StructuredQName getFunctionQName() { |
| 507 | return function.getName().getStructuredQName(); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Get the minimum number of arguments required by the function |
| 512 | * <p>The default implementation returns the number of items in the result of calling |
| 513 | * {@link #getArgumentTypes}</p> |
| 514 | * |
| 515 | * @return the minimum number of arguments that must be supplied in a call to this function |
| 516 | */ |
| 517 | @Override |
| 518 | public int getMinimumNumberOfArguments() { |
| 519 | return function.getArgumentTypes().length; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Get the maximum number of arguments allowed by the function. |
| 524 | * <p>The default implementation returns the value of {@link #getMinimumNumberOfArguments} |
| 525 | * |
| 526 | * @return the maximum number of arguments that may be supplied in a call to this function |
| 527 | */ |
| 528 | @Override |
| 529 | public int getMaximumNumberOfArguments() { |
| 530 | return function.getArgumentTypes().length; |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Get the required types for the arguments of this function. |
| 535 | * <p>This method must be implemented in all subtypes.</p> |
| 536 | * |
| 537 | * @return the required types of the arguments, as defined by the function signature. Normally |
| 538 | * this should be an array of size {@link #getMaximumNumberOfArguments()}; however for functions |
| 539 | * that allow a variable number of arguments, the array can be smaller than this, with the last |
| 540 | * entry in the array providing the required type for all the remaining arguments. |
| 541 | */ |
| 542 | /*@NotNull*/ |
| 543 | @Override |
| 544 | public net.sf.saxon.value.SequenceType[] getArgumentTypes() { |
| 545 | net.sf.saxon.s9api.SequenceType[] declaredArgs = function.getArgumentTypes(); |
| 546 | net.sf.saxon.value.SequenceType[] types = new net.sf.saxon.value.SequenceType[declaredArgs.length]; |
| 547 | for (int i = 0; i < declaredArgs.length; i++) { |
| 548 | types[i] = net.sf.saxon.value.SequenceType.makeSequenceType( |
| 549 | declaredArgs[i].getItemType().getUnderlyingItemType(), |
| 550 | declaredArgs[i].getOccurrenceIndicator().getCardinality()); |
| 551 | } |
| 552 | return types; |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Get the type of the result of the function |
| 557 | * <p>This method must be implemented in all subtypes.</p> |
| 558 | * |
| 559 | * @param suppliedArgumentTypes the static types of the supplied arguments to the function. |
| 560 | * This is provided so that a more precise result type can be returned in the common |
| 561 | * case where the type of the result depends on the types of the arguments. |
| 562 | * @return the return type of the function, as defined by its function signature |
| 563 | */ |
| 564 | @Override |
| 565 | public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) { |
| 566 | net.sf.saxon.s9api.SequenceType declaredResult = function.getResultType(); |
| 567 | return net.sf.saxon.value.SequenceType.makeSequenceType( |
| 568 | declaredResult.getItemType().getUnderlyingItemType(), |
| 569 | declaredResult.getOccurrenceIndicator().getCardinality()); |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * Ask whether the result actually returned by the function can be trusted, |
| 574 | * or whether it should be checked against the declared type. |
| 575 | * |
| 576 | * @return true if the function implementation warrants that the value it returns will |
| 577 | * be an instance of the declared result type. The default value is false, in which case |
| 578 | * the result will be checked at run-time to ensure that it conforms to the declared type. |
| 579 | * If the value true is returned, but the function returns a value of the wrong type, the |
| 580 | * consequences are unpredictable. |
| 581 | */ |
| 582 | @Override |
| 583 | public boolean trustResultType() { |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Ask whether the result of the function depends on the focus, or on other variable parts |
| 589 | * of the context. |
| 590 | * |
| 591 | * @return true if the result of the function depends on the context item, position, or size. |
| 592 | * Despite the method name, the method should also return true if the function depends on other |
| 593 | * parts of the context that vary from one part of the query/stylesheet to another, for example |
| 594 | * the XPath default namespace. |
| 595 | * <p>The default implementation returns false.</p> |
| 596 | * <p>The method must return true if the function |
| 597 | * makes use of any of these values from the dynamic context. Returning true inhibits certain |
| 598 | * optimizations, such as moving the function call out of the body of an xsl:for-each loop, |
| 599 | * or extracting it into a global variable.</p> |
| 600 | */ |
| 601 | @Override |
| 602 | public boolean dependsOnFocus() { |
| 603 | return false; |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Ask whether the function has side-effects. If the function does have side-effects, the optimizer |
| 608 | * will be less aggressive in moving or removing calls to the function. However, calls on functions |
| 609 | * with side-effects can never be guaranteed. |
| 610 | * |
| 611 | * @return true if the function has side-effects (including creation of new nodes, if the |
| 612 | * identity of those nodes is significant). The default implementation returns false. |
| 613 | */ |
| 614 | @Override |
| 615 | public boolean hasSideEffects() { |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | /** |
| 620 | * Create a call on this function. This method is called by the compiler when it identifies |
| 621 | * a function call that calls this function. |
| 622 | */ |
| 623 | /*@NotNull*/ |
| 624 | @Override |
| 625 | public ExtensionFunctionCall makeCallExpression() { |
| 626 | return new ExtensionFunctionCall() { |
| 627 | @Override |
| 628 | public Sequence<?> call( |
| 629 | /*@NotNull*/ XPathContext context, Sequence[] arguments) throws XPathException { |
| 630 | XdmValue[] args = new XdmValue[arguments.length]; |
| 631 | for (int i = 0; i < args.length; i++) { |
| 632 | GroundedValue<?> val = (GroundedValue<?>)arguments[i].materialize(); |
| 633 | args[i] = XdmValue.wrap(val); |
| 634 | } |
| 635 | try { |
| 636 | XdmValue result = function.call(args); |
| 637 | return result.getUnderlyingValue(); |
| 638 | } catch (SaxonApiException e) { |
| 639 | throw new XPathException(e); |
| 640 | } |
| 641 | } |
| 642 | }; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | } |
| 647 | |
| 648 | private SchemaManager makeSchemaManager() { |
| 649 | SchemaManager manager = null; |
| 650 | //#if EE==true |
| 651 | manager = new com.saxonica.ee.s9api.SchemaManagerImpl(this); |
| 652 | //#endif |
| 653 | return manager; |
| 654 | } |
| 655 | |
| 656 | |
| 657 | } |
| 658 |