nokogiri 1.11.0.rc4-java → 1.11.5-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.

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