AbstractSAXTransformer.java
12.8 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.cocoon.xml.sax.AbstractSAXPipe
* org.apache.cocoon.xml.sax.AttributesImpl
* org.apache.cocoon.xml.sax.SAXUtils
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.rewriter.ProcessingComponentConfiguration
* org.apache.sling.rewriter.ProcessingContext
* org.apache.sling.rewriter.Transformer
*/
package com.day.cq.rewriter.xml;
import com.day.cq.rewriter.xml.helpers.TextRecorder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import org.apache.cocoon.xml.sax.AbstractSAXPipe;
import org.apache.cocoon.xml.sax.AttributesImpl;
import org.apache.cocoon.xml.sax.SAXUtils;
import org.apache.excalibur.source.SourceParameters;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.rewriter.ProcessingComponentConfiguration;
import org.apache.sling.rewriter.ProcessingContext;
import org.apache.sling.rewriter.Transformer;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;
public abstract class AbstractSAXTransformer
extends AbstractSAXPipe
implements Transformer {
protected boolean ignoreWhitespaces;
protected boolean ignoreEmptyCharacters;
protected int ignoreEventsCount;
protected int ignoreHooksCount;
protected String namespaceURI;
protected String defaultNamespaceURI;
protected final Stack stack = new Stack();
protected final Stack<ContentHandler> recorderStack = new Stack();
private boolean isInitialized;
private final List<String[]> namespaces = new ArrayList<String[]>(5);
private String ourPrefix;
protected boolean removeOurNamespacePrefixes = false;
private LexicalHandler originalLexicalHandler;
private ContentHandler originalContentHandler;
public void init(ProcessingContext context, ProcessingComponentConfiguration config) throws IOException {
if (this.defaultNamespaceURI == null) {
this.defaultNamespaceURI = "";
}
this.isInitialized = false;
this.namespaceURI = config.getConfiguration().containsKey((Object)"namespaceURI") ? config.getConfiguration().get((Object)"namespaceURI").toString() : this.defaultNamespaceURI;
this.ignoreHooksCount = 0;
this.ignoreEventsCount = 0;
this.ignoreWhitespaces = true;
this.ignoreEmptyCharacters = false;
}
public void setDocumentLocator(Locator locator) {
if (this.ignoreEventsCount == 0) {
super.setDocumentLocator(locator);
}
}
public void startDocument() throws SAXException {
if (!this.isInitialized) {
try {
this.setupTransforming();
}
catch (IOException e) {
throw new SAXException("IOException: " + e, e);
}
this.isInitialized = true;
}
if (this.ignoreEventsCount == 0) {
super.startDocument();
}
}
public void endDocument() throws SAXException {
if (this.ignoreEventsCount == 0) {
super.endDocument();
}
}
public void startPrefixMapping(String prefix, String uri) throws SAXException {
boolean isOurPrefix = false;
if (prefix != null) {
this.namespaces.add(new String[]{prefix, uri});
}
if (this.namespaceURI.equals(uri)) {
this.ourPrefix = prefix;
isOurPrefix = true;
}
if (!(this.ignoreEventsCount != 0 || this.removeOurNamespacePrefixes && isOurPrefix)) {
super.startPrefixMapping(prefix, uri);
}
}
public void endPrefixMapping(String prefix) throws SAXException {
boolean isOurPrefix = false;
if (prefix != null) {
String[] prefixAndUri;
int i;
boolean found = false;
for (i = this.namespaces.size() - 1; i >= 0; --i) {
prefixAndUri = this.namespaces.get(i);
if (!prefixAndUri[0].equals(prefix)) continue;
this.namespaces.remove(i);
found = true;
break;
}
if (!found) {
throw new SAXException("Namespace for prefix '" + prefix + "' not found.");
}
if (prefix.equals(this.ourPrefix)) {
isOurPrefix = true;
this.ourPrefix = null;
for (i = this.namespaces.size() - 1; i >= 0; --i) {
prefixAndUri = this.namespaces.get(i);
if (!this.namespaceURI.equals(prefixAndUri[1])) continue;
this.ourPrefix = prefixAndUri[0];
break;
}
}
}
if (!(this.ignoreEventsCount != 0 || this.removeOurNamespacePrefixes && isOurPrefix)) {
super.endPrefixMapping(prefix);
}
}
public void startElement(String uri, String name, String raw, Attributes attr) throws SAXException {
if (this.namespaceURI.equals(uri) && this.ignoreHooksCount == 0) {
try {
this.startTransformingElement(uri, name, raw, attr);
}
catch (IOException e) {
throw new SAXException("IOException occured during processing: " + e, e);
}
} else if (this.ignoreEventsCount == 0) {
super.startElement(uri, name, raw, attr);
}
}
public void endElement(String uri, String name, String raw) throws SAXException {
if (this.namespaceURI.equals(uri) && this.ignoreHooksCount == 0) {
try {
this.endTransformingElement(uri, name, raw);
}
catch (IOException e) {
throw new SAXException("IOException occured during processing: " + e, e);
}
} else if (this.ignoreEventsCount == 0) {
super.endElement(uri, name, raw);
}
}
public void characters(char[] p0, int p1, int p2) throws SAXException {
if (this.ignoreEventsCount == 0) {
if (this.ignoreEmptyCharacters) {
String value = new String(p0, p1, p2);
if (value.trim().length() > 0) {
super.characters(p0, p1, p2);
}
} else {
super.characters(p0, p1, p2);
}
}
}
public void ignorableWhitespace(char[] p0, int p1, int p2) throws SAXException {
if (!this.ignoreWhitespaces && this.ignoreEventsCount == 0) {
super.ignorableWhitespace(p0, p1, p2);
}
}
public void processingInstruction(String target, String data) throws SAXException {
if (this.ignoreEventsCount == 0) {
super.processingInstruction(target, data);
}
}
public void skippedEntity(String name) throws SAXException {
if (this.ignoreEventsCount == 0) {
super.skippedEntity(name);
}
}
public void startDTD(String name, String public_id, String system_id) throws SAXException {
if (this.ignoreEventsCount == 0) {
super.startDTD(name, public_id, system_id);
}
}
public void endDTD() throws SAXException {
if (this.ignoreEventsCount == 0) {
super.endDTD();
}
}
public void startEntity(String name) throws SAXException {
if (this.ignoreEventsCount == 0) {
super.startEntity(name);
}
}
public void endEntity(String name) throws SAXException {
if (this.ignoreEventsCount == 0) {
super.endEntity(name);
}
}
public void startCDATA() throws SAXException {
if (this.ignoreEventsCount == 0) {
super.startCDATA();
}
}
public void endCDATA() throws SAXException {
if (this.ignoreEventsCount == 0) {
super.endCDATA();
}
}
public void comment(char[] ary, int start, int length) throws SAXException {
if (this.ignoreEventsCount == 0) {
super.comment(ary, start, length);
}
}
protected void addRecorder(ContentHandler recorder) {
if (this.recorderStack.empty()) {
this.originalLexicalHandler = this.lexicalHandler;
this.originalContentHandler = this.contentHandler;
}
this.setContentHandler(recorder);
this.recorderStack.push(recorder);
}
protected Object removeRecorder() {
ContentHandler recorder = this.recorderStack.pop();
if (this.recorderStack.empty()) {
this.contentHandler = this.originalContentHandler;
this.lexicalHandler = this.originalLexicalHandler;
this.originalLexicalHandler = null;
this.originalContentHandler = null;
} else {
ContentHandler next = this.recorderStack.peek();
this.setContentHandler(next);
}
return recorder;
}
public void startTextRecording() throws SAXException {
this.addRecorder(new TextRecorder());
this.sendStartPrefixMapping();
}
public String endTextRecording() throws SAXException {
this.sendEndPrefixMapping();
TextRecorder recorder = (TextRecorder)this.removeRecorder();
String text = recorder.getText();
return text;
}
public void setupTransforming() throws IOException, SAXException {
this.stack.clear();
this.recorderStack.clear();
this.ignoreWhitespaces = true;
this.ignoreEmptyCharacters = false;
}
public void startTransformingElement(String uri, String name, String raw, Attributes attr) throws IOException, SAXException {
if (this.ignoreEventsCount == 0) {
super.startElement(uri, name, raw, attr);
}
}
public void endTransformingElement(String uri, String name, String raw) throws IOException, SAXException {
if (this.ignoreEventsCount == 0) {
super.endElement(uri, name, raw);
}
}
public void sendTextEvent(String text) throws SAXException {
this.characters(text.toCharArray(), 0, text.length());
}
public void sendStartElementEvent(String localname) throws SAXException {
this.startElement("", localname, localname, SAXUtils.EMPTY_ATTRIBUTES);
}
public void sendStartElementEventNS(String localname) throws SAXException {
this.startElement(this.namespaceURI, localname, this.ourPrefix + ':' + localname, SAXUtils.EMPTY_ATTRIBUTES);
}
public void sendStartElementEvent(String localname, Attributes attr) throws SAXException {
this.startElement("", localname, localname, attr);
}
public void sendStartElementEventNS(String localname, Attributes attr) throws SAXException {
this.startElement(this.namespaceURI, localname, this.ourPrefix + ':' + localname, attr);
}
public void sendEndElementEvent(String localname) throws SAXException {
this.endElement("", localname, localname);
}
public void sendEndElementEventNS(String localname) throws SAXException {
this.endElement(this.namespaceURI, localname, this.ourPrefix + ':' + localname);
}
public void sendParametersEvents(SourceParameters pars) throws SAXException {
if (pars != null) {
Iterator names = pars.getParameterNames();
while (names.hasNext()) {
String currentName = (String)names.next();
Iterator values = pars.getParameterValues(currentName);
while (values.hasNext()) {
String currentValue = (String)values.next();
this.sendStartElementEvent(currentName);
this.sendTextEvent(currentValue);
this.sendEndElementEvent(currentName);
}
}
}
}
protected void sendStartPrefixMapping() throws SAXException {
int l = this.namespaces.size();
for (int i = 0; i < l; ++i) {
String[] prefixAndUri = this.namespaces.get(i);
super.startPrefixMapping(prefixAndUri[0], prefixAndUri[1]);
}
}
protected void sendEndPrefixMapping() throws SAXException {
int l = this.namespaces.size();
for (int i = 0; i < l; ++i) {
String[] prefixAndUri = this.namespaces.get(i);
super.endPrefixMapping(prefixAndUri[0]);
}
}
protected String findPrefixMapping(String uri) {
int l = this.namespaces.size();
for (int i = 0; i < l; ++i) {
String[] prefixAndUri = this.namespaces.get(i);
if (!prefixAndUri[1].equals(uri)) continue;
return prefixAndUri[0];
}
return null;
}
protected AttributesImpl getMutableAttributes(Attributes a) {
if (a instanceof AttributesImpl) {
return (AttributesImpl)a;
}
return new AttributesImpl(a);
}
}