|
31 | 31 | public class XmlGeneratorInitializer |
32 | 32 | implements GeneratorInitializer |
33 | 33 | { |
| 34 | + /** |
| 35 | + * Prolog Directives to pass for generator to write. |
| 36 | + */ |
34 | 37 | protected List<PrologDirective> _directives; |
35 | 38 |
|
| 39 | + /** |
| 40 | + * Namespace bindings (prefix to URI) to register with generator. |
| 41 | + */ |
| 42 | + protected List<NamespaceBinding> _namespaceBindings; |
| 43 | + |
36 | 44 | protected boolean _addLfBetweenPrologDirectives = true; |
37 | 45 |
|
38 | 46 | protected boolean _hasDTD; |
39 | 47 |
|
40 | 48 | @Override |
41 | 49 | public void initialize(SerializationConfig config, JsonGenerator g) throws JacksonException { |
42 | 50 | if (g instanceof ToXmlGenerator xg) { |
43 | | - xg.initProlog(_addLfBetweenPrologDirectives, _directives); |
| 51 | + xg.initDocument(_addLfBetweenPrologDirectives, _directives, |
| 52 | + _namespaceBindings); |
44 | 53 | } |
45 | 54 | } |
46 | 55 |
|
@@ -124,6 +133,39 @@ public XmlGeneratorInitializer addPI(String target, String data) { |
124 | 133 | return _add(new PrologPI(target, data)); |
125 | 134 | } |
126 | 135 |
|
| 136 | + /** |
| 137 | + * Method for specifying namespace URI to preferentially bind to the |
| 138 | + * "default namespace" (one used when element has no prefix). |
| 139 | + * This will guide underlying generator to add necessary |
| 140 | + * declarations when actually writing elements with matching |
| 141 | + * namespace URI. |
| 142 | + * |
| 143 | + * @param namespaceURI URI of the default namespace |
| 144 | + * |
| 145 | + * @return This initializer for call chaining |
| 146 | + */ |
| 147 | + public XmlGeneratorInitializer addDefaultNamespace(String namespaceURI) { |
| 148 | + return addNamespace(null, namespaceURI); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Method for adding a mapping (binding) between given prefix and matching |
| 153 | + * namespace URI. This will guide underlying generator to add necessary |
| 154 | + * declarations when actually writing namespaced attributes and elements. |
| 155 | + * |
| 156 | + * @param prefix Prefix to use for namespace |
| 157 | + * @param namespaceURI URI of the namespace |
| 158 | + * |
| 159 | + * @return This initializer for call chaining |
| 160 | + */ |
| 161 | + public XmlGeneratorInitializer addNamespace(String prefix, String namespaceURI) { |
| 162 | + if (_namespaceBindings == null) { |
| 163 | + _namespaceBindings = new ArrayList<>(); |
| 164 | + } |
| 165 | + _namespaceBindings.add(new NamespaceBinding(prefix, namespaceURI)); |
| 166 | + return this; |
| 167 | + } |
| 168 | + |
127 | 169 | protected XmlGeneratorInitializer _add(PrologDirective d) { |
128 | 170 | if (_directives == null) { |
129 | 171 | _directives = new ArrayList<>(); |
|
0 commit comments