Skip to content

Commit 5bf729f

Browse files
authoredSep 26, 2021
Merge pull request #2328 from sparklemotion/flavorjones-GHSA-2rr5-8q37-2w7h_main
fix JRuby SAX parser entity handling
2 parents 04032e5 + 3828603 commit 5bf729f

12 files changed

+804
-868
lines changed
 

‎ext/java/nokogiri/Html4SaxPushParser.java

+14-19
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
package nokogiri;
22

3-
import static nokogiri.XmlSaxPushParser.terminateExecution;
4-
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
5-
import static org.jruby.runtime.Helpers.invoke;
6-
7-
import java.io.ByteArrayInputStream;
8-
import java.io.InputStream;
9-
import java.io.IOException;
10-
import java.util.concurrent.Callable;
11-
import java.util.concurrent.ExecutionException;
12-
import java.util.concurrent.ExecutorService;
13-
import java.util.concurrent.Executors;
14-
import java.util.concurrent.Future;
15-
import java.util.concurrent.FutureTask;
16-
import java.util.concurrent.ThreadFactory;
17-
18-
import nokogiri.internals.*;
19-
3+
import nokogiri.internals.ClosedStreamException;
4+
import nokogiri.internals.NokogiriBlockingQueueInputStream;
5+
import nokogiri.internals.NokogiriHelpers;
6+
import nokogiri.internals.ParserContext;
207
import org.jruby.Ruby;
218
import org.jruby.RubyClass;
229
import org.jruby.RubyObject;
2310
import org.jruby.anno.JRubyClass;
2411
import org.jruby.anno.JRubyMethod;
25-
import org.jruby.exceptions.RaiseException;
2612
import org.jruby.runtime.ThreadContext;
2713
import org.jruby.runtime.builtin.IRubyObject;
2814

15+
import java.io.ByteArrayInputStream;
16+
import java.io.IOException;
17+
import java.io.InputStream;
18+
import java.util.concurrent.*;
19+
20+
import static nokogiri.XmlSaxPushParser.terminateExecution;
21+
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
22+
import static org.jruby.runtime.Helpers.invoke;
23+
2924
/**
3025
* Class for Nokogiri::HTML4::SAX::PushParser
3126
*
@@ -134,7 +129,7 @@ public class Html4SaxPushParser extends RubyObject
134129

135130
if (!options.recover && parserTask.getErrorCount() > errorCount0) {
136131
terminateTask(context.runtime);
137-
throw parserTask.getLastError();
132+
throw parserTask.getLastError().toThrowable();
138133
}
139134

140135
return this;

‎ext/java/nokogiri/XmlSaxParserContext.java

+31-93
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
11
package nokogiri;
22

3-
import static org.jruby.runtime.Helpers.invoke;
4-
5-
import java.io.IOException;
6-
import java.io.InputStream;
7-
3+
import nokogiri.internals.*;
84
import org.apache.xerces.parsers.AbstractSAXParser;
95
import org.jruby.Ruby;
106
import org.jruby.RubyClass;
117
import org.jruby.RubyFixnum;
12-
import org.jruby.RubyModule;
13-
import org.jruby.RubyObjectAdapter;
148
import org.jruby.anno.JRubyClass;
159
import org.jruby.anno.JRubyMethod;
1610
import org.jruby.exceptions.RaiseException;
17-
import org.jruby.javasupport.JavaEmbedUtils;
11+
import org.jruby.runtime.Helpers;
1812
import org.jruby.runtime.ThreadContext;
1913
import org.jruby.runtime.builtin.IRubyObject;
20-
import org.xml.sax.ContentHandler;
21-
import org.xml.sax.ErrorHandler;
2214
import org.xml.sax.SAXException;
23-
import org.xml.sax.SAXNotRecognizedException;
24-
import org.xml.sax.SAXNotSupportedException;
2515
import org.xml.sax.SAXParseException;
2616

27-
import nokogiri.internals.NokogiriHandler;
28-
import nokogiri.internals.NokogiriHelpers;
29-
import nokogiri.internals.ParserContext;
30-
import nokogiri.internals.XmlSaxParser;
17+
import java.io.IOException;
18+
import java.io.InputStream;
19+
20+
import static org.jruby.runtime.Helpers.invoke;
3121

3222
/**
3323
* Base class for the SAX parsers.
@@ -51,6 +41,7 @@ public class XmlSaxParserContext extends ParserContext
5141
protected AbstractSAXParser parser;
5242

5343
protected NokogiriHandler handler;
44+
protected NokogiriErrorHandler errorHandler;
5445
private boolean replaceEntities = true;
5546
private boolean recovery = false;
5647

@@ -168,31 +159,12 @@ public class XmlSaxParserContext extends ParserContext
168159
return (XmlSaxParserContext) NokogiriService.XML_SAXPARSER_CONTEXT_ALLOCATOR.allocate(runtime, klazz);
169160
}
170161

171-
/**
172-
* Set a property of the underlying parser.
173-
*/
174-
protected void
175-
setProperty(String key, Object val)
176-
throws SAXNotRecognizedException, SAXNotSupportedException
177-
{
178-
parser.setProperty(key, val);
179-
}
180-
181-
protected void
182-
setContentHandler(ContentHandler handler)
183-
{
184-
parser.setContentHandler(handler);
185-
}
186-
187-
protected void
188-
setErrorHandler(ErrorHandler handler)
189-
{
190-
parser.setErrorHandler(handler);
191-
}
192-
193162
public final NokogiriHandler
194163
getNokogiriHandler() { return handler; }
195164

