nokogiri 1.11.0.rc3-java → 1.11.4-java
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of nokogiri might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/LICENSE-DEPENDENCIES.md +1015 -947
- data/LICENSE.md +1 -1
- data/README.md +168 -91
- data/dependencies.yml +12 -12
- data/ext/java/nokogiri/EncodingHandler.java +76 -89
- data/ext/java/nokogiri/HtmlDocument.java +135 -144
- data/ext/java/nokogiri/HtmlElementDescription.java +102 -117
- data/ext/java/nokogiri/HtmlEntityLookup.java +33 -60
- data/ext/java/nokogiri/HtmlSaxParserContext.java +218 -222
- data/ext/java/nokogiri/HtmlSaxPushParser.java +162 -169
- data/ext/java/nokogiri/NokogiriService.java +595 -556
- data/ext/java/nokogiri/XmlAttr.java +118 -126
- data/ext/java/nokogiri/XmlAttributeDecl.java +95 -106
- data/ext/java/nokogiri/XmlCdata.java +35 -58
- data/ext/java/nokogiri/XmlComment.java +46 -67
- data/ext/java/nokogiri/XmlDocument.java +645 -572
- data/ext/java/nokogiri/XmlDocumentFragment.java +125 -137
- data/ext/java/nokogiri/XmlDtd.java +448 -414
- data/ext/java/nokogiri/XmlElement.java +23 -48
- data/ext/java/nokogiri/XmlElementContent.java +343 -316
- data/ext/java/nokogiri/XmlElementDecl.java +124 -125
- data/ext/java/nokogiri/XmlEntityDecl.java +119 -127
- data/ext/java/nokogiri/XmlEntityReference.java +49 -72
- data/ext/java/nokogiri/XmlNamespace.java +175 -175
- data/ext/java/nokogiri/XmlNode.java +1843 -1622
- data/ext/java/nokogiri/XmlNodeSet.java +361 -331
- data/ext/java/nokogiri/XmlProcessingInstruction.java +47 -69
- data/ext/java/nokogiri/XmlReader.java +513 -450
- data/ext/java/nokogiri/XmlRelaxng.java +89 -101
- data/ext/java/nokogiri/XmlSaxParserContext.java +328 -310
- data/ext/java/nokogiri/XmlSaxPushParser.java +227 -220
- data/ext/java/nokogiri/XmlSchema.java +335 -242
- data/ext/java/nokogiri/XmlSyntaxError.java +113 -119
- data/ext/java/nokogiri/XmlText.java +55 -76
- data/ext/java/nokogiri/XmlXpathContext.java +242 -210
- data/ext/java/nokogiri/XsltStylesheet.java +280 -269
- data/ext/java/nokogiri/internals/ClosedStreamException.java +5 -2
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +201 -190
- data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +17 -10
- data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +43 -16
- data/ext/java/nokogiri/internals/NokogiriDomParser.java +63 -80
- data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +107 -88
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +27 -52
- data/ext/java/nokogiri/internals/NokogiriHandler.java +316 -286
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +736 -652
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +184 -173
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +79 -89
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +64 -79
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +84 -99
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +48 -65
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +119 -78
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -54
- data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +23 -46
- data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +55 -72
- data/ext/java/nokogiri/internals/ParserContext.java +206 -211
- data/ext/java/nokogiri/internals/ReaderNode.java +478 -403
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +822 -739
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +31 -54
- data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +129 -123
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +3 -34
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +206 -207
- data/ext/java/nokogiri/internals/XmlSaxParser.java +22 -47
- data/ext/java/nokogiri/internals/c14n/AttrCompare.java +71 -68
- data/ext/java/nokogiri/internals/c14n/C14nHelper.java +137 -118
- data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +27 -21
- data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +74 -61
- data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +230 -205
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +572 -547
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +17 -10
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +17 -10
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +323 -302
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +232 -219
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +22 -15
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +23 -16
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +23 -16
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +22 -15
- data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +575 -545
- data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +141 -120
- data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +39 -38
- data/ext/java/nokogiri/internals/c14n/Constants.java +13 -10
- data/ext/java/nokogiri/internals/c14n/ElementProxy.java +279 -247
- data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +66 -53
- data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +44 -37
- data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +135 -120
- data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +59 -48
- data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +384 -334
- data/ext/java/nokogiri/internals/c14n/NodeFilter.java +25 -24
- data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +151 -140
- data/ext/java/nokogiri/internals/c14n/XMLUtils.java +456 -423
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1466 -1500
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +626 -574
- data/ext/nokogiri/depend +37 -358
- data/ext/nokogiri/extconf.rb +581 -374
- data/ext/nokogiri/html_document.c +78 -82
- data/ext/nokogiri/html_element_description.c +84 -71
- data/ext/nokogiri/html_entity_lookup.c +21 -16
- data/ext/nokogiri/html_sax_parser_context.c +69 -66
- data/ext/nokogiri/html_sax_push_parser.c +42 -34
- data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
- data/ext/nokogiri/nokogiri.c +192 -93
- data/ext/nokogiri/test_global_handlers.c +40 -0
- data/ext/nokogiri/xml_attr.c +15 -15
- data/ext/nokogiri/xml_attribute_decl.c +18 -18
- data/ext/nokogiri/xml_cdata.c +13 -18
- data/ext/nokogiri/xml_comment.c +19 -26
- data/ext/nokogiri/xml_document.c +246 -188
- data/ext/nokogiri/xml_document_fragment.c +13 -15
- data/ext/nokogiri/xml_dtd.c +54 -48
- data/ext/nokogiri/xml_element_content.c +30 -27
- data/ext/nokogiri/xml_element_decl.c +22 -22
- data/ext/nokogiri/xml_encoding_handler.c +17 -11
- data/ext/nokogiri/xml_entity_decl.c +32 -30
- data/ext/nokogiri/xml_entity_reference.c +16 -18
- data/ext/nokogiri/xml_namespace.c +56 -49
- data/ext/nokogiri/xml_node.c +371 -320
- data/ext/nokogiri/xml_node_set.c +168 -156
- data/ext/nokogiri/xml_processing_instruction.c +17 -19
- data/ext/nokogiri/xml_reader.c +191 -157
- data/ext/nokogiri/xml_relax_ng.c +52 -28
- data/ext/nokogiri/xml_sax_parser.c +118 -118
- data/ext/nokogiri/xml_sax_parser_context.c +103 -86
- data/ext/nokogiri/xml_sax_push_parser.c +36 -27
- data/ext/nokogiri/xml_schema.c +95 -47
- data/ext/nokogiri/xml_syntax_error.c +42 -21
- data/ext/nokogiri/xml_text.c +13 -17
- data/ext/nokogiri/xml_xpath_context.c +206 -123
- data/ext/nokogiri/xslt_stylesheet.c +158 -161
- data/lib/nokogiri.rb +3 -7
- data/lib/nokogiri/css/parser.rb +3 -3
- data/lib/nokogiri/css/parser.y +2 -2
- data/lib/nokogiri/css/xpath_visitor.rb +70 -42
- data/lib/nokogiri/extension.rb +26 -0
- data/lib/nokogiri/html/document.rb +12 -26
- data/lib/nokogiri/html/document_fragment.rb +15 -15
- data/lib/nokogiri/nokogiri.jar +0 -0
- data/lib/nokogiri/version.rb +2 -149
- data/lib/nokogiri/version/constant.rb +5 -0
- data/lib/nokogiri/version/info.rb +205 -0
- data/lib/nokogiri/xml/document.rb +91 -35
- data/lib/nokogiri/xml/document_fragment.rb +4 -6
- data/lib/nokogiri/xml/node.rb +89 -69
- data/lib/nokogiri/xml/parse_options.rb +6 -0
- data/lib/nokogiri/xml/reader.rb +2 -9
- data/lib/nokogiri/xml/relax_ng.rb +6 -2
- data/lib/nokogiri/xml/schema.rb +12 -4
- data/lib/nokogiri/xml/searchable.rb +3 -1
- data/lib/nokogiri/xml/xpath.rb +1 -3
- data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
- metadata +86 -177
- data/ext/nokogiri/html_document.h +0 -10
- data/ext/nokogiri/html_element_description.h +0 -10
- data/ext/nokogiri/html_entity_lookup.h +0 -8
- data/ext/nokogiri/html_sax_parser_context.h +0 -11
- data/ext/nokogiri/html_sax_push_parser.h +0 -9
- data/ext/nokogiri/nokogiri.h +0 -134
- data/ext/nokogiri/xml_attr.h +0 -9
- data/ext/nokogiri/xml_attribute_decl.h +0 -9
- data/ext/nokogiri/xml_cdata.h +0 -9
- data/ext/nokogiri/xml_comment.h +0 -9
- data/ext/nokogiri/xml_document.h +0 -23
- data/ext/nokogiri/xml_document_fragment.h +0 -10
- data/ext/nokogiri/xml_dtd.h +0 -10
- data/ext/nokogiri/xml_element_content.h +0 -10
- data/ext/nokogiri/xml_element_decl.h +0 -9
- data/ext/nokogiri/xml_encoding_handler.h +0 -8
- data/ext/nokogiri/xml_entity_decl.h +0 -10
- data/ext/nokogiri/xml_entity_reference.h +0 -9
- data/ext/nokogiri/xml_io.c +0 -63
- data/ext/nokogiri/xml_io.h +0 -11
- data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
- data/ext/nokogiri/xml_libxml2_hacks.h +0 -12
- data/ext/nokogiri/xml_namespace.h +0 -14
- data/ext/nokogiri/xml_node.h +0 -13
- data/ext/nokogiri/xml_node_set.h +0 -12
- data/ext/nokogiri/xml_processing_instruction.h +0 -9
- data/ext/nokogiri/xml_reader.h +0 -10
- data/ext/nokogiri/xml_relax_ng.h +0 -9
- data/ext/nokogiri/xml_sax_parser.h +0 -39
- data/ext/nokogiri/xml_sax_parser_context.h +0 -10
- data/ext/nokogiri/xml_sax_push_parser.h +0 -9
- data/ext/nokogiri/xml_schema.h +0 -9
- data/ext/nokogiri/xml_syntax_error.h +0 -13
- data/ext/nokogiri/xml_text.h +0 -9
- data/ext/nokogiri/xml_xpath_context.h +0 -10
- data/ext/nokogiri/xslt_stylesheet.h +0 -14
@@ -1,35 +1,3 @@
|
|
1
|
-
/**
|
2
|
-
* (The MIT License)
|
3
|
-
*
|
4
|
-
* Copyright (c) 2008 - 2011:
|
5
|
-
*
|
6
|
-
* * {Aaron Patterson}[http://tenderlovemaking.com]
|
7
|
-
* * {Mike Dalessio}[http://mike.daless.io]
|
8
|
-
* * {Charles Nutter}[http://blog.headius.com]
|
9
|
-
* * {Sergio Arbeo}[http://www.serabe.com]
|
10
|
-
* * {Patrick Mahoney}[http://polycrystal.org]
|
11
|
-
* * {Yoko Harada}[http://yokolet.blogspot.com]
|
12
|
-
*
|
13
|
-
* Permission is hereby granted, free of charge, to any person obtaining
|
14
|
-
* a copy of this software and associated documentation files (the
|
15
|
-
* 'Software'), to deal in the Software without restriction, including
|
16
|
-
* without limitation the rights to use, copy, modify, merge, publish,
|
17
|
-
* distribute, sublicense, and/or sell copies of the Software, and to
|
18
|
-
* permit persons to whom the Software is furnished to do so, subject to
|
19
|
-
* the following conditions:
|
20
|
-
*
|
21
|
-
* The above copyright notice and this permission notice shall be
|
22
|
-
* included in all copies or substantial portions of the Software.
|
23
|
-
*
|
24
|
-
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
25
|
-
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
26
|
-
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
27
|
-
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
28
|
-
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
29
|
-
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
30
|
-
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
31
|
-
*/
|
32
|
-
|
33
1
|
package nokogiri;
|
34
2
|
|
35
3
|
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
|
@@ -56,89 +24,109 @@ import org.jruby.Ruby;
|
|
56
24
|
import org.jruby.RubyClass;
|
57
25
|
import org.jruby.anno.JRubyClass;
|
58
26
|
import org.jruby.runtime.ThreadContext;
|
27
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
59
28
|
import org.w3c.dom.Document;
|
60
29
|
import org.xml.sax.ErrorHandler;
|
61
30
|
import org.xml.sax.SAXException;
|
62
31
|
|
63
32
|
/**
|
64
33
|
* Class for Nokogiri::XML::RelaxNG
|
65
|
-
*
|
34
|
+
*
|
66
35
|
* @author sergio
|
67
36
|
* @author Yoko Harada <yokolet@gmail.com>
|
68
37
|
*/
|
69
|
-
@JRubyClass(name="Nokogiri::XML::RelaxNG", parent="Nokogiri::XML::Schema")
|
70
|
-
public class XmlRelaxng extends XmlSchema
|
71
|
-
|
38
|
+
@JRubyClass(name = "Nokogiri::XML::RelaxNG", parent = "Nokogiri::XML::Schema")
|
39
|
+
public class XmlRelaxng extends XmlSchema
|
40
|
+
{
|
41
|
+
private Verifier verifier;
|
72
42
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
43
|
+
public
|
44
|
+
XmlRelaxng(Ruby ruby, RubyClass klazz)
|
45
|
+
{
|
46
|
+
super(ruby, klazz);
|
47
|
+
}
|
48
|
+
|
49
|
+
private void
|
50
|
+
setVerifier(Verifier verifier)
|
51
|
+
{
|
52
|
+
this.verifier = verifier;
|
53
|
+
}
|
54
|
+
|
55
|
+
static XmlSchema
|
56
|
+
createSchemaInstance(ThreadContext context, RubyClass klazz, Source source, IRubyObject parseOptions)
|
57
|
+
{
|
58
|
+
Ruby runtime = context.getRuntime();
|
59
|
+
XmlRelaxng xmlRelaxng = (XmlRelaxng) NokogiriService.XML_RELAXNG_ALLOCATOR.allocate(runtime, klazz);
|
60
|
+
|
61
|
+
if (parseOptions == null) {
|
62
|
+
parseOptions = defaultParseOptions(context.getRuntime());
|
93
63
|
}
|
94
64
|
|
95
|
-
|
96
|
-
|
97
|
-
VerifierFactory factory = new com.thaiopensource.relaxng.jarv.VerifierFactoryImpl();
|
98
|
-
if (source instanceof StreamSource) {
|
99
|
-
StreamSource ss = (StreamSource)source;
|
100
|
-
is = ss.getInputStream();
|
101
|
-
} else { //if (this.source instanceof DOMSource)
|
102
|
-
DOMSource ds = (DOMSource)source;
|
103
|
-
StringWriter xmlAsWriter = new StringWriter();
|
104
|
-
StreamResult result = new StreamResult(xmlAsWriter);
|
105
|
-
try {
|
106
|
-
TransformerFactory.newInstance().newTransformer().transform(ds, result);
|
107
|
-
} catch (TransformerConfigurationException ex) {
|
108
|
-
throw context.getRuntime()
|
109
|
-
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
110
|
-
} catch (TransformerException ex) {
|
111
|
-
throw context.getRuntime()
|
112
|
-
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
113
|
-
}
|
114
|
-
try {
|
115
|
-
is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
|
116
|
-
} catch (UnsupportedEncodingException ex) {
|
117
|
-
throw context.getRuntime()
|
118
|
-
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
119
|
-
}
|
120
|
-
}
|
65
|
+
xmlRelaxng.setInstanceVariable("@errors", runtime.newEmptyArray());
|
66
|
+
xmlRelaxng.setInstanceVariable("@parse_options", parseOptions);
|
121
67
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
throw context.getRuntime()
|
129
|
-
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
130
|
-
} catch (IOException ex) {
|
131
|
-
throw context.getRuntime().newIOError(ex.getMessage());
|
132
|
-
}
|
68
|
+
try {
|
69
|
+
Schema schema = xmlRelaxng.getSchema(source, context);
|
70
|
+
xmlRelaxng.setVerifier(schema.newVerifier());
|
71
|
+
return xmlRelaxng;
|
72
|
+
} catch (VerifierConfigurationException ex) {
|
73
|
+
throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
|
133
74
|
}
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
75
|
+
}
|
76
|
+
|
77
|
+
private Schema
|
78
|
+
getSchema(Source source, ThreadContext context)
|
79
|
+
{
|
80
|
+
InputStream is;
|
81
|
+
VerifierFactory factory = new com.thaiopensource.relaxng.jarv.VerifierFactoryImpl();
|
82
|
+
if (source instanceof StreamSource) {
|
83
|
+
StreamSource ss = (StreamSource)source;
|
84
|
+
is = ss.getInputStream();
|
85
|
+
} else { //if (this.source instanceof DOMSource)
|
86
|
+
DOMSource ds = (DOMSource)source;
|
87
|
+
StringWriter xmlAsWriter = new StringWriter();
|
88
|
+
StreamResult result = new StreamResult(xmlAsWriter);
|
89
|
+
try {
|
90
|
+
TransformerFactory.newInstance().newTransformer().transform(ds, result);
|
91
|
+
} catch (TransformerConfigurationException ex) {
|
92
|
+
throw context.getRuntime()
|
93
|
+
.newRuntimeError("Could not parse document: " + ex.getMessage());
|
94
|
+
} catch (TransformerException ex) {
|
95
|
+
throw context.getRuntime()
|
96
|
+
.newRuntimeError("Could not parse document: " + ex.getMessage());
|
97
|
+
}
|
98
|
+
try {
|
99
|
+
is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
|
100
|
+
} catch (UnsupportedEncodingException ex) {
|
101
|
+
throw context.getRuntime()
|
102
|
+
.newRuntimeError("Could not parse document: " + ex.getMessage());
|
103
|
+
}
|
138
104
|
}
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
105
|
+
|
106
|
+
try {
|
107
|
+
return factory.compileSchema(is);
|
108
|
+
} catch (VerifierConfigurationException ex) {
|
109
|
+
throw context.getRuntime()
|
110
|
+
.newRuntimeError("Could not parse document: " + ex.getMessage());
|
111
|
+
} catch (SAXException ex) {
|
112
|
+
throw context.getRuntime()
|
113
|
+
.newRuntimeError("Could not parse document: " + ex.getMessage());
|
114
|
+
} catch (IOException ex) {
|
115
|
+
throw context.getRuntime().newIOError(ex.getClass() + ": " + ex.getMessage());
|
143
116
|
}
|
117
|
+
}
|
118
|
+
|
119
|
+
@Override
|
120
|
+
protected void
|
121
|
+
setErrorHandler(ErrorHandler errorHandler)
|
122
|
+
{
|
123
|
+
verifier.setErrorHandler(errorHandler);
|
124
|
+
}
|
125
|
+
|
126
|
+
@Override
|
127
|
+
protected void
|
128
|
+
validate(Document document) throws SAXException, IOException
|
129
|
+
{
|
130
|
+
verifier.verify(document);
|
131
|
+
}
|
144
132
|
}
|
@@ -1,35 +1,3 @@
|
|
1
|
-
/**
|
2
|
-
* (The MIT License)
|
3
|
-
*
|
4
|
-
* Copyright (c) 2008 - 2011:
|
5
|
-
*
|
6
|
-
* * {Aaron Patterson}[http://tenderlovemaking.com]
|
7
|
-
* * {Mike Dalessio}[http://mike.daless.io]
|
8
|
-
* * {Charles Nutter}[http://blog.headius.com]
|
9
|
-
* * {Sergio Arbeo}[http://www.serabe.com]
|
10
|
-
* * {Patrick Mahoney}[http://polycrystal.org]
|
11
|
-
* * {Yoko Harada}[http://yokolet.blogspot.com]
|
12
|
-
*
|
13
|
-
* Permission is hereby granted, free of charge, to any person obtaining
|
14
|
-
* a copy of this software and associated documentation files (the
|
15
|
-
* 'Software'), to deal in the Software without restriction, including
|
16
|
-
* without limitation the rights to use, copy, modify, merge, publish,
|
17
|
-
* distribute, sublicense, and/or sell copies of the Software, and to
|
18
|
-
* permit persons to whom the Software is furnished to do so, subject to
|
19
|
-
* the following conditions:
|
20
|
-
*
|
21
|
-
* The above copyright notice and this permission notice shall be
|
22
|
-
* included in all copies or substantial portions of the Software.
|
23
|
-
*
|
24
|
-
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
25
|
-
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
26
|
-
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
27
|
-
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
28
|
-
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
29
|
-
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
30
|
-
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
31
|
-
*/
|
32
|
-
|
33
1
|
package nokogiri;
|
34
2
|
|
35
3
|
import static org.jruby.runtime.Helpers.invoke;
|
@@ -67,303 +35,353 @@ import nokogiri.internals.XmlSaxParser;
|
|
67
35
|
* @author Patrick Mahoney <pat@polycrystal.org>
|
68
36
|
* @author Yoko Harada <yokolet@gmail.com>
|
69
37
|
*/
|
70
|
-
@JRubyClass(name="Nokogiri::XML::SAX::ParserContext")
|
71
|
-
public class XmlSaxParserContext extends ParserContext
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
*/
|
106
|
-
@Override
|
107
|
-
public Object clone() throws CloneNotSupportedException {
|
108
|
-
return super.clone();
|
109
|
-
}
|
110
|
-
|
111
|
-
protected AbstractSAXParser createParser() throws SAXException {
|
112
|
-
XmlSaxParser parser = new XmlSaxParser();
|
113
|
-
parser.setFeature(FEATURE_NAMESPACE_PREFIXES, true);
|
114
|
-
parser.setFeature(FEATURE_LOAD_EXTERNAL_DTD, false);
|
115
|
-
return parser;
|
38
|
+
@JRubyClass(name = "Nokogiri::XML::SAX::ParserContext")
|
39
|
+
public class XmlSaxParserContext extends ParserContext
|
40
|
+
{
|
41
|
+
|
42
|
+
protected static final String FEATURE_NAMESPACES =
|
43
|
+
"http://xml.org/sax/features/namespaces";
|
44
|
+
protected static final String FEATURE_NAMESPACE_PREFIXES =
|
45
|
+
"http://xml.org/sax/features/namespace-prefixes";
|
46
|
+
protected static final String FEATURE_LOAD_EXTERNAL_DTD =
|
47
|
+
"http://apache.org/xml/features/nonvalidating/load-external-dtd";
|
48
|
+
protected static final String FEATURE_CONTINUE_AFTER_FATAL_ERROR =
|
49
|
+
"http://apache.org/xml/features/continue-after-fatal-error";
|
50
|
+
|
51
|
+
protected AbstractSAXParser parser;
|
52
|
+
|
53
|
+
protected NokogiriHandler handler;
|
54
|
+
private boolean replaceEntities = true;
|
55
|
+
private boolean recovery = false;
|
56
|
+
|
57
|
+
public
|
58
|
+
XmlSaxParserContext(final Ruby ruby, RubyClass rubyClass)
|
59
|
+
{
|
60
|
+
super(ruby, rubyClass);
|
61
|
+
}
|
62
|
+
|
63
|
+
protected void
|
64
|
+
initialize(Ruby runtime)
|
65
|
+
{
|
66
|
+
try {
|
67
|
+
parser = createParser();
|
68
|
+
} catch (SAXException se) {
|
69
|
+
// Unexpected failure in XML subsystem
|
70
|
+
RaiseException ex = runtime.newRuntimeError(se.toString());
|
71
|
+
ex.initCause(se);
|
72
|
+
throw ex;
|
116
73
|
}
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
74
|
+
}
|
75
|
+
|
76
|
+
/**
|
77
|
+
* Create and return a copy of this object.
|
78
|
+
*
|
79
|
+
* @return a clone of this object
|
80
|
+
*/
|
81
|
+
@Override
|
82
|
+
public Object
|
83
|
+
clone() throws CloneNotSupportedException
|
84
|
+
{
|
85
|
+
return super.clone();
|
86
|
+
}
|
87
|
+
|
88
|
+
protected AbstractSAXParser
|
89
|
+
createParser() throws SAXException
|
90
|
+
{
|
91
|
+
XmlSaxParser parser = new XmlSaxParser();
|
92
|
+
parser.setFeature(FEATURE_NAMESPACE_PREFIXES, true);
|
93
|
+
parser.setFeature(FEATURE_LOAD_EXTERNAL_DTD, false);
|
94
|
+
return parser;
|
95
|
+
}
|
96
|
+
|
97
|
+
/**
|
98
|
+
* Create a new parser context that will parse the string
|
99
|
+
* <code>data</code>.
|
100
|
+
*/
|
101
|
+
@JRubyMethod(name = "memory", meta = true)
|
102
|
+
public static IRubyObject
|
103
|
+
parse_memory(ThreadContext context,
|
104
|
+
IRubyObject klazz,
|
105
|
+
IRubyObject data)
|
106
|
+
{
|
107
|
+
final Ruby runtime = context.runtime;
|
108
|
+
XmlSaxParserContext ctx = newInstance(runtime, (RubyClass) klazz);
|
109
|
+
ctx.initialize(runtime);
|
110
|
+
ctx.setStringInputSource(context, data, runtime.getNil());
|
111
|
+
return ctx;
|
112
|
+
}
|
113
|
+
|
114
|
+
/**
|
115
|
+
* Create a new parser context that will read from the file
|
116
|
+
* <code>data</code> and parse.
|
117
|
+
*/
|
118
|
+
@JRubyMethod(name = "file", meta = true)
|
119
|
+
public static IRubyObject
|
120
|
+
parse_file(ThreadContext context,
|
121
|
+
IRubyObject klazz,
|
122
|
+
IRubyObject data)
|
123
|
+
{
|
124
|
+
final Ruby runtime = context.runtime;
|
125
|
+
XmlSaxParserContext ctx = newInstance(runtime, (RubyClass) klazz);
|
126
|
+
ctx.initialize(context.getRuntime());
|
127
|
+
ctx.setInputSourceFile(context, data);
|
128
|
+
return ctx;
|
129
|
+
}
|
130
|
+
|
131
|
+
/**
|
132
|
+
* Create a new parser context that will read from the IO or
|
133
|
+
* StringIO <code>data</code> and parse.
|
134
|
+
*
|
135
|
+
* TODO: Currently ignores encoding <code>enc</code>.
|
136
|
+
*/
|
137
|
+
@JRubyMethod(name = "io", meta = true)
|
138
|
+
public static IRubyObject
|
139
|
+
parse_io(ThreadContext context,
|
140
|
+
IRubyObject klazz,
|
141
|
+
IRubyObject data,
|
142
|
+
IRubyObject enc)
|
143
|
+
{
|
144
|
+
//int encoding = (int)enc.convertToInteger().getLongValue();
|
145
|
+
final Ruby runtime = context.runtime;
|
146
|
+
XmlSaxParserContext ctx = newInstance(runtime, (RubyClass) klazz);
|
147
|
+
ctx.initialize(runtime);
|
148
|
+
ctx.setIOInputSource(context, data, runtime.getNil());
|
149
|
+
return ctx;
|
150
|
+
}
|
151
|
+
|
152
|
+
/**
|
153
|
+
* Create a new parser context that will read from a raw input stream.
|
154
|
+
* Meant to be run in a separate thread by XmlSaxPushParser.
|
155
|
+
*/
|
156
|
+
static XmlSaxParserContext
|
157
|
+
parse_stream(final Ruby runtime, RubyClass klazz, InputStream stream)
|
158
|
+
{
|
159
|
+
XmlSaxParserContext ctx = newInstance(runtime, klazz);
|
160
|
+
ctx.initialize(runtime);
|
161
|
+
ctx.setInputSource(stream);
|
162
|
+
return ctx;
|
163
|
+
}
|
164
|
+
|
165
|
+
private static XmlSaxParserContext
|
166
|
+
newInstance(final Ruby runtime, final RubyClass klazz)
|
167
|
+
{
|
168
|
+
return (XmlSaxParserContext) NokogiriService.XML_SAXPARSER_CONTEXT_ALLOCATOR.allocate(runtime, klazz);
|
169
|
+
}
|
170
|
+
|
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
|
+
|
193
|
+
public final NokogiriHandler
|
194
|
+
getNokogiriHandler() { return handler; }
|
195
|
+
|
196
|
+
/**
|
197
|
+
* Perform any initialization prior to parsing with the handler
|
198
|
+
* <code>handlerRuby</code>. Convenience hook for subclasses.
|
199
|
+
*/
|
200
|
+
protected void
|
201
|
+
preParse(Ruby runtime, IRubyObject handlerRuby, NokogiriHandler handler)
|
202
|
+
{
|
203
|
+
((XmlSaxParser) parser).setXmlDeclHandler(handler);
|
204
|
+
if (recovery) {
|
205
|
+
try {
|
206
|
+
parser.setFeature(FEATURE_CONTINUE_AFTER_FATAL_ERROR, true);
|
207
|
+
} catch (Exception e) {
|
208
|
+
// Unexpected failure in XML subsystem
|
209
|
+
throw runtime.newRuntimeError(e.getMessage());
|
210
|
+
}
|
131
211
|
}
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
212
|
+
}
|
213
|
+
|
214
|
+
protected void
|
215
|
+
postParse(Ruby runtime, IRubyObject handlerRuby, NokogiriHandler handler)
|
216
|
+
{
|
217
|
+
// noop
|
218
|
+
}
|
219
|
+
|
220
|
+
protected void
|
221
|
+
do_parse() throws SAXException, IOException
|
222
|
+
{
|
223
|
+
parser.parse(getInputSource());
|
224
|
+
}
|
225
|
+
|
226
|
+
@JRubyMethod
|
227
|
+
public IRubyObject
|
228
|
+
parse_with(ThreadContext context, IRubyObject handlerRuby)
|
229
|
+
{
|
230
|
+
final Ruby runtime = context.getRuntime();
|
231
|
+
|
232
|
+
if (!invoke(context, handlerRuby, "respond_to?", runtime.newSymbol("document")).isTrue()) {
|
233
|
+
throw runtime.newArgumentError("argument must respond_to document");
|
146
234
|
}
|
147
235
|
|
148
|
-
|
149
|
-
|
150
|
-
* StringIO <code>data</code> and parse.
|
151
|
-
*
|
152
|
-
* TODO: Currently ignores encoding <code>enc</code>.
|
153
|
-
*/
|
154
|
-
@JRubyMethod(name="io", meta=true)
|
155
|
-
public static IRubyObject parse_io(ThreadContext context,
|
156
|
-
IRubyObject klazz,
|
157
|
-
IRubyObject data,
|
158
|
-
IRubyObject enc) {
|
159
|
-
//int encoding = (int)enc.convertToInteger().getLongValue();
|
160
|
-
final Ruby runtime = context.runtime;
|
161
|
-
XmlSaxParserContext ctx = newInstance(runtime, (RubyClass) klazz);
|
162
|
-
ctx.initialize(runtime);
|
163
|
-
ctx.setIOInputSource(context, data, runtime.getNil());
|
164
|
-
return ctx;
|
165
|
-
}
|
236
|
+
NokogiriHandler handler = this.handler = new NokogiriHandler(runtime, handlerRuby);
|
237
|
+
preParse(runtime, handlerRuby, handler);
|
166
238
|
|
167
|
-
|
168
|
-
|
169
|
-
* Meant to be run in a separate thread by XmlSaxPushParser.
|
170
|
-
*/
|
171
|
-
static XmlSaxParserContext parse_stream(final Ruby runtime, RubyClass klazz, InputStream stream) {
|
172
|
-
XmlSaxParserContext ctx = newInstance(runtime, klazz);
|
173
|
-
ctx.initialize(runtime);
|
174
|
-
ctx.setInputSource(stream);
|
175
|
-
return ctx;
|
176
|
-
}
|
239
|
+
setContentHandler(handler);
|
240
|
+
setErrorHandler(handler);
|
177
241
|
|
178
|
-
|
179
|
-
|
242
|
+
try {
|
243
|
+
setProperty("http://xml.org/sax/properties/lexical-handler", handler);
|
244
|
+
} catch (Exception ex) {
|
245
|
+
throw runtime.newRuntimeError("Problem while creating XML SAX Parser: " + ex.toString());
|
180
246
|
}
|
181
247
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
parser.setErrorHandler(handler);
|
196
|
-
}
|
197
|
-
|
198
|
-
public final NokogiriHandler getNokogiriHandler() { return handler; }
|
199
|
-
|
200
|
-
/**
|
201
|
-
* Perform any initialization prior to parsing with the handler
|
202
|
-
* <code>handlerRuby</code>. Convenience hook for subclasses.
|
203
|
-
*/
|
204
|
-
protected void preParse(Ruby runtime, IRubyObject handlerRuby, NokogiriHandler handler) {
|
205
|
-
((XmlSaxParser) parser).setXmlDeclHandler(handler);
|
206
|
-
if (recovery) {
|
207
|
-
try {
|
208
|
-
parser.setFeature(FEATURE_CONTINUE_AFTER_FATAL_ERROR, true);
|
209
|
-
}
|
210
|
-
catch (Exception e) {
|
211
|
-
throw RaiseException.createNativeRaiseException(runtime, e);
|
212
|
-
}
|
248
|
+
try {
|
249
|
+
try {
|
250
|
+
do_parse();
|
251
|
+
} catch (SAXParseException ex) {
|
252
|
+
// A bad document (<foo><bar></foo>) should call the
|
253
|
+
// error handler instead of raising a SAX exception.
|
254
|
+
|
255
|
+
// However, an EMPTY document should raise a RuntimeError.
|
256
|
+
// This is a bit kludgy, but AFAIK SAX doesn't distinguish
|
257
|
+
// between empty and bad whereas Nokogiri does.
|
258
|
+
String message = ex.getMessage();
|
259
|
+
if (message != null && message.contains("Premature end of file.") && stringDataSize < 1) {
|
260
|
+
throw runtime.newRuntimeError("couldn't parse document: " + message);
|
213
261
|
}
|
262
|
+
handler.error(ex);
|
263
|
+
}
|
264
|
+
} catch (SAXException ex) {
|
265
|
+
// Unexpected failure in XML subsystem
|
266
|
+
throw runtime.newRuntimeError(ex.getMessage());
|
267
|
+
} catch (IOException ex) {
|
268
|
+
throw runtime.newIOErrorFromException(ex);
|
214
269
|
}
|
215
270
|
|
216
|
-
|
217
|
-
|
271
|
+
postParse(runtime, handlerRuby, handler);
|
272
|
+
|
273
|
+
//maybeTrimLeadingAndTrailingWhitespace(context, handlerRuby);
|
274
|
+
|
275
|
+
return runtime.getNil();
|
276
|
+
}
|
277
|
+
|
278
|
+
/**
|
279
|
+
* Can take a boolean assignment.
|
280
|
+
*
|
281
|
+
* @param context
|
282
|
+
* @param value
|
283
|
+
* @return
|
284
|
+
*/
|
285
|
+
@JRubyMethod(name = "replace_entities=")
|
286
|
+
public IRubyObject
|
287
|
+
set_replace_entities(ThreadContext context, IRubyObject value)
|
288
|
+
{
|
289
|
+
replaceEntities = value.isTrue();
|
290
|
+
return this;
|
291
|
+
}
|
292
|
+
|
293
|
+
@JRubyMethod(name = "replace_entities")
|
294
|
+
public IRubyObject
|
295
|
+
get_replace_entities(ThreadContext context)
|
296
|
+
{
|
297
|
+
return context.runtime.newBoolean(replaceEntities);
|
298
|
+
}
|
299
|
+
|
300
|
+
/**
|
301
|
+
* Can take a boolean assignment.
|
302
|
+
*
|
303
|
+
* @param context
|
304
|
+
* @param value
|
305
|
+
* @return
|
306
|
+
*/
|
307
|
+
@JRubyMethod(name = "recovery=")
|
308
|
+
public IRubyObject
|
309
|
+
set_recovery(ThreadContext context, IRubyObject value)
|
310
|
+
{
|
311
|
+
recovery = value.isTrue();
|
312
|
+
return this;
|
313
|
+
}
|
314
|
+
|
315
|
+
@JRubyMethod(name = "recovery")
|
316
|
+
public IRubyObject
|
317
|
+
get_recovery(ThreadContext context)
|
318
|
+
{
|
319
|
+
return context.runtime.newBoolean(recovery);
|
320
|
+
}
|
321
|
+
|
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;
|
218
338
|
}
|
219
|
-
|
220
|
-
|
221
|
-
|
339
|
+
IRubyObject stack = adapter.getInstanceVariable(handler, "@stack");
|
340
|
+
if (stack == null || stack.isNil()) {
|
341
|
+
return;
|
222
342
|
}
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
if(!invoke(context, handlerRuby, "respond_to?", runtime.newSymbol("document")).isTrue()) {
|
229
|
-
throw runtime.newArgumentError("argument must respond_to document");
|
230
|
-
}
|
231
|
-
|
232
|
-
NokogiriHandler handler = this.handler = new NokogiriHandler(runtime, handlerRuby);
|
233
|
-
preParse(runtime, handlerRuby, handler);
|
234
|
-
|
235
|
-
setContentHandler(handler);
|
236
|
-
setErrorHandler(handler);
|
237
|
-
|
238
|
-
try{
|
239
|
-
setProperty("http://xml.org/sax/properties/lexical-handler", handler);
|
240
|
-
}
|
241
|
-
catch (Exception ex) {
|
242
|
-
throw runtime.newRuntimeError("Problem while creating XML SAX Parser: " + ex.toString());
|
243
|
-
}
|
244
|
-
|
245
|
-
try{
|
246
|
-
try {
|
247
|
-
do_parse();
|
248
|
-
}
|
249
|
-
catch (SAXParseException ex) {
|
250
|
-
// A bad document (<foo><bar></foo>) should call the
|
251
|
-
// error handler instead of raising a SAX exception.
|
252
|
-
|
253
|
-
// However, an EMPTY document should raise a RuntimeError.
|
254
|
-
// This is a bit kludgy, but AFAIK SAX doesn't distinguish
|
255
|
-
// between empty and bad whereas Nokogiri does.
|
256
|
-
String message = ex.getMessage();
|
257
|
-
if (message != null && message.contains("Premature end of file.") && stringDataSize < 1) {
|
258
|
-
throw runtime.newRuntimeError("couldn't parse document: " + message);
|
259
|
-
}
|
260
|
-
handler.error(ex);
|
261
|
-
}
|
262
|
-
}
|
263
|
-
catch (SAXException ex) {
|
264
|
-
throw RaiseException.createNativeRaiseException(runtime, ex);
|
265
|
-
}
|
266
|
-
catch (IOException ex) {
|
267
|
-
throw runtime.newIOErrorFromException(ex);
|
268
|
-
}
|
269
|
-
|
270
|
-
postParse(runtime, handlerRuby, handler);
|
271
|
-
|
272
|
-
//maybeTrimLeadingAndTrailingWhitespace(context, handlerRuby);
|
273
|
-
|
274
|
-
return runtime.getNil();
|
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;
|
275
347
|
}
|
276
348
|
|
277
|
-
|
278
|
-
* Can take a boolean assignment.
|
279
|
-
*
|
280
|
-
* @param context
|
281
|
-
* @param value
|
282
|
-
* @return
|
283
|
-
*/
|
284
|
-
@JRubyMethod(name = "replace_entities=")
|
285
|
-
public IRubyObject set_replace_entities(ThreadContext context, IRubyObject value) {
|
286
|
-
replaceEntities = value.isTrue();
|
287
|
-
return this;
|
288
|
-
}
|
289
|
-
|
290
|
-
@JRubyMethod(name="replace_entities")
|
291
|
-
public IRubyObject get_replace_entities(ThreadContext context) {
|
292
|
-
return context.runtime.newBoolean(replaceEntities);
|
293
|
-
}
|
294
|
-
|
295
|
-
/**
|
296
|
-
* Can take a boolean assignment.
|
297
|
-
*
|
298
|
-
* @param context
|
299
|
-
* @param value
|
300
|
-
* @return
|
301
|
-
*/
|
302
|
-
@JRubyMethod(name = "recovery=")
|
303
|
-
public IRubyObject set_recovery(ThreadContext context, IRubyObject value) {
|
304
|
-
recovery = value.isTrue();
|
305
|
-
return this;
|
306
|
-
}
|
307
|
-
|
308
|
-
@JRubyMethod(name="recovery")
|
309
|
-
public IRubyObject get_recovery(ThreadContext context) {
|
310
|
-
return context.runtime.newBoolean(recovery);
|
311
|
-
}
|
312
|
-
|
313
|
-
/**
|
314
|
-
* If the handler's document is a FragmentHandler, attempt to trim
|
315
|
-
* leading and trailing whitespace.
|
316
|
-
*
|
317
|
-
* This is a bit hackish and depends heavily on the internals of
|
318
|
-
* FragmentHandler.
|
319
|
-
*/
|
320
|
-
protected void maybeTrimLeadingAndTrailingWhitespace(ThreadContext context, IRubyObject parser) {
|
321
|
-
RubyObjectAdapter adapter = JavaEmbedUtils.newObjectAdapter();
|
322
|
-
RubyModule mod = context.getRuntime().getClassFromPath("Nokogiri::XML::FragmentHandler");
|
323
|
-
|
324
|
-
IRubyObject handler = adapter.getInstanceVariable(parser, "@document");
|
325
|
-
if (handler == null || handler.isNil() || !adapter.isKindOf(handler, mod))
|
326
|
-
return;
|
327
|
-
IRubyObject stack = adapter.getInstanceVariable(handler, "@stack");
|
328
|
-
if (stack == null || stack.isNil())
|
329
|
-
return;
|
330
|
-
// doc is finally a DocumentFragment whose nodes we can check
|
331
|
-
IRubyObject doc = adapter.callMethod(stack, "first");
|
332
|
-
if (doc == null || doc.isNil())
|
333
|
-
return;
|
334
|
-
|
335
|
-
IRubyObject children;
|
336
|
-
|
337
|
-
for (;;) {
|
338
|
-
children = adapter.callMethod(doc, "children");
|
339
|
-
IRubyObject first = adapter.callMethod(children, "first");
|
340
|
-
if (NokogiriHelpers.isBlank(first)) adapter.callMethod(first, "unlink");
|
341
|
-
else break;
|
342
|
-
}
|
349
|
+
IRubyObject children;
|
343
350
|
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
}
|
350
|
-
|
351
|
-
// While we have a document, normalize it.
|
352
|
-
((XmlNode) doc).normalize();
|
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; }
|
353
356
|
}
|
354
357
|
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
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; }
|
360
363
|
}
|
361
364
|
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
365
|
+
// While we have a document, normalize it.
|
366
|
+
((XmlNode) doc).normalize();
|
367
|
+
}
|
368
|
+
|
369
|
+
@JRubyMethod(name = "column")
|
370
|
+
public IRubyObject
|
371
|
+
column(ThreadContext context)
|
372
|
+
{
|
373
|
+
final Integer number = handler.getColumn();
|
374
|
+
if (number == null) { return context.getRuntime().getNil(); }
|
375
|
+
return RubyFixnum.newFixnum(context.getRuntime(), number.longValue());
|
376
|
+
}
|
377
|
+
|
378
|
+
@JRubyMethod(name = "line")
|
379
|
+
public IRubyObject
|
380
|
+
line(ThreadContext context)
|
381
|
+
{
|
382
|
+
final Integer number = handler.getLine();
|
383
|
+
if (number == null) { return context.getRuntime().getNil(); }
|
384
|
+
return RubyFixnum.newFixnum(context.getRuntime(), number.longValue());
|
385
|
+
}
|
368
386
|
|
369
387
|
}
|