| 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.event.EventSource; |
| 11 | import net.sf.saxon.event.PipelineConfiguration; |
| 12 | import net.sf.saxon.event.Receiver; |
| 13 | import net.sf.saxon.jaxp.ReceivingDestination; |
| 14 | import net.sf.saxon.lib.InvalidityHandler; |
| 15 | import net.sf.saxon.serialize.SerializationProperties; |
| 16 | import net.sf.saxon.trans.XPathException; |
| 17 | import net.sf.saxon.type.SchemaType; |
| 18 | |
| 19 | import javax.xml.transform.ErrorListener; |
| 20 | import javax.xml.transform.Source; |
| 21 | |
| 22 | /** |
| 23 | * A <tt>SchemaValidator</tt> is an object that is used for validating instance documents against a schema. |
| 24 | * The schema consists of the collection of schema components that are available within the schema |
| 25 | * cache maintained by the SchemaManager, together with any additional schema components located |
| 26 | * during the course of validation by means of an xsl:schemaLocation or xsi:noNamespaceSchemaLocation |
| 27 | * attribute within the instance document. |
| 28 | * <p>If validation fails, an exception is thrown. If validation succeeds, the validated document |
| 29 | * can optionally be written to a specified destination. This will be a copy of the original document, |
| 30 | * augmented with default values for absent elements and attributes, and carrying type annotations |
| 31 | * derived from the schema processing. Expansion of defaults can be suppressed by means of the method |
| 32 | * {@link #setExpandAttributeDefaults(boolean)}.</p> |
| 33 | * <p>A <tt>SchemaValidator</tt> is serially reusable but not thread-safe. That is, it should normally |
| 34 | * be used in the thread where it is created, but it can be used more than once, to validate multiple |
| 35 | * input documents.</p> |
| 36 | * <p>A <tt>SchemaValidator</tt> is a <tt>Destination</tt>, which allows it to receive the output of a |
| 37 | * query or transformation to be validated.</p> |
| 38 | * <p>Saxon does not deliver the full PSVI as described in the XML schema specifications, |
| 39 | * only the subset of the PSVI properties featured in the XDM data model.</p> |
| 40 | */ |
| 41 | |
| 42 | public abstract class SchemaValidator extends AbstractDestination { |
| 43 | |
| 44 | /** |
| 45 | * The validation mode may be either strict or lax. The default is strict; this method may be called |
| 46 | * to indicate that lax validation is required. With strict validation, validation fails if no element |
| 47 | * declaration can be located for the outermost element. With lax validation, the absence of an |
| 48 | * element declaration results in the content being considered valid. |
| 49 | * |
| 50 | * @param lax true if validation is to be lax, false if it is to be strict |
| 51 | */ |
| 52 | |
| 53 | public abstract void setLax(boolean lax); |
| 54 | |
| 55 | /** |
| 56 | * Ask whether validation is to be in lax mode. |
| 57 | * |
| 58 | * @return true if validation is to be in lax mode, false if it is to be in strict mode. |
| 59 | */ |
| 60 | |
| 61 | public abstract boolean isLax(); |
| 62 | |
| 63 | /** |
| 64 | * Set the ErrorListener to be used while validating instance documents. |
| 65 | * The setErrorListener, setInvalidityHandler, and setValidityReporting |
| 66 | * are mutually exclusive - setting any one of them will cancel the others. Please note |
| 67 | * that setErrorListener has the drawback of creating an exception for every |
| 68 | * validation error, which is expensive. |
| 69 | * |
| 70 | * @param listener The error listener to be used. This is notified of all errors detected during the |
| 71 | * validation episode. |
| 72 | */ |
| 73 | |
| 74 | public abstract void setErrorListener(ErrorListener listener); |
| 75 | |
| 76 | /** |
| 77 | * Get the ErrorListener being used while validating instance documents |
| 78 | * |
| 79 | * @return listener The error listener in use. This is notified of all errors detected during the |
| 80 | * validation episode. Returns null if no user-supplied ErrorListener has been set. |
| 81 | */ |
| 82 | |
| 83 | public abstract ErrorListener getErrorListener(); |
| 84 | |
| 85 | /** |
| 86 | * Set the InvalidityHandler to be used when validating instance documents. |
| 87 | * The setErrorListener, setInvalidityHandler, and setValidityReporting |
| 88 | * are mutually exclusive - setting any one of them will cancel the others. |
| 89 | * @param handler the InvalidityHandler to be used. |
| 90 | */ |
| 91 | |
| 92 | public abstract void setInvalidityHandler(InvalidityHandler handler); |
| 93 | |
| 94 | /** |
| 95 | * Get the InvalidityHandler used when validating instance documents |
| 96 | * @return the InvalidityHandler being used |
| 97 | */ |
| 98 | |
| 99 | public abstract InvalidityHandler getInvalidityHandler(); |
| 100 | /** |
| 101 | * Say whether validation statistics are to be collected. Calling this method |
| 102 | * has the side-effect of deleting any statistics previously collected. |
| 103 | * |
| 104 | * @param collect true if validation statistics are to be collected (default is false) |
| 105 | * @since 9.6 |
| 106 | */ |
| 107 | |
| 108 | public abstract void setCollectStatistics(boolean collect); |
| 109 | |
| 110 | /** |
| 111 | * Ask whether validation statistics are to be collected |
| 112 | * |
| 113 | * @return true if validation statistics are to be collected (default is false) |
| 114 | * @since 9.6 |
| 115 | */ |
| 116 | |
| 117 | public abstract boolean isCollectStatistics(); |
| 118 | |
| 119 | /** |
| 120 | * Report the validation statistics from the most recent validation episode |
| 121 | * Does nothing if no validation statistics have been collected. |
| 122 | * |
| 123 | * @param destination the destination to which the statistics will be written. |
| 124 | * The XML format of the destination is not guaranteed to be stable across |
| 125 | * Saxon releases. |
| 126 | * @throws SaxonApiException if any error occurs writing the statistics |
| 127 | * @since Saxon 9.6 |
| 128 | */ |
| 129 | |
| 130 | public abstract void reportValidationStatistics(Destination destination) throws SaxonApiException; |
| 131 | |
| 132 | /** |
| 133 | * This method can be called before running a validation to define a destination to which validation |
| 134 | * reports should be written. The validation report is in XML format, and the Destination may therefore |
| 135 | * be (for example) a Serializer, an XdmDestination, or an XsltTransformer. (An XsltTransformer might be |
| 136 | * used to render the report as HTML, for example). The format of the validation report is defined in a |
| 137 | * schema which is available in the saxon-resources download file. |
| 138 | * |
| 139 | * Calling this method has the effect of setting an InvalidityHandler internally, which cancels any |
| 140 | * user-defined InvalidityHandler or ErrorListener that has been set. |
| 141 | * |
| 142 | * This option applies only to the next call of validate() or validateMultiple [or whatever we call it]. |
| 143 | * After such a call, the registered InvalidityHandler is reset to its initial default state (which causes |
| 144 | * validation errors to be output to System.err), and setValidityReport() must be called again if reports |
| 145 | * are required for subsequent validations. |
| 146 | * |
| 147 | * If multiple documents are validated using the validateMultiple method, the reports for each validation will |
| 148 | * be combined into a single report. Using this mechanism when validating multiple documents simultaneously is |
| 149 | * recommended, because with other mechanisms, validation failures for different source documents will be |
| 150 | * interleaved, and it becomes an application responsibility to organize which failures relate to which |
| 151 | * source document. |
| 152 | * |
| 153 | * The setErrorListener, setInvalidityHandler, and setValidityReporting |
| 154 | * are mutually exclusive - setting any one of them will cancel the others. |
| 155 | * @param destination where XML will be sent |
| 156 | * @throws SaxonApiException if the destination is unsuitable |
| 157 | */ |
| 158 | public abstract void setValidityReporting(Destination destination) throws SaxonApiException; |
| 159 | |
| 160 | |
| 161 | /** |
| 162 | * Say whether the schema processor is to take account of any xsi:schemaLocation and |
| 163 | * xsi:noNamespaceSchemaLocation attributes encountered while validating an instance document |
| 164 | * |
| 165 | * @param recognize true if these two attributes are to be recognized; false if they are to |
| 166 | * be ignored. Default is true. |
| 167 | */ |
| 168 | |
| 169 | public abstract void setUseXsiSchemaLocation(boolean recognize); |
| 170 | |
| 171 | /** |
| 172 | * Ask whether the schema processor is to take account of any xsi:schemaLocation and |
| 173 | * xsi:noNamespaceSchemaLocation attributes encountered while validating an instance document |
| 174 | * |
| 175 | * @return true if these two attributes are to be recognized; false if they are to |
| 176 | * be ignored. Default is true. |
| 177 | */ |
| 178 | |
| 179 | public abstract boolean isUseXsiSchemaLocation(); |
| 180 | |
| 181 | |
| 182 | /** |
| 183 | * Set the Destination to receive the validated document. If no destination is supplied, the |
| 184 | * validated document is discarded. |
| 185 | * |
| 186 | * @param destination the destination to receive the validated document |
| 187 | */ |
| 188 | |
| 189 | public abstract void setDestination(/*@Nullable*/ Destination destination); |
| 190 | |
| 191 | /** |
| 192 | * Get the Destination that will receive the validated document. Return null if no destination |
| 193 | * has been set. |
| 194 | * |
| 195 | * @return the destination to receive the validated document, or null if none has been supplied |
| 196 | */ |
| 197 | |
| 198 | |
| 199 | /*@Nullable*/ |
| 200 | public abstract Destination getDestination(); |
| 201 | |
| 202 | /** |
| 203 | * Set the name of the required top-level element of the document to be validated (that is, the |
| 204 | * name of the outermost element of the document). If no value is supplied, there is no constraint |
| 205 | * on the required element name |
| 206 | * |
| 207 | * @param name the name of the document element, as a QName; or null to remove a previously-specified |
| 208 | * value. |
| 209 | */ |
| 210 | |
| 211 | public abstract void setDocumentElementName(QName name); |
| 212 | |
| 213 | /** |
| 214 | * Get the name of the required top-level element of the document to be validated. |
| 215 | * |
| 216 | * @return the name of the required document element, or null if no value has been set. |
| 217 | */ |
| 218 | |
| 219 | public abstract QName getDocumentElementName(); |
| 220 | |
| 221 | /** |
| 222 | * Set the name of the required type of the top-level element of the document to be validated. |
| 223 | * If no value is supplied, there is no constraint on the required type |
| 224 | * |
| 225 | * @param name the name of the type of the document element, as a QName; |
| 226 | * or null to remove a previously-specified value. This must be the name of a type in the |
| 227 | * schema (typically but not necessarily a complex type). |
| 228 | * @throws SaxonApiException if there is no known type with this name |
| 229 | */ |
| 230 | |
| 231 | public abstract void setDocumentElementTypeName(QName name) throws SaxonApiException; |
| 232 | /** |
| 233 | * Get the name of the required type of the top-level element of the document to be validated. |
| 234 | * |
| 235 | * @return the name of the required type of the document element, or null if no value has been set. |
| 236 | */ |
| 237 | |
| 238 | public abstract QName getDocumentElementTypeName(); |
| 239 | |
| 240 | /** |
| 241 | * Get the schema type against which the document element is to be validated |
| 242 | * |
| 243 | * @return the schema type |
| 244 | */ |
| 245 | |
| 246 | protected abstract SchemaType getDocumentElementType(); |
| 247 | /** |
| 248 | * Set whether attribute defaults defined in a schema are to be expanded or not |
| 249 | * (by default, fixed and default attribute values are expanded, that is, they are inserted |
| 250 | * into the document during validation as if they were present in the instance being validated) |
| 251 | * |
| 252 | * @param expand true if defaults are to be expanded, false if not |
| 253 | */ |
| 254 | |
| 255 | public abstract void setExpandAttributeDefaults(boolean expand); |
| 256 | |
| 257 | /** |
| 258 | * Ask whether attribute defaults defined in a schema are to be expanded or not |
| 259 | * (by default, fixed and default attribute values are expanded, that is, they are inserted |
| 260 | * into the document during validation as if they were present in the instance being validated) |
| 261 | * |
| 262 | * @return true if defaults are to be expanded, false if not |
| 263 | */ |
| 264 | |
| 265 | public abstract boolean isExpandAttributeDefaults(); |
| 266 | |
| 267 | /** |
| 268 | * Set the value of a schema parameter (a parameter defined in the schema using |
| 269 | * the <code>saxon:param</code> extension) |
| 270 | * |
| 271 | * @param name the name of the schema parameter, as a QName |
| 272 | * @param value the value of the schema parameter, or null to clear a previously set value |
| 273 | * @since 9.5 |
| 274 | */ |
| 275 | |
| 276 | public abstract void setParameter(QName name, XdmValue value); |
| 277 | |
| 278 | /** |
| 279 | * Get the value that has been set for a schema parameter (a parameter defined in the schema using |
| 280 | * the <code>saxon:param</code> extension) |
| 281 | * |
| 282 | * @param name the parameter whose name is required |
| 283 | * @return the value that has been set for the parameter, or null if no value has been set |
| 284 | * @since 9.5 |
| 285 | */ |
| 286 | |
| 287 | public abstract XdmValue getParameter(QName name); |
| 288 | |
| 289 | |
| 290 | /** |
| 291 | * Validate an instance document supplied as a Source object |
| 292 | * |
| 293 | * @param source the instance document to be validated. The call getSystemId() applied to |
| 294 | * this source object must return the base URI used for dereferencing any xsi:schemaLocation |
| 295 | * or xsi:noNamespaceSchemaLocation attributes |
| 296 | * @throws SaxonApiException if the source document is found to be invalid, or if error conditions |
| 297 | * occur that prevented validation from taking place (such as failure to read or parse the input |
| 298 | * document). The wrapped exception acting as the cause of the SaxonApiException can be used to |
| 299 | * distinguish these failure conditions. |
| 300 | */ |
| 301 | |
| 302 | public abstract void validate(Source source) throws SaxonApiException; |
| 303 | |
| 304 | |
| 305 | |
| 306 | /** |
| 307 | * This method can be called to validate multiple source documents simultaneously (in parallel threads). |
| 308 | * The method returns when all the threads are complete. The number of threads used is set to the number |
| 309 | * of processors available on the machine. |
| 310 | * |
| 311 | * When this method is used, any destination set using the setDestination() method is ignored. The |
| 312 | * post-validation documents (with type annotations and expanded default values, for example) are not |
| 313 | * made available. |
| 314 | * |
| 315 | * It is recommended to use this method in conjunction with setValidityReport; the resulting report will |
| 316 | * detail the outcome of validation for each supplied Source, each in a separate section. |
| 317 | * |
| 318 | * It is important that the systemId property of each Source object should be set; otherwise, it will not |
| 319 | * be possible in the resulting report to associate validation failures with individual source documents. |
| 320 | * |
| 321 | * Note that the method does not throw an exception if any of the documents are found to be invalid, or if |
| 322 | * any of the documents cannot be validated (for example, because they are not well-formed XML, or because |
| 323 | * they cannot be retrieved from a web server). All such errors are reported in the validation report, or are |
| 324 | * notified to the registered ErrorListener or InvalidityHandler. By default, messages detailing the failures |
| 325 | * are simply written to System.err output. |
| 326 | * |
| 327 | * If the Configuration option FeatureKeys.ALLOW_MULTITHREADING is set to false, the source documents are |
| 328 | * validated synchronously in a single thread. |
| 329 | * |
| 330 | * @param sources the Iterable of instance documents to be validated. The call getSystemId() applied to |
| 331 | * the source objects must return the base URI used for dereferencing any xsi:schemaLocation |
| 332 | * or xsi:noNamespaceSchemaLocation attributes |
| 333 | * @throws SaxonApiException if the source document is found to be invalid, or if error conditions |
| 334 | * occur that prevented validation from taking place (such as failure to read or parse the input |
| 335 | * document). The wrapped exception acting as the cause of the SaxonApiException can be used to |
| 336 | * distinguish these failure conditions. |
| 337 | */ |
| 338 | public abstract void validateMultiple(Iterable<Source> sources) throws SaxonApiException; |
| 339 | |
| 340 | //#if DOTNET == false |
| 341 | /** |
| 342 | * Construct a Source object that applies validation to an underlying input Source. |
| 343 | * This method does not cause validation to take place immediately: the |
| 344 | * validation happens only when the returned {@code Source} is consumed. The {@code Destination} |
| 345 | * associated with this SchemaValidator is ignored; indeed, it is overwritten. After |
| 346 | * calling this method, the {@code SchemaValidator} should not be used again for any |
| 347 | * other purpose. |
| 348 | * @param input the input Source to be validated |
| 349 | * @return a Source object that reads the supplied input source and returns events |
| 350 | * corresponding to the result of validating the input source. The Source object |
| 351 | * will be recognized by all Saxon methods that expect a {@code Source} as input, |
| 352 | * but not (in general) by other products. |
| 353 | * @since 9.9 |
| 354 | */ |
| 355 | |
| 356 | public Source asSource(Source input) { |
| 357 | return new EventSource() { |
| 358 | { |
| 359 | setSystemId(input.getSystemId()); |
| 360 | } |
| 361 | @Override |
| 362 | public void send(Receiver out) throws XPathException { |
| 363 | setDestination(new ReceivingDestination(out)); |
| 364 | try { |
| 365 | validate(input); |
| 366 | } catch (SaxonApiException e) { |
| 367 | throw XPathException.makeXPathException(e); |
| 368 | } |
| 369 | } |
| 370 | }; |
| 371 | } |
| 372 | //#endif |
| 373 | |
| 374 | public abstract Receiver getReceiver(PipelineConfiguration pipe, SerializationProperties params) throws SaxonApiException; |
| 375 | |
| 376 | /** |
| 377 | * Close the destination, allowing resources to be released. Saxon calls this method when |
| 378 | * it has finished writing to the destination. |
| 379 | */ |
| 380 | |
| 381 | public abstract void close() throws SaxonApiException; |
| 382 | |
| 383 | } |
| 384 |