165+
public final NokogiriErrorHandler
166+
getNokogiriErrorHandler() { return errorHandler; }
167+
196168
/**
197169
* Perform any initialization prior to parsing with the handler
198170
* <code>handlerRuby</code>. Convenience hook for subclasses.
@@ -223,6 +195,17 @@ public class XmlSaxParserContext extends ParserContext
223195
parser.parse(getInputSource());
224196
}
225197

198+
protected static Options
199+
defaultParseOptions(ThreadContext context)
200+
{
201+
return new ParserContext.Options(
202+
RubyFixnum.fix2long(Helpers.invoke(context,
203+
((RubyClass)context.getRuntime().getClassFromPath("Nokogiri::XML::ParseOptions"))
204+
.getConstant("DEFAULT_XML"),
205+
"to_i"))
206+
);
207+
}
208+
226209
@JRubyMethod
227210
public IRubyObject
228211
parse_with(ThreadContext context, IRubyObject handlerRuby)
@@ -233,14 +216,19 @@ public class XmlSaxParserContext extends ParserContext
233216
throw runtime.newArgumentError("argument must respond_to document");
234217
}
235218

236-
NokogiriHandler handler = this.handler = new NokogiriHandler(runtime, handlerRuby);
237-
preParse(runtime, handlerRuby, handler);
219+
/* TODO: how should we pass in parse options? */
220+
ParserContext.Options options = defaultParseOptions(context);
221+
222+
errorHandler = new NokogiriStrictErrorHandler(runtime, options.noError, options.noWarning);
223+
handler = new NokogiriHandler(runtime, handlerRuby, errorHandler);
238224

239-
setContentHandler(handler);
240-
setErrorHandler(handler);
225+
preParse(runtime, handlerRuby, handler);
226+
parser.setContentHandler(handler);
227+
parser.setErrorHandler(handler);
228+
parser.setEntityResolver(new NokogiriEntityResolver(runtime, errorHandler, options));
241229

242230
try {
243-
setProperty("http://xml.org/sax/properties/lexical-handler", handler);
231+
parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
244232
} catch (Exception ex) {
245233
throw runtime.newRuntimeError("Problem while creating XML SAX Parser: " + ex.toString());
246234
}
@@ -270,8 +258,6 @@ public class XmlSaxParserContext extends ParserContext
270258

271259
postParse(runtime, handlerRuby, handler);
272260

273-
//maybeTrimLeadingAndTrailingWhitespace(context, handlerRuby);
274-
275261
return runtime.getNil();
276262
}
277263

@@ -319,53 +305,6 @@ public class XmlSaxParserContext extends ParserContext
319305
return context.runtime.newBoolean(recovery);
320306
}
321307

