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.

Files changed (187) 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 +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 -1622
  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 +89 -101
  32. data/ext/java/nokogiri/XmlSaxParserContext.java +328 -310
  33. data/ext/java/nokogiri/XmlSaxPushParser.java +227 -220
  34. data/ext/java/nokogiri/XmlSchema.java +335 -242
  35. data/ext/java/nokogiri/XmlSyntaxError.java +113 -119
  36. data/ext/java/nokogiri/XmlText.java +55 -76
  37. data/ext/java/nokogiri/XmlXpathContext.java +242 -210
  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 -190
  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 +79 -89
  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 +119 -78
  54. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -54
  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 +37 -358
  95. data/ext/nokogiri/extconf.rb +581 -374
  96. data/ext/nokogiri/html_document.c +78 -82
  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 +69 -66
  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 +192 -93
  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 +52 -28
  122. data/ext/nokogiri/xml_sax_parser.c +118 -118
  123. data/ext/nokogiri/xml_sax_parser_context.c +103 -86
  124. data/ext/nokogiri/xml_sax_push_parser.c +36 -27
  125. data/ext/nokogiri/xml_schema.c +95 -47
  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 +206 -123
  129. data/ext/nokogiri/xslt_stylesheet.c +158 -161
  130. data/lib/nokogiri.rb +3 -7
  131. data/lib/nokogiri/css/parser.rb +3 -3
  132. data/lib/nokogiri/css/parser.y +2 -2
  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 -149
  139. data/lib/nokogiri/version/constant.rb +5 -0
  140. data/lib/nokogiri/version/info.rb +205 -0
  141. data/lib/nokogiri/xml/document.rb +91 -35
  142. data/lib/nokogiri/xml/document_fragment.rb +4 -6
  143. data/lib/nokogiri/xml/node.rb +89 -69
  144. data/lib/nokogiri/xml/parse_options.rb +6 -0
  145. data/lib/nokogiri/xml/reader.rb +2 -9
  146. data/lib/nokogiri/xml/relax_ng.rb +6 -2
  147. data/lib/nokogiri/xml/schema.rb +12 -4
  148. data/lib/nokogiri/xml/searchable.rb +3 -1
  149. data/lib/nokogiri/xml/xpath.rb +1 -3
  150. data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
  151. metadata +86 -177
  152. data/ext/nokogiri/html_document.h +0 -10
  153. data/ext/nokogiri/html_element_description.h +0 -10
  154. data/ext/nokogiri/html_entity_lookup.h +0 -8
  155. data/ext/nokogiri/html_sax_parser_context.h +0 -11
  156. data/ext/nokogiri/html_sax_push_parser.h +0 -9
  157. data/ext/nokogiri/nokogiri.h +0 -134
  158. data/ext/nokogiri/xml_attr.h +0 -9
  159. data/ext/nokogiri/xml_attribute_decl.h +0 -9
  160. data/ext/nokogiri/xml_cdata.h +0 -9
  161. data/ext/nokogiri/xml_comment.h +0 -9
  162. data/ext/nokogiri/xml_document.h +0 -23
  163. data/ext/nokogiri/xml_document_fragment.h +0 -10
  164. data/ext/nokogiri/xml_dtd.h +0 -10
  165. data/ext/nokogiri/xml_element_content.h +0 -10
  166. data/ext/nokogiri/xml_element_decl.h +0 -9
  167. data/ext/nokogiri/xml_encoding_handler.h +0 -8
  168. data/ext/nokogiri/xml_entity_decl.h +0 -10
  169. data/ext/nokogiri/xml_entity_reference.h +0 -9
  170. data/ext/nokogiri/xml_io.c +0 -63
  171. data/ext/nokogiri/xml_io.h +0 -11
  172. data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
  173. data/ext/nokogiri/xml_libxml2_hacks.h +0 -12
  174. data/ext/nokogiri/xml_namespace.h +0 -14
  175. data/ext/nokogiri/xml_node.h +0 -13
  176. data/ext/nokogiri/xml_node_set.h +0 -12
  177. data/ext/nokogiri/xml_processing_instruction.h +0 -9
  178. data/ext/nokogiri/xml_reader.h +0 -10
  179. data/ext/nokogiri/xml_relax_ng.h +0 -9
  180. data/ext/nokogiri/xml_sax_parser.h +0 -39
  181. data/ext/nokogiri/xml_sax_parser_context.h +0 -10
  182. data/ext/nokogiri/xml_sax_push_parser.h +0 -9
  183. data/ext/nokogiri/xml_schema.h +0 -9
  184. data/ext/nokogiri/xml_syntax_error.h +0 -13
  185. data/ext/nokogiri/xml_text.h +0 -9
  186. data/ext/nokogiri/xml_xpath_context.h +0 -10
  187. 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.rubyStringToString;
@@ -49,52 +17,62 @@ import nokogiri.internals.SaveContextVisitor;
49
17
 
50
18
  /**
51
19
  * Class for Nokogiri::XML::ProcessingInstruction
52
- *
20
+ *
53
21
  * @author sergio
54
22
  * @author Yoko Harada <yokolet@gmail.com>
55
23
  */
