33import org .owasp .html .HtmlElementTables ;
44import org .owasp .html .HtmlElementTables .HtmlElementNames ;
55
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-
126import java .io .File ;
137import java .io .FileInputStream ;
148import java .io .FileOutputStream ;
159import java .io .IOException ;
1610import java .io .OutputStream ;
1711import 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 .*;
2214
2315import javax .json .Json ;
2416import javax .json .JsonArray ;
2517import javax .json .JsonObject ;
2618import javax .json .JsonReader ;
2719
20+ import static org .owasp .shim .Java8Shim .j8 ;
21+
2822/**
2923 * Can be run thus:
3024 * <pre>
@@ -134,7 +128,7 @@ void writePacked(boolean[] arr) {
134128 }
135129 }
136130 boolean [] reunpacked = HtmlElementTables .unpack (packed , arr .length );
137- Preconditions . checkState ( Arrays .equals (arr , reunpacked ));
131+ if (! Arrays .equals (arr , reunpacked )) { throw new IllegalStateException (); }
138132 line ("HtmlElementTables.unpack(new int[] {" );
139133 writeEls (packed );
140134 line ("}, " + arr .length + ")" );
@@ -224,16 +218,10 @@ public static void main(String... argv) throws IOException {
224218 ? argv [1 ] : "target/HtmlElementTablesCanned.java" ;
225219
226220 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 )) {
231223 obj = reader .readObject ();
232- } finally {
233- reader .close ();
234224 }
235- } finally {
236- in .close ();
237225 }
238226
239227 SourceLineWriter src = new SourceLineWriter ();
@@ -249,12 +237,12 @@ public static void main(String... argv) throws IOException {
249237 HtmlElementTables .HtmlElementNames elementNames ;
250238 String [] elementNamesArr ;
251239 {
252- ImmutableList . Builder <String > b = ImmutableList . builder ();
240+ List <String > b = new ArrayList <> ();
253241 JsonArray arr = obj .getJsonArray ("elementNames" );
254242 for (int i = 0 , n = arr .size (); i < n ; ++i ) {
255243 b .add (arr .getString (i ));
256244 }
257- ImmutableList <String > elementNameList = b . build ( );
245+ List <String > elementNameList = j8 (). listCopyOf ( b );
258246 elementNames = new HtmlElementTables .HtmlElementNames (elementNameList );
259247 elementNamesArr = elementNameList .toArray (new String [0 ]);
260248 }
@@ -297,17 +285,10 @@ public static void main(String... argv) throws IOException {
297285 "resumable);" );
298286
299287 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 )) {
305290 w .write (src .getSource ());
306- } finally {
307- w .close ();
308291 }
309- } finally {
310- out .close ();
311292 }
312293 }
313294
@@ -382,21 +363,21 @@ private static void newSparseElementToElements(
382363 JsonObject obj ,
383364 String fieldName ,
384365 SourceLineWriter src ) {
385- List <int []> arrs = Lists . newArrayList ();
366+ List <int []> arrs = new ArrayList <> ();
386367 for (String elname : obj .keySet ()) {
387368 int ei = en .getElementNameIndex (elname );
388- ImmutableSet . Builder <String > names = ImmutableSet . builder ();
369+ Set <String > names = new LinkedHashSet <> ();
389370 JsonArray arr = obj .getJsonArray (elname );
390371 for (int i = 0 , n = arr .size (); i < n ; ++i ) {
391372 names .add (arr .getString (i ));
392373 }
393- ImmutableSet <String > iset = names . build ( );
374+ Set <String > iset = j8 (). setCopyOf ( names );
394375 int [] vals = new int [iset .size ()];
395376 int i = 0 ;
396377 for (String name : iset ) {
397378 vals [i ++] = en .getElementNameIndex (name );
398379 }
399- Preconditions . checkState (vals .length == i );
380+ if (vals .length != 3 ) { throw new IllegalStateException (); }
400381 Arrays .sort (vals );
401382
402383 int [] ints = new int [vals .length + 1 ];
@@ -456,14 +437,13 @@ public int compare(int[] o1, int[] o2) {
456437 src .line (");" );
457438 }
458439
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+ );
469449}
0 commit comments