322-
/**
323-
* If the handler's document is a FragmentHandler, attempt to trim
324-
* leading and trailing whitespace.
325-
*
326-
* This is a bit hackish and depends heavily on the internals of
327-
* FragmentHandler.
328-
*/
329-
protected void
330-
maybeTrimLeadingAndTrailingWhitespace(ThreadContext context, IRubyObject parser)
331-
{
332-
RubyObjectAdapter adapter = JavaEmbedUtils.newObjectAdapter();
333-
RubyModule mod = context.getRuntime().getClassFromPath("Nokogiri::XML::FragmentHandler");
334-
335-
IRubyObject handler = adapter.getInstanceVariable(parser, "@document");
336-
if (handler == null || handler.isNil() || !adapter.isKindOf(handler, mod)) {
337-
return;
338-
}
339-
IRubyObject stack = adapter.getInstanceVariable(handler, "@stack");
340-
if (stack == null || stack.isNil()) {
341-
return;
342-
}
343-
// doc is finally a DocumentFragment whose nodes we can check
344-
IRubyObject doc = adapter.callMethod(stack, "first");
345-
if (doc == null || doc.isNil()) {
346-
return;
347-
}
348-
349-
IRubyObject children;
350-
351-
for (;;) {
352-
children = adapter.callMethod(doc, "children");
353-
IRubyObject first = adapter.callMethod(children, "first");
354-
if (NokogiriHelpers.isBlank(first)) { adapter.callMethod(first, "unlink"); }
355-
else { break; }
356-
}
357-
358-
for (;;) {
359-
children = adapter.callMethod(doc, "children");
360-
IRubyObject last = adapter.callMethod(children, "last");
361-
if (NokogiriHelpers.isBlank(last)) { adapter.callMethod(last, "unlink"); }
362-
else { break; }
363-
}
364-
365-
// While we have a document, normalize it.
366-
((XmlNode) doc).normalize();
367-
}
368-
369308
@JRubyMethod(name = "column")
370309
public IRubyObject
371310
column(ThreadContext context)
@@ -383,5 +322,4 @@ public class XmlSaxParserContext extends ParserContext
383322
if (number == null) { return context.getRuntime().getNil(); }
384323
return RubyFixnum.newFixnum(context.getRuntime(), number.longValue());
385324
}
386-
387325
}

‎ext/java/nokogiri/XmlSaxPushParser.java

+17-25
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
package nokogiri;
22

3-
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
4-
import static org.jruby.runtime.Helpers.invoke;
5-
6-
import java.io.ByteArrayInputStream;
7-
import java.io.IOException;
8-
import java.io.InputStream;
9-
import java.util.concurrent.ExecutionException;
10-
import java.util.concurrent.ExecutorService;
11-
import java.util.concurrent.Executors;
12-
import java.util.concurrent.Future;
13-
import java.util.concurrent.FutureTask;
14-
import java.util.concurrent.ThreadFactory;
15-
3+
import nokogiri.internals.*;
164
import org.jruby.Ruby;
175
import org.jruby.RubyClass;
6+
import org.jruby.RubyException;
187
import org.jruby.RubyObject;
198
import org.jruby.anno.JRubyClass;
209
import org.jruby.anno.JRubyMethod;
2110
import org.jruby.exceptions.RaiseException;
2211
import org.jruby.runtime.ThreadContext;
2312
import org.jruby.runtime.builtin.IRubyObject;
2413

25-
import nokogiri.internals.ClosedStreamException;
26-
import nokogiri.internals.NokogiriBlockingQueueInputStream;
27-
import nokogiri.internals.NokogiriHandler;
28-
import nokogiri.internals.NokogiriHelpers;
29-
import nokogiri.internals.ParserContext;
14+
import java.io.ByteArrayInputStream;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.util.List;
18+
import java.util.concurrent.*;
19+
20+
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
21+
import static org.jruby.runtime.Helpers.invoke;
3022

