forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTML.java
More file actions
220 lines (211 loc) · 6.41 KB
/
Copy pathHTML.java
File metadata and controls
220 lines (211 loc) · 6.41 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
package javaforce;
/** HTML generation functions.
*
* @author pquiring
*/
public class HTML {
public static final boolean debug = false;
/** Generate html.form.input
* @param name = element name (optional)
* @param id = element id (optional)
* @param value = default value (optional)
*/
public static String input_field(String name, String id, String value) {
StringBuilder sb = new StringBuilder();
sb.append("<input");
if (name != null) {
sb.append(" name='");
sb.append(name);
sb.append("'");
}
if (id != null) {
sb.append(" id='");
sb.append(id);
sb.append("'");
}
if (value != null) {
sb.append(" value='");
sb.append(value);
sb.append("'");
}
sb.append(">");
return sb.toString();
}
/** Generate html.form.input[type=checkbox]
* @param name = element name (optional)
* @param id = element id (optional)
* @param checked = checkbox is initial checked
*/
public static String input_checkbox(String name, String id, boolean checked) {
StringBuilder sb = new StringBuilder();
sb.append("<input type=checkbox");
if (name != null) {
sb.append(" name='");
sb.append(name);
sb.append("'");
}
if (id != null) {
sb.append(" id='");
sb.append(id);
sb.append("'");
}
if (checked) sb.append(" checked");
sb.append(">");
return sb.toString();
}
/** Generate html.form.select.
* @param name = element name (optional)
* @param id = element id (optional)
* @param options = array[value, text]
* @param selected_value = selected value (optional)
* @param selected_text = selected text (optional)
* @param show_default = include a null 'Select...' option
*/
public static String select(String name, String id, String[][] options, String selected_value, String selected_text, boolean show_default) {
StringBuilder sb = new StringBuilder();
sb.append("<select");
if (name != null) {
sb.append(" name='");
sb.append(name);
sb.append("'");
}
if (id != null) {
sb.append(" id='");
sb.append(id);
sb.append("'");
}
sb.append(">");
if (show_default) {
sb.append("<option value='null'>Select...</option>");
}
boolean found = false;
for(int a=0;a<options.length;a++) {
sb.append("<option value='");
sb.append(options[a][0]);
sb.append("'");
if (selected_value != null && selected_value.equals(options[a][0])) {
sb.append(" selected");
found = true;
}
sb.append(">");
sb.append(options[a][1]);
sb.append("</option>");
}
if (!found && selected_value != null && selected_text != null) {
sb.append("<option value='");
sb.append(selected_value);
sb.append("' selected>");
sb.append(selected_text);
sb.append("</option>");
}
sb.append("</select>");
return sb.toString();
}
/** Generate html.form.select.
* Invokes select() with show_default = true
*/
public static String select(String name, String id, String[][] options, String selected_value, String selected_text) {
return select(name, id, options, selected_value, selected_text, true);
}
/** Generate HTML stack trace of exception.
*/
public static String toString(Throwable t) {
StringBuilder sb = new StringBuilder();
sb.append("Exception:");
sb.append(t.toString());
sb.append("<br>");
StackTraceElement[] stes = t.getStackTrace();
for(StackTraceElement ste : stes) {
sb.append(ste.toString());
sb.append("<br>");
}
return sb.toString();
}
/** Adds css file. */
public static String addCSSfile(String file) {
return "<link type='stylesheet' href='" + file + "'>";
}
/** Adds css styles inline. */
public static String addCSSinline(String styles) {
return "<style>" + styles + "</style>";
}
/** Adds js file. */
public static String addJSfile(String file) {
return "<script src='" + file + "'></script>";
}
/** Adds js code inline. */
public static String addJSinline(String script) {
return "<script>" + script + "</script>";
}
/** Convert HTML & codes to text format. */
private static String convertAmpCodes(String html) {
StringBuilder txt = new StringBuilder();
int html_len = html.length();
int html_off = 0;
while (html_off < html_len) {
int i1 = html.indexOf('&', html_off);
if (i1 == -1) {
txt.append(html.substring(html_off, html_len));
html_off = html_len;
} else {
if (i1 > 0) {
txt.append(html.substring(html_off, i1));
}
int i2 = html.indexOf(';', i1);
if (i2 == -1) {
JFLog.log("HTML.toText() : amp code left open");
break;
} else {
String tag = html.substring(i1 + 1, i2);
switch (tag) {
case "amp": txt.append("&"); break;
case "lt": txt.append("<"); break;
case "gt": txt.append(">"); break;
}
html_off = i2 + 1;
}
}
}
return txt.toString();
}
/** Converts HTML to text/plain. */
public static String toText(String html) {
StringBuilder txt = new StringBuilder();
int html_len = html.length();
if (debug) JFLog.log("len=" + html_len);
int html_off = 0;
while (html_off < html_len) {
int i1 = html.indexOf('<', html_off);
if (debug) JFLog.log("i1=" + i1);
if (i1 == -1) {
if (debug) JFLog.log("substring=" + html_off + "," + html_len);
txt.append(convertAmpCodes(html.substring(html_off, html_len)));
html_off = html_len;
} else {
if (i1 > 0) {
if (debug) JFLog.log("substring=" + html_off + "," + i1);
txt.append(convertAmpCodes(html.substring(html_off, i1)));
}
int i2 = html.indexOf('>', i1);
if (debug) JFLog.log("i2=" + i2);
if (i2 == -1) {
JFLog.log("HTML.toText() : tag left open");
break;
} else {
String tag = html.substring(i1 + 1, i2);
switch (tag) {
case "br": txt.append("\r\n"); break;
}
html_off = i2 + 1;
}
}
}
return txt.toString();
}
public static void main(String[] args) {
String html = "<h1>This is HTML</h1><br>Converted to text!<br>Here are some amp codes & < >";
System.out.println(html);
String txt = toText(html);
System.out.println(txt);
}
}