| 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.jaxp; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.event.Receiver; |
| 12 | import net.sf.saxon.event.Sender; |
| 13 | import net.sf.saxon.lib.DelegatingErrorListener; |
| 14 | import net.sf.saxon.lib.ParseOptions; |
| 15 | import net.sf.saxon.lib.SerializerFactory; |
| 16 | import net.sf.saxon.serialize.SerializationProperties; |
| 17 | import net.sf.saxon.trans.XPathException; |
| 18 | import org.xml.sax.SAXParseException; |
| 19 | |
| 20 | import javax.xml.transform.*; |
| 21 | import java.util.Enumeration; |
| 22 | import java.util.Properties; |
| 23 | |
| 24 | /** |
| 25 | * Saxon implementation of the JAXP IdentityTransformer. |
| 26 | * This is used mainly for serializing various kinds of source under |
| 27 | * the control of serialization parameters. |
| 28 | */ |
| 29 | |
| 30 | public class IdentityTransformer extends Transformer { |
| 31 | |
| 32 | private Configuration configuration; |
| 33 | private Properties localOutputProperties; |
| 34 | private URIResolver uriResolver; |
| 35 | private ErrorListener errorListener; |
| 36 | |
| 37 | protected IdentityTransformer(Configuration config) { |
| 38 | this.configuration = config; |
| 39 | this.uriResolver = config.getURIResolver(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * <p>Reset this <code>Transformer</code> to its original configuration.</p> |
| 44 | * <p><code>Transformer</code> is reset to the same state as when it was created with |
| 45 | * {@link javax.xml.transform.TransformerFactory#newTransformer()}, |
| 46 | * {@link javax.xml.transform.TransformerFactory#newTransformer(javax.xml.transform.Source source)} or |
| 47 | * {@link javax.xml.transform.Templates#newTransformer()}. |
| 48 | * <code>reset()</code> is designed to allow the reuse of existing <code>Transformer</code>s |
| 49 | * thus saving resources associated with the creation of new <code>Transformer</code>s.</p> |
| 50 | * <p>The reset <code>Transformer</code> is not guaranteed to have the same {@link javax.xml.transform.URIResolver} |
| 51 | * or {@link javax.xml.transform.ErrorListener} <code>Object</code>s, e.g. {@link Object#equals(Object obj)}. |
| 52 | * It is guaranteed to have a functionally equal <code>URIResolver</code> |
| 53 | * and <code>ErrorListener</code>.</p> |
| 54 | * |
| 55 | * @throws UnsupportedOperationException When implementation does not |
| 56 | * override this method. |
| 57 | * @since 1.5 |
| 58 | */ |
| 59 | @Override |
| 60 | public void reset() { |
| 61 | localOutputProperties = null; |
| 62 | uriResolver = getConfiguration().getURIResolver(); |
| 63 | errorListener = null; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Set an object that will be used to resolve URIs used in |
| 68 | * document(). |
| 69 | * <p>If the resolver argument is null, the URIResolver value will |
| 70 | * be cleared and the transformer will no longer have a resolver.</p> |
| 71 | * |
| 72 | * @param resolver An object that implements the URIResolver interface, |
| 73 | * or null. |
| 74 | */ |
| 75 | @Override |
| 76 | public void setURIResolver(URIResolver resolver) { |
| 77 | this.uriResolver = resolver; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get an object that will be used to resolve URIs used in |
| 82 | * document(). |
| 83 | * |
| 84 | * @return An object that implements the URIResolver interface, |
| 85 | * or null. |
| 86 | */ |
| 87 | @Override |
| 88 | public URIResolver getURIResolver() { |
| 89 | return uriResolver; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Set the error event listener in effect for the transformation. |
| 94 | * |
| 95 | * @param listener The new error listener. |
| 96 | * @throws IllegalArgumentException if listener is null. |
| 97 | */ |
| 98 | @Override |
| 99 | public void setErrorListener(ErrorListener listener) throws IllegalArgumentException { |
| 100 | this.errorListener = listener; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get the error event handler in effect for the transformation. |
| 105 | * Implementations must provide a default error listener. |
| 106 | * |
| 107 | * @return The current error handler, which should never be null. |
| 108 | */ |
| 109 | @Override |
| 110 | public ErrorListener getErrorListener() { |
| 111 | if (errorListener instanceof DelegatingErrorListener) { |
| 112 | return ((DelegatingErrorListener)errorListener).getBaseErrorListener(); |
| 113 | } |
| 114 | return errorListener; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Set the output properties for the transformation. These |
| 119 | * properties will override properties set in the Templates |
| 120 | * with xsl:output. |
| 121 | * <p>If argument to this function is null, any properties |
| 122 | * previously set are removed, and the value will revert to the value |
| 123 | * defined in the templates object.</p> |
| 124 | * <p>Pass a qualified property key name as a two-part string, the namespace |
| 125 | * URI enclosed in curly braces ({}), followed by the local name. If the |
| 126 | * name has a null URL, the String only contain the local name. An |
| 127 | * application can safely check for a non-null URI by testing to see if the |
| 128 | * first character of the name is a '{' character.</p> |
| 129 | * <p>For example, if a URI and local name were obtained from an element |
| 130 | * defined with <xyz:foo |
| 131 | * xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>, |
| 132 | * then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo". |
| 133 | * Note that no prefix is used.</p> |
| 134 | * <p>An <code>IllegalArgumentException</code> is thrown if any of the |
| 135 | * argument keys are not recognized and are not namespace qualified.</p> |
| 136 | * <p>As well as the properties defined in the JAXP OutputKeys class, |
| 137 | * Saxon defines an additional set of properties in {@link net.sf.saxon.lib.SaxonOutputKeys}. |
| 138 | * These fall into two categories: Constants representing serialization |
| 139 | * properties defined in XSLT 2.0 (which are not yet supported by JAXP), |
| 140 | * and constants supporting Saxon extensions to the set of serialization</p> |
| 141 | * properties. |
| 142 | * |
| 143 | * @param properties A set of output properties that will be |
| 144 | * used to override any of the same properties in affect |
| 145 | * for the transformation. |
| 146 | * @throws IllegalArgumentException When keys are not recognized and |
| 147 | * are not namespace qualified. |
| 148 | * @see javax.xml.transform.OutputKeys |
| 149 | * @see java.util.Properties |
| 150 | */ |
| 151 | @Override |
| 152 | |
| 153 | public void setOutputProperties(Properties properties) { |
| 154 | if (properties == null) { |
| 155 | localOutputProperties = null; |
| 156 | } else { |
| 157 | for (String key : properties.stringPropertyNames()) { |
| 158 | setOutputProperty(key, properties.getProperty(key)); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get the output properties for the transformation. |
| 165 | * <p>As well as the properties defined in the JAXP OutputKeys class, |
| 166 | * Saxon defines an additional set of properties in {@link net.sf.saxon.lib.SaxonOutputKeys}. |
| 167 | * These fall into two categories: Constants representing serialization |
| 168 | * properties defined in XSLT 2.0 (which are not yet supported by JAXP), |
| 169 | * and constants supporting Saxon extensions to the set of serialization |
| 170 | * properties.</p> |
| 171 | * |
| 172 | * @return the output properties being used for the transformation, |
| 173 | * including properties defined in the stylesheet for the unnamed |
| 174 | * output format |
| 175 | * @see net.sf.saxon.lib.SaxonOutputKeys |
| 176 | */ |
| 177 | |
| 178 | public Properties getOutputProperties() { |
| 179 | |
| 180 | // Make a copy, so that modifications to the returned properties object have no effect (even on the |
| 181 | // local output properties) |
| 182 | |
| 183 | Properties newProps = new Properties(); |
| 184 | Properties sheetProperties = getStylesheetOutputProperties(); |
| 185 | Enumeration keys = sheetProperties.propertyNames(); |
| 186 | while (keys.hasMoreElements()) { |
| 187 | String key = (String) keys.nextElement(); |
| 188 | newProps.setProperty(key, sheetProperties.getProperty(key)); |
| 189 | } |
| 190 | if (localOutputProperties != null) { |
| 191 | keys = localOutputProperties.propertyNames(); |
| 192 | while (keys.hasMoreElements()) { |
| 193 | String key = (String) keys.nextElement(); |
| 194 | newProps.setProperty(key, localOutputProperties.getProperty(key)); |
| 195 | } |
| 196 | } |
| 197 | return newProps; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Get the output properties defined in the stylesheet. For an identity transformer this is an empty set, |
| 202 | * but the method is overridden in subclasses. |
| 203 | * |
| 204 | * @return the serialization properties defined in the stylesheet, if any. |
| 205 | */ |
| 206 | |
| 207 | protected Properties getStylesheetOutputProperties() { |
| 208 | return new Properties(); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Get the local output properties held in this Transformer object, that is the properties |
| 213 | * explicitly requested using setOutputProperty() or setOutputProperties() |
| 214 | * |
| 215 | * @return the local output properties |
| 216 | */ |
| 217 | |
| 218 | protected Properties getLocalOutputProperties() { |
| 219 | if (localOutputProperties == null) { |
| 220 | makeLocalOutputProperties(); |
| 221 | } |
| 222 | return localOutputProperties; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Make the localOutputProperties object. This is a Properties object containing |
| 227 | * the properties set by calls on setOutputProperty or setOutputProperties; it does not include |
| 228 | * properties set in the stylesheet or in the configuration. |
| 229 | */ |
| 230 | |
| 231 | private void makeLocalOutputProperties() { |
| 232 | localOutputProperties = new Properties(); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * <p>Get an output property that is in effect for the transformer.</p> |
| 237 | * <p>If a property has been set using {@link #setOutputProperty}, |
| 238 | * that value will be returned. Otherwise, if a property is explicitly |
| 239 | * specified in the stylesheet, that value will be returned. If |
| 240 | * the value of the property has been defaulted, that is, if no |
| 241 | * value has been set explicitly either with {@link #setOutputProperty} or |
| 242 | * in the stylesheet, the result may vary depending on |
| 243 | * implementation and input stylesheet.</p> |
| 244 | * |
| 245 | * @param name A non-null String that specifies an output |
| 246 | * property name, which may be namespace qualified. |
| 247 | * @return The string value of the output property, or null |
| 248 | * if no property was found. |
| 249 | * @throws IllegalArgumentException If the property is not supported. |
| 250 | * @see javax.xml.transform.OutputKeys |
| 251 | */ |
| 252 | @Override |
| 253 | public String getOutputProperty(String name) throws IllegalArgumentException { |
| 254 | try { |
| 255 | getConfiguration().getSerializerFactory().checkOutputProperty(name, null); |
| 256 | } catch (XPathException err) { |
| 257 | throw new IllegalArgumentException(err.getMessage()); |
| 258 | } |
| 259 | String value = null; |
| 260 | if (localOutputProperties != null) { |
| 261 | value = localOutputProperties.getProperty(name); |
| 262 | } |
| 263 | if (value == null) { |
| 264 | value = getStylesheetOutputProperties().getProperty(name); |
| 265 | } |
| 266 | return value; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Set an output property that will be in effect for the |
| 271 | * transformation. |
| 272 | * <p>Pass a qualified property name as a two-part string, the namespace URI |
| 273 | * enclosed in curly braces ({}), followed by the local name. If the |
| 274 | * name has a null URL, the String only contain the local name. An |
| 275 | * application can safely check for a non-null URI by testing to see if the |
| 276 | * first character of the name is a '{' character.</p> |
| 277 | * <p>For example, if a URI and local name were obtained from an element |
| 278 | * defined with <xyz:foo |
| 279 | * xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>, |
| 280 | * then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo". |
| 281 | * Note that no prefix is used.</p> |
| 282 | * <p>The Properties object that was passed to {@link #setOutputProperties} |
| 283 | * won't be effected by calling this method.</p> |
| 284 | * |
| 285 | * @param name A non-null String that specifies an output |
| 286 | * property name, which may be namespace qualified. |
| 287 | * @param value The non-null string value of the output property. |
| 288 | * @throws IllegalArgumentException If the property is not supported, and is |
| 289 | * not qualified with a namespace. |
| 290 | * @see javax.xml.transform.OutputKeys |
| 291 | */ |
| 292 | @Override |
| 293 | public void setOutputProperty(String name, String value) throws IllegalArgumentException { |
| 294 | if (localOutputProperties == null) { |
| 295 | makeLocalOutputProperties(); |
| 296 | } |
| 297 | try { |
| 298 | value = getConfiguration().getSerializerFactory().checkOutputProperty(name, value); |
| 299 | } catch (XPathException err) { |
| 300 | throw new IllegalArgumentException(err.getMessage()); |
| 301 | } |
| 302 | localOutputProperties.setProperty(name, value); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Add a parameter for the transformation. |
| 307 | * <p>Pass a qualified name as a two-part string, the namespace URI |
| 308 | * enclosed in curly braces ({}), followed by the local name. If the |
| 309 | * name has a null URL, the String only contain the local name. An |
| 310 | * application can safely check for a non-null URI by testing to see if the |
| 311 | * first character of the name is a '{' character.</p> |
| 312 | * <p>For example, if a URI and local name were obtained from an element |
| 313 | * defined with <xyz:foo |
| 314 | * xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>, |
| 315 | * then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo". |
| 316 | * Note that no prefix is used.</p> |
| 317 | * |
| 318 | * @param name The name of the parameter, which may begin with a |
| 319 | * namespace URI in curly braces ({}). |
| 320 | * @param value The value object. This can be any valid Java object. It is |
| 321 | * up to the processor to provide the proper object coersion or to simply |
| 322 | * pass the object on for use in an extension. |
| 323 | * @throws NullPointerException If value is null. |
| 324 | */ |
| 325 | @Override |
| 326 | public void setParameter(String name, Object value) { |
| 327 | // No action. Since parameters have no effect on the transformation, we ignore them |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Get a parameter that was explicitly set with setParameter. |
| 332 | * <p>This method does not return a default parameter value, which |
| 333 | * cannot be determined until the node context is evaluated during |
| 334 | * the transformation process. |
| 335 | * <p>The Saxon implementation for an IdentityTransformer always |
| 336 | * returns null, since parameters have no effect on an identity |
| 337 | * transformation.</p> |
| 338 | * |
| 339 | * @param name of <code>Object</code> to get |
| 340 | * @return A parameter that has been set with setParameter. |
| 341 | */ |
| 342 | @Override |
| 343 | public Object getParameter(String name) { |
| 344 | return null; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Clear all parameters set with setParameter. |
| 349 | */ |
| 350 | @Override |
| 351 | public void clearParameters() { |
| 352 | // No action |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Perform identify transformation from Source to Result |
| 357 | */ |
| 358 | |
| 359 | public void transform(Source source, Result result) |
| 360 | throws TransformerException { |
| 361 | try { |
| 362 | SerializerFactory sf = getConfiguration().getSerializerFactory(); |
| 363 | Receiver receiver = sf.getReceiver(result, new SerializationProperties(getOutputProperties())); |
| 364 | ParseOptions options = receiver.getPipelineConfiguration().getParseOptions(); |
| 365 | options.setContinueAfterValidationErrors(true); |
| 366 | Sender.send(source, receiver, options); |
| 367 | } catch (XPathException err) { |
| 368 | Throwable cause = err.getException(); |
| 369 | if (cause instanceof SAXParseException) { |
| 370 | // This generally means the error was already reported. |
| 371 | // But if a RuntimeException occurs in Saxon during a callback from |
| 372 | // the Crimson parser, Crimson wraps this in a SAXParseException without |
| 373 | // reporting it further. |
| 374 | SAXParseException spe = (SAXParseException) cause; |
| 375 | cause = spe.getException(); |
| 376 | if (cause instanceof RuntimeException) { |
| 377 | reportFatalError(err); |
| 378 | } |
| 379 | } else { |
| 380 | reportFatalError(err); |
| 381 | } |
| 382 | throw err; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | public Configuration getConfiguration() { |
| 387 | return configuration; |
| 388 | } |
| 389 | |
| 390 | protected void reportFatalError(XPathException err) { |
| 391 | try { |
| 392 | if (errorListener != null) { |
| 393 | errorListener.error(err); |
| 394 | } else { |
| 395 | getConfiguration().getErrorListener().error(err); |
| 396 | } |
| 397 | } catch (TransformerException e) { |
| 398 | // no action if error reporting fails |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | } |
| 403 |