3
3
import org .owasp .html .HtmlElementTables ;
4
4
import org .owasp .html .HtmlElementTables .HtmlElementNames ;
5
5
6
- import com .google .common .base .Preconditions ;
7
- import com .google .common .collect .ImmutableList ;
8
- import com .google .common .collect .ImmutableMap ;
9
- import com .google .common .collect .ImmutableSet ;
10
- import com .google .common .collect .Lists ;
11
-
12
6
import java .io .File ;
13
7
import java .io .FileInputStream ;
14
8
import java .io .FileOutputStream ;
15
9
import java .io .IOException ;
16
10
import java .io .OutputStream ;
17
11
import java .io .OutputStreamWriter ;
18
- import java .util .Arrays ;
19
- import java .util .Collections ;
20
- import java .util .Comparator ;
21
- import java .util .List ;
12
+ import java .nio .charset .StandardCharsets ;
13
+ import java .util .*;
22
14
23
15
import javax .json .Json ;
24
16
import javax .json .JsonArray ;
25
17
import javax .json .JsonObject ;
26
18
import javax .json .JsonReader ;
27
19
20
+ import static org .owasp .shim .Java8Shim .j8 ;
21
+
28
22
/**
29
23
* Can be run thus:
30
24
* <pre>
@@ -134,7 +128,7 @@ void writePacked(boolean[] arr) {
134
128
}
135
129
}
136
130
boolean [] reunpacked = HtmlElementTables .unpack (packed , arr .length );
137
- Preconditions . checkState ( Arrays .equals (arr , reunpacked ));
131
+ if (! Arrays .equals (arr , reunpacked )) { throw new IllegalStateException (); }
138
132
line ("HtmlElementTables.unpack(new int[] {" );
139
133
writeEls (packed );
140
134
line ("}, " + arr .length + ")" );
@@ -224,16 +218,10 @@ public static void main(String... argv) throws IOException {
224
218
? argv [1 ] : "target/HtmlElementTablesCanned.java" ;
225
219
226
220
JsonObject obj ;
227
- FileInputStream in = new FileInputStream (infile );
228
- try {
229
- JsonReader reader = Json .createReader (in );
230
- try {
221
+ try (FileInputStream in = new FileInputStream (infile )) {
222
+ try (JsonReader reader = Json .createReader (in )) {
231
223
obj = reader .readObject ();
232
- } finally {
233
- reader .close ();
234
224
}
235
- } finally {
236
- in .close ();
237
225
}
238
226
239
227
SourceLineWriter src = new SourceLineWriter ();
@@ -249,12 +237,12 @@ public static void main(String... argv) throws IOException {
249
237
HtmlElementTables .HtmlElementNames elementNames ;
250
238
String [] elementNamesArr ;
251
239
{
252
- ImmutableList . Builder <String > b = ImmutableList . builder ();
240
+ List <String > b = new ArrayList <> ();
253
241
JsonArray arr = obj .getJsonArray ("elementNames" );
254
242
for (int i = 0 , n = arr .size (); i < n ; ++i ) {
255
243
b .add (arr .getString (i ));
256
244
}
257
- ImmutableList <String > elementNameList = b . build ( );
245
+ List <String > elementNameList = j8 (). listCopyOf ( b );
258
246
elementNames = new HtmlElementTables .HtmlElementNames (elementNameList );
259
247
elementNamesArr = elementNameList .toArray (new String [0 ]);
260
248
}
@@ -297,17 +285,10 @@ public static void main(String... argv) throws IOException {
297
285
"resumable);" );
298
286
299
287
src .lines ("}" , "}" );
300
-
301
- OutputStream out = new FileOutputStream (outfile );
302
- try {
303
- OutputStreamWriter w = new OutputStreamWriter (out , "UTF-8" );
304
- try {
288
+ try (OutputStream out = new FileOutputStream (outfile )) {
289
+ try (OutputStreamWriter w = new OutputStreamWriter (out , StandardCharsets .UTF_8 )) {
305
290
w .write (src .getSource ());
306
- } finally {
307
- w .close ();
308
291
}
309
- } finally {
310
- out .close ();
311
292
}
312
293
}
313
294
@@ -382,21 +363,21 @@ private static void newSparseElementToElements(
382
363
JsonObject obj ,
383
364
String fieldName ,
384
365
SourceLineWriter src ) {
385
- List <int []> arrs = Lists . newArrayList ();
366
+ List <int []> arrs = new ArrayList <> ();
386
367
for (String elname : obj .keySet ()) {
387
368
int ei = en .getElementNameIndex (elname );
388
- ImmutableSet . Builder <String > names = ImmutableSet . builder ();
369
+ Set <String > names = new LinkedHashSet <> ();
389
370
JsonArray arr = obj .getJsonArray (elname );
390
371
for (int i = 0 , n = arr .size (); i < n ; ++i ) {
391
372
names .add (arr .getString (i ));
392
373
}
393
- ImmutableSet <String > iset = names . build ( );
374
+ Set <String > iset = j8 (). setCopyOf ( names );
394
375
int [] vals = new int [iset .size ()];
395
376
int i = 0 ;
396
377
for (String name : iset ) {
397
378
vals [i ++] = en .getElementNameIndex (name );
398
379
}
399
- Preconditions . checkState (vals .length == i );
380
+ if (vals .length != 3 ) { throw new IllegalStateException (); }
400
381
Arrays .sort (vals );
401
382
402
383
int [] ints = new int [vals .length + 1 ];
@@ -456,14 +437,13 @@ public int compare(int[] o1, int[] o2) {
456
437
src .line (");" );
457
438
}
458
439
459
- private static final
460
- ImmutableMap <String , HtmlElementTables .TextContentModelBit > MODEL_BITS =
461
- ImmutableMap .<String , HtmlElementTables .TextContentModelBit >builder ()
462
- .put ("comments" , HtmlElementTables .TextContentModelBit .COMMENTS )
463
- .put ("entities" , HtmlElementTables .TextContentModelBit .ENTITIES )
464
- .put ("raw" , HtmlElementTables .TextContentModelBit .RAW )
465
- .put ("text" , HtmlElementTables .TextContentModelBit .TEXT )
466
- .put ("plain_text" , HtmlElementTables .TextContentModelBit .PLAIN_TEXT )
467
- .put ("unended" , HtmlElementTables .TextContentModelBit .UNENDED )
468
- .build ();
440
+ private static final Map <String , HtmlElementTables .TextContentModelBit > MODEL_BITS =
441
+ j8 ().mapOfEntries (
442
+ j8 ().mapEntry ("comments" , HtmlElementTables .TextContentModelBit .COMMENTS ),
443
+ j8 ().mapEntry ("entities" , HtmlElementTables .TextContentModelBit .ENTITIES ),
444
+ j8 ().mapEntry ("raw" , HtmlElementTables .TextContentModelBit .RAW ),
445
+ j8 ().mapEntry ("text" , HtmlElementTables .TextContentModelBit .TEXT ),
446
+ j8 ().mapEntry ("plain_text" , HtmlElementTables .TextContentModelBit .PLAIN_TEXT ),
447
+ j8 ().mapEntry ("unended" , HtmlElementTables .TextContentModelBit .UNENDED )
448
+ );
469
449
}
0 commit comments