3123
/**
3224
* Class for Nokogiri::XML::SAX::PushParser
@@ -159,7 +151,8 @@ public class XmlSaxPushParser extends RubyObject
159151

160152
if (!options.recover && parserTask.getErrorCount() > errorCount0) {
161153
terminateTask(context.runtime);
162-
throw ex = parserTask.getLastError();
154+
ex = parserTask.getLastError().toThrowable();
155+
throw ex;
163156
}
164157

165158
return this;
@@ -278,16 +271,15 @@ static class ParserTask extends ParserContext.ParserTask<XmlSaxParserContext>
278271
getErrorCount()
279272
{
280273
// check for null because thread may not have started yet
281-
if (parser.getNokogiriHandler() == null) { return 0; }
282-
return parser.getNokogiriHandler().getErrorCount();
274+
if (parser.getNokogiriErrorHandler() == null) { return 0; }
275+
return parser.getNokogiriErrorHandler().getErrors().size();
283276
}
284277

285-
synchronized final RaiseException
278+
synchronized final RubyException
286279
getLastError()
287280
{
288-
return parser.getNokogiriHandler().getLastError();
281+
List<RubyException> errors = parser.getNokogiriErrorHandler().getErrors();
282+
return errors.get(errors.size() - 1);
289283
}
290-
291284
}
292-
293285
}

‎ext/java/nokogiri/internals/NokogiriEntityResolver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class NokogiriEntityResolver implements EntityResolver2
8585
private void
8686
addError(String errorMessage)
8787
{
88-
if (handler != null) { handler.errors.add(new Exception(errorMessage)); }
88+
if (handler != null) { handler.addError(new Exception(errorMessage)); }
8989
}
9090

9191
/**

‎ext/java/nokogiri/internals/NokogiriErrorHandler.java

+29-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package nokogiri.internals;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
3+
import nokogiri.XmlSyntaxError;
64
import org.apache.xerces.xni.parser.XMLErrorHandler;
5+
import org.jruby.Ruby;
6+
import org.jruby.RubyException;
7+
import org.jruby.exceptions.RaiseException;
78
import org.xml.sax.ErrorHandler;
89

10+
import java.util.ArrayList;
11+
import java.util.List;
12+
913
/**
1014
* Super class of error handlers.
1115
*
@@ -17,23 +21,40 @@
1721
*/
1822
public abstract class NokogiriErrorHandler implements ErrorHandler, XMLErrorHandler
1923
{
20-
protected final List<Exception> errors;
24+
private final Ruby runtime;
25+
protected final List<RubyException> errors;
2126
protected boolean noerror;
2227
protected boolean nowarning;
2328

2429
public
25-
NokogiriErrorHandler(boolean noerror, boolean nowarning)
30+
NokogiriErrorHandler(Ruby runtime, boolean noerror, boolean nowarning)
2631
{
27-
this.errors = new ArrayList<Exception>(4);
32+
this.runtime = runtime;
33+
this.errors = new ArrayList<RubyException>(4);
2834
this.noerror = noerror;
2935
this.nowarning = nowarning;
3036
}
3137

32-
List<Exception>
38+
public List<RubyException>
3339
getErrors() { return errors; }
3440

3541
public void
36-
addError(Exception ex) { errors.add(ex); }
42+
addError(Exception ex)
43+
{
44+
addError(XmlSyntaxError.createXMLSyntaxError(runtime, ex));
45+
}
46+
47+
public void
48+
addError(RubyException ex)
49+
{
50+
errors.add(ex);
51+
}
52+
53+
public void
54+
addError(RaiseException ex)
55+
{
56+
addError(ex.getException());
57+
}
3758

3859
protected boolean
3960
usesNekoHtml(String domain)

‎ext/java/nokogiri/internals/NokogiriHandler.java

+6-24
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,19 @@ public class NokogiriHandler extends DefaultHandler2 implements XmlDeclHandler
3838
private final Ruby runtime;
3939
private final RubyClass attrClass;
4040
private final IRubyObject object;
41-
42-
/**
43-
* Stores parse errors with the most-recent error last.
44-
*
45-
* TODO: should these be stored in the document 'errors' array?
46-
* Currently only string messages are stored there.
47-
*/
48-
private final LinkedList<RaiseException> errors = new LinkedList<RaiseException>();
41+
private NokogiriErrorHandler errorHandler;
4942

5043
private Locator locator;
5144
private boolean needEmptyAttrCheck;
5245

5346
public
54-
NokogiriHandler(Ruby runtime, IRubyObject object)
47+
NokogiriHandler(Ruby runtime, IRubyObject object, NokogiriErrorHandler errorHandler)
5548
{
5649
assert object != null;
5750
this.runtime = runtime;
5851
this.attrClass = (RubyClass) runtime.getClassFromPath("Nokogiri::XML::SAX::Parser::Attribute");
5952
this.object = object;
53+
this.errorHandler = errorHandler;
6054
charactersBuilder = new StringBuilder();
6155
String objectName = object.getMetaClass().getName();
6256
if ("Nokogiri::HTML4::SAX::Parser".equals(objectName)) { needEmptyAttrCheck = true; }
@@ -253,9 +247,9 @@ public class NokogiriHandler extends DefaultHandler2 implements XmlDeclHandler
253247
try {
254248
final String msg = ex.getMessage();
255249
call("error", runtime.newString(msg == null ? "" : msg));
256-
addError(XmlSyntaxError.createError(runtime, ex).toThrowable());
250+
errorHandler.addError(ex);
257251
} catch (RaiseException e) {
258-
addError(e);
252+
errorHandler.addError(e);
259253
throw e;
260254
}
261255
}
@@ -282,22 +276,10 @@ public class NokogiriHandler extends DefaultHandler2 implements XmlDeclHandler
282276
call("warning", runtime.newString(msg == null ? "" : msg));
283277
}
284278

