1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.xml.stream;
18
19 import javax.xml.stream.Location;
20 import javax.xml.stream.XMLStreamException;
21
22 import org.xml.sax.ContentHandler;
23 import org.xml.sax.InputSource;
24 import org.xml.sax.Locator;
25 import org.xml.sax.SAXException;
26 import org.xml.sax.SAXParseException;
27
28 import org.springframework.xml.sax.AbstractXmlReader;
29
30
31
32
33
34
35
36
37
38
39
40 public abstract class AbstractStaxXmlReader extends AbstractXmlReader {
41
42
43
44
45
46
47
48
49
50 public final void parse(InputSource ignored) throws SAXException {
51 parse();
52 }
53
54
55
56
57
58
59
60
61
62 public final void parse(String ignored) throws SAXException {
63 parse();
64 }
65
66 private void parse() throws SAXException {
67 try {
68 parseInternal();
69 }
70 catch (XMLStreamException ex) {
71 Locator locator = null;
72 if (ex.getLocation() != null) {
73 locator = new StaxLocator(ex.getLocation());
74 }
75 SAXParseException saxException = new SAXParseException(ex.getMessage(), locator, ex);
76 if (getErrorHandler() != null) {
77 getErrorHandler().fatalError(saxException);
78 }
79 else {
80 throw saxException;
81 }
82 }
83 }
84
85
86
87
88
89
90
91 protected void setLocator(Location location) {
92 if (getContentHandler() != null) {
93 getContentHandler().setDocumentLocator(new StaxLocator(location));
94 }
95 }
96
97
98 protected abstract void parseInternal() throws SAXException, XMLStreamException;
99
100
101
102
103
104
105
106 private static class StaxLocator implements Locator {
107
108 private Location location;
109
110 protected StaxLocator(Location location) {
111 this.location = location;
112 }
113
114 public String getPublicId() {
115 return location.getPublicId();
116 }
117
118 public String getSystemId() {
119 return location.getSystemId();
120 }
121
122 public int getLineNumber() {
123 return location.getLineNumber();
124 }
125
126 public int getColumnNumber() {
127 return location.getColumnNumber();
128 }
129 }
130 }