56
- @JRubyClass(name="Nokogiri::XML::ProcessingInstruction", parent="Nokogiri::XML::Node")
57
- public class XmlProcessingInstruction extends XmlNode {
24
+ @JRubyClass(name = "Nokogiri::XML::ProcessingInstruction", parent = "Nokogiri::XML::Node")
25
+ public class XmlProcessingInstruction extends XmlNode
26
+ {
27
+
28
+ public
29
+ XmlProcessingInstruction(Ruby ruby, RubyClass klazz)
30
+ {
31
+ super(ruby, klazz);
32
+ }
33
+
34
+ public
35
+ XmlProcessingInstruction(Ruby ruby, RubyClass klazz, Node node)
36
+ {
37
+ super(ruby, klazz, node);
38
+ }
58
39
 
59
- public XmlProcessingInstruction(Ruby ruby, RubyClass klazz) {
60
- super(ruby, klazz);
61
- }
62
-
63
- public XmlProcessingInstruction(Ruby ruby, RubyClass klazz, Node node) {
64
- super(ruby, klazz, node);
65
- }
40
+ @JRubyMethod(name = "new", meta = true, rest = true, required = 3)
41
+ public static IRubyObject
42
+ rbNew(ThreadContext context,
43
+ IRubyObject klazz,
44
+ IRubyObject[] args)
45
+ {
66
46
 
67
- @JRubyMethod(name="new", meta=true, rest=true, required=3)
68
- public static IRubyObject rbNew(ThreadContext context,
69
- IRubyObject klazz,
70
- IRubyObject[] args) {
47
+ IRubyObject doc = args[0];
48
+ IRubyObject target = args[1];
49
+ IRubyObject data = args[2];
71
50
 
72
- IRubyObject doc = args[0];
73
- IRubyObject target = args[1];
74
- IRubyObject data = args[2];
51
+ Document document = ((XmlNode) doc).getOwnerDocument();
52
+ Node node =
53
+ document.createProcessingInstruction(rubyStringToString(target),
54
+ rubyStringToString(data));
55
+ XmlProcessingInstruction self =
56
+ new XmlProcessingInstruction(context.getRuntime(),
57
+ (RubyClass) klazz,
58
+ node);
75
59
 
76
- Document document = ((XmlNode) doc).getOwnerDocument();
77
- Node node =
78
- document.createProcessingInstruction(rubyStringToString(target),
79
- rubyStringToString(data));
80
- XmlProcessingInstruction self =
81
- new XmlProcessingInstruction(context.getRuntime(),
82
- (RubyClass) klazz,
83
- node);
60
+ Helpers.invoke(context, self, "initialize", args);
84
61
 
85
- Helpers.invoke(context, self, "initialize", args);
62
+ // TODO: if_block_given.
86
63
 
87
- // TODO: if_block_given.
64
+ return self;
65
+ }
88
66
 
89
- return self;
90
- }
67
+ @Override
68
+ public boolean
69
+ isProcessingInstruction() { return true; }
91
70
 
92
- @Override
93
- public boolean isProcessingInstruction() { return true; }
94
-
95
- @Override
96
- public void accept(ThreadContext context, SaveContextVisitor visitor) {
97
- visitor.enter((ProcessingInstruction)node);
98
- visitor.leave((ProcessingInstruction)node);
99
- }
71
+ @Override
72
+ public void
73
+ accept(ThreadContext context, SaveContextVisitor visitor)
74
+ {
75
+ visitor.enter((ProcessingInstruction)node);
76
+ visitor.leave((ProcessingInstruction)node);
77
+ }
100
78
  }
@@ -1,35 +1,3 @@
1
- /**
2
- * (The MIT License)
3
- *
4
- * Copyright (c) 2008 - 2012:
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;
@@ -86,446 +54,541 @@ import nokogiri.internals.ReaderNode.TextNode;
86
54
  * @author sergio
87
55
  * @author Yoko Harada <yokolet@gmail.com>
88
56
  */
89
- @JRubyClass(name="Nokogiri::XML::Reader")
90
- public class XmlReader extends RubyObject {
91
-
92
- private static final int XML_TEXTREADER_MODE_INITIAL = 0;
93
- private static final int XML_TEXTREADER_MODE_INTERACTIVE = 1;
94
- private static final int XML_TEXTREADER_MODE_ERROR = 2;
95
- private static final int XML_TEXTREADER_MODE_EOF = 3;
96
- private static final int XML_TEXTREADER_MODE_CLOSED = 4;
97
- private static final int XML_TEXTREADER_MODE_READING = 5;
98
-
99
- List<ReaderNode> nodeQueue;
100
- private int state;
101
- private int position = 0;
102
- private XMLPullParserConfiguration config;
103
- private boolean continueParsing = true;
104
-
105
- public XmlReader(Ruby runtime, RubyClass klazz) {
106
- super(runtime, klazz);
57
+ @JRubyClass(name = "Nokogiri::XML::Reader")
58
+ public class XmlReader extends RubyObject
59
+ {
60
+
61
+ private static final int XML_TEXTREADER_MODE_INITIAL = 0;
62
+ private static final int XML_TEXTREADER_MODE_INTERACTIVE = 1;
63
+ private static final int XML_TEXTREADER_MODE_ERROR = 2;
64
+ private static final int XML_TEXTREADER_MODE_EOF = 3;
65
+ private static final int XML_TEXTREADER_MODE_CLOSED = 4;
66
+ private static final int XML_TEXTREADER_MODE_READING = 5;
67
+
68
+ List<ReaderNode> nodeQueue;
69
+ private int state;
70
+ private int position = 0;
71
+ private XMLPullParserConfiguration config;
72
+ private boolean continueParsing = true;
73
+
74
+ public
75
+ XmlReader(Ruby runtime, RubyClass klazz)
76
+ {
77
+ super(runtime, klazz);
78
+ }
79
+
80
+ /**
81
+ * Create and return a copy of this object.
82
+ *
83
+ * @return a clone of this object
84
+ */
85
+ @Override
86
+ public Object
87
+ clone() throws CloneNotSupportedException
88
+ {
89
+ return super.clone();
90
+ }
91
+
92
+ public void
93
+ init(Ruby runtime)
94
+ {
95
+ nodeQueue = new LinkedList<ReaderNode>();
96
+ nodeQueue.add(new ReaderNode.EmptyNode(runtime));
97
+ }
98
+
99
+ private void
100
+ setInput(ThreadContext context, InputStream in, IRubyObject url, Options options)
101
+ {
102
+ this.setState(XML_TEXTREADER_MODE_READING);
103
+ config = this.createReader(context.getRuntime(), options);
104
+ InputSource inputSource = new InputSource();
105
+ ParserContext.setUrl(context, inputSource, url);
106
+ XMLInputSource xmlInputSource = new XMLInputSource(inputSource.getPublicId(),
107
+ inputSource.getSystemId(), null, in, null);
108
+ try {
109
+ config.setInputSource(xmlInputSource);
110
+ } catch (IOException e) {
111
+ throw context.getRuntime().newRuntimeError(e.getMessage());
112
+ }
113
+ this.setState(XML_TEXTREADER_MODE_CLOSED);
114
+ }
115
+
116
+ private void
117
+ setState(int state) { this.state = state; }
118
+
119
+ @JRubyMethod
120
+ public IRubyObject
121
+ attribute(ThreadContext context, IRubyObject name)
122
+ {
123
+ return currentNode().getAttributeByName(name);
124
+ }
125
+
126
+ @JRubyMethod
127
+ public IRubyObject
128
+ attribute_at(ThreadContext context, IRubyObject index)
129
+ {
130
+ return currentNode().getAttributeByIndex(index);
131
+ }
132
+
133
+ @JRubyMethod
134
+ public IRubyObject
135
+ attribute_count(ThreadContext context)
136
+ {
137
+ return currentNode().getAttributeCount();
138
+ }
139
+
140
+ @JRubyMethod
141
+ public IRubyObject
142
+ attribute_nodes(ThreadContext context)
143
+ {
144
+ return currentNode().getAttributesNodes();
145
+ }
146
+
147
+ @JRubyMethod(name = "attributes?")
148
+ public IRubyObject
149
+ attributes_p(ThreadContext context)
150
+ {
151
+ return currentNode().hasAttributes();
152
+ }
153
+
154
+ @JRubyMethod
155
+ public IRubyObject
156
+ base_uri(ThreadContext context)
157
+ {
158
+ return currentNode().getXmlBase();
159
+ }
160
+
161
+ @JRubyMethod(name = "default?")
162
+ public IRubyObject
163
+ default_p(ThreadContext context)
164
+ {
165
+ return currentNode().isDefault();
166
+ }
167
+
168
+ @JRubyMethod
169
+ public IRubyObject
170
+ depth(ThreadContext context)
171
+ {
172
+ return currentNode().getDepth();
173
+ }
174
+
175
+ @JRubyMethod(name = {"empty_element?", "self_closing?"})
176
+ public IRubyObject
177
+ empty_element_p(ThreadContext context)
178
+ {
179
+ ReaderNode readerNode = currentNode();
180
+ ensureNodeClosed(context);
181
+
182
+ if (readerNode == null) { return context.getRuntime().getNil(); }
183
+ if (!(readerNode instanceof ElementNode)) { context.getRuntime().getFalse(); }
184
+ return RubyBoolean.newBoolean(context.getRuntime(), !readerNode.hasChildren);
185
+ }
186
+
187
+ @JRubyMethod(meta = true, rest = true)
188
+ public static IRubyObject
189
+ from_io(ThreadContext context, IRubyObject cls, IRubyObject args[])
190
+ {
191
+ // Only to pass the source test.
192
+ Ruby runtime = context.getRuntime();
193
+ // Not nil allowed!
194
+ if (args[0].isNil()) { throw runtime.newArgumentError("io cannot be nil"); }
195
+
196
+ XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime,
197
+ "Nokogiri::XML::Reader"));
198
+ reader.init(runtime);
199
+ reader.setInstanceVariable("@source", args[0]);
200
+ reader.setInstanceVariable("@errors", runtime.newArray());
201
+ IRubyObject url = context.nil;
202
+ if (args.length > 1) { url = args[1]; }
203
+ if (args.length > 2) { reader.setInstanceVariable("@encoding", args[2]); }
204
+
205
+ Options options;
206
+ if (args.length > 3) {
207
+ options = new ParserContext.Options((Long)args[3].toJava(Long.class));
208
+ } else {
209
+ // use the default options RECOVER | NONET
210
+ options = new ParserContext.Options(2048 | 1);
211
+ }
212
+
213
+ InputStream in = new IOInputStream(args[0]);
214
+ reader.setInput(context, in, url, options);
215
+ return reader;
216
+ }
217
+
218
+ @JRubyMethod(meta = true, rest = true)
219
+ public static IRubyObject
220
+ from_memory(ThreadContext context, IRubyObject cls, IRubyObject args[])
221
+ {
222
+ // args[0]: string, args[1]: url, args[2]: encoding, args[3]: options
223
+ Ruby runtime = context.getRuntime();
224
+ // Not nil allowed!
225
+ if (args[0].isNil()) { throw runtime.newArgumentError("string cannot be nil"); }
226
+
227
+ XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime,
228
+ "Nokogiri::XML::Reader"));
229
+ reader.init(runtime);
230
+ reader.setInstanceVariable("@source", args[0]);
231
+ reader.setInstanceVariable("@errors", runtime.newArray());
232
+ IRubyObject url = context.nil;
233
+ if (args.length > 1) { url = args[1]; }
234
+ if (args.length > 2) { reader.setInstanceVariable("@encoding", args[2]); }
235
+
236
+ Options options;
237
+ if (args.length > 3) {
238
+ options = new ParserContext.Options((Long)args[3].toJava(Long.class));
239
+ } else {
240
+ // use the default options RECOVER | NONET
241
+ options = new ParserContext.Options(2048 | 1);
242
+ }
243
+ IRubyObject stringIO = runtime.getClass("StringIO").newInstance(context, args[0], Block.NULL_BLOCK);
244
+ InputStream in = new IOInputStream(stringIO);
245
+ reader.setInput(context, in, url, options);
246
+ return reader;
247
+ }
248
+
249
+ @JRubyMethod
250
+ public IRubyObject
251
+ node_type(ThreadContext context)
252
+ {
253
+ IRubyObject node_type = currentNode().getNodeType();
254
+ return node_type == null ? RubyFixnum.zero(context.getRuntime()) : node_type;
255
+ }
256
+
257
+ @JRubyMethod
258
+ public IRubyObject
259
+ inner_xml(ThreadContext context)
260
+ {
261
+ ensureNodeClosed(context);
262
+ return stringOrBlank(context.getRuntime(), getInnerXml(currentNode()));
263
+ }
264
+
265
+ private String
266
+ getInnerXml(ReaderNode current)
267
+ {
268
+ if (current.depth < 0) { return null; }
269
+ if (!current.hasChildren) { return null; }
270
+ StringBuffer sb = new StringBuffer();
271
+ for (int i = current.startOffset + 1; i <= current.endOffset - 1; i++) {
272
+ sb.append(nodeQueue.get(i).getString());
273
+ }
274
+ return new String(sb);
275
+ }
276
+
277
+ @JRubyMethod
278
+ public IRubyObject
279
+ outer_xml(ThreadContext context)
280
+ {
281
+ ensureNodeClosed(context);
282
+ return stringOrBlank(context.getRuntime(), getOuterXml());
283
+ }
284
+
285
+ private String
286
+ getOuterXml()
287
+ {
288
+ ReaderNode current = currentNode();
289
+ if (current == null || current.depth < 0) { return null; }
290
+
291
+ if (current instanceof ClosingNode) {
292
+ return "<" + current.name + "/>";
293
+ }
294
+
295
+ StringBuilder sb = new StringBuilder();
296
+ for (int i = position; i <= current.endOffset; i++) {
297
+ sb.append(nodeQueue.get(i).getString());
298
+ }
299
+ return new String(sb);
300
+ }
301
+
302
+ @JRubyMethod
303
+ public IRubyObject
304
+ lang(ThreadContext context)
305
+ {
306
+ return currentNode().getLang();
307
+ }
308
+
309
+ @JRubyMethod
310
+ public IRubyObject
311
+ local_name(ThreadContext context)
312
+ {
313
+ return currentNode().getLocalName();
314
+ }
315
+
316
+ @JRubyMethod
317
+ public IRubyObject
318
+ name(ThreadContext context)
319
+ {
320
+ return currentNode().getName();
321
+ }
322
+
323
+ @JRubyMethod
324
+ public IRubyObject
325
+ namespace_uri(ThreadContext context)
326
+ {
327
+ return currentNode().getUri();
328
+ }
329
+
330
+ @JRubyMethod
331
+ public IRubyObject
332
+ namespaces(ThreadContext context)
333
+ {
334
+ return currentNode().getNamespaces(context);
335
+ }
336
+
337
+ @JRubyMethod
338
+ public IRubyObject
339
+ prefix(ThreadContext context)
340
+ {
341
+ return currentNode().getPrefix();
342
+ }
343
+
344
+ private void
345
+ readMoreData(ThreadContext context)
346
+ {
347
+ if (!continueParsing) { throw context.runtime.newRuntimeError("Cannot parse more data"); }
348
+ try {
349
+ continueParsing = config.parse(false);
350
+ } catch (XNIException e) {
351
+ throw XmlSyntaxError.createXMLSyntaxError(context.runtime, e).toThrowable(); // Nokogiri::XML::SyntaxError
352
+ } catch (IOException e) {
353
+ throw context.runtime.newRuntimeError(e.toString());
354
+ }
355
+ }
356
+
357
+ private void
358
+ ensureNodeClosed(ThreadContext context)
359
+ {
360
+ ReaderNode node = currentNode();
361
+ if (node instanceof TextNode) { return; }
362
+ while (node.endOffset < 1) { readMoreData(context); }
363
+ }
364
+
365
+ @JRubyMethod
366
+ public IRubyObject
367
+ read(ThreadContext context)
368
+ {
369
+ position++;
370
+ try {
371
+ while (nodeQueue.size() <= position && continueParsing) {
372
+ readMoreData(context);
373
+ }
374
+ return setAndRaiseErrorsIfAny(context.runtime, null);
375
+ } catch (RaiseException ex) {
376
+ return setAndRaiseErrorsIfAny(context.runtime, ex);
377
+ }
378
+ }
379
+
380
+ private IRubyObject
381
+ setAndRaiseErrorsIfAny(final Ruby runtime, final RaiseException ex) throws RaiseException
382
+ {
383
+ final ReaderNode currentNode = currentNode();
384
+ if (currentNode == null) { return runtime.getNil(); }
385
+ if (currentNode.isError()) {
386
+ RubyArray errors = (RubyArray) getInstanceVariable("@errors");
387
+ IRubyObject error = currentNode.toSyntaxError();
388
+ errors.append(error);
389
+ setInstanceVariable("@errors", errors);
390
+
391
+ throw ex != null ? ex : ((XmlSyntaxError) error).toThrowable();
392
+ }
393
+ if (ex != null) { throw ex; }
394
+ return this;
395
+ }
396
+
397
+ private ReaderNode
398
+ currentNode()
399
+ {
400
+ if (position >= nodeQueue.size()) { return null; }
401
+ return nodeQueue.get(position);
402
+ }
403
+
404
+ @JRubyMethod
405
+ public IRubyObject
406
+ state(ThreadContext context)
407
+ {
408
+ return context.getRuntime().newFixnum(this.state);
409
+ }
410
+
411
+ @JRubyMethod
412
+ public IRubyObject
413
+ value(ThreadContext context)
414
+ {
415
+ return currentNode().getValue();
416
+ }
417
+
418
+ @JRubyMethod(name = "value?")
419
+ public IRubyObject
420
+ value_p(ThreadContext context)
421
+ {
422
+ return currentNode().hasValue();
423
+ }
424
+
425
+ @JRubyMethod
426
+ public IRubyObject
427
+ xml_version(ThreadContext context)
428
+ {
429
+ return currentNode().getXmlVersion();
430
+ }
431
+
432
+ protected XMLPullParserConfiguration
433
+ createReader(Ruby ruby, Options options)
434
+ {
435
+ StandardParserConfiguration config = new StandardParserConfiguration();
436
+ DocumentHandler handler = new DocumentHandler(ruby);
437
+ // XMLReader reader = XMLReaderFactory.createXMLReader();
438
+ config.setDocumentHandler(handler);
439
+ config.setDTDHandler(handler);
440
+ config.setErrorHandler(handler);
441
+ config.setEntityResolver(new EntityResolver2Wrapper(new NokogiriEntityResolver(ruby, null, options)));
442
+ // config.setFeature("http://xml.org/sax/features/xmlns-uris", true);
443
+ // config.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
444
+ config.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", options.dtdLoad
445
+ || options.dtdValid);
446
+ return config;
447
+ }
448
+
449
+ private class DocumentHandler extends DefaultXMLDocumentHandler implements XMLErrorHandler
450
+ {
451
+
452
+ Stack<String> langStack;
453
+ int depth;
454
+ Stack<String> xmlBaseStack;
455
+ Stack<ReaderNode.ElementNode> elementStack;
456
+ private final Ruby ruby;
457
+
458
+ public
459
+ DocumentHandler(Ruby ruby)
460
+ {
461
+ this.ruby = ruby;
107
462
  }
108
463
 
109
- /**
110
- * Create and return a copy of this object.
111
- *
112
- * @return a clone of this object
113
- */
114
464
  @Override
115
- public Object clone() throws CloneNotSupportedException {
116
- return super.clone();
465
+ public void
466
+ startGeneralEntity(String name, XMLResourceIdentifier identifier,
467
+ String encoding, Augmentations augs) throws XNIException
468
+ {
469
+ Object entitySkipped;
470
+ if (augs != null && (entitySkipped = augs.getItem(Constants.ENTITY_SKIPPED)) != null && ((Boolean) entitySkipped)) {
471
+ nodeQueue.add(new ReaderNode.ExceptionNode(ruby, null));
472
+ }
117
473
  }
118
474
 
119
- public void init(Ruby runtime) {
120
- nodeQueue = new LinkedList<ReaderNode>();
121
- nodeQueue.add(new ReaderNode.EmptyNode(runtime));
122
- }
123
475
 
124
- private void setInput(ThreadContext context, InputStream in, IRubyObject url, Options options){
125
- this.setState(XML_TEXTREADER_MODE_READING);
126
- config = this.createReader(context.getRuntime(), options);
127
- InputSource inputSource = new InputSource();
128
- ParserContext.setUrl(context, inputSource, url);
129
- XMLInputSource xmlInputSource = new XMLInputSource(inputSource.getPublicId(),
130
- inputSource.getSystemId(), null, in, null);
131
- try {
132
- config.setInputSource(xmlInputSource);
133
- } catch (IOException e) {
134
- throw context.getRuntime().newRuntimeError(e.getMessage());
135
- }
136
- this.setState(XML_TEXTREADER_MODE_CLOSED);
137
- }
138
-
139
- private void setState(int state) { this.state = state; }
140
-
141
- @JRubyMethod
142
- public IRubyObject attribute(ThreadContext context, IRubyObject name) {
143
- return currentNode().getAttributeByName(name);
144
- }
145
-
146
- @JRubyMethod
147
- public IRubyObject attribute_at(ThreadContext context, IRubyObject index) {
148
- return currentNode().getAttributeByIndex(index);
149
- }
150
-
151
- @JRubyMethod
152
- public IRubyObject attribute_count(ThreadContext context) {
153
- return currentNode().getAttributeCount();
154
- }
155
-
156
- @JRubyMethod
157
- public IRubyObject attribute_nodes(ThreadContext context) {
158
- return currentNode().getAttributesNodes();
159
- }
160
476
 
161
- @JRubyMethod
162
- public IRubyObject attr_nodes(ThreadContext context) {
163
- return currentNode().getAttributesNodes();
164
- }
165
-
166
- @JRubyMethod(name = "attributes?")
167
- public IRubyObject attributes_p(ThreadContext context) {
168
- return currentNode().hasAttributes();
169
- }
170
-
171
- @JRubyMethod
172
- public IRubyObject base_uri(ThreadContext context) {
173
- return currentNode().getXmlBase();
174
- }
175
-
176
- @JRubyMethod(name="default?")
177
- public IRubyObject default_p(ThreadContext context){
178
- return currentNode().isDefault();
179
- }
180
-
181
- @JRubyMethod
182
- public IRubyObject depth(ThreadContext context) {
183
- return currentNode().getDepth();
184
- }
185
-
186
- @JRubyMethod(name = {"empty_element?", "self_closing?"})
187
- public IRubyObject empty_element_p(ThreadContext context) {
188
- ReaderNode readerNode = currentNode();
189
- ensureNodeClosed(context);
190
-
191
- if (readerNode == null) return context.getRuntime().getNil();
192
- if (!(readerNode instanceof ElementNode)) context.getRuntime().getFalse();
193
- return RubyBoolean.newBoolean(context.getRuntime(), !readerNode.hasChildren);
194
- }
195
-
196
- @JRubyMethod(meta = true, rest = true)
197
- public static IRubyObject from_io(ThreadContext context, IRubyObject cls, IRubyObject args[]) {
198
- // Only to pass the source test.
199
- Ruby runtime = context.getRuntime();
200
- // Not nil allowed!
201
- if(args[0].isNil()) throw runtime.newArgumentError("io cannot be nil");
202
-
203
- XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
204
- reader.init(runtime);
205
- reader.setInstanceVariable("@source", args[0]);
206
- reader.setInstanceVariable("@errors", runtime.newArray());
207
- IRubyObject url = context.nil;
208
- if (args.length > 1) url = args[1];
209
- if (args.length > 2) reader.setInstanceVariable("@encoding", args[2]);
210
-
211
- Options options;
212
- if (args.length > 3) {
213
- options = new ParserContext.Options((Long)args[3].toJava(Long.class));
214
- } else {
215
- // use the default options RECOVER | NONET
216
- options = new ParserContext.Options(2048 | 1);
217
- }
218
-
219
- InputStream in = new IOInputStream(args[0]);
220
- reader.setInput(context, in, url, options);
221
- return reader;
222
- }
223
-
224
- @JRubyMethod(meta = true, rest = true)
225
- public static IRubyObject from_memory(ThreadContext context, IRubyObject cls, IRubyObject args[]) {
226
- // args[0]: string, args[1]: url, args[2]: encoding, args[3]: options
227
- Ruby runtime = context.getRuntime();
228
- // Not nil allowed!
229
- if(args[0].isNil()) throw runtime.newArgumentError("string cannot be nil");
230
-
231
- XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
232
- reader.init(runtime);
233
- reader.setInstanceVariable("@source", args[0]);
234
- reader.setInstanceVariable("@errors", runtime.newArray());
235
- IRubyObject url = context.nil;
236
- if (args.length > 1) url = args[1];
237
- if (args.length > 2) reader.setInstanceVariable("@encoding", args[2]);
238
-
239
- Options options;
240
- if (args.length > 3) {
241
- options = new ParserContext.Options((Long)args[3].toJava(Long.class));
242
- } else {
243
- // use the default options RECOVER | NONET
244
- options = new ParserContext.Options(2048 | 1);
245
- }
246
- IRubyObject stringIO = runtime.getClass("StringIO").newInstance(context, args[0], Block.NULL_BLOCK);
247
- InputStream in = new IOInputStream(stringIO);
248
- reader.setInput(context, in, url, options);
249
- return reader;
250
- }
251
-
252
- @JRubyMethod
253
- public IRubyObject node_type(ThreadContext context) {
254
- IRubyObject node_type = currentNode().getNodeType();
255
- return node_type == null ? RubyFixnum.zero(context.getRuntime()) : node_type;
256
- }
257
-
258
- @JRubyMethod
259
- public IRubyObject inner_xml(ThreadContext context) {
260
- ensureNodeClosed(context);
261
- return stringOrBlank(context.getRuntime(), getInnerXml(currentNode()));
262
- }
263
-
264
- private String getInnerXml(ReaderNode current) {
265
- if (current.depth < 0) return null;
266
- if (!current.hasChildren) return null;
267
- StringBuffer sb = new StringBuffer();
268
- for (int i = current.startOffset + 1; i <= current.endOffset - 1; i++) {
269
- sb.append(nodeQueue.get(i).getString());
270
- }
271
- return new String(sb);
272
- }
273
-
274
- @JRubyMethod
275
- public IRubyObject outer_xml(ThreadContext context) {
276
- ensureNodeClosed(context);
277
- return stringOrBlank(context.getRuntime(), getOuterXml());
278
- }
279
-
280
- private String getOuterXml() {
281
- ReaderNode current = currentNode();
282
- if (current == null || current.depth < 0) return null;
283
-
284
- if (current instanceof ClosingNode) {
285
- return "<" + current.name + "/>";
286
- }
287
-
288
- StringBuilder sb = new StringBuilder();
289
- for (int i = position; i <= current.endOffset; i++) {
290
- sb.append(nodeQueue.get(i).getString());
291
- }
292
- return new String(sb);
293
- }
294
-
295
- @JRubyMethod
296
- public IRubyObject lang(ThreadContext context) {
297
- return currentNode().getLang();
298
- }
299
-
300
- @JRubyMethod
301
- public IRubyObject local_name(ThreadContext context) {
302
- return currentNode().getLocalName();
303
- }
304
-
305
- @JRubyMethod
306
- public IRubyObject name(ThreadContext context) {
307
- return currentNode().getName();
308
- }
309
-
310
- @JRubyMethod
311
- public IRubyObject namespace_uri(ThreadContext context) {
312
- return currentNode().getUri();
313
- }
314
-
315
- @JRubyMethod
316
- public IRubyObject namespaces(ThreadContext context) {
317
- return currentNode().getNamespaces(context);
318
- }
319
-
320
- @JRubyMethod
321
- public IRubyObject prefix(ThreadContext context) {
322
- return currentNode().getPrefix();
323
- }
324
-
325
- private void readMoreData(ThreadContext context) {
326
- if (!continueParsing) throw context.runtime.newRuntimeError("Cannot parse more data");
327
- try {
328
- continueParsing = config.parse(false);
329
- }
330
- catch (XNIException e) {
331
- throw new RaiseException(XmlSyntaxError.createXMLSyntaxError(context.runtime, e)); // Nokogiri::XML::SyntaxError
332
- }
333
- catch (IOException e) {
334
- throw context.runtime.newRuntimeError(e.toString());
335
- }
336
- }
337
-
338
- private void ensureNodeClosed(ThreadContext context) {
339
- ReaderNode node = currentNode();
340
- if (node instanceof TextNode) return;
341
- while (node.endOffset < 1) readMoreData(context);
342
- }
343
-
344
- @JRubyMethod
345
- public IRubyObject read(ThreadContext context) {
346
- position++;
347
- try {
348
- while (nodeQueue.size() <= position && continueParsing) {
349
- readMoreData(context);
350
- }
351
- return setAndRaiseErrorsIfAny(context.runtime, null);
352
- }
353
- catch (RaiseException ex) {
354
- return setAndRaiseErrorsIfAny(context.runtime, ex);
355
- }
477
+ @Override
478
+ public void
479
+ startDocument(XMLLocator locator, String encoding, NamespaceContext context, Augmentations augs)
480
+ {
481
+ depth = 0;
482
+ langStack = new Stack<String>();
483
+ xmlBaseStack = new Stack<String>();
484
+ elementStack = new Stack<ReaderNode.ElementNode>();
356
485
  }
357
486
 
358
- private IRubyObject setAndRaiseErrorsIfAny(final Ruby runtime, final RaiseException ex) throws RaiseException {
359
- final ReaderNode currentNode = currentNode();
360
- if (currentNode == null) return runtime.getNil();
361
- if (currentNode.isError()) {
362
- RubyArray errors = (RubyArray) getInstanceVariable("@errors");
363
- IRubyObject error = currentNode.toSyntaxError();
364
- errors.append(error);
365
- setInstanceVariable("@errors", errors);
366
-
367
- throw ex != null ? ex : new RaiseException((XmlSyntaxError) error);
368
- }
369
- if ( ex != null ) throw ex;
370
- return this;
487
+ @Override
488
+ public void
489
+ endDocument(Augmentations augs)
490
+ {
491
+ langStack = null;
492
+ xmlBaseStack = null;
493
+ elementStack = null;
371
494
  }
372
495
 
373
- private ReaderNode currentNode() {
374
- if (position >= nodeQueue.size()) return null;
375
- return nodeQueue.get(position);
496
+ @Override
497
+ public void
498
+ startElement(QName element, XMLAttributes attrs, Augmentations augs)
499
+ {
500
+ commonElement(element, attrs, false);
376
501
  }
377
502
 
378
- @JRubyMethod
379
- public IRubyObject state(ThreadContext context) {
380
- return context.getRuntime().newFixnum(this.state);
503
+ @Override
504
+ public void
505
+ endElement(QName element, Augmentations augs)
506
+ {
507
+ String uri = element.uri;
508
+ String localName = element.localpart;
509
+ String qName = element.rawname;
510
+ depth--;
511
+ ElementNode startElementNode = elementStack.pop();
512
+ ReaderNode node = ReaderNode.createClosingNode(ruby, uri, localName, qName, depth, langStack, xmlBaseStack);
513
+
514
+ startElementNode.endOffset = nodeQueue.size() - 1;
515
+
516
+ if (startElementNode.endOffset != startElementNode.startOffset) {
517
+ // this node isn't empty
518
+ node.attributeList = startElementNode.attributeList;
519
+ node.namespaces = startElementNode.namespaces;
520
+ node.startOffset = startElementNode.startOffset;
521
+ node.endOffset = ++startElementNode.endOffset;
522
+ node.hasChildren = startElementNode.hasChildren = true;
523
+ nodeQueue.add(node);
524
+ }
525
+ if (!langStack.isEmpty()) { langStack.pop(); }
526
+ if (!xmlBaseStack.isEmpty()) { xmlBaseStack.pop(); }
381
527
  }
382
528
 
383
- @JRubyMethod
384
- public IRubyObject value(ThreadContext context) {
385
- return currentNode().getValue();
529
+ @Override
530
+ public void
531
+ emptyElement(QName element, XMLAttributes attrs, Augmentations augs)
532
+ {
533
+ commonElement(element, attrs, true);
534
+ }
535
+
536
+ private void
537
+ commonElement(QName element, XMLAttributes attrs, boolean isEmpty)
538
+ {
539
+ String qName = element.rawname;
540
+ String uri = element.uri;
541
+ String localName = element.localpart;
542
+ ReaderNode readerNode = ReaderNode.createElementNode(ruby, uri, localName, qName, attrs, depth, langStack,
543
+ xmlBaseStack);
544
+ if (!elementStack.isEmpty()) {
545
+ ElementNode parent = elementStack.peek();
546
+ parent.hasChildren = true;
547
+ }
548
+ nodeQueue.add(readerNode);
549
+ readerNode.startOffset = nodeQueue.size() - 1;
550
+ if (!isEmpty) {
551
+ depth++;
552
+ if (readerNode.lang != null) { langStack.push(readerNode.lang); }
553
+ if (readerNode.xmlBase != null) { xmlBaseStack.push(readerNode.xmlBase); }
554
+ elementStack.push((ReaderNode.ElementNode)readerNode);
555
+ } else {
556
+ readerNode.endOffset = readerNode.startOffset;
557
+ readerNode.hasChildren = false;
558
+ }
386
559
  }
387
560
 
388
- @JRubyMethod(name = "value?")
389
- public IRubyObject value_p(ThreadContext context) {
390
- return currentNode().hasValue();
561
+ @Override
562
+ public void
563
+ characters(XMLString string, Augmentations augs)
564
+ {
565
+ ReaderNode.TextNode node = ReaderNode.createTextNode(ruby, string.toString(), depth, langStack, xmlBaseStack);
566
+ nodeQueue.add(node);
567
+ node.startOffset = node.endOffset = nodeQueue.size() - 1;
391
568
  }
392
569
 
393
- @JRubyMethod
394
- public IRubyObject xml_version(ThreadContext context) {
395
- return currentNode().getXmlVersion();
570
+ @Override
571
+ public void
572
+ error(String domain, String key, XMLParseException ex)
573
+ {
574
+ nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
575
+ throw ex;
396
576
  }
397
577
 
398
- protected XMLPullParserConfiguration createReader(Ruby ruby, Options options) {
399
- StandardParserConfiguration config = new StandardParserConfiguration();
400
- DocumentHandler handler = new DocumentHandler(ruby);
401
- // XMLReader reader = XMLReaderFactory.createXMLReader();
402
- config.setDocumentHandler(handler);
403
- config.setDTDHandler(handler);
404
- config.setErrorHandler(handler);
405
- config.setEntityResolver(new EntityResolver2Wrapper(new NokogiriEntityResolver(ruby, null, options)));
406
- // config.setFeature("http://xml.org/sax/features/xmlns-uris", true);
407
- // config.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
408
- config.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", options.dtdLoad || options.dtdValid);
409
- return config;
578
+ @Override
579
+ public void
580
+ fatalError(String domain, String key, XMLParseException ex)
581
+ {
582
+ nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
583
+ throw ex;
410
584
  }
411
585
 
412
- private class DocumentHandler extends DefaultXMLDocumentHandler implements XMLErrorHandler {
413
-
414
- Stack<String> langStack;
415
- int depth;
416
- Stack<String> xmlBaseStack;
417
- Stack<ReaderNode.ElementNode> elementStack;
418
- private final Ruby ruby;
419
-
420
- public DocumentHandler(Ruby ruby) {
421
- this.ruby = ruby;
422
- }
423
-
424
- @Override
425
- public void startGeneralEntity(String name, XMLResourceIdentifier identifier,
426
- String encoding, Augmentations augs) throws XNIException {
427
- Object entitySkipped;
428
- if (augs != null && (entitySkipped = augs.getItem(Constants.ENTITY_SKIPPED)) != null && ((Boolean) entitySkipped)) {
429
- nodeQueue.add(new ReaderNode.ExceptionNode(ruby, null));
430
- }
431
- }
432
-
433
-
434
-
435
- @Override
436
- public void startDocument(XMLLocator locator, String encoding, NamespaceContext context, Augmentations augs) {
437
- depth = 0;
438
- langStack = new Stack<String>();
439
- xmlBaseStack = new Stack<String>();
440
- elementStack = new Stack<ReaderNode.ElementNode>();
441
- }
442
-
443
- @Override
444
- public void endDocument(Augmentations augs) {
445
- langStack = null;
446
- xmlBaseStack = null;
447
- elementStack = null;
448
- }
449
-
450
- @Override
451
- public void startElement(QName element, XMLAttributes attrs, Augmentations augs) {
452
- commonElement(element, attrs, false);
453
- }
454
-
455
- @Override
456
- public void endElement(QName element, Augmentations augs) {
457
- String uri = element.uri;
458
- String localName = element.localpart;
459
- String qName = element.rawname;
460
- depth--;
461
- ElementNode startElementNode = elementStack.pop();
462
- ReaderNode node = ReaderNode.createClosingNode(ruby, uri, localName, qName, depth, langStack, xmlBaseStack);
463
-
464
- startElementNode.endOffset = nodeQueue.size() - 1;
465
-
466
- if (startElementNode.endOffset != startElementNode.startOffset) {
467
- // this node isn't empty
468
- node.attributeList = startElementNode.attributeList;
469
- node.namespaces = startElementNode.namespaces;
470
- node.startOffset = startElementNode.startOffset;
471
- node.endOffset = ++startElementNode.endOffset;
472
- node.hasChildren = startElementNode.hasChildren = true;
473
- nodeQueue.add(node);
474
- }
475
- if (!langStack.isEmpty()) langStack.pop();
476
- if (!xmlBaseStack.isEmpty()) xmlBaseStack.pop();
477
- }
478
-
479
- @Override
480
- public void emptyElement(QName element, XMLAttributes attrs, Augmentations augs) {
481
- commonElement(element, attrs, true);
482
- }
483
-
484
- private void commonElement(QName element, XMLAttributes attrs, boolean isEmpty) {
485
- String qName = element.rawname;
486
- String uri = element.uri;
487
- String localName = element.localpart;
488
- ReaderNode readerNode = ReaderNode.createElementNode(ruby, uri, localName, qName, attrs, depth, langStack, xmlBaseStack);
489
- if (!elementStack.isEmpty()) {
490
- ElementNode parent = elementStack.peek();
491
- parent.hasChildren = true;
492
- }
493
- nodeQueue.add(readerNode);
494
- readerNode.startOffset = nodeQueue.size() - 1;
495
- if (!isEmpty) {
496
- depth++;
497
- if (readerNode.lang != null) langStack.push(readerNode.lang);
498
- if (readerNode.xmlBase != null) xmlBaseStack.push(readerNode.xmlBase);
499
- elementStack.push((ReaderNode.ElementNode)readerNode);
500
- } else {
501
- readerNode.endOffset = readerNode.startOffset;
502
- readerNode.hasChildren = false;
503
- }
504
- }
505
-
506
- @Override
507
- public void characters(XMLString string, Augmentations augs) {
508
- ReaderNode.TextNode node = ReaderNode.createTextNode(ruby, string.toString(), depth, langStack, xmlBaseStack);
509
- nodeQueue.add(node);
510
- node.startOffset = node.endOffset = nodeQueue.size() - 1;
511
- }
512
-
513
- @Override
514
- public void error(String domain, String key, XMLParseException ex) {
515
- nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
516
- throw ex;
517
- }
518
-
519
- @Override
520
- public void fatalError(String domain, String key, XMLParseException ex) {
521
- nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
522
- throw ex;
523
- }
524
-
525
- @Override
526
- public void warning(String domain, String key, XMLParseException ex) {
527
- nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
528
- throw ex;
529
- }
586
+ @Override
587
+ public void
588
+ warning(String domain, String key, XMLParseException ex)
589
+ {
590
+ nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
591
+ throw ex;
530
592
  }
593
+ }
531
594
  }