HTMLParsingTransformer.java
10.4 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
/*
* 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.commons.html.HtmlParser
* org.apache.sling.rewriter.ProcessingComponentConfiguration
* org.apache.sling.rewriter.ProcessingContext
* org.apache.sling.rewriter.Transformer
*/
package com.day.cq.rewriter.xml;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
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.sling.commons.html.HtmlParser;
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 class HTMLParsingTransformer
extends AbstractSAXPipe
implements Transformer {
private static final String ATTR_TEXT = "text";
private static final String ATTR_RICH_TEXT = "textIsRich";
private static final String ATTR_RESOURCE_TYPE = "sling:resourceType";
private static final String COLCTRL_RESOURCE_TYPE = "foundation/components/parsys/colctrl";
private static final String ATTR_LAYOUT = "layout";
private static final String ATTR_CONTROL_TYPE = "controlType";
private static final String COLCTRL = "colctrl";
private static final String TYPE_START = "start";
private static final String TYPE_END = "end";
private static final String TYPE_BREAK = "break";
private final HtmlParser htmlParser;
private int ignoreEndElement = 0;
private final Stack<Boolean> isInRowStack = new Stack();
public HTMLParsingTransformer(HtmlParser parser) {
this.htmlParser = parser;
}
public void init(ProcessingContext context, ProcessingComponentConfiguration config) throws IOException {
this.isInRowStack.push(false);
}
public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException {
if ("file".equals(loc) && "nt:file".equals(a.getValue("jcr:primaryType"))) {
++this.ignoreEndElement;
return;
}
if (this.ignoreEndElement > 0) {
++this.ignoreEndElement;
return;
}
String resourceType = a.getValue("sling:resourceType");
if (resourceType != null && resourceType.endsWith("colctrl")) {
AttributesImpl ai = new AttributesImpl(a);
ai.removeAttribute("sling:resourceType");
ai.addCDATAAttribute("sling:resourceType", "foundation/components/parsys/colctrl");
String type = a.getValue("controlType");
if (type == null || "start".equals(type)) {
ai.removeAttribute("controlType");
ai.addCDATAAttribute("controlType", "start");
super.startElement("", "colctrl", "colctrl", (Attributes)ai);
String layout = a.getValue("layout");
int numCols = 1;
if (layout != null) {
int pos = layout.indexOf(";");
numCols = pos == -1 ? Integer.valueOf(layout).intValue() : Integer.valueOf(layout.substring(0, pos)).intValue();
}
for (int i = 0; i < numCols; ++i) {
super.startElement("", "col", "col", SAXUtils.EMPTY_ATTRIBUTES);
super.endElement("", "col", "col");
}
super.startElement("", "row", "row", SAXUtils.EMPTY_ATTRIBUTES);
super.startElement("", "cell", "cell", SAXUtils.EMPTY_ATTRIBUTES);
super.startElement("", "colctrl", "colctrl", (Attributes)ai);
super.startElement("", "col", "col", SAXUtils.EMPTY_ATTRIBUTES);
super.endElement("", "col", "col");
this.isInRowStack.push(true);
} else if ("break".equals(type)) {
super.endElement("", "colctrl", "colctrl");
super.endElement("", "cell", "cell");
super.startElement("", "cell", "cell", SAXUtils.EMPTY_ATTRIBUTES);
super.startElement("", "colctrl", "colctrl", (Attributes)ai);
super.startElement("", "col", "col", SAXUtils.EMPTY_ATTRIBUTES);
super.endElement("", "col", "col");
} else if ("end".equals(type)) {
super.endElement("", "colctrl", "colctrl");
super.endElement("", "cell", "cell");
super.endElement("", "row", "row");
this.isInRowStack.pop();
super.endElement("", "colctrl", "colctrl");
}
++this.ignoreEndElement;
return;
}
if (this.isInRowStack.peek().booleanValue()) {
super.startElement("", "row", "row", SAXUtils.EMPTY_ATTRIBUTES);
super.startElement("", "cell", "cell", SAXUtils.EMPTY_ATTRIBUTES);
}
super.startElement(uri, loc, raw, a);
this.isInRowStack.push(false);
String text = a.getValue("text");
if (text != null) {
super.startElement("", "text", "text", (Attributes)new AttributesImpl());
if ("true".equals(a.getValue("textIsRich")) || a.getValue("textIsRich") == null) {
String html = "<html><body>" + text + "</body></html>";
try {
this.htmlParser.parse((InputStream)new ByteArrayInputStream(html.getBytes("UTF-8")), "UTF-8", HTMLParsingTransformer.getContentFilter(this.contentHandler));
}
catch (UnsupportedEncodingException e) {}
} else {
super.characters(text.toCharArray(), 0, text.length());
}
super.endElement("", "text", "text");
}
}
public void endElement(String uri, String loc, String raw) throws SAXException {
if (this.ignoreEndElement > 0) {
--this.ignoreEndElement;
return;
}
super.endElement(uri, loc, raw);
this.isInRowStack.pop();
if (this.isInRowStack.peek().booleanValue()) {
super.endElement("", "cell", "cell");
super.endElement("", "row", "row");
}
}
public void dispose() {
}
public static ContentHandler getContentFilter(ContentHandler ch) {
if (ch instanceof LexicalHandler) {
return new ExtendedContentFilter(ch);
}
return new ContentFilter(ch);
}
protected static class ExtendedContentFilter
extends ContentFilter
implements LexicalHandler {
protected final LexicalHandler lh;
public ExtendedContentFilter(ContentHandler ch) {
super(ch);
this.lh = (LexicalHandler)((Object)ch);
}
@Override
public void comment(char[] arg0, int arg1, int arg2) throws SAXException {
this.lh.comment(arg0, arg1, arg2);
}
@Override
public void endCDATA() throws SAXException {
this.lh.endCDATA();
}
@Override
public void endDTD() throws SAXException {
this.lh.endDTD();
}
@Override
public void endEntity(String arg0) throws SAXException {
this.lh.endEntity(arg0);
}
@Override
public void startCDATA() throws SAXException {
this.lh.startCDATA();
}
@Override
public void startDTD(String arg0, String arg1, String arg2) throws SAXException {
this.lh.startDTD(arg0, arg1, arg2);
}
@Override
public void startEntity(String arg0) throws SAXException {
this.lh.startEntity(arg0);
}
}
protected static class ContentFilter
implements ContentHandler {
protected final ContentHandler ch;
private static final String NAMESPACE = "http://www.w3.org/1999/xhtml";
private String prefix;
public ContentFilter(ContentHandler ch) {
this.ch = ch;
}
@Override
public void endElement(String uri, String loc, String raw) throws SAXException {
if (!loc.equals("html") && !loc.equals("body")) {
if (uri.equals("http://www.w3.org/1999/xhtml")) {
uri = "";
}
this.ch.endElement(uri, loc, raw);
}
}
@Override
public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException {
if (!loc.equals("html") && !loc.equals("body")) {
if (uri.equals("http://www.w3.org/1999/xhtml")) {
uri = "";
}
this.ch.startElement(uri, loc, raw, a);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
this.ch.characters(ch, start, length);
}
@Override
public void endDocument() throws SAXException {
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
if (prefix.equals(this.prefix)) {
this.prefix = null;
} else {
this.ch.endPrefixMapping(prefix);
}
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
this.ch.ignorableWhitespace(ch, start, length);
}
@Override
public void processingInstruction(String target, String data) throws SAXException {
this.ch.processingInstruction(target, data);
}
@Override
public void setDocumentLocator(Locator locator) {
}
@Override
public void skippedEntity(String name) throws SAXException {
this.ch.skippedEntity(name);
}
@Override
public void startDocument() throws SAXException {
}
@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException {
if ("http://www.w3.org/1999/xhtml".equals(uri)) {
this.prefix = prefix;
} else {
this.ch.startPrefixMapping(prefix, uri);
}
}
}
}