DocumentHandlerToSAXAdapter.java
3.42 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
/*
* Decompiled with CFR 0_118.
*/
package com.day.crx.packaging.impl.proxy;
import com.day.crx.packaging.impl.proxy.AttributeList;
import com.day.crx.packaging.impl.proxy.AttributesImpl;
import com.day.crx.packaging.impl.proxy.DocumentHandler;
import java.io.IOException;
import java.util.Iterator;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
public class DocumentHandlerToSAXAdapter
implements DocumentHandler {
public static final String NAMESPACE = "http://com.day/cq/rewriter";
public static final String END_SLASH_ATTR = "endSlash";
public static final String QUOTES_ATTR = "quotes";
private final AttributesImpl atts = new AttributesImpl();
private final ContentHandler contentHandler;
private boolean gotStart = false;
public DocumentHandlerToSAXAdapter(ContentHandler handler) {
this.contentHandler = handler;
}
@Override
public void characters(char[] buffer, int offset, int length) throws IOException {
try {
this.contentHandler.characters(buffer, offset, length);
}
catch (SAXException e) {
throw this.handle(e);
}
}
@Override
public void onStartElement(String tagName, AttributeList attributes, char[] buffer, int offset, int length, boolean endSlash) throws IOException {
this.atts.clear();
char[] quotes = new char[attributes.attributeCount()];
int index = 0;
Iterator<String> names = attributes.attributeNames();
while (names.hasNext()) {
String name = names.next();
String value = attributes.getValue(name);
if (value != null) {
this.atts.addCDATAAttribute(name, value);
} else {
this.atts.addCDATAAttribute(name, "");
}
quotes[index] = attributes.getQuoteChar(name);
++index;
}
if (index > 0) {
this.atts.addCDATAAttribute("http://com.day/cq/rewriter", "quotes", new String(quotes));
}
try {
if (endSlash) {
this.atts.addCDATAAttribute("endSlash", "");
}
this.contentHandler.startElement("", tagName, tagName, this.atts);
}
catch (SAXException e) {
throw this.handle(e);
}
}
@Override
public void onEndElement(String tagName, char[] buffer, int offset, int length) throws IOException {
try {
this.contentHandler.endElement("", tagName, tagName);
}
catch (SAXException e) {
throw this.handle(e);
}
}
@Override
public void onEnd() throws IOException {
if (this.gotStart) {
try {
this.contentHandler.endDocument();
}
catch (SAXException e) {
throw this.handle(e);
}
}
}
@Override
public void onStart() throws IOException {
this.gotStart = true;
try {
this.contentHandler.startDocument();
}
catch (SAXException e) {
throw this.handle(e);
}
}
protected final IOException handle(SAXException se) {
if (se.getCause() != null && se.getCause() instanceof IOException) {
return (IOException)se.getCause();
}
IOException ioe = new IOException("Unable to parse document");
ioe.initCause(se);
return ioe;
}
}