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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312 | package Torello.JavaDoc.SyntaxHiLite;
import Torello.HTML.InnerTagKeyException;
import Torello.CSS.ClassNameCSSException;
import Torello.HTML.HTMLNode;
import Torello.HTML.TagNode;
import Torello.HTML.SD;
import Torello.HTML.HTMLPage;
import Torello.HTML.Util;
import Torello.Java.FileRW;
import Torello.Java.UnreachableError;
import Torello.Java.ReadOnly.ReadOnlyMap;
import Torello.Java.ReadOnly.ROTreeMapBuilder;
import Torello.Java.Additional.Ret2;
import static Torello.Java.C.BYELLOW;
import static Torello.Java.C.RESET;
import java.util.Vector;
import java.util.Objects;
import java.util.Iterator;
import java.io.IOException;
public class HiLiteHTML
extends AbstractHiLiter
implements java.io.Serializable
{
protected static final long serialVersionUID = 1;
// ********************************************************************************************
// ********************************************************************************************
// Special Attribute HiLiting: Additional / Extra Configuration Class
// ********************************************************************************************
// ********************************************************************************************
public static class Indices
{
private Indices() { }
public static final byte COMMENTS = 1;
public static final byte OPENING_TAGS = 2;
public static final byte CLOSING_TAGS = 3;
public static final byte LT_GT_S = 4;
public static final byte TAG_NAMES = 5;
public static final byte ATTR_KEYS = 6;
public static final byte ATTR_VALS = 7;
public static final byte SPLITS = 8;
// any_array.length => 9 (Note that any_array[0] is unused!)
public static final byte CONFIG_ARR_LEN = 9;
}
// ********************************************************************************************
// ********************************************************************************************
// STATIC-INNER-CLASS: Config
// ********************************************************************************************
// ********************************************************************************************
public static class Config
extends AbstractConfig<Torello.JavaDoc.SyntaxHiLite.HiLiteHTML.Config>
implements java.io.Serializable, Cloneable
{
protected static final long serialVersionUID = 1;
/*
spansOrBolds[0] = who-cares?
spansOrBolds[Indices.COMMENTS] = true;
spansOrBolds[Indices.OPENING_TAGS] = true;
spansOrBolds[Indices.CLOSING_TAGS] = true;
spansOrBolds[Indices.LT_GT_S] = false;
spansOrBolds[Indices.TAG_NAMES] = false;
spansOrBolds[Indices.ATTR_KEYS] = false;
spansOrBolds[Indices.ATTR_VALS] = false;
spansOrBolds[Indices.SPLITS] = false;
*/
private static final boolean[] default_SpansOrBolds = new boolean[]
{ false, true, true, true, false, false, false, false, false };
// This is "Package-Private" - it might be used by the equals, tostring and clone methods!
ROTreeMapBuilder<String, AttribRule> rotmb = null;
public Config()
{ super(Indices.CONFIG_ARR_LEN, default_SpansOrBolds, HiLiteHTML.Config.class); }
private Config(HiLiteHTML.Config c)
{
super(c);
if (c.rotmb != null) this.rotmb = c.rotmb.clone();
}
// ****************************************************************************************
// ****************************************************************************************
// Class-Config - java.lang.Object & java.lang.Cloneable
// ****************************************************************************************
// ****************************************************************************************
public boolean equals(Object other)
{
if (! super.equals(other)) return false;
return ((HiLiteHTML.Config) other).rotmb.equals(this.rotmb);
}
public String toString()
{ return ToStringHTML.classConfig(this); }
public HiLiteHTML.Config clone()
{ return new HiLiteHTML.Config(this); }
// ****************************************************************************************
// ****************************************************************************************
// Class-Config - The Methods added (in addition to) those in super-class "AbstractConfig"
// ****************************************************************************************
// ****************************************************************************************
public synchronized HiLiteHTML.Config addRule(AttribRule rule)
{
AttribRule ar = rule.clone();
ar.validate();
if (this.rotmb == null) this.rotmb = new ROTreeMapBuilder<>();
rotmb.put(rule.attributeName, rule);
return this;
}
public synchronized Iterator<AttribRule> getExtraAttribRules()
{
return (this.rotmb == null)
? java.util.Collections.emptyIterator()
: rotmb.values().iterator();
}
// ****************************************************************************************
// ****************************************************************************************
// STATIC-INNER-CLASS Config: Generate HiLiteHTML
// ****************************************************************************************
// ****************************************************************************************
public synchronized HiLiteHTML generateHiLiter()
{
Ret2<TagNode[], TagNode[]> r2 = generateHiLiterHelper();
final ReadOnlyMap<String, AttribRule> rom = (this.rotmb != null)
? this.rotmb.build()
: null;
return new HiLiteHTML(r2.a /* openers */, r2.b /* closers */, this.utilize, rom);
}
}
// ********************************************************************************************
// ********************************************************************************************
// Special Attribute HiLiting: Additional / Extra Configuration Class
// ********************************************************************************************
// ********************************************************************************************
public static class AttribRule implements java.io.Serializable, Cloneable
{
protected static final long serialVersionUID = 1;
public String attributeName = null;
public String attributeName_ClassName = null;
public String attributeValue_ClassName = null;
public boolean spanOrBold = true;
public AttribRule() { }
public void validate()
{
InnerTagKeyException.check(this.attributeName);
ClassNameCSSException.check(this.attributeName_ClassName);
ClassNameCSSException.check(this.attributeValue_ClassName);
}
public HiLiteHTML.AttribRule clone()
{
final HiLiteHTML.AttribRule ret = new HiLiteHTML.AttribRule();
ret.attributeName = this.attributeName;
ret.attributeName_ClassName = this.attributeName_ClassName;
ret.attributeValue_ClassName = this.attributeValue_ClassName;
ret.spanOrBold = this.spanOrBold;
return ret;
}
public boolean equals(Object other)
{
if (! (other instanceof HiLiteHTML.AttribRule)) return false;
HiLiteHTML.AttribRule o = (HiLiteHTML.AttribRule) other;
return
Objects.equals(this.attributeName, o.attributeName)
&& Objects.equals(this.attributeName_ClassName, o.attributeName_ClassName)
&& Objects.equals(this.attributeValue_ClassName, o.attributeValue_ClassName)
&& (this.spanOrBold == o.spanOrBold);
}
public String toString()
{ return ToStringHTML.classAttribRule(this); }
public int hashCode()
{ return this.attributeName.hashCode(); }
}
// ********************************************************************************************
// ********************************************************************************************
// MAIN-CLASS
// ********************************************************************************************
// ********************************************************************************************
private final DoHiLiteHTML.TagsNonPWA tags;
private final DoHiLiteHTML.TagsPWA tpwa;
public final ReadOnlyMap<String, AttribRule> extraRules;
private HiLiteHTML(
final TagNode[] openers,
final TagNode[] closers,
final boolean[] utilize,
final ReadOnlyMap<String, AttribRule> extraRules
)
{
super(openers, closers, utilize);
this.tags = new DoHiLiteHTML.TagsNonPWA(openers, closers, utilize);
this.tpwa = new DoHiLiteHTML.TagsPWA(openers, closers, utilize);
this.extraRules = extraRules;
}
public static HiLiteHTML generateHiLiterDefault()
{ return new Config().generateHiLiter(); }
public Vector<HTMLNode> hiLite(Vector<HTMLNode> html)
{ return DoHiLiteHTML.run(html, this.tpwa, this.tags); }
public String toString()
{ return ToStringHTML.classHiLiteHTML(this, this.CONFIG_ARR_LEN, this.tags, this.tpwa); }
public boolean equals(Object other)
{
return (! super.equals(other))
? false
: this.extraRules.equals(((HiLiteHTML) other).extraRules);
}
// ********************************************************************************************
// ********************************************************************************************
// Testing This
// ********************************************************************************************
// ********************************************************************************************
public static void main(String[] argv) throws IOException
{
String htmlAsStr =
FileRW.loadFileToString("Torello/BuildJAR/Documentation/Global/Appendable.html");
Vector<HTMLNode> htmlAsVec = HTMLPage.getPageTokens(htmlAsStr, false);
HiLiteHTML hl = new Config().generateHiLiter();
final String css = FileRW.loadFileToString
("Torello/HTML/Tools/SyntaxHiLite/data-files/html-default.css");
String hiLitedHTML =
"<HTML>\n<HEAD>\n" +
"<META HTTP-EQUIV=Cache-Control CONTENT='no-cache, no-store, must-revalidate'>\n" +
"<META HTTP-EQUIV=Pragma CONTENT=no-cache>\n" +
"<META HTTP-EQUIV=Expires CONTENT=0>\n" +
"<STYLE TYPE='text/css'>\n" +
css + '\n' +
"</STYLE>\n" +
"</HEAD>\n" +
"<BODY>\n" +
"<PRE CLASS=HL>\n" +
Util.pageToString(hl.hiLite(htmlAsVec)) + '\n' +
"</PRE>\n" +
"</BODY>\n" +
"</HTML>\n";
System.out.println("Writing File: " + BYELLOW + "OUT/out.html" + RESET);
FileRW.writeFile(hiLitedHTML, "OUT/out.html");
System.out.println(hl.toString());
}
}
|