From 5e573849c08641c2179f8b214152f15f51f06aac Mon Sep 17 00:00:00 2001
From: LiuXing-R <lb17724716515@163.com>
Date: Sat, 7 Aug 2021 09:27:57 +0800
Subject: [PATCH] refactor: partially invalid exception thrown

---
 .../java/org/owasp/validator/html/AntiSamy.java     | 13 ++++---------
 .../org/owasp/validator/html/InternalPolicy.java    |  2 +-
 src/main/java/org/owasp/validator/html/Policy.java  |  6 +++---
 .../validator/html/scan/AntiSamyDOMScanner.java     |  6 +-----
 4 files changed, 9 insertions(+), 18 deletions(-)

diff --git a/src/main/java/org/owasp/validator/html/AntiSamy.java b/src/main/java/org/owasp/validator/html/AntiSamy.java
index b7ec9151..d9c2fd95 100644
--- a/src/main/java/org/owasp/validator/html/AntiSamy.java
+++ b/src/main/java/org/owasp/validator/html/AntiSamy.java
@@ -67,11 +67,6 @@ public AntiSamy(Policy policy) {
 	 * @throws PolicyException When there is a problem reading the policy file.
 	 */
 	public CleanResults scan(String taintedHTML) throws ScanException, PolicyException {
-
-		if (policy == null) {
-			throw new PolicyException("No policy loaded");
-		}
-
 		return this.scan(taintedHTML, this.policy, SAX);
 	}
 
@@ -88,9 +83,6 @@ public CleanResults scan(String taintedHTML) throws ScanException, PolicyExcepti
 	 */
 	public CleanResults scan(String taintedHTML, int scanType) throws ScanException, PolicyException {
 
-		if (policy == null) {
-			throw new PolicyException("No policy loaded");
-		}
 		return this.scan(taintedHTML, this.policy, scanType);
 	}
 
@@ -106,7 +98,7 @@ public CleanResults scan(String taintedHTML, int scanType) throws ScanException,
 	 * @throws PolicyException When there is a problem reading the policy file.
 	 */
 	public CleanResults scan(String taintedHTML, Policy policy) throws ScanException, PolicyException {
-		return new AntiSamyDOMScanner(policy).scan(taintedHTML);
+		return this.scan(taintedHTML, policy, DOM);
 	}
 
 	/**
@@ -122,6 +114,9 @@ public CleanResults scan(String taintedHTML, Policy policy) throws ScanException
 	 * @throws PolicyException When there is a problem reading the policy file.
 	 */
 	public CleanResults scan(String taintedHTML, Policy policy, int scanType) throws ScanException, PolicyException {
+		if (policy == null) {
+			throw new PolicyException("No policy loaded");
+		}
 
 		if (scanType == DOM) {
 			return new AntiSamyDOMScanner(policy).scan(taintedHTML);
diff --git a/src/main/java/org/owasp/validator/html/InternalPolicy.java b/src/main/java/org/owasp/validator/html/InternalPolicy.java
index 5ab7ac02..70fb0ae0 100644
--- a/src/main/java/org/owasp/validator/html/InternalPolicy.java
+++ b/src/main/java/org/owasp/validator/html/InternalPolicy.java
@@ -30,7 +30,7 @@ public class InternalPolicy extends Policy {
     private final boolean allowDynamicAttributes;
 
 
-    protected InternalPolicy(ParseContext parseContext) throws PolicyException {
+    protected InternalPolicy(ParseContext parseContext) {
         super(parseContext);
         this.maxInputSize = determineMaxInputSize();
         this.isNofollowAnchors = isTrue(Policy.ANCHORS_NOFOLLOW);
diff --git a/src/main/java/org/owasp/validator/html/Policy.java b/src/main/java/org/owasp/validator/html/Policy.java
index c3f27853..96ceaac7 100644
--- a/src/main/java/org/owasp/validator/html/Policy.java
+++ b/src/main/java/org/owasp/validator/html/Policy.java
@@ -329,7 +329,7 @@ public static Policy getInstance(URL url) throws PolicyException {
         return new InternalPolicy(getParseContext(getTopLevelElement(url), url));
     }
 
-    protected Policy(ParseContext parseContext) throws PolicyException {
+    protected Policy(ParseContext parseContext) {
         this.allowedEmptyTagsMatcher = new TagMatcher(parseContext.allowedEmptyTags);
         this.requiresClosingTagsMatcher = new TagMatcher(parseContext.requireClosingTags);
         this.commonRegularExpressions = Collections.unmodifiableMap(parseContext.commonRegularExpressions);
@@ -657,7 +657,7 @@ private static void parseDirectives(Element root, Map<String, String> directives
      * @param allowedEmptyTags         The tags that can be empty
      */
     private static void parseAllowedEmptyTags(Element allowedEmptyTagsListNode,
-                                              List<String> allowedEmptyTags) throws PolicyException {
+                                              List<String> allowedEmptyTags) {
         if (allowedEmptyTagsListNode != null) {
             for (Element literalNode :
                     getGrandChildrenByTagName(allowedEmptyTagsListNode, "literal-list", "literal")) {
@@ -677,7 +677,7 @@ private static void parseAllowedEmptyTags(Element allowedEmptyTagsListNode,
      * @param requireClosingTags         The list of tags that require closing
      */
     private static void parseRequireClosingTags(Element requireClosingTagsListNode,
-                                                 List<String> requireClosingTags) throws PolicyException {
+                                                 List<String> requireClosingTags) {
         if (requireClosingTagsListNode != null) {
             for (Element literalNode :
                     getGrandChildrenByTagName(requireClosingTagsListNode, "literal-list", "literal")) {
diff --git a/src/main/java/org/owasp/validator/html/scan/AntiSamyDOMScanner.java b/src/main/java/org/owasp/validator/html/scan/AntiSamyDOMScanner.java
index 21a2cb97..6d1b7a53 100644
--- a/src/main/java/org/owasp/validator/html/scan/AntiSamyDOMScanner.java
+++ b/src/main/java/org/owasp/validator/html/scan/AntiSamyDOMScanner.java
@@ -680,11 +680,7 @@ private void removeNode(Node node) {
 	private boolean isAllowedEmptyTag(String tagName) {
         return "head".equals(tagName ) || policy.getAllowedEmptyTags().matches(tagName);
 	}
-
-    public static void main(String[] args) throws PolicyException {
-    }
-
-
+	
     /**
      * Used to promote the children of a parent to accomplish the "filterTag" action.
      *