nokogiri 1.11.0.rc1-java → 1.11.2-java

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