From dd5eb3bcc1a26f67a8cd13f882190fbeb6d17b12 Mon Sep 17 00:00:00 2001 From: Jonathan Hedley Date: Mon, 16 Dec 2024 11:19:04 +1100 Subject: [PATCH] Minor code cleanup --- src/main/java/org/jsoup/helper/HttpConnection.java | 11 +++++++---- src/main/java/org/jsoup/helper/W3CDom.java | 2 ++ src/main/java/org/jsoup/nodes/XmlDeclaration.java | 1 - src/main/java/org/jsoup/parser/HtmlTreeBuilder.java | 1 - src/main/java/org/jsoup/parser/Token.java | 6 ------ src/main/java/org/jsoup/parser/Tokeniser.java | 7 ------- src/main/java/org/jsoup/parser/XmlTreeBuilder.java | 1 - src/main/java/org/jsoup/safety/Safelist.java | 2 +- 8 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/jsoup/helper/HttpConnection.java b/src/main/java/org/jsoup/helper/HttpConnection.java index 501cce58bd..48a5d9c767 100644 --- a/src/main/java/org/jsoup/helper/HttpConnection.java +++ b/src/main/java/org/jsoup/helper/HttpConnection.java @@ -858,10 +858,12 @@ else if (methodHasBody) try { conn.connect(); if (conn.getDoOutput()) { - OutputStream out = conn.getOutputStream(); - try { writePost(req, out, mimeBoundary); } - catch (IOException e) { conn.disconnect(); throw e; } - finally { out.close(); } + try (OutputStream out = conn.getOutputStream()) { + writePost(req, out, mimeBoundary); + } catch (IOException e) { + conn.disconnect(); + throw e; + } } int status = conn.getResponseCode(); @@ -1389,6 +1391,7 @@ public String value() { return value; } + @Override public KeyVal inputStream(InputStream inputStream) { Validate.notNullParam(value, "inputStream"); this.stream = inputStream; diff --git a/src/main/java/org/jsoup/helper/W3CDom.java b/src/main/java/org/jsoup/helper/W3CDom.java index 0f7a491b5c..b4300f924a 100644 --- a/src/main/java/org/jsoup/helper/W3CDom.java +++ b/src/main/java/org/jsoup/helper/W3CDom.java @@ -369,6 +369,7 @@ public W3CBuilder(Document doc) { } } + @Override public void head(org.jsoup.nodes.Node source, int depth) { namespacesStack.push(new HashMap<>(namespacesStack.peek())); // inherit from above on the stack if (source instanceof org.jsoup.nodes.Element) { @@ -416,6 +417,7 @@ private void append(Node append, org.jsoup.nodes.Node source) { dest.appendChild(append); } + @Override public void tail(org.jsoup.nodes.Node source, int depth) { if (source instanceof org.jsoup.nodes.Element && dest.getParentNode() instanceof Element) { dest = dest.getParentNode(); // undescend diff --git a/src/main/java/org/jsoup/nodes/XmlDeclaration.java b/src/main/java/org/jsoup/nodes/XmlDeclaration.java index 1de4c26419..937509712c 100644 --- a/src/main/java/org/jsoup/nodes/XmlDeclaration.java +++ b/src/main/java/org/jsoup/nodes/XmlDeclaration.java @@ -2,7 +2,6 @@ import org.jsoup.SerializationException; import org.jsoup.internal.StringUtil; -import org.jsoup.helper.Validate; import java.io.IOException; diff --git a/src/main/java/org/jsoup/parser/HtmlTreeBuilder.java b/src/main/java/org/jsoup/parser/HtmlTreeBuilder.java index 969e21e06b..eeb4d799d9 100644 --- a/src/main/java/org/jsoup/parser/HtmlTreeBuilder.java +++ b/src/main/java/org/jsoup/parser/HtmlTreeBuilder.java @@ -15,7 +15,6 @@ import org.jspecify.annotations.Nullable; import java.io.Reader; -import java.io.StringReader; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/org/jsoup/parser/Token.java b/src/main/java/org/jsoup/parser/Token.java index 887024e5bc..e1749cf343 100644 --- a/src/main/java/org/jsoup/parser/Token.java +++ b/src/main/java/org/jsoup/parser/Token.java @@ -6,12 +6,6 @@ import org.jsoup.nodes.Range; import org.jspecify.annotations.Nullable; -import java.util.HashMap; -import java.util.Map; - -import static org.jsoup.internal.SharedConstants.*; - - /** * Parse tokens for the Tokeniser. */ diff --git a/src/main/java/org/jsoup/parser/Tokeniser.java b/src/main/java/org/jsoup/parser/Tokeniser.java index 750ee0701f..4df427313e 100644 --- a/src/main/java/org/jsoup/parser/Tokeniser.java +++ b/src/main/java/org/jsoup/parser/Tokeniser.java @@ -332,13 +332,6 @@ void error(String errorMsg, Object... args) { errors.add(new ParseError(reader, errorMsg, args)); } - static boolean currentNodeInHtmlNS() { - // todo: implement namespaces correctly - return true; - // Element currentNode = currentNode(); - // return currentNode != null && currentNode.namespace().equals("HTML"); - } - /** * Utility method to consume reader and unescape entities found within. * @param inAttribute if the text to be unescaped is in an attribute diff --git a/src/main/java/org/jsoup/parser/XmlTreeBuilder.java b/src/main/java/org/jsoup/parser/XmlTreeBuilder.java index 34bdf35f9a..b51d2d1ef8 100644 --- a/src/main/java/org/jsoup/parser/XmlTreeBuilder.java +++ b/src/main/java/org/jsoup/parser/XmlTreeBuilder.java @@ -11,7 +11,6 @@ import org.jsoup.nodes.Node; import org.jsoup.nodes.TextNode; import org.jsoup.nodes.XmlDeclaration; -import org.jspecify.annotations.Nullable; import java.io.Reader; import java.io.StringReader; diff --git a/src/main/java/org/jsoup/safety/Safelist.java b/src/main/java/org/jsoup/safety/Safelist.java index 20ec6118bc..486a23f67f 100644 --- a/src/main/java/org/jsoup/safety/Safelist.java +++ b/src/main/java/org/jsoup/safety/Safelist.java @@ -576,7 +576,7 @@ private boolean testValidProtocol(Element el, Attribute attr, Set prot return false; } - private boolean isValidAnchor(String value) { + private static boolean isValidAnchor(String value) { return value.startsWith("#") && !value.matches(".*\\s.*"); }