AutoFormatter.java
6.56 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
/*
* Decompiled with CFR 0_118.
*/
package com.day.text;
import com.day.text.Text;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.util.Properties;
public class AutoFormatter
extends Format {
public static final int FORMAT_NOISODEC = 256;
public static final int FORMAT_ISOENC = 512;
public static final int FORMAT_AUTOLISTS = 2048;
public static final int FORMAT_AUTOLINK = 4096;
public static final int FORMAT_BR = 8192;
public static final int FORMAT_URLENC = 16384;
public static final int FORMAT_AUTOBR = 10240;
private static final String DEFAULT_BR = "<br>";
private static final String DEFAULT_TAG_OL_OPEN = "<ol start=\"%s\">";
private static final String DEFAULT_TAG_OL_CLOSE = "</ol>";
private static final String DEFAULT_TAG_OL_ITEM_OPEN = "<li>";
private static final String DEFAULT_TAG_OL_ITEM_CLOSE = "</li>";
private static final String DEFAULT_TAG_UL_OPEN = "<ul>";
private static final String DEFAULT_TAG_UL_CLOSE = "</ul>";
private static final String DEFAULT_TAG_UL_ITEM_OPEN = "<li>";
private static final String DEFAULT_TAG_UL_ITEM_CLOSE = "</li>";
private static final int STATE_NORMAL = 1;
private static final int STATE_OL = 3;
private static final int STATE_UL = 4;
private final String tagBr;
private final String tagOlOpen;
private final String tagOlClose;
private final String tagOlItemOpen;
private final String tagOlItemClose;
private final String tagUlOpen;
private final String tagUlClose;
private final String tagUlItemStart;
private final String tagUlItemClose;
private final int DEFAULT_MODS = 10240;
public static final AutoFormatter DEFAULT_FORMATTER = new AutoFormatter(null);
public AutoFormatter() {
this(null);
}
public AutoFormatter(Properties config) {
if (config == null) {
config = new Properties();
}
this.tagBr = config.getProperty("/br.begin", "<br>");
this.tagOlOpen = config.getProperty("/ol.begin", "<ol start=\"%s\">");
this.tagOlClose = config.getProperty("/ol.end", "</ol>");
this.tagOlItemOpen = config.getProperty("/ol/item.begin", "<li>");
this.tagOlItemClose = config.getProperty("/ol/item.end", "</li>");
this.tagUlOpen = config.getProperty("/ul.begin", "<ul>");
this.tagUlClose = config.getProperty("/ul.end", "</ul>");
this.tagUlItemStart = config.getProperty("/ul/item.begin", "<li>");
this.tagUlItemClose = config.getProperty("/ul/item.end", "</li>");
}
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
return this.format((String)obj, toAppendTo, 10240);
}
public Object parseObject(String source, ParsePosition status) {
throw new UnsupportedOperationException("parse not implemented yet");
}
public String format(String str, int mods) {
return this.format(str, new StringBuffer(), mods).toString();
}
public StringBuffer format(String str, StringBuffer toAppendTo, int mods) {
int beginCopy = 0;
int state = 1;
boolean lineStart = true;
char[] buffer = str.toCharArray();
int bufferLen = buffer.length;
if (toAppendTo == null) {
toAppendTo = new StringBuffer();
}
int i = 0;
while (i < bufferLen) {
char c = buffer[i];
if (lineStart && (mods & 2048) != 0) {
lineStart = false;
int ignore = 0;
if (c == '-' && i + 1 < bufferLen && buffer[i + 1] == ' ') {
if (state == 3) {
toAppendTo.append(this.tagOlItemClose);
toAppendTo.append(this.tagOlClose);
}
if (state != 4) {
toAppendTo.append(this.tagUlOpen);
state = 4;
} else {
toAppendTo.append(this.tagUlItemClose);
}
toAppendTo.append(this.tagUlItemStart);
ignore = 2;
} else {
StringBuffer start = new StringBuffer();
int j = i;
while (j + 2 < bufferLen && c >= '0' && c <= '9') {
start.append(c);
c = buffer[++j];
}
if (j > i && j + 1 < bufferLen && buffer[j] == '.' && buffer[j + 1] == ' ') {
if (state == 4) {
toAppendTo.append(this.tagUlItemClose);
toAppendTo.append(this.tagUlClose);
}
if (state != 3) {
toAppendTo.append(Text.sprintf(this.tagOlOpen, start.toString()));
state = 3;
} else {
toAppendTo.append(this.tagOlItemClose);
}
toAppendTo.append(Text.sprintf(this.tagOlItemOpen, Integer.valueOf(start.toString())));
ignore = j - i + 2;
} else if (state == 4) {
toAppendTo.append(this.tagUlItemClose);
toAppendTo.append(this.tagUlClose);
state = 1;
} else if (state == 3) {
toAppendTo.append(this.tagOlItemClose);
toAppendTo.append(this.tagOlClose);
state = 1;
}
}
beginCopy += ignore;
i += ignore;
continue;
}
if (c == '\r' || c == '\n') {
toAppendTo.append(buffer, beginCopy, i - beginCopy);
i = i + 1 < bufferLen && c == '\r' && buffer[i + 1] == '\n' ? (i += 2) : ++i;
lineStart = true;
beginCopy = i;
if (state != 1 || (mods & 8192) == 0) continue;
toAppendTo.append(this.tagBr);
continue;
}
++i;
}
if (beginCopy < bufferLen) {
toAppendTo.append(buffer, beginCopy, bufferLen - beginCopy);
}
if (state == 4) {
toAppendTo.append(this.tagUlItemClose);
toAppendTo.append(this.tagUlClose);
state = 1;
} else if (state == 3) {
toAppendTo.append(this.tagOlItemClose);
toAppendTo.append(this.tagOlClose);
state = 1;
}
return toAppendTo;
}
}