SAXWriter.java
4.07 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Component
*/
package com.day.cq.rewriter.htmlparser;
import com.day.cq.rewriter.pipeline.Serializer;
import com.day.cq.rewriter.processor.ProcessingComponentConfiguration;
import com.day.cq.rewriter.processor.ProcessingContext;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.felix.scr.annotations.Component;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
@Deprecated
@Component(factory="com.day.cq.rewriter.pipeline.Serializer/htmlwriter")
public class SAXWriter
implements Serializer {
private PrintWriter delegatee;
private List<String> emptyTags;
private static final List<String> DEFAULT_EMPTY_TAGS = new ArrayList<String>();
@Override
public void init(ProcessingContext pipelineContext, ProcessingComponentConfiguration config) throws IOException {
PrintWriter writer = pipelineContext.getWriter();
if (writer == null) {
throw new IllegalArgumentException("Writer must not be null");
}
this.delegatee = writer;
this.emptyTags = DEFAULT_EMPTY_TAGS;
}
@Override
public void endDocument() throws SAXException {
this.delegatee.flush();
}
@Override
public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException {
boolean endSlash = false;
this.delegatee.write(60);
this.delegatee.write(localName);
String quotesString = atts.getValue("http://com.day/cq/rewriter", "quotes");
for (int i = 0; i < atts.getLength(); ++i) {
if ("endSlash".equals(atts.getQName(i))) {
endSlash = true;
continue;
}
if ("http://com.day/cq/rewriter".equals(atts.getURI(i))) continue;
this.delegatee.write(32);
this.delegatee.write(atts.getLocalName(i));
String value = atts.getValue(i);
if (value == null) continue;
this.delegatee.write(61);
int quoteChar = quotesString != null && quotesString.length() > i ? (int)quotesString.charAt(i) : 34;
this.delegatee.write(quoteChar);
this.delegatee.write(value);
this.delegatee.write(quoteChar);
}
if (endSlash) {
this.delegatee.write("/");
}
this.delegatee.write(">");
}
@Override
public void endElement(String uri, String localName, String name) throws SAXException {
if (!this.emptyTags.contains(localName)) {
this.delegatee.write("</");
this.delegatee.write(localName);
this.delegatee.write(62);
}
}
@Override
public void characters(char[] buffer, int offset, int length) throws SAXException {
if (length == 0) {
this.delegatee.flush();
} else {
this.delegatee.write(buffer, offset, length);
}
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
}
@Override
public void processingInstruction(String target, String data) throws SAXException {
}
@Override
public void setDocumentLocator(Locator locator) {
}
@Override
public void skippedEntity(String name) throws SAXException {
}
@Override
public void startDocument() throws SAXException {
}
@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException {
}
static {
DEFAULT_EMPTY_TAGS.add("br");
DEFAULT_EMPTY_TAGS.add("area");
DEFAULT_EMPTY_TAGS.add("link");
DEFAULT_EMPTY_TAGS.add("img");
DEFAULT_EMPTY_TAGS.add("param");
DEFAULT_EMPTY_TAGS.add("hr");
DEFAULT_EMPTY_TAGS.add("input");
DEFAULT_EMPTY_TAGS.add("col");
DEFAULT_EMPTY_TAGS.add("base");
DEFAULT_EMPTY_TAGS.add("meta");
}
}