Skip to content

Commit

Permalink
Make work with Java 9
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Strickroth <email@cs-ware.de>
  • Loading branch information
csware committed Feb 6, 2024
1 parent 032d11b commit ccaaa6c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 15 deletions.
29 changes: 29 additions & 0 deletions src/main/java/org/owasp/html/CollectionsHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.owasp.html;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Internal helper for common Collection creation/copy methods
*/
final class CollectionsHelper {
public static <E> List<E> copyToUnmodifiableList(Collection<? extends E> list) {
final ArrayList<E> newList = new ArrayList<>(list.size());
newList.addAll(list);
return Collections.unmodifiableList(newList);
}

public static <E> Set<E> copyToUnmodifiableSet(Collection<? extends E> set) {
return Collections.unmodifiableSet(new HashSet<E>(set));
}

public static <K, V> Map<K, V> copyToUnmodifiableMap(Map<? extends K, ? extends V> map) {
return Collections.unmodifiableMap(new HashMap<>(map));
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/owasp/html/CssSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public Property(
int bits, Set<String> literals,
Map<String, String> fnKeys) {
this.bits = bits;
this.literals = Set.copyOf(literals);
this.fnKeys = Map.copyOf(fnKeys);
this.literals = CollectionsHelper.copyToUnmodifiableSet(literals);
this.fnKeys = CollectionsHelper.copyToUnmodifiableMap(fnKeys);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ final class ElementAndAttributePolicies {
HtmlTagSkipType htmlTagSkipType) {
this.elementName = elementName;
this.elPolicy = elPolicy;
this.attrPolicies = Map.copyOf(attrPolicies);
this.attrPolicies = CollectionsHelper.copyToUnmodifiableMap(attrPolicies);
this.htmlTagSkipType = htmlTagSkipType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ class ElementAndAttributePolicyBasedSanitizerPolicy
Map<String, ElementAndAttributePolicies> elAndAttrPolicies,
Set<String> allowedTextContainers) {
this.out = out;
this.elAndAttrPolicies = Map.copyOf(elAndAttrPolicies);
this.allowedTextContainers = Set.copyOf(allowedTextContainers);
this.elAndAttrPolicies = CollectionsHelper.copyToUnmodifiableMap(elAndAttrPolicies);
this.allowedTextContainers = CollectionsHelper.copyToUnmodifiableSet(allowedTextContainers);
}

static final Set<String> SKIPPABLE_ELEMENT_CONTENT
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/owasp/html/HtmlElementTables.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public static final class HtmlElementNames {

/** */
public HtmlElementNames(List<String> canonNames) {
this.canonNames = List.copyOf(canonNames);
this.canonNames = CollectionsHelper.copyToUnmodifiableList(canonNames);
}

/** */
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/owasp/html/HtmlPolicyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ public PolicyFactory toFactory() {

return new PolicyFactory(
compiled.compiledPolicies, Collections.unmodifiableSet(textContainerSetBuilder),
Map.copyOf(compiled.globalAttrPolicies),
CollectionsHelper.copyToUnmodifiableMap(compiled.globalAttrPolicies),
preprocessor, postprocessor);
}

Expand Down Expand Up @@ -740,7 +740,7 @@ private CompiledState compilePolicies() {
Map<String, AttributePolicy> globalAttrPolicies
= new LinkedHashMap<>(this.globalAttrPolicies);
@SuppressWarnings("hiding")
Set<String> allowedProtocols = Set.copyOf(this.allowedProtocols);
Set<String> allowedProtocols = CollectionsHelper.copyToUnmodifiableSet(this.allowedProtocols);

// Implement requireRelsOnLinks & skip...
{
Expand Down Expand Up @@ -874,7 +874,7 @@ public final class AttributeBuilder {
private AttributePolicy policy = AttributePolicy.IDENTITY_ATTRIBUTE_POLICY;

AttributeBuilder(List<? extends String> attributeNames) {
this.attributeNames = List.copyOf(attributeNames);
this.attributeNames = CollectionsHelper.copyToUnmodifiableList(attributeNames);
}

/**
Expand Down Expand Up @@ -939,7 +939,7 @@ public AttributeBuilder matching(
*/
public AttributeBuilder matching(
final boolean ignoreCase, Set<? extends String> allowedValues) {
final Set<String> allowed = Set.copyOf(allowedValues);
final Set<String> allowed = CollectionsHelper.copyToUnmodifiableSet(allowedValues);
return matching(new AttributePolicy() {
public @Nullable String apply(
String elementName, String attributeName, String uncanonValue) {
Expand Down Expand Up @@ -1007,13 +1007,13 @@ static RelsOnLinksPolicy create(
RelsOnLinksPolicy(
Set<? extends String> extra,
Set<? extends String> skip) {
this.extra = Set.copyOf(extra);
this.skip = Set.copyOf(skip);
this.extra = CollectionsHelper.copyToUnmodifiableSet(extra);
this.skip = CollectionsHelper.copyToUnmodifiableSet(skip);
Set<String> targetOnly = new HashSet<>();
targetOnly.addAll(DEFAULT_RELS_ON_TARGETTED_LINKS);
targetOnly.removeAll(extra);
targetOnly.removeAll(skip);
this.whenTargetPresent = List.copyOf(targetOnly);
this.whenTargetPresent = CollectionsHelper.copyToUnmodifiableList(targetOnly);
}

private static int indexOfAttributeValue(
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/owasp/html/Benchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

package org.owasp.html;

import java.io.File;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.ListIterator;

Expand All @@ -58,7 +58,7 @@ public class Benchmark {
* specifies a benchmark to run and unspecified ones are not run.
*/
public static void main(String[] args) throws Exception {
String html = new String(Files.readAllBytes(new File(args[0]).toPath()), StandardCharsets.UTF_8);
String html = new String(Files.readAllBytes(Paths.get(args[0])), StandardCharsets.UTF_8);

boolean timeLibhtmlparser = true;
boolean timeSanitize = true;
Expand Down

0 comments on commit ccaaa6c

Please sign in to comment.