Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JDK9 #321

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,13 @@ application while protecting against XSS.
<value>bar</value>
</property>
</javaApiLinks>
<source>6</source>
<source>${maven.compiler.source}</source>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
<version>3.12.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
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
Loading