TableXMLBuilder.java
4.04 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.rewriter.htmlparser.HtmlParser
* org.apache.commons.io.IOUtils
*/
package com.day.cq.wcm.foundation;
import com.day.cq.rewriter.htmlparser.HtmlParser;
import com.day.cq.wcm.foundation.Table;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class TableXMLBuilder
extends DefaultHandler {
private static final Set<String> TABLE_TAGS = new HashSet<String>(Arrays.asList("TABLE", "/TABLE", "TR", "/TR", "TD", "/TD", "TH", "/TH", "CAPTION", "/CAPTION", "COL"));
private Table table;
private int rowNr = -1;
private int colNr;
private Table.Cell cell = null;
private Table.Tag caption;
public Table parse(Reader r) throws IOException {
this.table = new Table();
this.rowNr = -1;
this.colNr = 0;
this.cell = null;
HtmlParser parser = new HtmlParser();
parser.setTagInclusionSet(TABLE_TAGS);
parser.setContentHandler((ContentHandler)this);
IOUtils.copy((Reader)r, (Writer)parser);
parser.close();
parser.finished();
return this.table;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
String name = localName.toLowerCase();
if (name.equals("table")) {
TableXMLBuilder.addAttributes(this.table, attributes);
} else if (name.equals("tr")) {
++this.rowNr;
this.colNr = 0;
} else if (name.equals("td") || name.equals("th")) {
this.cell = this.table.getCell(this.rowNr, this.colNr, true);
while (this.cell.isInSpan()) {
this.cell = this.table.getCell(this.rowNr, ++this.colNr, true);
}
this.cell.setHeader(name.equals("th"));
TableXMLBuilder.addAttributes(this.cell, attributes);
} else if (name.equals("caption")) {
this.caption = this.table.setCaption("");
TableXMLBuilder.addAttributes(this.caption, attributes);
}
}
private static void addAttributes(Table.Cell cell, Attributes attrs) {
for (int i = 0; i < attrs.getLength(); ++i) {
String name = attrs.getLocalName(i);
if (name.equals("quotes")) continue;
String value = attrs.getValue(i);
cell.setAttribute(name, value);
}
}
private static void addAttributes(Table.Tag tag, Attributes attrs) {
for (int i = 0; i < attrs.getLength(); ++i) {
String name = attrs.getLocalName(i);
if (name.equals("quotes")) continue;
String value = attrs.getValue(i);
tag.setAttribute(name, value);
}
}
private static void addAttributes(Table tag, Attributes attrs) {
for (int i = 0; i < attrs.getLength(); ++i) {
String name = attrs.getLocalName(i);
if (name.equals("quotes")) continue;
String value = attrs.getValue(i);
tag.setAttribute(name, value);
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
String name = localName.toLowerCase();
if (name.equals("tr")) {
this.cell = null;
} else if (name.equals("td") || name.equals("th")) {
++this.colNr;
this.cell = null;
} else if (name.equals("caption")) {
this.caption = null;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (this.cell != null) {
this.cell.appendText(ch, start, length);
} else if (this.caption != null) {
this.caption.appendInnerHtml(ch, start, length);
}
}
}