| 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.event; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.Controller; |
| 12 | import net.sf.saxon.expr.XPathContext; |
| 13 | import net.sf.saxon.lib.ParseOptions; |
| 14 | import net.sf.saxon.lib.SchemaURIResolver; |
| 15 | import net.sf.saxon.lib.UnfailingErrorListener; |
| 16 | |
| 17 | import javax.xml.transform.ErrorListener; |
| 18 | import javax.xml.transform.URIResolver; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Map; |
| 21 | |
| 22 | /** |
| 23 | * A PipelineConfiguration sets options that apply to all the operations in a pipeline. |
| 24 | * Unlike the global Configuration, these options are always local to a process. |
| 25 | */ |
| 26 | |
| 27 | public class PipelineConfiguration { |
| 28 | |
| 29 | /*@NotNull*/ private Configuration config; |
| 30 | private URIResolver uriResolver; |
| 31 | private SchemaURIResolver schemaURIResolver; |
| 32 | private Controller controller; |
| 33 | private ParseOptions parseOptions; |
| 34 | private int hostLanguage = Configuration.XSLT; |
| 35 | private Map<String, Object> components; |
| 36 | private XPathContext context; |
| 37 | |
| 38 | /** |
| 39 | * Create a PipelineConfiguration. Note: the normal way to create |
| 40 | * a PipelineConfiguration is via the factory methods in the Controller and |
| 41 | * Configuration classes |
| 42 | * |
| 43 | * @param config the Saxon configuration |
| 44 | * @see Configuration#makePipelineConfiguration |
| 45 | * @see Controller#makePipelineConfiguration |
| 46 | */ |
| 47 | |
| 48 | public PipelineConfiguration(/*@NotNull*/ Configuration config) { |
| 49 | this.config = config; |
| 50 | parseOptions = new ParseOptions(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Create a PipelineConfiguration as a copy of an existing |
| 55 | * PipelineConfiguration |
| 56 | * |
| 57 | * @param p the existing PipelineConfiguration |
| 58 | */ |
| 59 | |
| 60 | public PipelineConfiguration(PipelineConfiguration p) { |
| 61 | config = p.config; |
| 62 | uriResolver = p.uriResolver; |
| 63 | schemaURIResolver = p.schemaURIResolver; |
| 64 | controller = p.controller; |
| 65 | parseOptions = new ParseOptions(p.parseOptions); |
| 66 | hostLanguage = p.hostLanguage; |
| 67 | if (p.components != null) { |
| 68 | components = new HashMap<>(p.components); |
| 69 | } |
| 70 | context = p.context; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get the Saxon Configuration object |
| 75 | * |
| 76 | * @return the Saxon Configuration |
| 77 | */ |
| 78 | |
| 79 | /*@NotNull*/ |
| 80 | public Configuration getConfiguration() { |
| 81 | return config; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Set the Saxon Configuration object |
| 86 | * |
| 87 | * @param config the Saxon Configuration |
| 88 | */ |
| 89 | |
| 90 | public void setConfiguration(/*@NotNull*/ Configuration config) { |
| 91 | this.config = config; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get the ErrorListener set as a property of this pipeline |
| 96 | * |
| 97 | * @return the ErrorListener; null if none has been set. |
| 98 | */ |
| 99 | |
| 100 | public UnfailingErrorListener getLocalErrorListener() { |
| 101 | return parseOptions.getErrorListener(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get an ErrorListener for reporting errors in processing this pipeline; this |
| 106 | * will be the ErrorListener set locally in the PipelineConfiguration if there is one, |
| 107 | * or the ErrorListener from the Configuration otherwise. |
| 108 | * |
| 109 | * @return the ErrorListener to be used; never null |
| 110 | */ |
| 111 | |
| 112 | public UnfailingErrorListener getErrorListener() { |
| 113 | UnfailingErrorListener listener = parseOptions.getErrorListener(); |
| 114 | if (listener == null) { |
| 115 | listener = config.getErrorListener(); |
| 116 | } |
| 117 | return listener; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Set the ErrorListener used for reporting errors in processing this pipeline |
| 122 | * |
| 123 | * @param errorListener the ErrorListener |
| 124 | */ |
| 125 | |
| 126 | public void setErrorListener(ErrorListener errorListener) { |
| 127 | parseOptions.setErrorListener(errorListener); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the URIResolver used for processing URIs encountered on this pipeline |
| 132 | * |
| 133 | * @return the URIResolver |
| 134 | */ |
| 135 | |
| 136 | public URIResolver getURIResolver() { |
| 137 | return uriResolver; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Set the URIResolver used for processing URIs encountered on this pipeline |
| 142 | * |
| 143 | * @param uriResolver the URIResolver |
| 144 | */ |
| 145 | |
| 146 | public void setURIResolver(URIResolver uriResolver) { |
| 147 | this.uriResolver = uriResolver; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get the user-defined SchemaURIResolver for resolving URIs used in "import schema" |
| 152 | * declarations; returns null if none has been explicitly set. |
| 153 | * |
| 154 | * @return the SchemaURIResolver |
| 155 | */ |
| 156 | |
| 157 | public SchemaURIResolver getSchemaURIResolver() { |
| 158 | return schemaURIResolver; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Set the document parsing and building options to be used on this pipeline |
| 163 | * |
| 164 | * @param options the options to be used |
| 165 | */ |
| 166 | |
| 167 | public void setParseOptions(ParseOptions options) { |
| 168 | parseOptions = options; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get the document parsing and building options to be used on this pipeline |
| 173 | * return the options to be used |
| 174 | * |
| 175 | * @return the parser options for this pipeline |
| 176 | */ |
| 177 | |
| 178 | public ParseOptions getParseOptions() { |
| 179 | return parseOptions; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Say whether xsi:schemaLocation and xsi:noNamespaceSchemaLocation attributes |
| 184 | * should be recognized while validating an instance document |
| 185 | * |
| 186 | * @param recognize true if these attributes should be recognized |
| 187 | */ |
| 188 | |
| 189 | public void setUseXsiSchemaLocation(boolean recognize) { |
| 190 | parseOptions.setUseXsiSchemaLocation(recognize); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Say whether validation errors encountered on this pipeline should be treated as fatal |
| 195 | * or as recoverable. |
| 196 | * <p>Note this is a shortcut for <code>getParseOptions().setContinueAfterValidationErrors()</code>, retained |
| 197 | * for backwards compatibility.</p> |
| 198 | * |
| 199 | * @param recover set to true if validation errors are to be treated as recoverable. If this option is set to true, |
| 200 | * such errors will be reported to the ErrorListener using the error() method, and validation will continue. |
| 201 | * If it is set to false, errors will be reported using the fatalError() method, and validation will |
| 202 | * be abandoned. The default depends on the circumstances: typically during standalone instance validation |
| 203 | * the default is true, but during XSLT and XQuery processing it is false. |
| 204 | */ |
| 205 | |
| 206 | public void setRecoverFromValidationErrors(boolean recover) { |
| 207 | parseOptions.setContinueAfterValidationErrors(recover); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Ask if this pipeline recovers from validation errors |
| 212 | * <p>Note this is a shortcut for <code>getParseOptions().isContinueAfterValidationErrors()</code>, retained |
| 213 | * for backwards compatibility.</p> |
| 214 | * |
| 215 | * @return true if validation errors on this pipeline are treated as recoverable; false if they are treated |
| 216 | * as fatal |
| 217 | */ |
| 218 | |
| 219 | public boolean isRecoverFromValidationErrors() { |
| 220 | return parseOptions.isContinueAfterValidationErrors(); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Set a user-defined SchemaURIResolver for resolving URIs used in "import schema" |
| 225 | * declarations. |
| 226 | * |
| 227 | * @param resolver the SchemaURIResolver |
| 228 | */ |
| 229 | |
| 230 | public void setSchemaURIResolver(SchemaURIResolver resolver) { |
| 231 | schemaURIResolver = resolver; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Get the controller associated with this pipelineConfiguration |
| 236 | * |
| 237 | * @return the controller if it is known; otherwise null. |
| 238 | */ |
| 239 | |
| 240 | public Controller getController() { |
| 241 | return controller; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Set the Controller associated with this pipelineConfiguration |
| 246 | * |
| 247 | * @param controller the Controller |
| 248 | */ |
| 249 | |
| 250 | public void setController(Controller controller) { |
| 251 | this.controller = controller; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Get the host language in use |
| 256 | * |
| 257 | * @return for example {@link Configuration#XSLT} or {@link Configuration#XQUERY} |
| 258 | */ |
| 259 | |
| 260 | public int getHostLanguage() { |
| 261 | return hostLanguage; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Set the host language in use |
| 266 | * |
| 267 | * @param language for example {@link Configuration#XSLT} or {@link Configuration#XQUERY} |
| 268 | */ |
| 269 | |
| 270 | public void setHostLanguage(int language) { |
| 271 | hostLanguage = language; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Set whether attribute defaults defined in a schema or DTD are to be expanded or not |
| 276 | * (by default, fixed and default attribute values are expanded, that is, they are inserted |
| 277 | * into the document during validation as if they were present in the instance being validated) |
| 278 | * |
| 279 | * @param expand true if defaults are to be expanded, false if not |
| 280 | */ |
| 281 | |
| 282 | public void setExpandAttributeDefaults(boolean expand) { |
| 283 | parseOptions.setExpandAttributeDefaults(expand); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Set a named component of the pipeline |
| 288 | * |
| 289 | * @param name string the component name |
| 290 | * @param value the component value |
| 291 | */ |
| 292 | |
| 293 | public void setComponent(String name, Object value) { |
| 294 | if (components == null) { |
| 295 | components = new HashMap<>(); |
| 296 | } |
| 297 | components.put(name, value); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Get a named component of the pipeline |
| 302 | * |
| 303 | * @param name string the component name |
| 304 | * @return the component value, or null if absent |
| 305 | */ |
| 306 | |
| 307 | public Object getComponent(String name) { |
| 308 | if (components == null) { |
| 309 | return null; |
| 310 | } else { |
| 311 | return components.get(name); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Set the XPathContext. Used during validation, for diagnostics, to identify the source node that was |
| 317 | * being processed at the time a validation error in the result document was found |
| 318 | * @param context the XPath dynamic context. |
| 319 | */ |
| 320 | |
| 321 | public void setXPathContext(XPathContext context) { |
| 322 | this.context = context; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Get the XPathContext. Used during validation, for diagnostics, to identify the source node that was |
| 327 | * being processed at the time a validation error in the result document was found |
| 328 | * |
| 329 | * @return the XPath dynamic context. |
| 330 | */ |
| 331 | |
| 332 | public XPathContext getXPathContext() { |
| 333 | return context; |
| 334 | } |
| 335 | |
| 336 | } |
| 337 |