| 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 | |
| 9 | package net.sf.saxon.serialize; |
| 10 | |
| 11 | import net.sf.saxon.event.*; |
| 12 | import net.sf.saxon.lib.SaxonOutputKeys; |
| 13 | |
| 14 | import javax.xml.transform.OutputKeys; |
| 15 | import java.util.Properties; |
| 16 | |
| 17 | /** |
| 18 | * Define a set of serialization properties. These may contain simple output properties such as {@code method} |
| 19 | * or {@code indent}, Saxon extensions such as {@code saxon:indent-spaces}, and may also contain an index |
| 20 | * of character maps. |
| 21 | */ |
| 22 | |
| 23 | public class SerializationProperties { |
| 24 | |
| 25 | Properties properties; |
| 26 | CharacterMapIndex charMapIndex; |
| 27 | FilterFactory validationFactory; |
| 28 | |
| 29 | /** |
| 30 | * Create a set of defaulted serialization parameters |
| 31 | */ |
| 32 | |
| 33 | public SerializationProperties() { |
| 34 | this.properties = new Properties(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Create a set of serialization parameters based on defined output properties, |
| 39 | * with no character maps |
| 40 | */ |
| 41 | |
| 42 | public SerializationProperties(Properties props) { |
| 43 | this.properties = props; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Create a set of serialization parameters based on defined output properties, |
| 48 | * with an index of named character maps that may be referred to from the |
| 49 | * {@code USE_CHARACTER_MAPS} property |
| 50 | */ |
| 51 | |
| 52 | public SerializationProperties(Properties props, CharacterMapIndex charMapIndex) { |
| 53 | this.properties = props; |
| 54 | this.charMapIndex = charMapIndex; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Set the value of a serialization property. |
| 59 | * @param name the property name in Clark notation (if this is constant, the available |
| 60 | * constants are defined in classes {@link OutputKeys} and {@link SaxonOutputKeys}). |
| 61 | * @param value the property value. In the case of QName-valued properties, such as |
| 62 | * {@code cdata-section-elements}, these should be in Clark notation |
| 63 | */ |
| 64 | |
| 65 | public void setProperty(String name, String value) { |
| 66 | properties.setProperty(name, value); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the value of a serialization property |
| 71 | * @param name the property name in Clark notation (if this is constant, the available |
| 72 | * constants are defined in classes {@link OutputKeys} and {@link SaxonOutputKeys}). |
| 73 | * @return the property value. In the case of QName-valued properties, such as |
| 74 | * {@code cdata-section-elements}, these should be in Clark notation |
| 75 | */ |
| 76 | |
| 77 | public String getProperty(String name) { |
| 78 | return (String)getProperties().get(name); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get the simple output properties defined in these serialization parameters. The property |
| 83 | * names will be in clark-name format, for example {@code "indent"} or |
| 84 | * {@code "{http://saxon.sf.net/}indent-spaces"} |
| 85 | * @return the output properties |
| 86 | */ |
| 87 | |
| 88 | public Properties getProperties() { |
| 89 | return properties; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the character map index, if any |
| 94 | * @return the character map index, if one is present, otherwise null |
| 95 | */ |
| 96 | |
| 97 | public CharacterMapIndex getCharacterMapIndex() { |
| 98 | return charMapIndex; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Although validation is not normally part of serialization, the {@code xsl:result-document} |
| 103 | * instruction allows a validator to be inserted into the serialization pipeline. This is achieved |
| 104 | * by adding a request to insert a validation stage to the serialization parameters. The request |
| 105 | * is in the form of a factory function that constructs the required validator |
| 106 | * @param validationFactory a function that inserts a validator into a {@code Receiver} pipeline |
| 107 | */ |
| 108 | |
| 109 | public void setValidationFactory(FilterFactory validationFactory) { |
| 110 | this.validationFactory = validationFactory; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get any validation factory that was added to the serialization parameters using |
| 115 | * {@link #setValidationFactory(FilterFactory)} |
| 116 | * @return the validation factory, or null if there is none. |
| 117 | */ |
| 118 | |
| 119 | public FilterFactory getValidationFactory() { |
| 120 | return validationFactory; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Convenience method to create an appropriate SequenceNormalizer, based |
| 125 | * on the item-separator appearing in the serialization parameters. If the |
| 126 | * serialization parameters include a request for validation, then a validator |
| 127 | * will also be inserted into the pipeline immediately after the SequenceNormalizer, |
| 128 | * as required by the rules for {@code xsl:result-document}. |
| 129 | * @param next the next {@code Receiver in the pipeline} |
| 130 | * @return the new {@code SequenceNormalizer}, feeding into the supplied {@code Receiver}, |
| 131 | * possibly via a new validating filter. |
| 132 | */ |
| 133 | |
| 134 | public SequenceNormalizer makeSequenceNormalizer(Receiver next) { |
| 135 | if (getValidationFactory() != null) { |
| 136 | next = getValidationFactory().makeFilter(next); |
| 137 | } |
| 138 | String itemSeparator = properties.getProperty(SaxonOutputKeys.ITEM_SEPARATOR); |
| 139 | return itemSeparator == null || "#absent".equals(itemSeparator) |
| 140 | ? new SequenceNormalizerWithSpaceSeparator(next) |
| 141 | : new SequenceNormalizerWithItemSeparator(next, itemSeparator); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Combine these serialization parameters with a set of default serialization |
| 146 | * parameters to create a new set of serialization parameters. Neither of the |
| 147 | * input parameter sets is modified |
| 148 | * @param defaults the parameters to use when no explicit values are supplied |
| 149 | */ |
| 150 | |
| 151 | public SerializationProperties combineWith(SerializationProperties defaults) { |
| 152 | CharacterMapIndex charMap = this.charMapIndex; |
| 153 | if (charMap == null || charMap.isEmpty()) { |
| 154 | charMap = defaults.getCharacterMapIndex(); |
| 155 | } |
| 156 | FilterFactory validationFactory = this.validationFactory; |
| 157 | if (validationFactory == null) { |
| 158 | validationFactory = defaults.validationFactory; |
| 159 | } |
| 160 | Properties props = new Properties(defaults.getProperties()); |
| 161 | for (String prop : this.getProperties().stringPropertyNames()) { |
| 162 | String value = this.getProperties().getProperty(prop); |
| 163 | if (prop.equals(OutputKeys.CDATA_SECTION_ELEMENTS) |
| 164 | || prop.equals(SaxonOutputKeys.SUPPRESS_INDENTATION)) { |
| 165 | String existing = defaults.getProperty(prop); |
| 166 | if (existing == null || existing.equals(value)) { |
| 167 | props.setProperty(prop, value); |
| 168 | } else { |
| 169 | props.setProperty(prop, existing + " " + value); |
| 170 | } |
| 171 | } else { |
| 172 | props.setProperty(prop, value); |
| 173 | } |
| 174 | } |
| 175 | SerializationProperties newParams = new SerializationProperties(props, charMap); |
| 176 | newParams.setValidationFactory(validationFactory); |
| 177 | return newParams; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | } |
| 182 |