PlainTextTransformer.java
2.49 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.dam.cfm.ContentFragmentException
* org.apache.sling.xss.XSSAPI
*/
package com.adobe.cq.dam.cfm.impl.transformer;
import com.adobe.cq.dam.cfm.ContentFragmentException;
import com.adobe.cq.dam.cfm.impl.transformer.Transformer;
import org.apache.sling.xss.XSSAPI;
public class PlainTextTransformer
implements Transformer {
@Override
public String getContentType() {
return "text/plain";
}
@Override
public String fromHTML(String html) throws ContentFragmentException {
html = html.replaceAll("[\\n\\r\\t]?", "");
html = html.replaceAll("<br[^<>]*>", "\n");
String text = html.replaceAll("</(p|h[123456]|div|pre|li|td|th|address|blockquote|center|caption)[^<>]*>", "\n\n");
text = text.trim();
text = text.replaceAll("</?[a-z][a-z0-9]*[^<>]*>", "");
text = text.replaceAll("<", "<").replaceAll(">", ">").replaceAll(""", "\"").replaceAll("&", "&").replaceAll(" ", " ");
return text;
}
@Override
public String toHTML(String formattedText, XSSAPI xss) throws ContentFragmentException {
StringBuilder htmlBuilder = new StringBuilder((int)((double)formattedText.length() * 1.1));
if (formattedText.length() == 0) {
return "";
}
formattedText = formattedText.trim();
int startPos = 0;
do {
int lfPos;
String frag;
if ((lfPos = formattedText.indexOf("\n\n", startPos)) > 0) {
htmlBuilder.append("<p>");
if (startPos != lfPos) {
frag = formattedText.substring(startPos, lfPos);
htmlBuilder.append(xss.encodeForHTML(frag));
} else {
htmlBuilder.append(" ");
}
htmlBuilder.append("</p>\r");
startPos = lfPos + 2;
continue;
}
htmlBuilder.append("<p>");
if (startPos < formattedText.length()) {
frag = formattedText.substring(startPos);
htmlBuilder.append(xss.encodeForHTML(frag));
} else {
htmlBuilder.append(" ");
}
htmlBuilder.append("</p>");
startPos = -1;
} while (startPos >= 0);
String html = htmlBuilder.toString();
html = html.replaceAll("\n", "<br>");
return html.replaceAll("\r", "\n");
}
}