View Javadoc

1   /*
2    * Copyright 2006 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.oxm;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.io.Reader;
23  import java.io.Writer;
24  import javax.xml.parsers.DocumentBuilder;
25  import javax.xml.parsers.DocumentBuilderFactory;
26  import javax.xml.parsers.ParserConfigurationException;
27  import javax.xml.stream.XMLEventReader;
28  import javax.xml.stream.XMLEventWriter;
29  import javax.xml.stream.XMLStreamReader;
30  import javax.xml.stream.XMLStreamWriter;
31  import javax.xml.transform.Result;
32  import javax.xml.transform.Source;
33  import javax.xml.transform.dom.DOMResult;
34  import javax.xml.transform.dom.DOMSource;
35  import javax.xml.transform.sax.SAXResult;
36  import javax.xml.transform.sax.SAXSource;
37  import javax.xml.transform.stream.StreamResult;
38  import javax.xml.transform.stream.StreamSource;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  import org.springframework.util.Assert;
43  import org.springframework.xml.transform.StaxResult;
44  import org.springframework.xml.transform.StaxSource;
45  import org.w3c.dom.Node;
46  import org.xml.sax.ContentHandler;
47  import org.xml.sax.InputSource;
48  import org.xml.sax.SAXException;
49  import org.xml.sax.XMLReader;
50  import org.xml.sax.ext.LexicalHandler;
51  import org.xml.sax.helpers.XMLReaderFactory;
52  
53  /**
54   * Abstract implementation of the <code>Marshaller</code> and <code>Unmarshaller</code> interface. This implementation
55   * inspects the given <code>Source</code> or <code>Result</code>, and defers further handling to overridable template
56   * methods.
57   *
58   * @author Arjen Poutsma
59   * @since 1.0.0
60   */
61  public abstract class AbstractMarshaller implements Marshaller, Unmarshaller {
62  
63      /**
64       * Logger available to subclasses.
65       */
66      protected final Log logger = LogFactory.getLog(getClass());
67  
68      private DocumentBuilderFactory documentBuilderFactory;
69  
70      /**
71       * Marshals the object graph with the given root into the provided <code>javax.xml.transform.Result</code>.
72       * <p/>
73       * This implementation inspects the given result, and calls <code>marshalDomResult</code>,
74       * <code>marshalSaxResult</code>, or <code>marshalStreamResult</code>.
75       *
76       * @param graph  the root of the object graph to marshal
77       * @param result the result to marshal to
78       * @throws XmlMappingException      if the given object cannot be marshalled to the result
79       * @throws IOException              if an I/O exception occurs
80       * @throws IllegalArgumentException if <code>result</code> if neither a <code>DOMResult</code>,
81       *                                  <code>SAXResult</code>, <code>StreamResult</code>
82       * @see #marshalDomResult(Object,javax.xml.transform.dom.DOMResult)
83       * @see #marshalSaxResult(Object,javax.xml.transform.sax.SAXResult)
84       * @see #marshalStreamResult(Object,javax.xml.transform.stream.StreamResult)
85       */
86      public final void marshal(Object graph, Result result) throws XmlMappingException, IOException {
87          if (result instanceof DOMResult) {
88              marshalDomResult(graph, (DOMResult) result);
89          }
90          else if (result instanceof StaxResult) {
91              marshalStaxResult(graph, (StaxResult) result);
92          }
93          else if (result instanceof SAXResult) {
94              marshalSaxResult(graph, (SAXResult) result);
95          }
96          else if (result instanceof StreamResult) {
97              marshalStreamResult(graph, (StreamResult) result);
98          }
99          else {
100             throw new IllegalArgumentException("Unknown Result type: " + result.getClass());
101         }
102     }
103 
104     /**
105      * Unmarshals the given provided <code>javax.xml.transform.Source</code> into an object graph.
106      * <p/>
107      * This implementation inspects the given result, and calls <code>unmarshalDomSource</code>,
108      * <code>unmarshalSaxSource</code>, or <code>unmarshalStreamSource</code>.
109      *
110      * @param source the source to marshal from
111      * @return the object graph
112      * @throws XmlMappingException      if the given source cannot be mapped to an object
113      * @throws IOException              if an I/O Exception occurs
114      * @throws IllegalArgumentException if <code>source</code> is neither a <code>DOMSource</code>, a
115      *                                  <code>SAXSource</code>, nor a <code>StreamSource</code>
116      * @see #unmarshalDomSource(javax.xml.transform.dom.DOMSource)
117      * @see #unmarshalSaxSource(javax.xml.transform.sax.SAXSource)
118      * @see #unmarshalStreamSource(javax.xml.transform.stream.StreamSource)
119      */
120     public final Object unmarshal(Source source) throws XmlMappingException, IOException {
121         if (source instanceof DOMSource) {
122             return unmarshalDomSource((DOMSource) source);
123         }
124         else if (source instanceof StaxSource) {
125             return unmarshalStaxSource((StaxSource) source);
126         }
127         else if (source instanceof SAXSource) {
128             return unmarshalSaxSource((SAXSource) source);
129         }
130         else if (source instanceof StreamSource) {
131             return unmarshalStreamSource((StreamSource) source);
132         }
133         else {
134             throw new IllegalArgumentException("Unknown Source type: " + source.getClass());
135         }
136     }
137 
138     /**
139      * Create a <code>DocumentBuilder</code> that this marshaller will use for creating DOM documents when passed an
140      * empty <code>DOMSource</code>. Can be overridden in subclasses, adding further initialization of the builder.
141      *
142      * @param factory the <code>DocumentBuilderFactory</code> that the DocumentBuilder should be created with
143      * @return the <code>DocumentBuilder</code>
144      * @throws javax.xml.parsers.ParserConfigurationException
145      *          if thrown by JAXP methods
146      */
147     protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory)
148             throws ParserConfigurationException {
149         return factory.newDocumentBuilder();
150     }
151 
152     /**
153      * Create a <code>DocumentBuilder</code> that this marshaller will use for creating DOM documents when passed an
154      * empty <code>DOMSource</code>. The resulting <code>DocumentBuilderFactory</code> is cached, so this method will
155      * only be called once.
156      *
157      * @return the DocumentBuilderFactory
158      * @throws ParserConfigurationException if thrown by JAXP methods
159      */
160     protected DocumentBuilderFactory createDocumentBuilderFactory() throws ParserConfigurationException {
161         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
162         factory.setValidating(false);
163         factory.setNamespaceAware(true);
164         return factory;
165     }
166 
167     /**
168      * Create a <code>XMLReader</code> that this marshaller will when passed an empty <code>SAXSource</code>.
169      *
170      * @return the XMLReader
171      * @throws SAXException if thrown by JAXP methods
172      */
173     protected XMLReader createXmlReader() throws SAXException {
174         return XMLReaderFactory.createXMLReader();
175     }
176 
177     //
178     // Marshalling
179     //
180 
181     /**
182      * Template method for handling <code>DOMResult</code>s. This implementation defers to <code>marshalDomNode</code>.
183      *
184      * @param graph     the root of the object graph to marshal
185      * @param domResult the <code>DOMResult</code>
186      * @throws XmlMappingException      if the given object cannot be marshalled to the result
187      * @throws IllegalArgumentException if the <code>domResult</code> is empty
188      * @see #marshalDomNode(Object,org.w3c.dom.Node)
189      */
190     protected void marshalDomResult(Object graph, DOMResult domResult) throws XmlMappingException {
191         Assert.notNull(domResult.getNode(), "DOMResult does not contain Node");
192         marshalDomNode(graph, domResult.getNode());
193     }
194 
195     /**
196      * Template method for handling <code>StaxResult</code>s.  This implementation defers to
197      * <code>marshalXMLSteamWriter</code>, or <code>marshalXMLEventConsumer</code>, depending on what is contained in
198      * the <code>StaxResult</code>.
199      *
200      * @param graph      the root of the object graph to marshal
201      * @param staxResult the <code>StaxResult</code>
202      * @throws XmlMappingException      if the given object cannot be marshalled to the result
203      * @throws IllegalArgumentException if the <code>domResult</code> is empty
204      * @see #marshalDomNode(Object,org.w3c.dom.Node)
205      */
206     protected void marshalStaxResult(Object graph, StaxResult staxResult) throws XmlMappingException {
207         if (staxResult.getXMLStreamWriter() != null) {
208             marshalXmlStreamWriter(graph, staxResult.getXMLStreamWriter());
209         }
210         else if (staxResult.getXMLEventWriter() != null) {
211             marshalXmlEventWriter(graph, staxResult.getXMLEventWriter());
212         }
213         else {
214             throw new IllegalArgumentException("StaxResult contains neither XMLStreamWriter nor XMLEventConsumer");
215         }
216     }
217 
218     /**
219      * Template method for handling <code>SAXResult</code>s. This implementation defers to
220      * <code>marshalSaxHandlers</code>.
221      *
222      * @param graph     the root of the object graph to marshal
223      * @param saxResult the <code>SAXResult</code>
224      * @throws XmlMappingException if the given object cannot be marshalled to the result
225      * @see #marshalSaxHandlers(Object,org.xml.sax.ContentHandler,org.xml.sax.ext.LexicalHandler)
226      */
227     protected void marshalSaxResult(Object graph, SAXResult saxResult) throws XmlMappingException {
228         ContentHandler contentHandler = saxResult.getHandler();
229         Assert.notNull(contentHandler, "ContentHandler not set on SAXResult");
230         LexicalHandler lexicalHandler = saxResult.getLexicalHandler();
231         marshalSaxHandlers(graph, contentHandler, lexicalHandler);
232     }
233 
234     /**
235      * Template method for handling <code>StreamResult</code>s. This implementation defers to
236      * <code>marshalOutputStream</code>, or <code>marshalWriter</code>, depending on what is contained in the
237      * <code>StreamResult</code>
238      *
239      * @param graph        the root of the object graph to marshal
240      * @param streamResult the <code>StreamResult</code>
241      * @throws IOException              if an I/O Exception occurs
242      * @throws XmlMappingException      if the given object cannot be marshalled to the result
243      * @throws IllegalArgumentException if <code>streamResult</code> contains neither <code>OutputStream</code> nor
244      *                                  <code>Writer</code>.
245      */
246     protected void marshalStreamResult(Object graph, StreamResult streamResult)
247             throws XmlMappingException, IOException {
248         if (streamResult.getOutputStream() != null) {
249             marshalOutputStream(graph, streamResult.getOutputStream());
250         }
251         else if (streamResult.getWriter() != null) {
252             marshalWriter(graph, streamResult.getWriter());
253         }
254         else {
255             throw new IllegalArgumentException("StreamResult contains neither OutputStream nor Writer");
256         }
257     }
258 
259     //
260     // Unmarshalling
261     //
262 
263     /**
264      * Template method for handling <code>DOMSource</code>s. This implementation defers to
265      * <code>unmarshalDomNode</code>. If the given source is empty, an empty source <code>Document</code> will be
266      * created as a placeholder.
267      *
268      * @param domSource the <code>DOMSource</code>
269      * @return the object graph
270      * @throws IllegalArgumentException if the <code>domSource</code> is empty
271      * @throws XmlMappingException      if the given source cannot be mapped to an object
272      * @see #unmarshalDomNode(org.w3c.dom.Node)
273      */
274     protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException {
275         if (domSource.getNode() == null) {
276             try {
277                 if (documentBuilderFactory == null) {
278                     documentBuilderFactory = createDocumentBuilderFactory();
279                 }
280                 DocumentBuilder documentBuilder = createDocumentBuilder(documentBuilderFactory);
281                 domSource.setNode(documentBuilder.newDocument());
282             }
283             catch (ParserConfigurationException ex) {
284                 throw new UnmarshallingFailureException(
285                         "Could not create document placeholder for DOMSource: " + ex.getMessage(), ex);
286             }
287         }
288         return unmarshalDomNode(domSource.getNode());
289     }
290 
291     /**
292      * Template method for handling <code>StaxSource</code>s. This implementation defers to
293      * <code>unmarshalXmlStreamReader</code>, or <code>unmarshalXmlEventReader</code>.
294      *
295      * @param staxSource the <code>StaxSource</code>
296      * @return the object graph
297      * @throws XmlMappingException if the given source cannot be mapped to an object
298      */
299     protected Object unmarshalStaxSource(StaxSource staxSource) throws XmlMappingException {
300         if (staxSource.getXMLStreamReader() != null) {
301             return unmarshalXmlStreamReader(staxSource.getXMLStreamReader());
302         }
303         else if (staxSource.getXMLEventReader() != null) {
304             return unmarshalXmlEventReader(staxSource.getXMLEventReader());
305         }
306         else {
307             throw new IllegalArgumentException("StaxSource contains neither XMLStreamReader nor XMLEventReader");
308         }
309     }
310 
311     /**
312      * Template method for handling <code>SAXSource</code>s. This implementation defers to
313      * <code>unmarshalSaxReader</code>.
314      *
315      * @param saxSource the <code>SAXSource</code>
316      * @return the object graph
317      * @throws XmlMappingException if the given source cannot be mapped to an object
318      * @throws IOException         if an I/O Exception occurs
319      * @see #unmarshalSaxReader(org.xml.sax.XMLReader,org.xml.sax.InputSource)
320      */
321     protected Object unmarshalSaxSource(SAXSource saxSource) throws XmlMappingException, IOException {
322         if (saxSource.getXMLReader() == null) {
323             try {
324                 saxSource.setXMLReader(createXmlReader());
325             }
326             catch (SAXException ex) {
327                 throw new UnmarshallingFailureException("Could not create XMLReader for SAXSource: " + ex.getMessage(),
328                         ex);
329             }
330         }
331         if (saxSource.getInputSource() == null) {
332             saxSource.setInputSource(new InputSource());
333         }
334         return unmarshalSaxReader(saxSource.getXMLReader(), saxSource.getInputSource());
335     }
336 
337     /**
338      * Template method for handling <code>StreamSource</code>s. This implementation defers to
339      * <code>unmarshalInputStream</code>, or <code>unmarshalReader</code>.
340      *
341      * @param streamSource the <code>StreamSource</code>
342      * @return the object graph
343      * @throws IOException         if an I/O exception occurs
344      * @throws XmlMappingException if the given source cannot be mapped to an object
345      */
346     protected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException {
347         if (streamSource.getInputStream() != null) {
348             return unmarshalInputStream(streamSource.getInputStream());
349         }
350         else if (streamSource.getReader() != null) {
351             return unmarshalReader(streamSource.getReader());
352         }
353         else {
354             throw new IllegalArgumentException("StreamSource contains neither InputStream nor Reader");
355         }
356     }
357 
358     //
359     // Abstract template methods
360     //
361 
362     /**
363      * Abstract template method for marshalling the given object graph to a DOM <code>Node</code>.
364      * <p/>
365      * In practice, node is be a <code>Document</code> node, a <code>DocumentFragment</code> node, or a
366      * <code>Element</code> node. In other words, a node that accepts children.
367      *
368      * @param graph the root of the object graph to marshal
369      * @param node  The DOM node that will contain the result tree
370      * @throws XmlMappingException if the given object cannot be marshalled to the DOM node
371      * @see org.w3c.dom.Document
372      * @see org.w3c.dom.DocumentFragment
373      * @see org.w3c.dom.Element
374      */
375     protected abstract void marshalDomNode(Object graph, Node node) throws XmlMappingException;
376 
377     /**
378      * Abstract template method for marshalling the given object to a StAX <code>XMLEventWriter</code>.
379      *
380      * @param graph       the root of the object graph to marshal
381      * @param eventWriter the <code>XMLEventWriter</code> to write to
382      * @throws XmlMappingException if the given object cannot be marshalled to the DOM node
383      */
384     protected abstract void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException;
385 
386     /**
387      * Abstract template method for marshalling the given object to a StAX <code>XMLStreamWriter</code>.
388      *
389      * @param graph        the root of the object graph to marshal
390      * @param streamWriter the <code>XMLStreamWriter</code> to write to
391      * @throws XmlMappingException if the given object cannot be marshalled to the DOM node
392      */
393     protected abstract void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter)
394             throws XmlMappingException;
395 
396     /**
397      * Abstract template method for marshalling the given object graph to a <code>OutputStream</code>.
398      *
399      * @param graph        the root of the object graph to marshal
400      * @param outputStream the <code>OutputStream</code> to write to
401      * @throws XmlMappingException if the given object cannot be marshalled to the writer
402      * @throws IOException         if an I/O exception occurs
403      */
404     protected abstract void marshalOutputStream(Object graph, OutputStream outputStream)
405             throws XmlMappingException, IOException;
406 
407     /**
408      * Abstract template method for marshalling the given object graph to a SAX <code>ContentHandler</code>.
409      *
410      * @param graph          the root of the object graph to marshal
411      * @param contentHandler the SAX <code>ContentHandler</code>
412      * @param lexicalHandler the SAX2 <code>LexicalHandler</code>. Can be <code>null</code>.
413      * @throws XmlMappingException if the given object cannot be marshalled to the handlers
414      */
415     protected abstract void marshalSaxHandlers(Object graph,
416                                                ContentHandler contentHandler,
417                                                LexicalHandler lexicalHandler) throws XmlMappingException;
418 
419     /**
420      * Abstract template method for marshalling the given object graph to a <code>Writer</code>.
421      *
422      * @param graph  the root of the object graph to marshal
423      * @param writer the <code>Writer</code> to write to
424      * @throws XmlMappingException if the given object cannot be marshalled to the writer
425      * @throws IOException         if an I/O exception occurs
426      */
427     protected abstract void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException;
428 
429     /**
430      * Abstract template method for unmarshalling from a given DOM <code>Node</code>.
431      *
432      * @param node The DOM node that contains the objects to be unmarshalled
433      * @return the object graph
434      * @throws XmlMappingException if the given DOM node cannot be mapped to an object
435      */
436     protected abstract Object unmarshalDomNode(Node node) throws XmlMappingException;
437 
438     /**
439      * Abstract template method for unmarshalling from a given Stax <code>XMLEventReader</code>.
440      *
441      * @param eventReader The <code>XMLEventReader</code> to read from
442      * @return the object graph
443      * @throws XmlMappingException if the given event reader cannot be converted to an object
444      */
445     protected abstract Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException;
446 
447     /**
448      * Abstract template method for unmarshalling from a given Stax <code>XMLStreamReader</code>.
449      *
450      * @param streamReader The <code>XMLStreamReader</code> to read from
451      * @return the object graph
452      * @throws XmlMappingException if the given stream reader cannot be converted to an object
453      */
454     protected abstract Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException;
455 
456     /**
457      * Abstract template method for unmarshalling from a given <code>InputStream</code>.
458      *
459      * @param inputStream the <code>InputStreamStream</code> to read from
460      * @return the object graph
461      * @throws XmlMappingException if the given stream cannot be converted to an object
462      * @throws IOException         if an I/O exception occurs
463      */
464     protected abstract Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException;
465 
466     /**
467      * Abstract template method for unmarshalling from a given <code>Reader</code>.
468      *
469      * @param reader the <code>Reader</code> to read from
470      * @return the object graph
471      * @throws XmlMappingException if the given reader cannot be converted to an object
472      * @throws IOException         if an I/O exception occurs
473      */
474     protected abstract Object unmarshalReader(Reader reader) throws XmlMappingException, IOException;
475 
476     /**
477      * Abstract template method for unmarshalling using a given SAX <code>XMLReader</code> and
478      * <code>InputSource</code>.
479      *
480      * @param xmlReader   the SAX <code>XMLReader</code> to parse with
481      * @param inputSource the input source to parse from
482      * @return the object graph
483      * @throws XmlMappingException if the given reader and input source cannot be converted to an object
484      * @throws java.io.IOException if an I/O exception occurs
485      */
486     protected abstract Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
487             throws XmlMappingException, IOException;
488 }