1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.oxm.support;
18
19 import java.io.IOException;
20 import javax.xml.transform.Source;
21 import javax.xml.transform.sax.SAXResult;
22 import javax.xml.transform.sax.SAXSource;
23
24 import org.springframework.oxm.Marshaller;
25 import org.springframework.util.Assert;
26 import org.springframework.xml.sax.AbstractXmlReader;
27 import org.xml.sax.InputSource;
28 import org.xml.sax.SAXException;
29 import org.xml.sax.SAXParseException;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class MarshallingSource extends SAXSource {
46
47 private final Marshaller marshaller;
48
49 private final Object content;
50
51
52
53
54
55
56
57 public MarshallingSource(Marshaller marshaller, Object content) {
58 Assert.notNull(marshaller, "'marshaller' must not be null");
59 Assert.notNull(content, "'content' must not be null");
60 this.marshaller = marshaller;
61 this.content = content;
62 setXMLReader(new MarshallingXmlReader());
63 setInputSource(new InputSource());
64 }
65
66
67 public Marshaller getMarshaller() {
68 return marshaller;
69 }
70
71
72 public Object getContent() {
73 return content;
74 }
75
76 private class MarshallingXmlReader extends AbstractXmlReader {
77
78 public void parse(InputSource input) throws IOException, SAXException {
79 parse();
80 }
81
82 public void parse(String systemId) throws IOException, SAXException {
83 parse();
84 }
85
86 private void parse() throws SAXException {
87 SAXResult result = new SAXResult(getContentHandler());
88 result.setLexicalHandler(getLexicalHandler());
89 try {
90 marshaller.marshal(content, result);
91 }
92 catch (IOException ex) {
93 SAXParseException saxException = new SAXParseException(ex.getMessage(), null, null, -1, -1, ex);
94 if (getErrorHandler() != null) {
95 getErrorHandler().fatalError(saxException);
96 }
97 else {
98 throw saxException;
99 }
100 }
101 }
102
103 }
104 }