nokogiriClassCache = new HashMap<>();
nokogiriClassCache.put("Nokogiri::HTML4::Document", (RubyClass)ruby.getClassFromPath("Nokogiri::HTML4::Document"));
nokogiriClassCache.put("Nokogiri::HTML4::ElementDescription",
(RubyClass)ruby.getClassFromPath("Nokogiri::HTML4::ElementDescription"));
@@ -78,6 +78,7 @@ public class NokogiriService implements BasicLibraryService
private void
init(Ruby ruby)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
RubyModule nokogiri = ruby.defineModule("Nokogiri");
RubyModule xmlModule = nokogiri.defineModuleUnder("XML");
RubyModule xmlSaxModule = xmlModule.defineModuleUnder("SAX");
@@ -97,6 +98,7 @@ public class NokogiriService implements BasicLibraryService
private void
createSyntaxErrors(Ruby ruby, RubyModule nokogiri, RubyModule xmlModule)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
RubyClass syntaxError = nokogiri.defineClassUnder("SyntaxError", ruby.getStandardError(),
ruby.getStandardError().getAllocator());
RubyClass xmlSyntaxError = xmlModule.defineClassUnder("SyntaxError", syntaxError, XML_SYNTAXERROR_ALLOCATOR);
@@ -106,6 +108,7 @@ public class NokogiriService implements BasicLibraryService
private RubyClass
createXmlModule(Ruby ruby, RubyModule xmlModule)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
RubyClass node = xmlModule.defineClassUnder("Node", ruby.getObject(), XML_NODE_ALLOCATOR);
node.defineAnnotatedMethods(XmlNode.class);
@@ -183,6 +186,7 @@ public class NokogiriService implements BasicLibraryService
private void
createHtmlModule(Ruby ruby, RubyModule htmlModule)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
RubyClass htmlElemDesc = htmlModule.defineClassUnder("ElementDescription", ruby.getObject(),
HTML_ELEMENT_DESCRIPTION_ALLOCATOR);
htmlElemDesc.defineAnnotatedMethods(Html4ElementDescription.class);
@@ -195,6 +199,7 @@ public class NokogiriService implements BasicLibraryService
private void
createDocuments(Ruby ruby, RubyModule xmlModule, RubyModule htmlModule, RubyClass node)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
RubyClass xmlDocument = xmlModule.defineClassUnder("Document", node, XML_DOCUMENT_ALLOCATOR);
xmlDocument.defineAnnotatedMethods(XmlDocument.class);
@@ -206,6 +211,7 @@ public class NokogiriService implements BasicLibraryService
private void
createSaxModule(Ruby ruby, RubyModule xmlSaxModule, RubyModule htmlSaxModule)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
RubyClass xmlSaxParserContext = xmlSaxModule.defineClassUnder("ParserContext", ruby.getObject(),
XML_SAXPARSER_CONTEXT_ALLOCATOR);
xmlSaxParserContext.defineAnnotatedMethods(XmlSaxParserContext.class);
@@ -225,6 +231,7 @@ public class NokogiriService implements BasicLibraryService
private void
createXsltModule(Ruby ruby, RubyModule xsltModule)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
RubyClass stylesheet = xsltModule.defineClassUnder("Stylesheet", ruby.getObject(), XSLT_STYLESHEET_ALLOCATOR);
stylesheet.defineAnnotatedMethods(XsltStylesheet.class);
}
@@ -259,21 +266,9 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
}
};
- private static ObjectAllocator HTML_ELEMENT_DESCRIPTION_ALLOCATOR =
- new ObjectAllocator()
- {
- public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
- return new Html4ElementDescription(runtime, klazz);
- }
- };
+ private static final ObjectAllocator HTML_ELEMENT_DESCRIPTION_ALLOCATOR = Html4ElementDescription::new;
- private static ObjectAllocator HTML_ENTITY_LOOKUP_ALLOCATOR =
- new ObjectAllocator()
- {
- public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
- return new Html4EntityLookup(runtime, klazz);
- }
- };
+ private static final ObjectAllocator HTML_ENTITY_LOOKUP_ALLOCATOR = Html4EntityLookup::new;
public static final ObjectAllocator XML_ATTR_ALLOCATOR = new ObjectAllocator()
{
@@ -486,25 +481,12 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
}
};
- private static ObjectAllocator XML_ATTRIBUTE_DECL_ALLOCATOR = new ObjectAllocator()
- {
- public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
- return new XmlAttributeDecl(runtime, klazz);
- }
- };
+ private static final ObjectAllocator XML_ATTRIBUTE_DECL_ALLOCATOR = XmlAttributeDecl::new;
- private static ObjectAllocator XML_ENTITY_DECL_ALLOCATOR = new ObjectAllocator()
- {
- public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
- return new XmlEntityDecl(runtime, klazz);
- }
- };
+ private static final ObjectAllocator XML_ENTITY_DECL_ALLOCATOR = XmlEntityDecl::new;
- private static ObjectAllocator XML_ELEMENT_CONTENT_ALLOCATOR = new ObjectAllocator()
- {
- public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
- throw runtime.newNotImplementedError("not implemented");
- }
+ private static final ObjectAllocator XML_ELEMENT_CONTENT_ALLOCATOR = (runtime, klazz) -> {
+ throw runtime.newNotImplementedError("not implemented");
};
public static final ObjectAllocator XML_RELAXNG_ALLOCATOR = new ObjectAllocator()
@@ -537,19 +519,9 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
}
};
- private static final ObjectAllocator XML_SAXPUSHPARSER_ALLOCATOR = new ObjectAllocator()
- {
- public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
- return new XmlSaxPushParser(runtime, klazz);
- }
- };
+ private static final ObjectAllocator XML_SAXPUSHPARSER_ALLOCATOR = XmlSaxPushParser::new;
- private static final ObjectAllocator HTML_SAXPUSHPARSER_ALLOCATOR = new ObjectAllocator()
- {
- public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
- return new Html4SaxPushParser(runtime, klazz);
- }
- };
+ private static final ObjectAllocator HTML_SAXPUSHPARSER_ALLOCATOR = Html4SaxPushParser::new;
public static final ObjectAllocator XML_SCHEMA_ALLOCATOR = new ObjectAllocator()
{
@@ -566,12 +538,7 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
}
};
- public static final ObjectAllocator XML_SYNTAXERROR_ALLOCATOR = new ObjectAllocator()
- {
- public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
- return new XmlSyntaxError(runtime, klazz);
- }
- };
+ public static final ObjectAllocator XML_SYNTAXERROR_ALLOCATOR = XmlSyntaxError::new;
public static final ObjectAllocator XML_TEXT_ALLOCATOR = new ObjectAllocator()
{
@@ -588,12 +555,7 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
}
};
- public static final ObjectAllocator XML_XPATHCONTEXT_ALLOCATOR = new ObjectAllocator()
- {
- public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
- return new XmlXpathContext(runtime, klazz);
- }
- };
+ public static final ObjectAllocator XML_XPATHCONTEXT_ALLOCATOR = XmlXpathContext::new;
public static ObjectAllocator XSLT_STYLESHEET_ALLOCATOR = new ObjectAllocator()
{
diff --git a/ext/java/nokogiri/XmlAttr.java b/ext/java/nokogiri/XmlAttr.java
index 029f6f407f..6273086d8e 100644
--- a/ext/java/nokogiri/XmlAttr.java
+++ b/ext/java/nokogiri/XmlAttr.java
@@ -27,6 +27,8 @@ public class XmlAttr extends XmlNode
{
private static final long serialVersionUID = 1L;
+ // unused
+ @Deprecated
public static final String[] HTML_BOOLEAN_ATTRS = {
"checked", "compact", "declare", "defer", "disabled", "ismap",
"multiple", "nohref", "noresize", "noshade", "nowrap", "readonly",
@@ -45,6 +47,8 @@ public class XmlAttr extends XmlNode
super(ruby, rubyClass);
}
+ // unused
+ @Deprecated
public
XmlAttr(Ruby ruby, RubyClass rubyClass, Node attr)
{
@@ -56,6 +60,7 @@ public class XmlAttr extends XmlNode
init(ThreadContext context, IRubyObject[] args)
{
if (args.length < 2) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.runtime.newArgumentError(args.length, 2);
}
diff --git a/ext/java/nokogiri/XmlAttributeDecl.java b/ext/java/nokogiri/XmlAttributeDecl.java
index 242fc804fd..0b7ffc44e6 100644
--- a/ext/java/nokogiri/XmlAttributeDecl.java
+++ b/ext/java/nokogiri/XmlAttributeDecl.java
@@ -32,7 +32,7 @@ public class XmlAttributeDecl extends XmlNode
/**
* Initialize based on an attributeDecl node from a NekoDTD parsed
* DTD.
- *
+ *
* Internally, XmlAttributeDecl combines these into a single node.
*/
public
@@ -102,17 +102,19 @@ public class XmlAttributeDecl extends XmlNode
{
final String atype = ((Element) node).getAttribute("atype");
- if (atype != null && atype.length() != 0 && atype.charAt(0) == '(') {
+ if (!atype.isEmpty() && atype.charAt(0) == '(') {
// removed enclosing parens
String valueStr = atype.substring(1, atype.length() - 1);
String[] values = valueStr.split("\\|");
RubyArray> enumVals = RubyArray.newArray(context.runtime, values.length);
- for (int i = 0; i < values.length; i++) {
- enumVals.append(context.runtime.newString(values[i]));
+ for (String value : values) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
+ enumVals.append(context.runtime.newString(value));
}
return enumVals;
}
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
return context.runtime.newEmptyArray();
}
diff --git a/ext/java/nokogiri/XmlCdata.java b/ext/java/nokogiri/XmlCdata.java
index f465a56a9e..719feecca8 100644
--- a/ext/java/nokogiri/XmlCdata.java
+++ b/ext/java/nokogiri/XmlCdata.java
@@ -30,6 +30,8 @@ public class XmlCdata extends XmlText
super(ruby, rubyClass);
}
+ // unused
+ @Deprecated
public
XmlCdata(Ruby ruby, RubyClass rubyClass, Node node)
{
@@ -41,16 +43,19 @@ public class XmlCdata extends XmlText
init(ThreadContext context, IRubyObject[] args)
{
if (args.length < 2) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw getRuntime().newArgumentError(args.length, 2);
}
IRubyObject rbDocument = args[0];
content = args[1];
if (content.isNil()) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.runtime.newTypeError("expected second parameter to be a String, received NilClass");
}
if (!(rbDocument instanceof XmlNode)) {
String msg = "expected first parameter to be a Nokogiri::XML::Document, received " + rbDocument.getMetaClass();
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.runtime.newTypeError(msg);
}
if (!(rbDocument instanceof XmlDocument)) {
diff --git a/ext/java/nokogiri/XmlComment.java b/ext/java/nokogiri/XmlComment.java
index f77a91e366..c9f53d6b02 100644
--- a/ext/java/nokogiri/XmlComment.java
+++ b/ext/java/nokogiri/XmlComment.java
@@ -23,6 +23,8 @@ public class XmlComment extends XmlNode
{
private static final long serialVersionUID = 1L;
+ // unused
+ @Deprecated
public
XmlComment(Ruby ruby, RubyClass rubyClass, Node node)
{
@@ -40,6 +42,7 @@ public class XmlComment extends XmlNode
init(ThreadContext context, IRubyObject[] args)
{
if (args.length < 2) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw getRuntime().newArgumentError(args.length, 2);
}
diff --git a/ext/java/nokogiri/XmlDocument.java b/ext/java/nokogiri/XmlDocument.java
index 033a0c64b8..d2ac592050 100644
--- a/ext/java/nokogiri/XmlDocument.java
+++ b/ext/java/nokogiri/XmlDocument.java
@@ -41,7 +41,6 @@
import nokogiri.internals.SaveContextVisitor;
import nokogiri.internals.XmlDomParserContext;
import nokogiri.internals.c14n.CanonicalFilter;
-import nokogiri.internals.c14n.CanonicalizationException;
import nokogiri.internals.c14n.Canonicalizer;
/**
@@ -71,9 +70,6 @@ public class XmlDocument extends XmlNode
private static final ByteList DOCUMENT = ByteList.create("document");
static { DOCUMENT.setEncoding(USASCIIEncoding.INSTANCE); }
- private static boolean substituteEntities = false;
- private static boolean loadExternalSubset = false; // TODO: Verify this.
-
/** cache variables */
protected IRubyObject encoding;
protected IRubyObject url;
@@ -273,7 +269,7 @@ private static class DocumentBuilderFactoryHolder
*
* Create a new document with +version+ (defaults to "1.0")
*/
- @JRubyMethod(name = "new", meta = true, rest = true, required = 0)
+ @JRubyMethod(name = "new", meta = true, rest = true)
public static IRubyObject
rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args)
{
@@ -281,6 +277,7 @@ private static class DocumentBuilderFactoryHolder
XmlDocument xmlDocument;
try {
Document docNode = createNewDocument(runtime);
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
if ("Nokogiri::HTML4::Document".equals(((RubyClass)klazz).getName())) {
xmlDocument = new Html4Document(context.runtime, (RubyClass) klazz, docNode);
} else {
@@ -344,7 +341,6 @@ private static class DocumentBuilderFactoryHolder
public static IRubyObject
load_external_subsets_set(ThreadContext context, IRubyObject cls, IRubyObject value)
{
- XmlDocument.loadExternalSubset = value.isTrue();
return context.nil;
}
@@ -395,8 +391,8 @@ private static class DocumentBuilderFactoryHolder
}
}
IRubyObject[] nodes = xmlNode.getChildren();
- for (int i = 0; i < nodes.length; i++) {
- XmlNode childNode = (XmlNode) nodes[i];
+ for (IRubyObject iRubyObject : nodes) {
+ XmlNode childNode = (XmlNode) iRubyObject;
removeNamespaceRecursively(childNode);
}
}
@@ -471,7 +467,6 @@ private static class DocumentBuilderFactoryHolder
public static IRubyObject
substitute_entities_set(ThreadContext context, IRubyObject cls, IRubyObject value)
{
- XmlDocument.substituteEntities = value.isTrue();
return context.nil;
}
@@ -631,16 +626,18 @@ private static class DocumentBuilderFactoryHolder
{
int mode = 0;
String inclusive_namespace = null;
- Boolean with_comments = false;
+ boolean with_comments = false;
if (args.length > 0 && !(args[0].isNil())) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
mode = RubyFixnum.fix2int(args[0]);
}
if (args.length > 1) {
if (!args[1].isNil() && !(args[1] instanceof List)) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.runtime.newTypeError("Expected array");
}
if (!args[1].isNil()) {
- inclusive_namespace = ((RubyArray)args[1])
+ inclusive_namespace = ((RubyArray>)args[1])
.join(context, context.runtime.newString(" "))
.asString()
.asJavaString(); // OMG I wish I knew JRuby better, this is ugly
diff --git a/ext/java/nokogiri/XmlDocumentFragment.java b/ext/java/nokogiri/XmlDocumentFragment.java
index 406b3d1d1c..59a8b4fd2c 100644
--- a/ext/java/nokogiri/XmlDocumentFragment.java
+++ b/ext/java/nokogiri/XmlDocumentFragment.java
@@ -1,29 +1,13 @@
package nokogiri;
-import static nokogiri.internals.NokogiriHelpers.getLocalNameForNamespace;
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
-import static nokogiri.internals.NokogiriHelpers.getPrefix;
-import static nokogiri.internals.NokogiriHelpers.isNamespace;
-import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import org.jruby.Ruby;
-import org.jruby.RubyArray;
import org.jruby.RubyClass;
-import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
-import org.jruby.runtime.Block;
-import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
-import org.jruby.util.ByteList;
-import org.w3c.dom.Attr;
-import org.w3c.dom.NamedNodeMap;
/**
* Class for Nokogiri::XML::DocumentFragment
@@ -36,6 +20,8 @@ public class XmlDocumentFragment extends XmlNode
{
private static final long serialVersionUID = 1L;
+ // unused
+ @Deprecated
public
XmlDocumentFragment(Ruby ruby)
{
diff --git a/ext/java/nokogiri/XmlDtd.java b/ext/java/nokogiri/XmlDtd.java
index 7dc96c1316..3417872133 100644
--- a/ext/java/nokogiri/XmlDtd.java
+++ b/ext/java/nokogiri/XmlDtd.java
@@ -138,7 +138,7 @@ public class XmlDtd extends XmlNode
* doc
. The attached dtd must be the tree from
* NekoDTD. The owner document of the returned tree will be
* doc.
- *
+ *
* NekoDTD parser returns a new document node containing elements
* representing the dtd declarations. The plan is to get the root
* element and adopt it into the correct document, stripping the
@@ -332,6 +332,7 @@ public class XmlDtd extends XmlNode
public IRubyObject
validate(ThreadContext context, IRubyObject doc)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
RubyArray> errors = RubyArray.newArray(context.getRuntime());
if (doc instanceof XmlDocument) {
errors = (RubyArray)((XmlDocument)doc).getInstanceVariable("@errors");
@@ -450,7 +451,7 @@ public class XmlDtd extends XmlNode
* The node
is either the first child of the root dtd
* node (as returned by getInternalSubset()) or the first child of
* the external subset node (as returned by getExternalSubset()).
- *
+ *
* This recursive function will not descend into an
* 'externalSubset' node, thus for an internal subset it only
* extracts nodes in the internal subset, and for an external
@@ -460,7 +461,7 @@ public class XmlDtd extends XmlNode
protected IRubyObject[]
extractDecls(ThreadContext context, Node node)
{
- List decls = new ArrayList();
+ List decls = new ArrayList<>();
while (node != null) {
if (isExternalSubset(node)) {
break;
diff --git a/ext/java/nokogiri/XmlElement.java b/ext/java/nokogiri/XmlElement.java
index b8e3225edc..5bcb76f754 100644
--- a/ext/java/nokogiri/XmlElement.java
+++ b/ext/java/nokogiri/XmlElement.java
@@ -4,7 +4,6 @@
import org.jruby.RubyClass;
import org.jruby.anno.JRubyClass;
import org.jruby.runtime.ThreadContext;
-import org.jruby.runtime.builtin.IRubyObject;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -27,6 +26,8 @@ public class XmlElement extends XmlNode
super(runtime, klazz);
}
+ // unused
+ @Deprecated
public
XmlElement(Ruby runtime, RubyClass klazz, Node element)
{
diff --git a/ext/java/nokogiri/XmlElementContent.java b/ext/java/nokogiri/XmlElementContent.java
index 501a4557be..d2921467e5 100644
--- a/ext/java/nokogiri/XmlElementContent.java
+++ b/ext/java/nokogiri/XmlElementContent.java
@@ -325,7 +325,7 @@ public IRubyObject value(Ruby runtime)
* moves to the parent of previous sibling). The null position is
* used to indicate the end of a list.
*/
- protected static class NodeIter
+ public static class NodeIter
{
protected Node pre;
protected Node cur;
diff --git a/ext/java/nokogiri/XmlElementDecl.java b/ext/java/nokogiri/XmlElementDecl.java
index bd4bc8cf25..a67b4a75ed 100644
--- a/ext/java/nokogiri/XmlElementDecl.java
+++ b/ext/java/nokogiri/XmlElementDecl.java
@@ -31,6 +31,7 @@ public class XmlElementDecl extends XmlNode
XmlElementDecl(Ruby runtime, RubyClass klazz)
{
super(runtime, klazz);
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
attrDecls = RubyArray.newArray(runtime);
contentModel = runtime.getNil();
}
@@ -49,6 +50,7 @@ public class XmlElementDecl extends XmlNode
setNode(Ruby runtime, Node node)
{
super.setNode(runtime, node);
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
attrDecls = RubyArray.newArray(runtime);
contentModel = runtime.getNil();
}
@@ -136,6 +138,7 @@ public class XmlElementDecl extends XmlNode
public void
appendAttrDecl(XmlAttributeDecl decl)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
attrDecls.append(decl);
}
diff --git a/ext/java/nokogiri/XmlEntityReference.java b/ext/java/nokogiri/XmlEntityReference.java
index 77db5ddb2c..c32ae5ba59 100644
--- a/ext/java/nokogiri/XmlEntityReference.java
+++ b/ext/java/nokogiri/XmlEntityReference.java
@@ -1,6 +1,5 @@
package nokogiri;
-import static nokogiri.internals.NokogiriHelpers.getCachedNodeOrCreate;
import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
import nokogiri.internals.SaveContextVisitor;
@@ -31,6 +30,8 @@ public class XmlEntityReference extends XmlNode
super(ruby, klazz);
}
+ // unused
+ @Deprecated
public
XmlEntityReference(Ruby ruby, RubyClass klass, Node node)
{
@@ -41,6 +42,7 @@ public class XmlEntityReference extends XmlNode
init(ThreadContext context, IRubyObject[] args)
{
if (args.length < 2) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.runtime.newArgumentError(args.length, 2);
}
diff --git a/ext/java/nokogiri/XmlNamespace.java b/ext/java/nokogiri/XmlNamespace.java
index b2804bb7bc..030cfac529 100644
--- a/ext/java/nokogiri/XmlNamespace.java
+++ b/ext/java/nokogiri/XmlNamespace.java
@@ -106,7 +106,8 @@ public class XmlNamespace extends RubyObject
Document document = owner.getOwnerDocument();
XmlDocument xmlDocument = (XmlDocument) getCachedNodeOrCreate(runtime, document);
- assert xmlDocument.getNamespaceCache().get(prefixStr, hrefStr) == null;
+ XmlNamespace cachedNamespace = xmlDocument.getNamespaceCache().get(prefixStr, hrefStr);
+ assert cachedNamespace == null;
// creating XmlNamespace instance
String attrName = "xmlns";
diff --git a/ext/java/nokogiri/XmlNode.java b/ext/java/nokogiri/XmlNode.java
index c74650cd6b..3386301688 100644
--- a/ext/java/nokogiri/XmlNode.java
+++ b/ext/java/nokogiri/XmlNode.java
@@ -1,10 +1,7 @@
package nokogiri;
-import static java.lang.Math.max;
import static nokogiri.internals.NokogiriHelpers.*;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.*;
@@ -16,12 +13,10 @@
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.RubyInteger;
-import org.jruby.RubyModule;
import org.jruby.RubyObject;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
-import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.Block;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
@@ -89,6 +84,7 @@ public class XmlNode extends RubyObject
{
if (!(node instanceof XmlNode)) {
final Ruby runtime = context.runtime;
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw runtime.newTypeError(node == null ? runtime.getNil() : node, getNokogiriClass(runtime, "Nokogiri::XML::Node"));
}
return (XmlNode) node;
@@ -107,7 +103,7 @@ public class XmlNode extends RubyObject
/**
* Coalesce to adjacent TextNodes.
- * @param context
+ * @param context The current context
* @param prev Previous node to cur.
* @param cur Next node to prev.
*/
@@ -132,13 +128,14 @@ public class XmlNode extends RubyObject
* are text nodes, the content will be merged into
* anchorNode
and the redundant nodes will be removed
* from the DOM.
- *
+ *
* To match libxml behavior (?) the final content of
* anchorNode
and any removed nodes will be
* identical.
*
- * @param context
- * @param anchorNode
+ * @param context the current context
+ * @param anchorNode the anchor node
+ * @param scheme the scheme
*/
protected static void
coalesceTextNodes(ThreadContext context,
@@ -230,18 +227,18 @@ public class XmlNode extends RubyObject
* object as the only argument. If cls
is
* Nokogiri::XML::Node, creates a new Nokogiri::XML::Element
* instead.
- *
+ *
* This static method seems to be inherited, strangely enough.
* E.g. creating a new XmlAttr from Ruby code calls this method if
* XmlAttr does not define its own 'new' method.
- *
+ *
* Since there is some Java bookkeeping that always needs to
* happen, we don't define the 'initialize' method in Java because
* we'd have to count on subclasses calling 'super'.
- *
+ *
* The main consequence of this is that every subclass needs to
* define its own 'new' method.
- *
+ *
* As a convenience, this method does the following:
*
*
@@ -269,10 +266,12 @@ public class XmlNode extends RubyObject
Ruby ruby = context.runtime;
RubyClass klazz = (RubyClass) cls;
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
if ("Nokogiri::XML::Node".equals(klazz.getName())) {
klazz = getNokogiriClass(ruby, "Nokogiri::XML::Element");
}
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
XmlNode xmlNode = (XmlNode) klazz.allocate();
xmlNode.init(context, args);
xmlNode.callInit(args, block);
@@ -289,7 +288,7 @@ public class XmlNode extends RubyObject
* interact means that subclasses cannot arbitrarily change the
* require arguments by defining an 'initialize' method. This is
* how the C libxml wrapper works also.
- *
+ *
* As written it performs initialization for a new Element with
* the given name
within the document
* doc
. So XmlElement need not override this. This
@@ -301,6 +300,7 @@ public class XmlNode extends RubyObject
init(ThreadContext context, IRubyObject[] args)
{
if (args.length < 2) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.runtime.newArgumentError(args.length, 2);
}
@@ -366,17 +366,19 @@ public class XmlNode extends RubyObject
public boolean
isElement()
{
- if (node instanceof Element) { return true; } // in case of subclassing
- else { return false; }
+ // in case of subclassing
+ return node instanceof Element;
}
+ // unused
+ @Deprecated
public boolean
isProcessingInstruction() { return false; }
/**
* Return the string value of the attribute key
or
* nil.
- *
+ *
* Only applies where the underlying Node is an Element node, but
* implemented here in XmlNode because not all nodes with
* underlying Element nodes subclass XmlElement, such as the DTD
@@ -401,7 +403,7 @@ public class XmlNode extends RubyObject
if (node.getNodeType() != Node.ELEMENT_NODE) { return null; }
String value = ((Element)node).getAttribute(key);
- return value.length() == 0 ? null : value;
+ return value.isEmpty() ? null : value;
}
/**
@@ -438,25 +440,24 @@ public class XmlNode extends RubyObject
if (ns_inherit.isTrue()) {
set_namespace(context, ((XmlNode)parent(context)).namespace(context));
}
- return;
- }
-
- String currentPrefix = e.getParentNode().lookupPrefix(nsURI);
- String currentURI = e.getParentNode().lookupNamespaceURI(prefix);
- boolean isDefault = e.getParentNode().isDefaultNamespace(nsURI);
-
- // add xmlns attribute if this is a new root node or if the node's
- // namespace isn't a default namespace in the new document
- if (e.getParentNode().getNodeType() == Node.DOCUMENT_NODE) {
- // this is the root node, so we must set the namespaces attributes anyway
- e.setAttribute(prefix == null ? "xmlns" : "xmlns:" + prefix, nsURI);
- } else if (prefix == null) {
- // this is a default namespace but isn't the default where this node is being added
- if (!isDefault) { e.setAttribute("xmlns", nsURI); }
- } else if (!prefix.equals(currentPrefix) || nsURI.equals(currentURI)) {
- // this is a prefixed namespace
- // but doesn't have the same prefix or the prefix is set to a different URI
- e.setAttribute("xmlns:" + prefix, nsURI);
+ } else {
+ String currentPrefix = e.getParentNode().lookupPrefix(nsURI);
+ String currentURI = e.getParentNode().lookupNamespaceURI(prefix);
+ boolean isDefault = e.getParentNode().isDefaultNamespace(nsURI);
+
+ // add xmlns attribute if this is a new root node or if the node's
+ // namespace isn't a default namespace in the new document
+ if (e.getParentNode().getNodeType() == Node.DOCUMENT_NODE) {
+ // this is the root node, so we must set the namespaces attributes anyway
+ e.setAttribute(prefix == null ? "xmlns" : "xmlns:" + prefix, nsURI);
+ } else if (prefix == null) {
+ // this is a default namespace but isn't the default where this node is being added
+ if (!isDefault) { e.setAttribute("xmlns", nsURI); }
+ } else if (!prefix.equals(currentPrefix) || !nsURI.equals(currentURI)) {
+ // this is a prefixed namespace
+ // but doesn't have the same prefix or the prefix is set to a different URI
+ e.setAttribute("xmlns:" + prefix, nsURI);
+ }
}
if (e.hasAttributes()) {
@@ -482,7 +483,7 @@ public class XmlNode extends RubyObject
nsUri = null;
}
- if (!(nsUri == null || "".equals(nsUri) || "http://www.w3.org/XML/1998/namespace".equals(nsUri))) {
+ if (!(nsUri == null || nsUri.isEmpty() || "http://www.w3.org/XML/1998/namespace".equals(nsUri))) {
// Create a new namespace object and add it to the document namespace cache.
// TODO: why do we need the namespace cache ?
XmlNamespace.createFromAttr(context.runtime, attr);
@@ -491,6 +492,32 @@ public class XmlNode extends RubyObject
}
}
+ // if this namespace is a duplicate of what's already in the document, remove it.
+ // a "duplicate" here is if the prefix and the URI both match what resolves in the parent.
+ if (e.getParentNode().getNodeType() == Node.ELEMENT_NODE) {
+ RubyArray> nsdefs = this.namespace_definitions(context);
+ for (int j = 0 ; j < nsdefs.getLength() ; j++) {
+ XmlNamespace ns = (XmlNamespace)nsdefs.get(j);
+
+ String selfPrefix = ns.getPrefix();
+ String selfURI = ns.getHref();
+ String parentPrefix = e.getParentNode().lookupPrefix(selfURI);
+ String parentURI = e.getParentNode().lookupNamespaceURI(selfPrefix);
+
+ boolean prefixMatch = ((selfPrefix == null && parentPrefix == null) ||
+ (selfPrefix != null && selfPrefix.equals(parentPrefix)));
+ boolean uriMatch = ((selfURI == null && parentURI == null) ||
+ (selfURI != null && selfURI.equals(parentURI)));
+
+ if (prefixMatch && uriMatch) {
+ String attrName = "xmlns";
+ if (selfPrefix != null && !selfPrefix.isEmpty()) { attrName = attrName + ':' + selfPrefix; }
+
+ e.removeAttribute(attrName);
+ }
+ }
+ }
+
if (this.node.hasChildNodes()) {
relink_namespace(context, getChildren());
}
@@ -499,9 +526,9 @@ public class XmlNode extends RubyObject
static void
relink_namespace(ThreadContext context, IRubyObject[] nodes)
{
- for (int i = 0; i < nodes.length; i++) {
- if (nodes[i] instanceof XmlNode) {
- ((XmlNode) nodes[i]).relink_namespace(context);
+ for (IRubyObject iRubyObject : nodes) {
+ if (iRubyObject instanceof XmlNode) {
+ ((XmlNode) iRubyObject).relink_namespace(context);
}
}
}
@@ -519,8 +546,7 @@ public class XmlNode extends RubyObject
acceptChildren(ThreadContext context, IRubyObject[] nodes, SaveContextVisitor visitor)
{
if (nodes.length > 0) {
- for (int i = 0; i < nodes.length; i++) {
- Object item = nodes[i];
+ for (Object item : nodes) {
if (item instanceof XmlNode) {
((XmlNode) item).accept(context, visitor);
} else if (item instanceof XmlNamespace) {
@@ -652,7 +678,7 @@ public class XmlNode extends RubyObject
Node attribute = attributes.item(j);
String localName = attribute.getLocalName();
if (localName == null) {
- continue;
+ localName = attribute.getNodeName();
}
if (localName.equals(name)) {
return getCachedNodeOrCreate(context.runtime, attribute);
@@ -669,12 +695,14 @@ public class XmlNode extends RubyObject
NamedNodeMap nodeMap = this.node.getAttributes();
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
if (nodeMap == null) { return runtime.newEmptyArray(); }
RubyArray> attr = runtime.newArray(nodeMap.getLength());
final XmlDocument doc = document(context.runtime);
for (int i = 0; i < nodeMap.getLength(); i++) {
if ((doc instanceof Html4Document) || !NokogiriHelpers.isNamespace(nodeMap.item(i))) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
attr.append(getCachedNodeOrCreate(runtime, nodeMap.item(i)));
}
}
@@ -742,7 +770,7 @@ public class XmlNode extends RubyObject
first_element_child(ThreadContext context)
{
List elementNodes = getElements(node, true);
- if (elementNodes.size() == 0) { return context.nil; }
+ if (elementNodes.isEmpty()) { return context.nil; }
return getCachedNodeOrCreate(context.runtime, elementNodes.get(0));
}
@@ -751,7 +779,7 @@ public class XmlNode extends RubyObject
last_element_child(ThreadContext context)
{
List elementNodes = getElements(node, false);
- if (elementNodes.size() == 0) { return context.nil; }
+ if (elementNodes.isEmpty()) { return context.nil; }
return getCachedNodeOrCreate(context.runtime, elementNodes.get(elementNodes.size() - 1));
}
@@ -771,7 +799,7 @@ public class XmlNode extends RubyObject
if (children.getLength() == 0) {
return Collections.emptyList();
}
- ArrayList elements = new ArrayList();
+ ArrayList elements = new ArrayList<>();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
@@ -833,7 +861,6 @@ public class XmlNode extends RubyObject
{
RubyClass klass;
XmlDomParserContext ctx;
- InputStream istream;
final Ruby runtime = context.runtime;
@@ -864,6 +891,7 @@ public class XmlNode extends RubyObject
RubyArray> docErrors = getErrors(doc);
if (checkNewErrors(documentErrors, docErrors)) {
for (int i = 0; i < docErrors.getLength(); i++) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
documentErrors.append(docErrors.entry(i));
}
document.setInstanceVariable("@errors", documentErrors);
@@ -887,14 +915,15 @@ public class XmlNode extends RubyObject
getErrors(XmlDocument document)
{
IRubyObject obj = document.getInstanceVariable("@errors");
- if (obj instanceof RubyArray) { return (RubyArray) obj; }
+ if (obj instanceof RubyArray) { return (RubyArray>) obj; }
return RubyArray.newEmptyArray(document.getRuntime());
}
private static boolean
checkNewErrors(RubyArray> baseErrors, RubyArray> newErrors)
{
- int length = ((RubyArray) newErrors.op_diff(baseErrors)).size();
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
+ int length = ((RubyArray>) newErrors.op_diff(baseErrors)).size();
return length > 0;
}
@@ -980,6 +1009,7 @@ public class XmlNode extends RubyObject
public IRubyObject
initialize_copy_with_args(ThreadContext context, IRubyObject other, IRubyObject level, IRubyObject document)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
boolean deep = level instanceof RubyInteger && RubyFixnum.fix2int(level) != 0;
this.node = asXmlNode(context, other).node.cloneNode(deep);
setDocument(context, (XmlDocument)document);
@@ -1049,8 +1079,7 @@ public class XmlNode extends RubyObject
XmlDocument xdoc =
(XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
- IRubyObject xdtd = xdoc.getInternalSubset(context);
- return xdtd;
+ return xdoc.getInternalSubset(context);
}
@JRubyMethod
@@ -1072,9 +1101,8 @@ public class XmlNode extends RubyObject
XmlDocument xdoc =
(XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
- IRubyObject xdtd = xdoc.createInternalSubset(context, name,
+ return xdoc.createInternalSubset(context, name,
external_id, system_id);
- return xdtd;
}
@JRubyMethod
@@ -1089,8 +1117,7 @@ public class XmlNode extends RubyObject
XmlDocument xdoc =
(XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
- IRubyObject xdtd = xdoc.getExternalSubset(context);
- return xdtd;
+ return xdoc.getExternalSubset(context);
}
@JRubyMethod
@@ -1110,8 +1137,7 @@ public class XmlNode extends RubyObject
return context.getRuntime().getNil();
}
XmlDocument xdoc = (XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
- IRubyObject xdtd = xdoc.createExternalSubset(context, name, external_id, system_id);
- return xdtd;
+ return xdoc.createExternalSubset(context, name, external_id, system_id);
}
/**
@@ -1175,28 +1201,23 @@ public class XmlNode extends RubyObject
public RubyArray>
namespace_definitions(ThreadContext context)
{
- // don't use namespace_definitions cache anymore since
- // namespaces might be deleted. Reflecting the result of
- // namespace removals is complicated, so the cache might not be
- // updated.
+ // don't use namespace_definitions cache anymore since namespaces might be deleted. Reflecting
+ // the result of namespace removals is complicated, so the cache might not be updated.
final XmlDocument doc = document(context.runtime);
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
if (doc == null) { return context.runtime.newEmptyArray(); }
if (doc instanceof Html4Document) { return context.runtime.newEmptyArray(); }
- List namespaces = doc.getNamespaceCache().get(node);
- return RubyArray.newArray(context.runtime, namespaces);
-
- // // TODO: I think this implementation would be better but there are edge cases
- // // See https://github.com/sparklemotion/nokogiri/issues/2543
- // RubyArray> nsdefs = RubyArray.newArray(context.getRuntime());
- // NamedNodeMap attrs = node.getAttributes();
- // for (int j = 0 ; j < attrs.getLength() ; j++) {
- // Attr attr = (Attr)attrs.item(j);
- // if ("http://www.w3.org/2000/xmlns/" == attr.getNamespaceURI()) {
- // nsdefs.append(XmlNamespace.createFromAttr(context.getRuntime(), attr));
- // }
- // }
- // return nsdefs;
+ RubyArray> nsdefs = RubyArray.newArray(context.getRuntime());
+ NamedNodeMap attrs = node.getAttributes();
+ for (int j = 0 ; j < attrs.getLength() ; j++) {
+ Attr attr = (Attr)attrs.item(j);
+ if ("http://www.w3.org/2000/xmlns/".equals(attr.getNamespaceURI())) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
+ nsdefs.append(XmlNamespace.createFromAttr(context.getRuntime(), attr));
+ }
+ }
+ return nsdefs;
}
/**
@@ -1208,6 +1229,7 @@ public class XmlNode extends RubyObject
namespace_scopes(ThreadContext context)
{
final XmlDocument doc = document(context.runtime);
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
if (doc == null) { return context.runtime.newEmptyArray(); }
if (doc instanceof Html4Document) { return context.runtime.newEmptyArray(); }
@@ -1219,6 +1241,8 @@ public class XmlNode extends RubyObject
} else {
previousNode = findPreviousElement(node);
}
+
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
if (previousNode == null) { return context.runtime.newEmptyArray(); }
final RubyArray> scoped_namespaces = context.runtime.newArray();
@@ -1228,6 +1252,7 @@ public class XmlNode extends RubyObject
List namespaces = nsCache.get(previous);
for (XmlNamespace namespace : namespaces) {
if (prefixes_in_scope.contains(namespace.getPrefix())) { continue; }
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
scoped_namespaces.append(namespace);
prefixes_in_scope.add(namespace.getPrefix());
}
@@ -1261,7 +1286,7 @@ public class XmlNode extends RubyObject
{
String javaContent = rubyStringToString(content);
node.setTextContent(javaContent);
- if (javaContent == null || javaContent.length() == 0) { return; }
+ if (javaContent == null || javaContent.isEmpty()) { return; }
if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) { return; }
if (node.getFirstChild() != null) {
node.getFirstChild().setUserData(NokogiriHelpers.ENCODED_STRING, true, null);
@@ -1321,6 +1346,7 @@ public class XmlNode extends RubyObject
IRubyObject encoding = args[1];
IRubyObject indentString = args[2];
IRubyObject options_rb = args[3];
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
int options = RubyFixnum.fix2int(options_rb);
String encString = rubyStringToString(encoding);
@@ -1361,8 +1387,7 @@ public class XmlNode extends RubyObject
isFragment()
{
if (node instanceof DocumentFragment) { return true; }
- if (node.getParentNode() != null && node.getParentNode() instanceof DocumentFragment) { return true; }
- return false;
+ return node.getParentNode() != null && node.getParentNode() instanceof DocumentFragment;
}
@JRubyMethod(name = {"next_sibling", "next"})
@@ -1588,6 +1613,7 @@ public class XmlNode extends RubyObject
return context.runtime.newFixnum(0);
}
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
return getNokogiriClass(context.runtime, "Nokogiri::XML::Node").getConstant(type);
}
@@ -1837,7 +1863,7 @@ protected enum AdoptScheme {
parentNode.replaceChild(otherNode, thisNode);
} catch (Exception e) {
String prefix = "could not replace child: ";
- throw context.runtime.newRuntimeError(prefix + e.toString());
+ throw context.runtime.newRuntimeError(prefix + e);
}
}
@@ -1894,7 +1920,7 @@ protected enum AdoptScheme {
process_xincludes(ThreadContext context, IRubyObject options)
{
XmlDocument xmlDocument = (XmlDocument)document(context);
- RubyArray> errors = (RubyArray)xmlDocument.getInstanceVariable("@errors");
+ RubyArray> errors = (RubyArray>)xmlDocument.getInstanceVariable("@errors");
while (errors.getLength() > 0) {
XmlSyntaxError error = (XmlSyntaxError)errors.shift(context);
if (error.toString().contains("Include operation failed")) {
diff --git a/ext/java/nokogiri/XmlNodeSet.java b/ext/java/nokogiri/XmlNodeSet.java
index bdb5a06e0e..080ae6eda7 100644
--- a/ext/java/nokogiri/XmlNodeSet.java
+++ b/ext/java/nokogiri/XmlNodeSet.java
@@ -2,7 +2,6 @@
import static nokogiri.XmlNode.setDocumentAndDecorate;
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
-import static nokogiri.internals.NokogiriHelpers.nodeListToRubyArray;
import java.util.Arrays;
@@ -151,11 +150,9 @@ public class XmlNodeSet extends RubyObject implements NodeList
int last = 0;
outer:
- for (int i = 0; i < curr.length; i++) {
- IRubyObject n = curr[i];
-
- for (int j = 0; j < other.length; j++) {
- if (other[j] == n) {
+ for (IRubyObject n : curr) {
+ for (IRubyObject iRubyObject : other) {
+ if (iRubyObject == n) {
result[last++] = n;
continue outer;
}
@@ -182,9 +179,7 @@ public class XmlNodeSet extends RubyObject implements NodeList
int last = 0;
- for (int i = 0; i < orig.length; i++) {
- IRubyObject n = orig[i];
-
+ for (IRubyObject n : orig) {
if (n == nodeOrNamespace) {
continue;
}
@@ -223,8 +218,8 @@ public class XmlNodeSet extends RubyObject implements NodeList
public IRubyObject
include_p(ThreadContext context, IRubyObject node_or_namespace)
{
- for (int i = 0; i < nodes.length; i++) {
- if (nodes[i] == node_or_namespace) {
+ for (IRubyObject node : nodes) {
+ if (node == node_or_namespace) {
return context.tru;
}
}
@@ -259,11 +254,9 @@ public class XmlNodeSet extends RubyObject implements NodeList
int last = 0;
outer:
- for (int i = 0; i < curr.length; i++) {
- IRubyObject n = curr[i];
-
- for (int j = 0; j < other.length; j++) {
- if (other[j] == n) {
+ for (IRubyObject n : curr) {
+ for (IRubyObject iRubyObject : other) {
+ if (iRubyObject == n) {
continue outer;
}
}
@@ -283,6 +276,8 @@ public class XmlNodeSet extends RubyObject implements NodeList
IRubyObject[] otherNodes = getNodes(context, nodeSet);
if (nodes.length == 0) {
+ // TODO: switch to interface method when it has been in 9.4 for a year.
+ // The "useless" cast here on JRuby 10 is necessary on 9.4 for now.
return ((XmlNodeSet) nodeSet).dup(context);
}
@@ -296,11 +291,9 @@ public class XmlNodeSet extends RubyObject implements NodeList
int last = curr.length;
outer:
- for (int i = 0; i < other.length; i++) {
- IRubyObject n = other[i];
-
- for (int j = 0; j < curr.length; j++) {
- if (curr[j] == n) {
+ for (IRubyObject n : other) {
+ for (IRubyObject iRubyObject : curr) {
+ if (iRubyObject == n) {
continue outer;
}
}
@@ -329,6 +322,7 @@ public class XmlNodeSet extends RubyObject implements NodeList
rangeBeginLength(ThreadContext context, IRubyObject rangeMaybe, int len, int[] begLen)
{
RubyRange range = (RubyRange) rangeMaybe;
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
int min = range.begin(context).convertToInteger().getIntValue();
int max = range.end(context).convertToInteger().getIntValue();
@@ -358,6 +352,7 @@ public class XmlNodeSet extends RubyObject implements NodeList
slice(ThreadContext context, IRubyObject indexOrRange)
{
if (indexOrRange instanceof RubyFixnum) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
return slice(context, ((RubyFixnum) indexOrRange).getIntValue());
}
if (indexOrRange instanceof RubyRange) {
@@ -367,6 +362,7 @@ public class XmlNodeSet extends RubyObject implements NodeList
int max = begLen[1];
return subseq(context, min, max - min);
}
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.runtime.newTypeError("index must be an Integer or a Range");
}
@@ -388,6 +384,7 @@ public class XmlNodeSet extends RubyObject implements NodeList
public IRubyObject
slice(ThreadContext context, IRubyObject start, IRubyObject length)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
int s = ((RubyFixnum) start).getIntValue();
int l = ((RubyFixnum) length).getIntValue();
@@ -422,6 +419,7 @@ public class XmlNodeSet extends RubyObject implements NodeList
public RubyArray>
to_a(ThreadContext context)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
return context.runtime.newArrayNoCopy(nodes);
}
diff --git a/ext/java/nokogiri/XmlProcessingInstruction.java b/ext/java/nokogiri/XmlProcessingInstruction.java
index fc41098b39..ab0a80ed02 100644
--- a/ext/java/nokogiri/XmlProcessingInstruction.java
+++ b/ext/java/nokogiri/XmlProcessingInstruction.java
@@ -65,6 +65,8 @@ public class XmlProcessingInstruction extends XmlNode
return self;
}
+ // unused
+ @Deprecated
@Override
public boolean
isProcessingInstruction() { return true; }
diff --git a/ext/java/nokogiri/XmlReader.java b/ext/java/nokogiri/XmlReader.java
index 74ef72d631..1531b7ffdd 100644
--- a/ext/java/nokogiri/XmlReader.java
+++ b/ext/java/nokogiri/XmlReader.java
@@ -93,7 +93,7 @@ public class XmlReader extends RubyObject
public void
init(Ruby runtime)
{
- nodeQueue = new LinkedList();
+ nodeQueue = new LinkedList<>();
nodeQueue.add(new ReaderNode.EmptyNode(runtime));
}
@@ -181,8 +181,8 @@ public class XmlReader extends RubyObject
ensureNodeClosed(context);
if (readerNode == null) { return context.getRuntime().getNil(); }
- if (!(readerNode instanceof ElementNode)) { context.getRuntime().getFalse(); }
- return RubyBoolean.newBoolean(context.getRuntime(), !readerNode.hasChildren);
+ if (!(readerNode instanceof ElementNode)) { return context.getRuntime().getFalse(); }
+ return RubyBoolean.newBoolean(context, !readerNode.hasChildren);
}
@JRubyMethod
@@ -210,6 +210,7 @@ public class XmlReader extends RubyObject
"Nokogiri::XML::Reader"));
reader.init(runtime);
reader.setInstanceVariable("@source", args[0]);
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
reader.setInstanceVariable("@errors", runtime.newArray());
IRubyObject url = context.nil;
if (args.length > 1) { url = args[1]; }
@@ -241,6 +242,7 @@ public class XmlReader extends RubyObject
"Nokogiri::XML::Reader"));
reader.init(runtime);
reader.setInstanceVariable("@source", args[0]);
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
reader.setInstanceVariable("@errors", runtime.newArray());
IRubyObject url = context.nil;
if (args.length > 1) { url = args[1]; }
@@ -280,7 +282,7 @@ public class XmlReader extends RubyObject
{
if (current.depth < 0) { return null; }
if (!current.hasChildren) { return null; }
- StringBuffer sb = new StringBuffer();
+ StringBuilder sb = new StringBuilder();
for (int i = current.startOffset + 1; i <= current.endOffset - 1; i++) {
sb.append(nodeQueue.get(i).getString());
}
@@ -396,8 +398,9 @@ public class XmlReader extends RubyObject
final ReaderNode currentNode = currentNode();
if (currentNode == null) { return runtime.getNil(); }
if (currentNode.isError()) {
- RubyArray> errors = (RubyArray) getInstanceVariable("@errors");
+ RubyArray> errors = (RubyArray>) getInstanceVariable("@errors");
IRubyObject error = currentNode.toSyntaxError();
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
errors.append(error);
setInstanceVariable("@errors", errors);
@@ -492,9 +495,9 @@ private class DocumentHandler extends DefaultXMLDocumentHandler implements XMLEr
startDocument(XMLLocator locator, String encoding, NamespaceContext context, Augmentations augs)
{
depth = 0;
- langStack = new Stack();
- xmlBaseStack = new Stack();
- elementStack = new Stack();
+ langStack = new Stack<>();
+ xmlBaseStack = new Stack<>();
+ elementStack = new Stack<>();
}
@Override
@@ -552,7 +555,7 @@ private class DocumentHandler extends DefaultXMLDocumentHandler implements XMLEr
String qName = element.rawname;
String uri = element.uri;
String localName = element.localpart;
- ReaderNode readerNode = ReaderNode.createElementNode(ruby, uri, localName, qName, attrs, depth, langStack,
+ ElementNode readerNode = ReaderNode.createElementNode(ruby, uri, localName, qName, attrs, depth, langStack,
xmlBaseStack);
if (!elementStack.isEmpty()) {
ElementNode parent = elementStack.peek();
@@ -564,7 +567,7 @@ private class DocumentHandler extends DefaultXMLDocumentHandler implements XMLEr
depth++;
if (readerNode.lang != null) { langStack.push(readerNode.lang); }
if (readerNode.xmlBase != null) { xmlBaseStack.push(readerNode.xmlBase); }
- elementStack.push((ReaderNode.ElementNode)readerNode);
+ elementStack.push(readerNode);
} else {
readerNode.endOffset = readerNode.startOffset;
readerNode.hasChildren = false;
diff --git a/ext/java/nokogiri/XmlRelaxng.java b/ext/java/nokogiri/XmlRelaxng.java
index eee9113a65..ed8f5a0a18 100644
--- a/ext/java/nokogiri/XmlRelaxng.java
+++ b/ext/java/nokogiri/XmlRelaxng.java
@@ -1,15 +1,12 @@
package nokogiri;
-import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
-
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
import javax.xml.transform.Source;
-import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
@@ -63,6 +60,7 @@ public class XmlRelaxng extends XmlSchema
parseOptions = defaultParseOptions(context.getRuntime());
}
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
xmlRelaxng.setInstanceVariable("@errors", runtime.newEmptyArray());
xmlRelaxng.setInstanceVariable("@parse_options", parseOptions);
@@ -89,27 +87,16 @@ public class XmlRelaxng extends XmlSchema
StreamResult result = new StreamResult(xmlAsWriter);
try {
TransformerFactory.newInstance().newTransformer().transform(ds, result);
- } catch (TransformerConfigurationException ex) {
- throw context.getRuntime()
- .newRuntimeError("Could not parse document: " + ex.getMessage());
} catch (TransformerException ex) {
throw context.getRuntime()
.newRuntimeError("Could not parse document: " + ex.getMessage());
}
- try {
- is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
- } catch (UnsupportedEncodingException ex) {
- throw context.getRuntime()
- .newRuntimeError("Could not parse document: " + ex.getMessage());
- }
+ is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes(StandardCharsets.UTF_8));
}
try {
return factory.compileSchema(is);
- } catch (VerifierConfigurationException ex) {
- throw context.getRuntime()
- .newRuntimeError("Could not parse document: " + ex.getMessage());
- } catch (SAXException ex) {
+ } catch (VerifierConfigurationException | SAXException ex) {
throw context.getRuntime()
.newRuntimeError("Could not parse document: " + ex.getMessage());
} catch (IOException ex) {
diff --git a/ext/java/nokogiri/XmlSaxParserContext.java b/ext/java/nokogiri/XmlSaxParserContext.java
index 4c20349ea3..00568b0f75 100644
--- a/ext/java/nokogiri/XmlSaxParserContext.java
+++ b/ext/java/nokogiri/XmlSaxParserContext.java
@@ -1,14 +1,12 @@
package nokogiri;
import nokogiri.internals.*;
-import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
import org.apache.xerces.parsers.AbstractSAXParser;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyEncoding;
import org.jruby.RubyFixnum;
-import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
@@ -18,7 +16,6 @@
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -104,7 +101,7 @@ public class XmlSaxParserContext extends ParserContext
if (!(encoding instanceof RubyEncoding)) {
throw context.runtime.newTypeError("encoding must be kind_of Encoding");
}
- java_encoding = ((RubyEncoding)encoding).toString();
+ java_encoding = encoding.toString();
}
XmlSaxParserContext ctx = newInstance(context.runtime, (RubyClass) klazz);
@@ -129,9 +126,10 @@ public class XmlSaxParserContext extends ParserContext
String java_encoding = null;
if (encoding != context.runtime.getNil()) {
if (!(encoding instanceof RubyEncoding)) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.runtime.newTypeError("encoding must be kind_of Encoding");
}
- java_encoding = ((RubyEncoding)encoding).toString();
+ java_encoding = encoding.toString();
}
XmlSaxParserContext ctx = newInstance(context.runtime, (RubyClass) klazz);
@@ -156,15 +154,17 @@ public class XmlSaxParserContext extends ParserContext
parse_io(ThreadContext context, IRubyObject klazz, IRubyObject data, IRubyObject encoding)
{
if (!invoke(context, data, "respond_to?", context.runtime.newSymbol("read")).isTrue()) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.runtime.newTypeError("argument expected to respond to :read");
}
String java_encoding = null;
if (encoding != context.runtime.getNil()) {
if (!(encoding instanceof RubyEncoding)) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.runtime.newTypeError("encoding must be kind_of Encoding");
}
- java_encoding = ((RubyEncoding)encoding).toString();
+ java_encoding = encoding.toString();
}
XmlSaxParserContext ctx = newInstance(context.runtime, (RubyClass) klazz);
@@ -236,9 +236,10 @@ public class XmlSaxParserContext extends ParserContext
protected static Options
defaultParseOptions(ThreadContext context)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
return new ParserContext.Options(
RubyFixnum.fix2long(Helpers.invoke(context,
- ((RubyClass)context.getRuntime().getClassFromPath("Nokogiri::XML::ParseOptions"))
+ context.getRuntime().getClassFromPath("Nokogiri::XML::ParseOptions")
.getConstant("DEFAULT_XML"),
"to_i"))
);
@@ -272,7 +273,7 @@ public class XmlSaxParserContext extends ParserContext
parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
parser.setProperty("http://xml.org/sax/properties/declaration-handler", handler);
} catch (Exception ex) {
- throw runtime.newRuntimeError("Problem while creating XML SAX Parser: " + ex.toString());
+ throw runtime.newRuntimeError("Problem while creating XML SAX Parser: " + ex);
}
try {
diff --git a/ext/java/nokogiri/XmlSaxPushParser.java b/ext/java/nokogiri/XmlSaxPushParser.java
index 26261a33e3..d6c69eb1ca 100644
--- a/ext/java/nokogiri/XmlSaxPushParser.java
+++ b/ext/java/nokogiri/XmlSaxPushParser.java
@@ -89,6 +89,7 @@ public class XmlSaxPushParser extends RubyObject
setOptions(ThreadContext context, IRubyObject opts)
{
invoke(context, parse_options(context), "options=", opts);
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
options = new ParserContext.Options(opts.convertToInteger().getLongValue());
return getOptions(context);
}
@@ -169,15 +170,12 @@ public class XmlSaxPushParser extends RubyObject
assert saxParser != null : "saxParser null";
parserTask = new ParserTask(context, saxParser, stream);
- futureTask = new FutureTask(parserTask);
- executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
- @Override
- public Thread newThread(Runnable r) {
- Thread t = new Thread(r);
- t.setName("XmlSaxPushParser");
- t.setDaemon(true);
- return t;
- }
+ futureTask = new FutureTask<>(parserTask);
+ executor = Executors.newSingleThreadExecutor(r -> {
+ Thread t = new Thread(r);
+ t.setName("XmlSaxPushParser");
+ t.setDaemon(true);
+ return t;
});
executor.submit(futureTask);
}
@@ -190,8 +188,6 @@ public Thread newThread(Runnable r) {
try {
terminateImpl();
- } catch (InterruptedException e) {
- throw runtime.newRuntimeError(e.toString());
} catch (Exception e) {
throw runtime.newRuntimeError(e.toString());
}
diff --git a/ext/java/nokogiri/XmlSchema.java b/ext/java/nokogiri/XmlSchema.java
index 381953c64d..d1432471e2 100644
--- a/ext/java/nokogiri/XmlSchema.java
+++ b/ext/java/nokogiri/XmlSchema.java
@@ -6,21 +6,17 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
-import java.io.StringReader;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
-import nokogiri.internals.IgnoreSchemaErrorsErrorHandler;
import nokogiri.internals.SchemaErrorHandler;
import nokogiri.internals.XmlDomParserContext;
import nokogiri.internals.ParserContext;
-import nokogiri.internals.ParserContext.Options;
import org.jruby.Ruby;
import org.jruby.RubyArray;
@@ -29,7 +25,6 @@
import org.jruby.RubyObject;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
-import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
@@ -106,14 +101,16 @@ public class XmlSchema extends RubyObject
if (parseOptions == null) {
parseOptions = defaultParseOptions(context.getRuntime());
}
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
long intParseOptions = RubyFixnum.fix2long(Helpers.invoke(context, parseOptions, "to_i"));
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
xmlSchema.setInstanceVariable("@errors", runtime.newEmptyArray());
xmlSchema.setInstanceVariable("@parse_options", parseOptions);
try {
SchemaErrorHandler errorHandler =
- new SchemaErrorHandler(context.getRuntime(), (RubyArray)xmlSchema.getInstanceVariable("@errors"));
+ new SchemaErrorHandler(context.getRuntime(), (RubyArray>)xmlSchema.getInstanceVariable("@errors"));
Schema schema =
xmlSchema.getSchema(source,
context.getRuntime().getCurrentDirectory(),
@@ -130,7 +127,7 @@ public class XmlSchema extends RubyObject
protected static IRubyObject
defaultParseOptions(Ruby runtime)
{
- return ((RubyClass)runtime.getClassFromPath("Nokogiri::XML::ParseOptions")).getConstant("DEFAULT_SCHEMA");
+ return runtime.getClassFromPath("Nokogiri::XML::ParseOptions").getConstant("DEFAULT_SCHEMA");
}
/*
@@ -151,6 +148,7 @@ public class XmlSchema extends RubyObject
if (!(rbDocument instanceof XmlNode)) {
String msg = "expected parameter to be a Nokogiri::XML::Document, received " + rbDocument.getMetaClass();
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.runtime.newTypeError(msg);
}
if (!(rbDocument instanceof XmlDocument)) {
@@ -159,8 +157,9 @@ public class XmlSchema extends RubyObject
XmlDocument doc = ((XmlDocument)((XmlNode) rbDocument).document(context));
- RubyArray> errors = (RubyArray) doc.getInstanceVariable("@errors");
+ RubyArray> errors = (RubyArray>) doc.getInstanceVariable("@errors");
if (!errors.isEmpty()) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw((XmlSyntaxError) errors.first()).toThrowable();
}
@@ -178,6 +177,7 @@ public class XmlSchema extends RubyObject
private static IRubyObject
getSchema(ThreadContext context, RubyClass klazz, Source source, IRubyObject parseOptions)
{
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
String moduleName = klazz.getName();
if ("Nokogiri::XML::Schema".equals(moduleName)) {
return XmlSchema.createSchemaInstance(context, klazz, source, parseOptions);
@@ -206,9 +206,11 @@ public class XmlSchema extends RubyObject
XmlDocument xmlDocument = ctx.parse(context, getNokogiriClass(runtime, "Nokogiri::XML::Document"), context.nil);
return validate_document_or_file(context, xmlDocument);
} catch (Exception ex) {
- RubyArray errors = (RubyArray)context.runtime.newEmptyArray();
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
+ RubyArray> errors = context.runtime.newEmptyArray();
XmlSyntaxError xmlSyntaxError = XmlSyntaxError.createXMLSyntaxError(context.runtime);
xmlSyntaxError.setException(ex);
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
errors.append(xmlSyntaxError);
return errors;
}
@@ -217,7 +219,8 @@ public class XmlSchema extends RubyObject
IRubyObject
validate_document_or_file(ThreadContext context, XmlDocument xmlDocument)
{
- RubyArray errors = context.runtime.newEmptyArray();
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
+ RubyArray> errors = context.runtime.newEmptyArray();
ErrorHandler errorHandler = new SchemaErrorHandler(context.runtime, errors);
setErrorHandler(errorHandler);
@@ -226,6 +229,7 @@ public class XmlSchema extends RubyObject
} catch (SAXException ex) {
XmlSyntaxError xmlSyntaxError = XmlSyntaxError.createXMLSyntaxError(context.runtime);
xmlSyntaxError.setException(ex);
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
errors.append(xmlSyntaxError);
} catch (IOException ex) {
throw context.runtime.newIOError(ex.getMessage());
@@ -291,7 +295,7 @@ private class SchemaResourceResolver implements LSResourceResolver
}
try {
this.errorHandler.warning(new SAXParseException(String.format("Attempt to load network entity '%s'", systemId), null));
- } catch (SAXException ex) {
+ } catch (SAXException ignored) {
}
} else {
String adjusted = adjustSystemIdIfNecessary(currentDir, scriptFileName, baseURI, systemId);
@@ -303,7 +307,7 @@ private class SchemaResourceResolver implements LSResourceResolver
}
}
- private class SchemaLSInput implements LSInput
+ private static class SchemaLSInput implements LSInput
{
protected String fPublicId;
protected String fSystemId;
diff --git a/ext/java/nokogiri/XmlSyntaxError.java b/ext/java/nokogiri/XmlSyntaxError.java
index 0fe1b649e4..d3265499b4 100644
--- a/ext/java/nokogiri/XmlSyntaxError.java
+++ b/ext/java/nokogiri/XmlSyntaxError.java
@@ -91,6 +91,8 @@ public class XmlSyntaxError extends RubyException
return xmlSyntaxError;
}
+ // unused
+ @Deprecated
public static XmlSyntaxError
createFatalError(Ruby runtime, SAXParseException e)
{
diff --git a/ext/java/nokogiri/XmlText.java b/ext/java/nokogiri/XmlText.java
index f51bc6b91c..12f984c67c 100644
--- a/ext/java/nokogiri/XmlText.java
+++ b/ext/java/nokogiri/XmlText.java
@@ -47,6 +47,7 @@ public class XmlText extends XmlNode
init(ThreadContext context, IRubyObject[] args)
{
if (args.length < 2) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.runtime.newArgumentError(args.length, 2);
}
@@ -55,6 +56,7 @@ public class XmlText extends XmlNode
if (!(rbDocument instanceof XmlNode)) {
String msg = "expected second parameter to be a Nokogiri::XML::Document, received " + rbDocument.getMetaClass();
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.runtime.newTypeError(msg);
}
if (!(rbDocument instanceof XmlDocument)) {
diff --git a/ext/java/nokogiri/XmlXpathContext.java b/ext/java/nokogiri/XmlXpathContext.java
index 5996956f68..83044d4919 100644
--- a/ext/java/nokogiri/XmlXpathContext.java
+++ b/ext/java/nokogiri/XmlXpathContext.java
@@ -17,7 +17,6 @@
import org.jruby.RubyObject;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
-import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.SafePropertyAccessor;
@@ -121,7 +120,6 @@ public class XmlXpathContext extends RubyObject
while (xpathFunctionCalls.find()) {
namespacedQuery.append(query.subSequence(jchar, xpathFunctionCalls.start()));
- jchar = xpathFunctionCalls.start();
if (methodNames.contains(xpathFunctionCalls.group())) {
namespacedQuery.append(NokogiriNamespaceContext.NOKOGIRI_PREFIX);
@@ -198,7 +196,7 @@ public class XmlXpathContext extends RubyObject
return tryGetNodeSet(context, expr, fnResolver);
} catch (TransformerException | RuntimeException ex) {
throw XmlSyntaxError.createXMLXPathSyntaxError(context.runtime,
- (expr + ": " + ex.toString()),
+ (expr + ": " + ex),
ex).toThrowable();
}
}
diff --git a/ext/java/nokogiri/XsltStylesheet.java b/ext/java/nokogiri/XsltStylesheet.java
index 1870b60d35..c71b4750c2 100644
--- a/ext/java/nokogiri/XsltStylesheet.java
+++ b/ext/java/nokogiri/XsltStylesheet.java
@@ -83,8 +83,9 @@ public class XsltStylesheet extends RubyObject
if (parameters instanceof RubyHash) {
setHashParameters(transf, (RubyHash)parameters);
} else if (parameters instanceof RubyArray) {
- setArrayParameters(transf, context, (RubyArray)parameters);
+ setArrayParameters(transf, context, (RubyArray>)parameters);
} else {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw context.getRuntime().newTypeError("parameters should be given either Array or Hash");
}
}
@@ -170,8 +171,9 @@ public class XsltStylesheet extends RubyObject
ensureDocumentHasNoError(ThreadContext context, XmlDocument xmlDoc)
{
Ruby runtime = context.getRuntime();
- RubyArray> errors_of_xmlDoc = (RubyArray) xmlDoc.getInstanceVariable("@errors");
+ RubyArray> errors_of_xmlDoc = (RubyArray>) xmlDoc.getInstanceVariable("@errors");
if (!errors_of_xmlDoc.isEmpty()) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
throw runtime.newRuntimeError(errors_of_xmlDoc.first().asString().asJavaString());
}
}
@@ -212,11 +214,7 @@ public class XsltStylesheet extends RubyObject
if (result.getNode().getFirstChild() == null) {
stringResult = retryXsltTransformation(context, args, domSource, elistener); // StreamResult
}
- } catch (TransformerConfigurationException ex) {
- throw runtime.newRuntimeError(ex.getMessage());
- } catch (TransformerException ex) {
- throw runtime.newRuntimeError(ex.getMessage());
- } catch (IOException ex) {
+ } catch (TransformerException | IOException ex) {
throw runtime.newRuntimeError(ex.getMessage());
}
@@ -230,7 +228,7 @@ public class XsltStylesheet extends RubyObject
}
if (stringResult == null) {
- return createDocumentFromDomResult(context, runtime, result);
+ return createDocumentFromDomResult(context, result);
} else {
return createDocumentFromString(context, runtime, stringResult);
}
@@ -291,7 +289,7 @@ public class XsltStylesheet extends RubyObject
}
private IRubyObject
- createDocumentFromDomResult(ThreadContext context, Ruby runtime, DOMResult domResult)
+ createDocumentFromDomResult(ThreadContext context, DOMResult domResult)
{
if ("html".equals(domResult.getNode().getFirstChild().getNodeName())) {
return new Html4Document(context.runtime, (Document) domResult.getNode());
@@ -329,15 +327,17 @@ public class XsltStylesheet extends RubyObject
args[2] = runtime.getNil(); // encoding
RubyClass parse_options = (RubyClass)runtime.getClassFromPath("Nokogiri::XML::ParseOptions");
if (htmlish) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
args[3] = parse_options.getConstant("DEFAULT_HTML");
RubyClass htmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::HTML4::Document");
return Helpers.invoke(context, htmlDocumentClass, "parse", args);
} else {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
args[3] = parse_options.getConstant("DEFAULT_XML");
RubyClass xmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
XmlDocument xmlDocument = (XmlDocument) Helpers.invoke(context, xmlDocumentClass, "parse", args);
if (((Document)xmlDocument.getNode()).getDocumentElement() == null) {
- RubyArray> errors = (RubyArray) xmlDocument.getInstanceVariable("@errors");
+ RubyArray> errors = (RubyArray>) xmlDocument.getInstanceVariable("@errors");
Helpers.invoke(context, errors, "<<", args[0]);
}
return xmlDocument;
diff --git a/ext/java/nokogiri/internals/NokogiriHelpers.java b/ext/java/nokogiri/internals/NokogiriHelpers.java
index 97ffc50034..8dd28ee6d1 100644
--- a/ext/java/nokogiri/internals/NokogiriHelpers.java
+++ b/ext/java/nokogiri/internals/NokogiriHelpers.java
@@ -40,6 +40,8 @@
import nokogiri.XmlText;
import nokogiri.XmlXpathContext;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
/**
* A class for various utility methods.
*
@@ -59,6 +61,8 @@ public class NokogiriHelpers
return (XmlNode) node.getUserData(CACHED_NODE);
}
+ // unused
+ @Deprecated
public static void
clearCachedNode(Node node)
{
@@ -221,7 +225,7 @@ public class NokogiriHelpers
public static IRubyObject
nonEmptyStringOrNil(Ruby runtime, String s)
{
- if (s == null || s.length() == 0) { return runtime.getNil(); }
+ if (s == null || s.isEmpty()) { return runtime.getNil(); }
return RubyString.newString(runtime, s);
}
@@ -287,7 +291,7 @@ public class NokogiriHelpers
Node cur, tmp, next;
- String buffer = "";
+ StringBuilder buffer = new StringBuilder();
cur = node;
@@ -295,10 +299,10 @@ public class NokogiriHelpers
String name = "";
String sep = "?";
int occur = 0;
- boolean generic = false;
+ boolean generic;
if (cur.getNodeType() == Node.DOCUMENT_NODE) {
- if (buffer.startsWith("/")) { break; }
+ if (buffer.toString().startsWith("/")) { break; }
sep = "/";
next = null;
@@ -471,18 +475,20 @@ public class NokogiriHelpers
}
if (occur == 0) {
- buffer = sep + name + buffer;
+ buffer.insert(0, sep + name);
} else {
- buffer = sep + name + "[" + occur + "]" + buffer;
+ buffer.insert(0, sep + name + "[" + occur + "]");
}
cur = next;
} while (cur != null);
- return buffer;
+ return buffer.toString();
}
+ // unused
+ @Deprecated
static boolean
compareTwoNodes(Node m, Node n)
{
@@ -494,7 +500,7 @@ public class NokogiriHelpers
nodesAreEqual(Object a, Object b)
{
return (((a == null) && (b == null)) ||
- ((a != null) && (b != null) && (b.equals(a))));
+ ((b != null) && (b.equals(a))));
}
private static boolean
@@ -505,7 +511,7 @@ public class NokogiriHelpers
private static final Pattern encoded_pattern = Pattern.compile("&|>|<|
");
private static final String[] encoded = {"&", ">", "<", "
"};
- private static final Pattern decoded_pattern = Pattern.compile("&|>|<|\r");
+ private static final Pattern decoded_pattern = Pattern.compile("[&><\r]");
private static final String[] decoded = {"&", ">", "<", "\r"};
private static StringBuffer
@@ -555,6 +561,8 @@ public class NokogiriHelpers
return (nodeName.startsWith("xmlns"));
}
+ // unused
+ @Deprecated
public static boolean
isNonDefaultNamespace(Node node)
{
@@ -591,6 +599,8 @@ public class NokogiriHelpers
return str.isEmpty() || isBlank((CharSequence) str);
}
+ // unused
+ @Deprecated
public static boolean
isNullOrEmpty(String str)
{
@@ -649,8 +659,9 @@ public class NokogiriHelpers
nodeArrayToRubyArray(Ruby ruby, Node[] nodes)
{
RubyArray> n = RubyArray.newArray(ruby, nodes.length);
- for (int i = 0; i < nodes.length; i++) {
- n.append(NokogiriHelpers.getCachedNodeOrCreate(ruby, nodes[i]));
+ for (Node node : nodes) {
+ // TODO: switch to common undeprecated API when 9.4 adds 10 methods
+ n.append(NokogiriHelpers.getCachedNodeOrCreate(ruby, node));
}
return n;
}
@@ -690,7 +701,7 @@ public class NokogiriHelpers
private static String
resolveSystemId(String baseName, String systemId)
{
- if (baseName == null || baseName.length() < 1) { return null; }
+ if (baseName == null || baseName.isEmpty()) { return null; }
String parentName;
baseName = baseName.replace("%20", " ");
File base = new File(baseName);
@@ -703,15 +714,13 @@ public class NokogiriHelpers
return null;
}
- private static final Charset UTF8 = Charset.forName("UTF-8");
-
public static boolean
isUTF8(String encoding)
{
if (encoding == null) { return true; } // no need to convert encoding
if ("UTF-8".equals(encoding)) { return true; }
- return UTF8.aliases().contains(encoding);
+ return UTF_8.aliases().contains(encoding);
}
public static ByteBuffer
@@ -720,6 +729,8 @@ public class NokogiriHelpers
return output_charset.encode(CharBuffer.wrap(input_string)); // does replace implicitly on un-mappable characters
}
+ // unused
+ @Deprecated
public static CharSequence
convertEncodingByNKFIfNecessary(ThreadContext context, XmlDocument doc, CharSequence str)
{
@@ -766,15 +777,8 @@ public class NokogiriHelpers
RubyString r_str =
(RubyString)nkf_method.invoke(null, context, null, runtime.newString(opt), runtime.newString(str.toString()));
return NokogiriHelpers.rubyStringToString(r_str);
- } catch (SecurityException e) {
- return str;
- } catch (NoSuchMethodException e) {
- return str;
- } catch (IllegalArgumentException e) {
- return str;
- } catch (IllegalAccessException e) {
- return str;
- } catch (InvocationTargetException e) {
+ } catch (SecurityException | NoSuchMethodException | IllegalArgumentException | IllegalAccessException |
+ InvocationTargetException e) {
return str;
}
}
diff --git a/ext/java/nokogiri/internals/SaveContextVisitor.java b/ext/java/nokogiri/internals/SaveContextVisitor.java
index 462a9bd69b..aac04a8430 100644
--- a/ext/java/nokogiri/internals/SaveContextVisitor.java
+++ b/ext/java/nokogiri/internals/SaveContextVisitor.java
@@ -58,6 +58,7 @@ public class SaveContextVisitor
private final Deque c14nNamespaceStack;
private final Deque c14nAttrStack;
//private List c14nExclusiveInclusivePrefixes = null;
+ private final Stack