JaxpSAXParser.java
7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.rewriter.xml;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.ContentHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;
public final class JaxpSAXParser
implements ErrorHandler {
private final Logger logger;
protected SAXParserFactory factory;
protected boolean nsPrefixes;
protected boolean stopOnWarning;
protected boolean stopOnRecoverableError;
protected boolean dropDtdComments;
protected EntityResolver resolver;
protected boolean validate;
public JaxpSAXParser() {
this.logger = LoggerFactory.getLogger(this.getClass());
this.nsPrefixes = false;
this.stopOnWarning = true;
this.stopOnRecoverableError = true;
this.dropDtdComments = false;
this.validate = false;
}
public void setEntityResolver(EntityResolver r) {
this.resolver = r;
}
public EntityResolver getEntityResolver() {
return this.resolver;
}
public boolean isValidate() {
return this.validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public boolean isDropDtdComments() {
return this.dropDtdComments;
}
public void setDropDtdComments(boolean dropDtdComments) {
this.dropDtdComments = dropDtdComments;
}
public boolean isNsPrefixes() {
return this.nsPrefixes;
}
public void setNsPrefixes(boolean nsPrefixes) {
this.nsPrefixes = nsPrefixes;
}
public boolean isStopOnRecoverableError() {
return this.stopOnRecoverableError;
}
public void setStopOnRecoverableError(boolean stopOnRecoverableError) {
this.stopOnRecoverableError = stopOnRecoverableError;
}
public boolean isStopOnWarning() {
return this.stopOnWarning;
}
public void setStopOnWarning(boolean stopOnWarning) {
this.stopOnWarning = stopOnWarning;
}
protected synchronized void initSaxParserFactory() throws Exception {
if (this.factory == null) {
this.factory = SAXParserFactory.newInstance();
this.factory.setNamespaceAware(true);
this.factory.setValidating(this.validate);
}
}
public void parse(InputSource in, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws SAXException, IOException {
XMLReader tmpReader = this.setupXMLReader();
try {
LexicalHandler theLexicalHandler = null;
if (null == lexicalHandler && contentHandler instanceof LexicalHandler) {
theLexicalHandler = (LexicalHandler)((Object)contentHandler);
}
if (null != lexicalHandler) {
theLexicalHandler = lexicalHandler;
}
if (theLexicalHandler != null) {
if (this.dropDtdComments) {
theLexicalHandler = new DtdCommentEater(theLexicalHandler);
}
tmpReader.setProperty("http://xml.org/sax/properties/lexical-handler", theLexicalHandler);
}
}
catch (SAXException e) {
String message = "SAX2 driver does not support property: 'http://xml.org/sax/properties/lexical-handler'";
this.logger.warn("SAX2 driver does not support property: 'http://xml.org/sax/properties/lexical-handler'");
}
tmpReader.setContentHandler(contentHandler);
tmpReader.parse(in);
}
public void parse(InputSource in, ContentHandler consumer) throws SAXException, IOException {
this.parse(in, consumer, consumer instanceof LexicalHandler ? (LexicalHandler)((Object)consumer) : null);
}
protected XMLReader setupXMLReader() throws SAXException {
XMLReader reader;
if (this.factory == null) {
try {
this.initSaxParserFactory();
}
catch (Exception e) {
String message = "Cannot initialize sax parser factory";
throw new SAXException("Cannot initialize sax parser factory", e);
}
}
try {
reader = this.factory.newSAXParser().getXMLReader();
}
catch (ParserConfigurationException pce) {
String message = "Cannot produce a valid parser";
throw new SAXException("Cannot produce a valid parser", pce);
}
reader.setFeature("http://xml.org/sax/features/namespaces", true);
if (this.nsPrefixes) {
try {
reader.setFeature("http://xml.org/sax/features/namespace-prefixes", this.nsPrefixes);
}
catch (SAXException se) {
String message = "SAX2 XMLReader does not support setting feature: 'http://xml.org/sax/features/namespace-prefixes'";
this.logger.warn("SAX2 XMLReader does not support setting feature: 'http://xml.org/sax/features/namespace-prefixes'");
}
}
reader.setErrorHandler(this);
if (this.resolver != null) {
reader.setEntityResolver(this.resolver);
}
return reader;
}
@Override
public void error(SAXParseException spe) throws SAXException {
String message = "Error parsing " + spe.getSystemId() + " (line " + spe.getLineNumber() + " col. " + spe.getColumnNumber() + "): " + spe.getMessage();
if (this.stopOnRecoverableError) {
throw new SAXException(message, spe);
}
this.logger.error(message, (Throwable)spe);
}
@Override
public void fatalError(SAXParseException spe) throws SAXException {
String message = "Fatal error parsing " + spe.getSystemId() + " (line " + spe.getLineNumber() + " col. " + spe.getColumnNumber() + "): " + spe.getMessage();
throw new SAXException(message, spe);
}
@Override
public void warning(SAXParseException spe) throws SAXException {
String message = "Warning parsing " + spe.getSystemId() + " (line " + spe.getLineNumber() + " col. " + spe.getColumnNumber() + "): " + spe.getMessage();
if (this.stopOnWarning) {
throw new SAXException(message, spe);
}
this.logger.warn(message, (Throwable)spe);
}
protected static class DtdCommentEater
implements LexicalHandler {
protected LexicalHandler next;
protected boolean inDTD;
public DtdCommentEater(LexicalHandler nextHandler) {
this.next = nextHandler;
}
@Override
public void startDTD(String name, String publicId, String systemId) throws SAXException {
this.inDTD = true;
this.next.startDTD(name, publicId, systemId);
}
@Override
public void endDTD() throws SAXException {
this.inDTD = false;
this.next.endDTD();
}
@Override
public void startEntity(String name) throws SAXException {
this.next.startEntity(name);
}
@Override
public void endEntity(String name) throws SAXException {
this.next.endEntity(name);
}
@Override
public void startCDATA() throws SAXException {
this.next.startCDATA();
}
@Override
public void endCDATA() throws SAXException {
this.next.endCDATA();
}
@Override
public void comment(char[] ch, int start, int length) throws SAXException {
if (!this.inDTD) {
this.next.comment(ch, start, length);
}
}
}
}