From fbfe3cc7b3540bfb9be29245c2961a10e1ca4aa5 Mon Sep 17 00:00:00 2001 From: Mike Samuel Date: Mon, 25 Mar 2024 12:32:40 -0600 Subject: [PATCH] empiricism: remove uses of Guava Signed-off-by: Mike Samuel --- empiricism/pom.xml | 5 ++ .../JsonToSerializedHtmlElementTables.java | 68 +++++++------------ pom.xml | 1 + 3 files changed, 30 insertions(+), 44 deletions(-) diff --git a/empiricism/pom.xml b/empiricism/pom.xml index 7d6302ad..fe43e2ff 100644 --- a/empiricism/pom.xml +++ b/empiricism/pom.xml @@ -34,6 +34,11 @@ owasp-java-html-sanitizer ${project.version} + + com.googlecode.owasp-java-html-sanitizer + java8-shim + ${project.version} + javax.json javax.json-api diff --git a/empiricism/src/main/java/org/owasp/html/empiricism/JsonToSerializedHtmlElementTables.java b/empiricism/src/main/java/org/owasp/html/empiricism/JsonToSerializedHtmlElementTables.java index 8e2251d3..0a46ae0e 100644 --- a/empiricism/src/main/java/org/owasp/html/empiricism/JsonToSerializedHtmlElementTables.java +++ b/empiricism/src/main/java/org/owasp/html/empiricism/JsonToSerializedHtmlElementTables.java @@ -3,28 +3,22 @@ import org.owasp.html.HtmlElementTables; import org.owasp.html.HtmlElementTables.HtmlElementNames; -import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; - import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; +import java.nio.charset.StandardCharsets; +import java.util.*; import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonObject; import javax.json.JsonReader; +import static org.owasp.shim.Java8Shim.j8; + /** * Can be run thus: *
@@ -134,7 +128,7 @@ void writePacked(boolean[] arr) {
         }
       }
       boolean[] reunpacked = HtmlElementTables.unpack(packed, arr.length);
-      Preconditions.checkState(Arrays.equals(arr, reunpacked));
+      if (!Arrays.equals(arr, reunpacked)) { throw new IllegalStateException(); }
       line("HtmlElementTables.unpack(new int[] {");
       writeEls(packed);
       line("}, " + arr.length + ")");
@@ -224,16 +218,10 @@ public static void main(String... argv) throws IOException {
         ? argv[1] : "target/HtmlElementTablesCanned.java";
 
     JsonObject obj;
-    FileInputStream in = new FileInputStream(infile);
-    try {
-      JsonReader reader = Json.createReader(in);
-      try {
+    try (FileInputStream in = new FileInputStream(infile)) {
+      try (JsonReader reader = Json.createReader(in)) {
         obj = reader.readObject();
-      } finally {
-        reader.close();
       }
-    } finally {
-      in.close();
     }
 
     SourceLineWriter src = new SourceLineWriter();
@@ -249,12 +237,12 @@ public static void main(String... argv) throws IOException {
     HtmlElementTables.HtmlElementNames elementNames;
     String[] elementNamesArr;
     {
-      ImmutableList.Builder b = ImmutableList.builder();
+      List b = new ArrayList<>();
       JsonArray arr = obj.getJsonArray("elementNames");
       for (int i = 0, n = arr.size(); i < n; ++i) {
         b.add(arr.getString(i));
       }
-      ImmutableList elementNameList = b.build();
+      List elementNameList = j8().listCopyOf(b);
       elementNames = new HtmlElementTables.HtmlElementNames(elementNameList);
       elementNamesArr = elementNameList.toArray(new String[0]);
     }
@@ -297,17 +285,10 @@ public static void main(String... argv) throws IOException {
         "resumable);");
 
     src.lines("}", "}");
-
-    OutputStream out = new FileOutputStream(outfile);
-    try {
-      OutputStreamWriter w = new OutputStreamWriter(out, "UTF-8");
-      try {
+    try (OutputStream out = new FileOutputStream(outfile)) {
+      try (OutputStreamWriter w = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
         w.write(src.getSource());
-      } finally {
-        w.close();
       }
-    } finally {
-      out.close();
     }
   }
 
@@ -382,21 +363,21 @@ private static void newSparseElementToElements(
       JsonObject obj,
       String fieldName,
       SourceLineWriter src) {
-    List arrs = Lists.newArrayList();
+    List arrs = new ArrayList<>();
     for (String elname : obj.keySet()) {
       int ei = en.getElementNameIndex(elname);
-      ImmutableSet.Builder names = ImmutableSet.builder();
+      Set names = new LinkedHashSet<>();
       JsonArray arr = obj.getJsonArray(elname);
       for (int i = 0, n = arr.size(); i < n; ++i) {
         names.add(arr.getString(i));
       }
-      ImmutableSet iset = names.build();
+      Set iset = j8().setCopyOf(names);
       int[] vals = new int[iset.size()];
       int i = 0;
       for (String name : iset) {
         vals[i++] = en.getElementNameIndex(name);
       }
-      Preconditions.checkState(vals.length == i);
+      if (vals.length != 3) { throw new IllegalStateException(); }
       Arrays.sort(vals);
 
       int[] ints = new int[vals.length + 1];
@@ -456,14 +437,13 @@ public int compare(int[] o1, int[] o2) {
     src.line(");");
   }
 
-  private static final
-  ImmutableMap MODEL_BITS =
-      ImmutableMap.builder()
-      .put("comments", HtmlElementTables.TextContentModelBit.COMMENTS)
-      .put("entities", HtmlElementTables.TextContentModelBit.ENTITIES)
-      .put("raw", HtmlElementTables.TextContentModelBit.RAW)
-      .put("text", HtmlElementTables.TextContentModelBit.TEXT)
-      .put("plain_text", HtmlElementTables.TextContentModelBit.PLAIN_TEXT)
-      .put("unended", HtmlElementTables.TextContentModelBit.UNENDED)
-      .build();
+  private static final Map MODEL_BITS =
+      j8().mapOfEntries(
+        j8().mapEntry("comments", HtmlElementTables.TextContentModelBit.COMMENTS),
+        j8().mapEntry("entities", HtmlElementTables.TextContentModelBit.ENTITIES),
+        j8().mapEntry("raw", HtmlElementTables.TextContentModelBit.RAW),
+        j8().mapEntry("text", HtmlElementTables.TextContentModelBit.TEXT),
+        j8().mapEntry("plain_text", HtmlElementTables.TextContentModelBit.PLAIN_TEXT),
+        j8().mapEntry("unended", HtmlElementTables.TextContentModelBit.UNENDED)
+      );
 }
diff --git a/pom.xml b/pom.xml
index 3c626714..3d30a00d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -10,6 +10,7 @@
     java8-shim
     java10-shim
     owasp-java-html-sanitizer
+    empiricism
   
 
   OWASP Java HTML Sanitizer Project Parent