| 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.lib; |
| 9 | |
| 10 | import net.sf.saxon.om.NameChecker; |
| 11 | import net.sf.saxon.om.NamespaceResolver; |
| 12 | import net.sf.saxon.om.QNameException; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.type.ValidationException; |
| 15 | import net.sf.saxon.value.BigDecimalValue; |
| 16 | |
| 17 | import javax.xml.transform.OutputKeys; |
| 18 | import java.math.BigDecimal; |
| 19 | import java.util.Properties; |
| 20 | import java.util.StringTokenizer; |
| 21 | import java.util.regex.Pattern; |
| 22 | |
| 23 | /** |
| 24 | * Provides string constants that can be used to set |
| 25 | * output properties for a Transformer, or to retrieve |
| 26 | * output properties from a Transformer or Templates object. |
| 27 | * <p>These keys are private Saxon keys that supplement the standard keys |
| 28 | * defined in javax.xml.transform.OutputKeys. As well as Saxon extension |
| 29 | * attributes, the list includes new attributes defined in XSLT 2.0 which |
| 30 | * are not yet supported in JAXP</p> |
| 31 | */ |
| 32 | |
| 33 | public class SaxonOutputKeys { |
| 34 | |
| 35 | /** |
| 36 | * This class is not instantiated |
| 37 | */ |
| 38 | |
| 39 | private SaxonOutputKeys() { |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * String constant representing the saxon:xquery output method name |
| 44 | */ |
| 45 | |
| 46 | /*@NotNull*/ public static final String SAXON_XQUERY_METHOD = "{http://saxon.sf.net/}xquery"; |
| 47 | |
| 48 | /** |
| 49 | * String constant representing the saxon:base64Binary output method name |
| 50 | */ |
| 51 | |
| 52 | /*@NotNull*/ public static final String SAXON_BASE64_BINARY_METHOD = "{http://saxon.sf.net/}base64Binary"; |
| 53 | |
| 54 | /** |
| 55 | * String constant representing the saxon:hexBinary output method name |
| 56 | */ |
| 57 | |
| 58 | /*@NotNull*/ public static final String SAXON_HEX_BINARY_METHOD = "{http://saxon.sf.net/}hexBinary"; |
| 59 | |
| 60 | /** |
| 61 | * String constant representing the saxon:ptree output method name |
| 62 | */ |
| 63 | |
| 64 | /*@NotNull*/ public static final String SAXON_PTREE_METHOD = "{http://saxon.sf.net/}ptree"; |
| 65 | |
| 66 | /** |
| 67 | * allow-duplicate-names = yes|no. |
| 68 | * <p>Defines whether duplicate keys are allowed in a JSON map (new in 3.1)</p> |
| 69 | */ |
| 70 | |
| 71 | /*@NotNull*/ public static final String ALLOW_DUPLICATE_NAMES = "allow-duplicate-names"; |
| 72 | |
| 73 | /** |
| 74 | * build-tree = yes|no. |
| 75 | * <p>Defines whether the raw output is used to build an XML document tree</p> |
| 76 | */ |
| 77 | |
| 78 | /*@NotNull*/ public static final String BUILD_TREE = "build-tree"; |
| 79 | |
| 80 | /** |
| 81 | * saxon:indent-spaces = integer. |
| 82 | * <p>Defines the number of spaces used for indentation of output</p> |
| 83 | */ |
| 84 | |
| 85 | /*@NotNull*/ public static final String INDENT_SPACES = "{http://saxon.sf.net/}indent-spaces"; |
| 86 | |
| 87 | /** |
| 88 | * saxon:line-length = integer. |
| 89 | * <p>Defines the desired maximum line length used when indenting output</p> |
| 90 | */ |
| 91 | |
| 92 | /*@NotNull*/ public static final String LINE_LENGTH = "{http://saxon.sf.net/}line-length"; |
| 93 | |
| 94 | /** |
| 95 | * saxon:single-quotes = boolean. |
| 96 | * <p>Indicates that attributes should be delimited by apostrophes rather than quotation marks</p> |
| 97 | */ |
| 98 | |
| 99 | /*@NotNull*/ public static final String SINGLE_QUOTES = "{http://saxon.sf.net/}single-quotes"; |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * suppress-indentation = list of element names |
| 104 | * <p>Defines elements within which no indentation will occur</p> |
| 105 | */ |
| 106 | |
| 107 | /*@NotNull*/ public static final String SUPPRESS_INDENTATION = "suppress-indentation"; |
| 108 | |
| 109 | /** |
| 110 | * html-version = decimal |
| 111 | * <p>Defines the version of HTML. For the XHTML output method this allows separate |
| 112 | * specification of the XHTML version and the XML version. This is a new serialization |
| 113 | * parameter in the draft 3.0 specification.</p> |
| 114 | */ |
| 115 | |
| 116 | public static final String HTML_VERSION = "html-version"; |
| 117 | |
| 118 | /** |
| 119 | * item-separator = string |
| 120 | * <p>Relevant to XQuery, where an arbitrary sequence can be serialized; defines a separator |
| 121 | * to be inserted between successive items in the sequence.</p> |
| 122 | */ |
| 123 | |
| 124 | public static final String ITEM_SEPARATOR = "item-separator"; |
| 125 | |
| 126 | /** |
| 127 | * json-node-output-method = method-name |
| 128 | * <p>Defines the serialization method for nodes encountered while serializing as JSON</p> |
| 129 | */ |
| 130 | |
| 131 | /*@NotNull*/ public static final String JSON_NODE_OUTPUT_METHOD = "json-node-output-method"; |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * saxon:attribute-order = list of attribute names |
| 136 | * <p>Defines an ordering for attributes in the serialized output. Any attribute present in the list |
| 137 | * will appear correctly ordered according to the list; other attributes will be ordered first by namespace, |
| 138 | * then by local name.</p> |
| 139 | */ |
| 140 | |
| 141 | /*@NotNull*/ public static final String ATTRIBUTE_ORDER = "{http://saxon.sf.net/}attribute-order"; |
| 142 | |
| 143 | /** |
| 144 | * saxon:canonical = yes/no |
| 145 | * <p>When used in conjunction with the XML output method, delivers the output in C14N canonical form. |
| 146 | * Any serialization properties inconsistent with C14N (for example, encoding, indent, or character maps) are |
| 147 | * ignored if saxon:canonical=yes is specified. Provisions of the W3C serialization specification |
| 148 | * (for example, requiring the use of empty element tags) are also ignored.</p> |
| 149 | * @since 9.9 |
| 150 | */ |
| 151 | |
| 152 | /*@NotNull*/ public static final String CANONICAL = "{http://saxon.sf.net/}canonical"; |
| 153 | |
| 154 | /** |
| 155 | * saxon:property-order = list of strings |
| 156 | * <p>Defines an ordering for properties in the serialized JSON output of a map. Any property present in the list |
| 157 | * will appear correctly ordered according to the list; other attributes will be ordered by name.</p> |
| 158 | */ |
| 159 | |
| 160 | /*@NotNull*/ public static final String PROPERTY_ORDER = "{http://saxon.sf.net/}property-order"; |
| 161 | |
| 162 | /** |
| 163 | * saxon:double-space = list of element names |
| 164 | * <p>Defines elements that will have an extra blank line added before the start tag, in addition |
| 165 | * to normal indentation</p> |
| 166 | */ |
| 167 | |
| 168 | /*@NotNull*/ public static final String DOUBLE_SPACE = "{http://saxon.sf.net/}double-space"; |
| 169 | |
| 170 | /** |
| 171 | * saxon:newline = string |
| 172 | * <p>Defines the sequence of characters used to represent a newline when using the text |
| 173 | * output method</p> |
| 174 | */ |
| 175 | |
| 176 | /*@NotNull*/ public static final String NEWLINE = "{http://saxon.sf.net/}newline"; |
| 177 | |
| 178 | /** |
| 179 | * stylesheet-version. This serialization parameter is set automatically by the XSLT processor |
| 180 | * to the value of the version attribute on the principal stylesheet module. |
| 181 | */ |
| 182 | |
| 183 | /*@NotNull*/ public static final String STYLESHEET_VERSION = "{http://saxon.sf.net/}stylesheet-version"; |
| 184 | |
| 185 | /** |
| 186 | * use-character-map = list-of-qnames. |
| 187 | * <p>Defines the character maps used in this output definition. The QNames |
| 188 | * are represented in Clark notation as {uri}local-name.</p> |
| 189 | */ |
| 190 | |
| 191 | /*@NotNull*/ public static final String USE_CHARACTER_MAPS = "use-character-maps"; |
| 192 | |
| 193 | |
| 194 | /** |
| 195 | * include-content-type = "yes" | "no". This attribute is defined in XSLT 2.0 |
| 196 | * <p>Indicates whether the META tag is to be added to HTML output</p> |
| 197 | */ |
| 198 | |
| 199 | /*@NotNull*/ public static final String INCLUDE_CONTENT_TYPE = "include-content-type"; |
| 200 | |
| 201 | /** |
| 202 | * undeclare-prefixes = "yes" | "no". This attribute is defined in XSLT 2.0 |
| 203 | * <p>Indicates XML 1.1 namespace undeclarations are to be output when required</p> |
| 204 | */ |
| 205 | |
| 206 | /*@NotNull*/ public static final String UNDECLARE_PREFIXES = "undeclare-prefixes"; |
| 207 | |
| 208 | /** |
| 209 | * escape-uri-attributes = "yes" | "no". This attribute is defined in XSLT 2.0 |
| 210 | * <p>Indicates whether HTML attributes of type URI are to be URI-escaped</p> |
| 211 | */ |
| 212 | |
| 213 | /*@NotNull*/ public static final String ESCAPE_URI_ATTRIBUTES = "escape-uri-attributes"; |
| 214 | |
| 215 | /** |
| 216 | * representation = rep1[;rep2]. |
| 217 | * <p>Indicates the preferred way of representing non-ASCII characters in HTML |
| 218 | * and XML output. rep1 is for characters in the range 128-256, rep2 for those |
| 219 | * above 256.</p> |
| 220 | */ |
| 221 | /*@NotNull*/ public static final String CHARACTER_REPRESENTATION = "{http://saxon.sf.net/}character-representation"; |
| 222 | |
| 223 | /** |
| 224 | * saxon:next-in-chain = URI. |
| 225 | * <p>Indicates that the output is to be piped into another XSLT stylesheet |
| 226 | * to perform another transformation. The auxiliary property NEXT_IN_CHAIN_BASE_URI |
| 227 | * records the base URI of the stylesheet element where this attribute was found.</p> |
| 228 | */ |
| 229 | /*@NotNull*/ public static final String NEXT_IN_CHAIN = "{http://saxon.sf.net/}next-in-chain"; |
| 230 | /*@NotNull*/ public static final String NEXT_IN_CHAIN_BASE_URI = "{http://saxon.sf.net/}next-in-chain-base-uri"; |
| 231 | |
| 232 | /** |
| 233 | * parameter-document = URI. |
| 234 | * <p>Indicates that the output is to be piped into another XSLT stylesheet |
| 235 | * to perform another transformation. The auxiliary property NEXT_IN_CHAIN_BASE_URI |
| 236 | * records the base URI of the stylesheet element where this attribute was found.</p> |
| 237 | */ |
| 238 | /*@NotNull*/ public static final String PARAMETER_DOCUMENT = "parameter-document"; |
| 239 | /*@NotNull*/ public static final String PARAMETER_DOCUMENT_BASE_URI = "{http://saxon.sf.net/}parameter-document-base-uri"; |
| 240 | |
| 241 | |
| 242 | /** |
| 243 | * byte-order-mark = yes|no. |
| 244 | * <p>Indicates whether UTF-8/UTF-16 output is to start with a byte order mark. Values are "yes" or "no", |
| 245 | * default is "no" |
| 246 | */ |
| 247 | |
| 248 | /*@NotNull*/ public static final String BYTE_ORDER_MARK = "byte-order-mark"; |
| 249 | |
| 250 | /** |
| 251 | * normalization-form = NFC|NFD|NFKC|NFKD|non. |
| 252 | * <p>Indicates that a given Unicode normalization form (or no normalization) is required. |
| 253 | */ |
| 254 | |
| 255 | /*@NotNull*/ public static final String NORMALIZATION_FORM = "normalization-form"; |
| 256 | |
| 257 | /** |
| 258 | * recognize-binary = yes|no. |
| 259 | * <p>If set to "yes", and the output is being written using output method "text", Saxon will recognize |
| 260 | * two processing instructions <?hex XXXX?> and <b64 XXXX?> containing binary data encoded |
| 261 | * as a hexBinary or base64 string respectively. The corresponding strings will be decoded as characters |
| 262 | * in the encoding being used for the output file, and will be written out to the output without checking |
| 263 | * that they represent valid XML strings.</p> |
| 264 | */ |
| 265 | |
| 266 | /*@NotNull*/ public static final String RECOGNIZE_BINARY = "{http://saxon.sf.net/}recognize-binary"; |
| 267 | |
| 268 | /** |
| 269 | * saxon:require-well-formed = yes|no. |
| 270 | * <p>Indicates whether a user-supplied ContentHandler requires the stream of SAX events to be |
| 271 | * well-formed (that is, to have a single element node and no text nodes as children of the root). |
| 272 | * The default is "no".</p> |
| 273 | */ |
| 274 | |
| 275 | /*@NotNull*/ public static final String REQUIRE_WELL_FORMED = "{http://saxon.sf.net/}require-well-formed"; |
| 276 | |
| 277 | /** |
| 278 | * supply-source-locator = yes|no. |
| 279 | * <p>If set to "yes", and the output is being sent to a SAXResult (or to a user-supplied content handler), |
| 280 | * indicates that the SAX Locator made available to the ContentHandler will contain information about the |
| 281 | * location of the context node in the source document as well as the location in the stylesheet or query.</p> |
| 282 | */ |
| 283 | |
| 284 | /*@NotNull*/ public static final String SUPPLY_SOURCE_LOCATOR = "{http://saxon.sf.net/}supply-source-locator"; |
| 285 | |
| 286 | /** |
| 287 | * wrap="yes"|"no". |
| 288 | * <p>This property is only available in the XQuery API. The value "yes" indicates that the result |
| 289 | * sequence produced by the query is to be wrapped, that is, each item in the result is represented |
| 290 | * as a separate element. This format allows any sequence to be represented as an XML document, |
| 291 | * including for example sequences consisting of parentless attribute nodes.</p> |
| 292 | */ |
| 293 | |
| 294 | /*@NotNull*/ public static final String WRAP = "{http://saxon.sf.net/}wrap-result-sequence"; |
| 295 | |
| 296 | /** |
| 297 | * Property used internally to identify the XSLT implicit result document |
| 298 | */ |
| 299 | |
| 300 | /*@NotNull*/ public static final String IMPLICIT_RESULT_DOCUMENT = "{http://saxon.sf.net/}implicit-result-document"; |
| 301 | |
| 302 | /** |
| 303 | * Property saxon:unfailing used to indicate that serialization should not fail. This is used when a serialization method such |
| 304 | * as XML or JSON is invoked from the ADAPTIVE serialization method, and it tailors the error-handling behaviour |
| 305 | * of the subsidiary output method. There is nothing to stop the property being set directly by the user, though |
| 306 | * (at least in the XSLT case) the resulting behaviour is non-conformant. The values of the property are yes/no. |
| 307 | */ |
| 308 | |
| 309 | public static final String UNFAILING = "{http://saxon.sf.net/}unfailing"; |
| 310 | |
| 311 | private static Pattern publicIdPattern = Pattern.compile("^[\\s\\r\\na-zA-Z0-9\\-'()+,./:=?;!*#@$_%]*$"); |
| 312 | |
| 313 | /** |
| 314 | * Process a serialization property whose value is a list of element names, for example cdata-section-elements |
| 315 | * |
| 316 | * |
| 317 | * @param value The value of the property as written |
| 318 | * @param nsResolver The namespace resolver to use; may be null if prevalidated is set or if names are supplied |
| 319 | * in Clark format |
| 320 | * @param useDefaultNS True if unprefixed names are to be treated as being in the default namespace |
| 321 | * @param prevalidated true if the property has already been validated |
| 322 | * @param allowStar true if the pseudo-name "*" is permitted; it will be retained in the output |
| 323 | *@param errorCode The error code to return in the event of problems @return The list of element names with lexical QNames replaced by Clark names, starting with a single space |
| 324 | * @throws XPathException if any error is found in the list of element names, for example, an undeclared namespace prefix |
| 325 | */ |
| 326 | |
| 327 | /*@NotNull*/ |
| 328 | public static String parseListOfNodeNames( |
| 329 | String value, /*@Nullable*/ NamespaceResolver nsResolver, boolean useDefaultNS, boolean prevalidated, /*@NotNull*/ boolean allowStar, String errorCode) |
| 330 | throws XPathException { |
| 331 | StringBuilder s = new StringBuilder(); |
| 332 | StringTokenizer st = new StringTokenizer(value, " \t\n\r", false); |
| 333 | while (st.hasMoreTokens()) { |
| 334 | String displayname = st.nextToken(); |
| 335 | if (allowStar && "*".equals(displayname)) { |
| 336 | s.append(' ').append(displayname); |
| 337 | } else if (prevalidated || (nsResolver == null)) { |
| 338 | s.append(' ').append(displayname); |
| 339 | } else if (displayname.startsWith("Q{")) { |
| 340 | s.append(' ').append(displayname.substring(1)); |
| 341 | } else { |
| 342 | try { |
| 343 | String[] parts = NameChecker.getQNameParts(displayname); |
| 344 | String muri = nsResolver.getURIForPrefix(parts[0], useDefaultNS); |
| 345 | if (muri == null) { |
| 346 | throw new XPathException("Namespace prefix '" + parts[0] + "' has not been declared", errorCode); |
| 347 | } |
| 348 | s.append(" {").append(muri).append('}').append(parts[1]); |
| 349 | } catch (QNameException err) { |
| 350 | throw new XPathException("Invalid QName. " + err.getMessage(), errorCode); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | return s.toString(); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Ask whether a particular serialization property is to be considered as string-valued, |
| 359 | * in which case the value is used exactly as specified without any whitespace stripping. |
| 360 | * |
| 361 | * <p>The logic here is a little pragmatic.</p> |
| 362 | * <p>For XSLT (xsl:output and xsl:result-document) the properties doctype-system, doctype-public, |
| 363 | * item-separator, and media-type have string type and whitespace should therefore be retained.</p> |
| 364 | * <p>For values in a parameter document, doctype-system is not whitespace stripped (and is restricted |
| 365 | * by a pattern); doctype-public is whitespace-stripped; media-type is whitespace-stripped</p> |
| 366 | * <p>For values in an fn:serialize parameter map, doctype-system, doctype-public, encoding, |
| 367 | * item-separator, media-type, and version are non-stripped strings</p> |
| 368 | * <p>XQuery output declarations follow the rules for parameter documents</p> |
| 369 | * <p>Pragmatically, it makes sense to be consistent. Including whitespace in doctype-system, |
| 370 | * doctype-public, encoding, version, or media-type is never useful. So we apply |
| 371 | * whitespace-stripping to all properties other than item-separator.</p> |
| 372 | * |
| 373 | * @param key the property name, in Clark notation |
| 374 | * @return true if the property is retained as written without whitespace stripping |
| 375 | */ |
| 376 | public static boolean isUnstrippedProperty(String key) { |
| 377 | return ITEM_SEPARATOR.equals(key) || NEWLINE.equals(key); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Examine the already-validated properties to see whether the html-version property is present |
| 382 | * with the decimal value 5.; used to decide whether to produce XHTML 5.0 in the XHTML output |
| 383 | * method. |
| 384 | * |
| 385 | * @param properties the properties to be examined |
| 386 | * @return true if the properties include html-version="5.0". The property is a decimal value, so |
| 387 | * it can also be written, for example, "5" or "+5.00". |
| 388 | */ |
| 389 | public static boolean isXhtmlHtmlVersion5(Properties properties) { |
| 390 | String htmlVersion = properties.getProperty(SaxonOutputKeys.HTML_VERSION); |
| 391 | try { |
| 392 | return htmlVersion != null && |
| 393 | ((BigDecimalValue)BigDecimalValue.makeDecimalValue(htmlVersion, false).asAtomic()) |
| 394 | .getDecimalValue().equals(BigDecimal.valueOf(5)); |
| 395 | } catch (ValidationException e) { |
| 396 | return false; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Examine the already-validated properties to see whether the html-version property is present |
| 402 | * with the decimal value 5.0, or if absent, the version property is present with the value 5.0. |
| 403 | * Used to decide whether to produce HTML5 output in the HTML output method. |
| 404 | * |
| 405 | * @param properties the properties to be examined |
| 406 | * @return true if the properties include html-version="5.0". The property is a decimal value, so |
| 407 | * it can also be written, for example, "5" or "+5.00". |
| 408 | */ |
| 409 | public static boolean isHtmlVersion5(Properties properties) { |
| 410 | String htmlVersion = properties.getProperty(SaxonOutputKeys.HTML_VERSION); |
| 411 | if (htmlVersion == null) { |
| 412 | htmlVersion = properties.getProperty(OutputKeys.VERSION); |
| 413 | } |
| 414 | if (htmlVersion != null) { |
| 415 | try { |
| 416 | return ((BigDecimalValue)BigDecimalValue.makeDecimalValue(htmlVersion, false).asAtomic()) |
| 417 | .getDecimalValue().equals(BigDecimal.valueOf(5)); |
| 418 | } catch (ValidationException e) { |
| 419 | return false; |
| 420 | } |
| 421 | } else { |
| 422 | return false; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | public static boolean isBuildTree(Properties properties) { |
| 427 | String buildTreeProperty = properties.getProperty("build-tree"); |
| 428 | if (buildTreeProperty != null) { |
| 429 | return "yes".equals(buildTreeProperty); |
| 430 | } |
| 431 | String method = properties.getProperty("method"); |
| 432 | return !("json".equals(method) || "adaptive".equals(method)); |
| 433 | } |
| 434 | } |
| 435 |