diff --git a/src/main/java/org/jsoup/nodes/Comment.java b/src/main/java/org/jsoup/nodes/Comment.java index 848d82d69e..f6ef7da61d 100644 --- a/src/main/java/org/jsoup/nodes/Comment.java +++ b/src/main/java/org/jsoup/nodes/Comment.java @@ -16,7 +16,7 @@ public class Comment extends LeafNode { @param data The contents of the comment */ public Comment(String data) { - value = data; + super(data); } @Override public String nodeName() { diff --git a/src/main/java/org/jsoup/nodes/DataNode.java b/src/main/java/org/jsoup/nodes/DataNode.java index 0580a11ab0..c63fe74073 100644 --- a/src/main/java/org/jsoup/nodes/DataNode.java +++ b/src/main/java/org/jsoup/nodes/DataNode.java @@ -13,7 +13,7 @@ public class DataNode extends LeafNode { @param data data contents */ public DataNode(String data) { - value = data; + super(data); } @Override public String nodeName() { diff --git a/src/main/java/org/jsoup/nodes/DocumentType.java b/src/main/java/org/jsoup/nodes/DocumentType.java index b2c2dc6623..f1c2237eed 100644 --- a/src/main/java/org/jsoup/nodes/DocumentType.java +++ b/src/main/java/org/jsoup/nodes/DocumentType.java @@ -3,6 +3,7 @@ import org.jsoup.internal.StringUtil; import org.jsoup.helper.Validate; import org.jsoup.nodes.Document.OutputSettings.Syntax; +import org.jspecify.annotations.Nullable; import java.io.IOException; @@ -13,10 +14,10 @@ public class DocumentType extends LeafNode { // todo needs a bit of a chunky cleanup. this level of detail isn't needed public static final String PUBLIC_KEY = "PUBLIC"; public static final String SYSTEM_KEY = "SYSTEM"; - private static final String NAME = "name"; - private static final String PUB_SYS_KEY = "pubSysKey"; // PUBLIC or SYSTEM - private static final String PUBLIC_ID = "publicId"; - private static final String SYSTEM_ID = "systemId"; + private static final String Name = "#doctype"; + private static final String PubSysKey = "pubSysKey"; // PUBLIC or SYSTEM + private static final String PublicId = "publicId"; + private static final String SystemId = "systemId"; // todo: quirk mode from publicId and systemId /** @@ -26,25 +27,25 @@ public class DocumentType extends LeafNode { * @param systemId the doctype's system ID */ public DocumentType(String name, String publicId, String systemId) { - Validate.notNull(name); + super(name); Validate.notNull(publicId); Validate.notNull(systemId); - attr(NAME, name); - attr(PUBLIC_ID, publicId); - attr(SYSTEM_ID, systemId); + attr(Name, name); + attr(PublicId, publicId); + attr(SystemId, systemId); updatePubSyskey(); } - public void setPubSysKey(String value) { + public void setPubSysKey(@Nullable String value) { if (value != null) - attr(PUB_SYS_KEY, value); + attr(PubSysKey, value); } private void updatePubSyskey() { - if (has(PUBLIC_ID)) { - attr(PUB_SYS_KEY, PUBLIC_KEY); - } else if (has(SYSTEM_ID)) - attr(PUB_SYS_KEY, SYSTEM_KEY); + if (has(PublicId)) { + attr(PubSysKey, PUBLIC_KEY); + } else if (has(SystemId)) + attr(PubSysKey, SYSTEM_KEY); } /** @@ -52,7 +53,7 @@ private void updatePubSyskey() { * @return doctype name */ public String name() { - return attr(NAME); + return attr(Name); } /** @@ -60,7 +61,7 @@ public String name() { * @return doctype Public ID */ public String publicId() { - return attr(PUBLIC_ID); + return attr(PublicId); } /** @@ -68,12 +69,12 @@ public String publicId() { * @return doctype System ID */ public String systemId() { - return attr(SYSTEM_ID); + return attr(SystemId); } @Override public String nodeName() { - return "#doctype"; + return Name; } @Override @@ -82,20 +83,20 @@ void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) thr if (siblingIndex > 0 && out.prettyPrint()) accum.append('\n'); - if (out.syntax() == Syntax.html && !has(PUBLIC_ID) && !has(SYSTEM_ID)) { + if (out.syntax() == Syntax.html && !has(PublicId) && !has(SystemId)) { // looks like a html5 doctype, go lowercase for aesthetics accum.append("'); } diff --git a/src/main/java/org/jsoup/nodes/LeafNode.java b/src/main/java/org/jsoup/nodes/LeafNode.java index ade5fece11..adffc3021e 100644 --- a/src/main/java/org/jsoup/nodes/LeafNode.java +++ b/src/main/java/org/jsoup/nodes/LeafNode.java @@ -1,5 +1,7 @@ package org.jsoup.nodes; +import org.jsoup.helper.Validate; + import java.util.List; /** @@ -8,7 +10,16 @@ public abstract class LeafNode extends Node { Object value; // either a string value, or an attribute map (in the rare case multiple attributes are set) - protected final boolean hasAttributes() { + public LeafNode() { + value = ""; + } + + protected LeafNode(String coreValue) { + Validate.notNull(coreValue); + value = coreValue; + } + + @Override protected final boolean hasAttributes() { return value instanceof Attributes; } @@ -19,12 +30,11 @@ public final Attributes attributes() { } private void ensureAttributes() { - if (!hasAttributes()) { - Object coreValue = value; + if (!hasAttributes()) { // then value is String coreValue + String coreValue = (String) value; Attributes attributes = new Attributes(); value = attributes; - if (coreValue != null) - attributes.put(nodeName(), (String) coreValue); + attributes.put(nodeName(), coreValue); } } diff --git a/src/main/java/org/jsoup/nodes/TextNode.java b/src/main/java/org/jsoup/nodes/TextNode.java index 5bbb19ee55..e2a8f3d5e2 100644 --- a/src/main/java/org/jsoup/nodes/TextNode.java +++ b/src/main/java/org/jsoup/nodes/TextNode.java @@ -17,7 +17,7 @@ Create a new TextNode representing the supplied (unencoded) text). @see #createFromEncoded(String) */ public TextNode(String text) { - value = text; + super(text); } @Override public String nodeName() { diff --git a/src/main/java/org/jsoup/nodes/XmlDeclaration.java b/src/main/java/org/jsoup/nodes/XmlDeclaration.java index a032d0fd9a..1de4c26419 100644 --- a/src/main/java/org/jsoup/nodes/XmlDeclaration.java +++ b/src/main/java/org/jsoup/nodes/XmlDeclaration.java @@ -19,8 +19,7 @@ public class XmlDeclaration extends LeafNode { * @param isProcessingInstruction is processing instruction */ public XmlDeclaration(String name, boolean isProcessingInstruction) { - Validate.notNull(name); - value = name; + super(name); this.isProcessingInstruction = isProcessingInstruction; } diff --git a/src/main/java/org/jsoup/parser/Token.java b/src/main/java/org/jsoup/parser/Token.java index d79fefcdea..a0c411ddc8 100644 --- a/src/main/java/org/jsoup/parser/Token.java +++ b/src/main/java/org/jsoup/parser/Token.java @@ -62,7 +62,7 @@ static void reset(StringBuilder sb) { static final class Doctype extends Token { final StringBuilder name = new StringBuilder(); - String pubSysKey = null; + @Nullable String pubSysKey = null; final StringBuilder publicIdentifier = new StringBuilder(); final StringBuilder systemIdentifier = new StringBuilder(); boolean forceQuirks = false; @@ -86,7 +86,7 @@ String getName() { return name.toString(); } - String getPubSysKey() { + @Nullable String getPubSysKey() { return pubSysKey; }