21
21
import java .io .OutputStreamWriter ;
22
22
import java .io .Reader ;
23
23
import java .io .Writer ;
24
+ import java .util .Iterator ;
25
+ import java .util .Properties ;
24
26
import javax .xml .stream .XMLEventReader ;
25
27
import javax .xml .stream .XMLEventWriter ;
26
28
import javax .xml .stream .XMLStreamReader ;
@@ -95,6 +97,8 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
95
97
96
98
private boolean ignoreExtraElements = false ;
97
99
100
+ private Properties namespaceMappings ;
101
+
98
102
/** Returns whether the Castor {@link Unmarshaller} should ignore attributes that do not match a specific field. */
99
103
public boolean getIgnoreExtraAttributes () {
100
104
return ignoreExtraAttributes ;
@@ -103,6 +107,8 @@ public boolean getIgnoreExtraAttributes() {
103
107
/**
104
108
* Sets whether the Castor {@link Unmarshaller} should ignore attributes that do not match a specific field.
105
109
* Default is <code>true</code>: extra attributes are ignored.
110
+ *
111
+ * @see org.exolab.castor.xml.Unmarshaller#setIgnoreExtraAttributes(boolean)
106
112
*/
107
113
public void setIgnoreExtraAttributes (boolean ignoreExtraAttributes ) {
108
114
this .ignoreExtraAttributes = ignoreExtraAttributes ;
@@ -116,6 +122,8 @@ public boolean getIgnoreExtraElements() {
116
122
/**
117
123
* Sets whether the Castor {@link Unmarshaller} should ignore elements that do not match a specific field. Default
118
124
* is <code>false</code>, extra attributes are flagged as an error.
125
+ *
126
+ * @see org.exolab.castor.xml.Unmarshaller#setIgnoreExtraElements(boolean)
119
127
*/
120
128
public void setIgnoreExtraElements (boolean ignoreExtraElements ) {
121
129
this .ignoreExtraElements = ignoreExtraElements ;
@@ -129,6 +137,8 @@ public boolean getWhitespacePreserve() {
129
137
/**
130
138
* Sets whether the Castor {@link Unmarshaller} should preserve "ignorable" whitespace. Default is
131
139
* <code>false</code>.
140
+ *
141
+ * @see org.exolab.castor.xml.Unmarshaller#setWhitespacePreserve(boolean)
132
142
*/
133
143
public void setWhitespacePreserve (boolean whitespacePreserve ) {
134
144
this .whitespacePreserve = whitespacePreserve ;
@@ -139,11 +149,29 @@ public boolean isValidating() {
139
149
return validating ;
140
150
}
141
151
142
- /** Sets whether this marshaller should validate in- and outgoing documents. Default is <code>false</code>. */
152
+ /**
153
+ * Sets whether this marshaller should validate in- and outgoing documents. Default is <code>false</code>.
154
+ *
155
+ * @see Marshaller#setValidation(boolean)
156
+ */
143
157
public void setValidating (boolean validating ) {
144
158
this .validating = validating ;
145
159
}
146
160
161
+ /** Returns the namespace mappings. Property names are interpreted as namespace prefixes; values are namespace URIs. */
162
+ public Properties getNamespaceMappings () {
163
+ return namespaceMappings ;
164
+ }
165
+
166
+ /**
167
+ * Sets the namespace mappings. Property names are interpreted as namespace prefixes; values are namespace URIs.
168
+ *
169
+ * @see org.exolab.castor.xml.Marshaller#setNamespaceMapping(String, String)
170
+ */
171
+ public void setNamespaceMappings (Properties namespaceMappings ) {
172
+ this .namespaceMappings = namespaceMappings ;
173
+ }
174
+
147
175
/**
148
176
* Sets the encoding to be used for stream access. If this property is not set, the default encoding is used.
149
177
*
@@ -280,10 +308,19 @@ private void marshal(Object graph, Marshaller marshaller) {
280
308
* Template method that allows for customizing of the given Castor {@link Marshaller}.
281
309
* <p/>
282
310
* Default implementation invokes {@link Marshaller#setValidation(boolean)} with the property set on this
283
- * marshaller.
311
+ * marshaller, and calls {@link Marshaller#setNamespaceMapping(String, String)} with the {@linkplain
312
+ * #setNamespaceMappings(java.util.Properties) namespace mappings}.
284
313
*/
285
314
protected void customizeMarshaller (Marshaller marshaller ) {
286
315
marshaller .setValidation (isValidating ());
316
+ Properties namespaceMappings = getNamespaceMappings ();
317
+ if (namespaceMappings != null ) {
318
+ for (Iterator iterator = namespaceMappings .keySet ().iterator (); iterator .hasNext ();) {
319
+ String prefix = (String ) iterator .next ();
320
+ String uri = namespaceMappings .getProperty (prefix );
321
+ marshaller .setNamespaceMapping (prefix , uri );
322
+ }
323
+ }
287
324
}
288
325
289
326
//
0 commit comments