285-
protected synchronized void
286-
addError(RaiseException e)
287-
{
288-
errors.add(e);
289-
}
290-
291279
public synchronized int
292280
getErrorCount()
293281
{
294-
return errors.size();
295-
}
296-
297-
public synchronized RaiseException
298-
getLastError()
299-
{
300-
return errors.getLast();
282+
return errorHandler.getErrors().size();
301283
}
302284

303285
private void

‎ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nokogiri.internals;
22

33
import org.apache.xerces.xni.parser.XMLParseException;
4+
import org.jruby.Ruby;
45
import org.xml.sax.SAXException;
56
import org.xml.sax.SAXParseException;
67

@@ -13,21 +14,21 @@
1314
public class NokogiriNonStrictErrorHandler extends NokogiriErrorHandler
1415
{
1516
public
16-
NokogiriNonStrictErrorHandler(boolean noerror, boolean nowarning)
17+
NokogiriNonStrictErrorHandler(Ruby runtime, boolean noerror, boolean nowarning)
1718
{
18-
super(noerror, nowarning);
19+
super(runtime, noerror, nowarning);
1920
}
2021

2122
public void
2223
warning(SAXParseException ex) throws SAXException
2324
{
24-
errors.add(ex);
25+
addError(ex);
2526
}
2627

2728
public void
2829
error(SAXParseException ex) throws SAXException
2930
{
30-
errors.add(ex);
31+
addError(ex);
3132
}
3233

3334
public void
@@ -38,7 +39,7 @@ public class NokogiriNonStrictErrorHandler extends NokogiriErrorHandler
3839
// found in the prolog, instead it will keep calling this method and we'll
3940
// keep inserting the error in the document errors array until we run
4041
// out of memory
41-
errors.add(ex);
42+
addError(ex);
4243
String message = ex.getMessage();
4344

4445
// The problem with Xerces is that some errors will cause the
@@ -53,19 +54,19 @@ public class NokogiriNonStrictErrorHandler extends NokogiriErrorHandler
5354
public void
5455
error(String domain, String key, XMLParseException e)
5556
{
56-
errors.add(e);
57+
addError(e);
5758
}
5859

5960
public void
6061
fatalError(String domain, String key, XMLParseException e)
6162
{
62-
errors.add(e);
63+
addError(e);
6364
}
6465

6566
public void
6667
warning(String domain, String key, XMLParseException e)
6768
{
68-
errors.add(e);
69+
addError(e);
6970
}
7071

7172
/*

‎ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nokogiri.internals;
22

33
import org.apache.xerces.xni.parser.XMLParseException;
4+
import org.jruby.Ruby;
45
import org.xml.sax.SAXException;
56
import org.xml.sax.SAXParseException;
67

@@ -20,15 +21,15 @@ public class NokogiriNonStrictErrorHandler4NekoHtml extends NokogiriErrorHandler
2021
{
2122

2223
public
23-
NokogiriNonStrictErrorHandler4NekoHtml(boolean nowarning)
24+
NokogiriNonStrictErrorHandler4NekoHtml(Ruby runtime, boolean nowarning)
2425
{
25-
super(false, nowarning);
26+
super(runtime, false, nowarning);
2627
}
2728

2829
public
29-
NokogiriNonStrictErrorHandler4NekoHtml(boolean noerror, boolean nowarning)
30+
NokogiriNonStrictErrorHandler4NekoHtml(Ruby runtime, boolean noerror, boolean nowarning)
3031
{
31-
super(noerror, nowarning);
32+
super(runtime, noerror, nowarning);
3233
}
3334

3435
public void
@@ -40,13 +41,13 @@ public class NokogiriNonStrictErrorHandler4NekoHtml extends NokogiriErrorHandler
4041
public void
4142
error(SAXParseException ex) throws SAXException
4243
{
43-
errors.add(ex);
44+
addError(ex);
4445
}
4546

4647
public void
4748
fatalError(SAXParseException ex) throws SAXException
4849
{
49-
errors.add(ex);
50+
addError(ex);
5051
}
5152

5253
/**
@@ -64,7 +65,7 @@ public class NokogiriNonStrictErrorHandler4NekoHtml extends NokogiriErrorHandler
6465
public void
6566
error(String domain, String key, XMLParseException e)
6667
{
67-
errors.add(e);
68+
addError(e);
6869
}
6970

7071
/**
@@ -82,7 +83,7 @@ public class NokogiriNonStrictErrorHandler4NekoHtml extends NokogiriErrorHandler
8283
public void
8384
fatalError(String domain, String key, XMLParseException e)
8485
{
85-
errors.add(e);
86+
addError(e);
8687
}
8788

8889
/**
@@ -100,7 +101,7 @@ public class NokogiriNonStrictErrorHandler4NekoHtml extends NokogiriErrorHandler
100101
public void
101102
warning(String domain, String key, XMLParseException e)
102103
{
103-
errors.add(e);
104+
addError(e);
104105
}
105106

106107
}

‎ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nokogiri.internals;
22

33
import org.apache.xerces.xni.parser.XMLParseException;
4+
import org.jruby.Ruby;
45
import org.xml.sax.SAXException;
56
import org.xml.sax.SAXParseException;
67

@@ -14,23 +15,23 @@
1415
public class NokogiriStrictErrorHandler extends NokogiriErrorHandler
1516
{
1617
public
17-
NokogiriStrictErrorHandler(boolean noerror, boolean nowarning)
18+
NokogiriStrictErrorHandler(Ruby runtime, boolean noerror, boolean nowarning)
1819
{
19-
super(noerror, nowarning);
20+
super(runtime, noerror, nowarning);
2021
}
2122

2223
public void
2324
warning(SAXParseException spex) throws SAXException
2425
{
2526
if (!nowarning) { throw spex; }
26-
else { errors.add(spex); }
27+
else { addError(spex); }
2728
}
2829

2930
public void
3031
error(SAXParseException spex) throws SAXException
3132
{
3233
if (!noerror) { throw spex; }
33-
else { errors.add(spex); }
34+
else { addError(spex); }
3435
}
3536

3637
public void
@@ -43,7 +44,7 @@ public class NokogiriStrictErrorHandler extends NokogiriErrorHandler
4344
error(String domain, String key, XMLParseException e) throws XMLParseException
4445
{
4546
if (!noerror) { throw e; }
46-
else { errors.add(e); }
47+
else { addError(e); }
4748
}
4849

4950
public void
@@ -56,6 +57,6 @@ public class NokogiriStrictErrorHandler extends NokogiriErrorHandler
5657
warning(String domain, String key, XMLParseException e) throws XMLParseException
5758
{
5859
if (!nowarning) { throw e; }
59-
if (!usesNekoHtml(domain)) { errors.add(e); }
60+
if (!usesNekoHtml(domain)) { addError(e); }
6061
}
6162
}

‎ext/java/nokogiri/internals/XmlDomParserContext.java

+16-25
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
package nokogiri.internals;
22

3-
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
4-
import static nokogiri.internals.NokogiriHelpers.isBlank;
5-
6-
import java.io.IOException;
7-
import java.util.ArrayList;
8-
import java.util.List;
9-
3+
import nokogiri.XmlDocument;
4+
import nokogiri.XmlDtd;
5+
import nokogiri.XmlSyntaxError;
106
import org.apache.xerces.parsers.DOMParser;
11-
import org.jruby.Ruby;
12-
import org.jruby.RubyArray;
13-
import org.jruby.RubyClass;
14-
import org.jruby.RubyFixnum;
7+
import org.jruby.*;
158
import org.jruby.exceptions.RaiseException;
16-
import org.jruby.runtime.ThreadContext;
179
import org.jruby.runtime.Helpers;
10+
import org.jruby.runtime.ThreadContext;
1811
import org.jruby.runtime.builtin.IRubyObject;
1912
import org.w3c.dom.Document;
2013
import org.w3c.dom.Node;
2114
import org.w3c.dom.NodeList;
2215
import org.xml.sax.SAXException;
2316

24-
import nokogiri.NokogiriService;
25-
import nokogiri.XmlDocument;
26-
import nokogiri.XmlDtd;
27-
import nokogiri.XmlSyntaxError;
17+
import java.io.IOException;
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
import static nokogiri.internals.NokogiriHelpers.isBlank;
2822

2923
/**
3024
* Parser class for XML DOM processing. This class actually parses XML document
@@ -48,7 +42,6 @@ public class XmlDomParserContext extends ParserContext
4842
protected static final String FEATURE_NOT_EXPAND_ENTITY =
4943
"http://apache.org/xml/features/dom/create-entity-ref-nodes";
5044
protected static final String FEATURE_VALIDATION = "http://xml.org/sax/features/validation";
51-
private static final String XINCLUDE_FEATURE_ID = "http://apache.org/xml/features/xinclude";
5245
private static final String SECURITY_MANAGER = "http://apache.org/xml/properties/security-manager";
5346

5447
protected ParserContext.Options options;
@@ -69,17 +62,17 @@ public class XmlDomParserContext extends ParserContext
6962
this.options = new ParserContext.Options(RubyFixnum.fix2long(options));
7063
java_encoding = NokogiriHelpers.getValidEncodingOrNull(encoding);
7164
ruby_encoding = encoding;
72-
initErrorHandler();
65+
initErrorHandler(runtime);
7366
initParser(runtime);
7467
}
7568

7669
protected void
77-
initErrorHandler()
70+
initErrorHandler(Ruby runtime)
7871
{
7972
if (options.recover) {
80-
errorHandler = new NokogiriNonStrictErrorHandler(options.noError, options.noWarning);
73+
errorHandler = new NokogiriNonStrictErrorHandler(runtime, options.noError, options.noWarning);
8174
} else {
82-
errorHandler = new NokogiriStrictErrorHandler(options.noError, options.noWarning);
75+
errorHandler = new NokogiriStrictErrorHandler(runtime, options.noError, options.noWarning);
8376
}
8477
}
8578

@@ -161,12 +154,10 @@ public class XmlDomParserContext extends ParserContext
161154
mapErrors(ThreadContext context, NokogiriErrorHandler errorHandler)
162155
{
163156
final Ruby runtime = context.runtime;
164-
final List<Exception> errors = errorHandler.getErrors();
157+
final List<RubyException> errors = errorHandler.getErrors();
165158
final IRubyObject[] errorsAry = new IRubyObject[errors.size()];
166159
for (int i = 0; i < errors.size(); i++) {
167-
XmlSyntaxError xmlSyntaxError = XmlSyntaxError.createXMLSyntaxError(runtime);
168-
xmlSyntaxError.setException(errors.get(i));
169-
errorsAry[i] = xmlSyntaxError;
160+
errorsAry[i] = errors.get(i);
170161
}
171162
return runtime.newArrayNoCopy(errorsAry);
172163
}

‎test/xml/sax/test_parser.rb

+455-418
Large diffs are not rendered by default.

‎test/xml/sax/test_push_parser.rb

+209-232
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.