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.getNokogiriClass;
@@ -77,271 +45,314 @@ import nokogiri.internals.NokogiriXsltErrorListener;
77
45
  * @author sergio
78
46
  * @author Yoko Harada <yokolet@gmail.com>
79
47
  */
80
- @JRubyClass(name="Nokogiri::XSLT::Stylesheet")
81
- public class XsltStylesheet extends RubyObject {
48
+ @JRubyClass(name = "Nokogiri::XSLT::Stylesheet")
49
+ public class XsltStylesheet extends RubyObject
50
+ {
82
51
 
83
- private TransformerFactory factory = null;
84
- private Templates sheet = null;
85
- private IRubyObject stylesheet = null;
86
- private boolean htmlish = false;
52
+ private TransformerFactory factory = null;
53
+ private Templates sheet = null;
54
+ private IRubyObject stylesheet = null;
55
+ private boolean htmlish = false;
87
56
 
88
- public XsltStylesheet(Ruby ruby, RubyClass rubyClass) {
89
- super(ruby, rubyClass);
90
- }
91
-
92
- /**
93
- * Create and return a copy of this object.
94
- *
95
- * @return a clone of this object
96
- */
97
- @Override
98
- public Object clone() throws CloneNotSupportedException {
99
- return super.clone();
100
- }
57
+ public
58
+ XsltStylesheet(Ruby ruby, RubyClass rubyClass)
59
+ {
60
+ super(ruby, rubyClass);
61
+ }
101
62
 
102
- private void addParametersToTransformer(ThreadContext context, Transformer transf, IRubyObject parameters) {
103
- Ruby runtime = context.getRuntime();
63
+ /**
64
+ * Create and return a copy of this object.
65
+ *
66
+ * @return a clone of this object
67
+ */
68
+ @Override
69
+ public Object
70
+ clone() throws CloneNotSupportedException
71
+ {
72
+ return super.clone();
73
+ }
104
74
 
105
- if (parameters instanceof RubyHash) {
106
- setHashParameters(transf, (RubyHash)parameters);
107
- } else if (parameters instanceof RubyArray) {
108
- setArrayParameters(transf, runtime, (RubyArray)parameters);
109
- } else {
110
- throw runtime.newTypeError("parameters should be given either Array or Hash");
111
- }
75
+ private void
76
+ addParametersToTransformer(ThreadContext context, Transformer transf, IRubyObject parameters)
77
+ {
78
+ Ruby runtime = context.getRuntime();
79
+
80
+ if (parameters instanceof RubyHash) {
81
+ setHashParameters(transf, (RubyHash)parameters);
82
+ } else if (parameters instanceof RubyArray) {
83
+ setArrayParameters(transf, runtime, (RubyArray)parameters);
84
+ } else {
85
+ throw runtime.newTypeError("parameters should be given either Array or Hash");
112
86
  }
113
-
114
- private void setHashParameters(Transformer transformer, RubyHash hash) {
115
- Set<String> keys = hash.keySet();
116
- for (String key : keys) {
117
- String value = (String)hash.get(key);
118
- transformer.setParameter(key, unparseValue(value));
119
- }
87
+ }
88
+
89
+ private void
90
+ setHashParameters(Transformer transformer, RubyHash hash)
91
+ {
92
+ Set<String> keys = hash.keySet();
93
+ for (String key : keys) {
94
+ String value = (String)hash.get(key);
95
+ transformer.setParameter(key, unparseValue(value));
120
96
  }
121
-
122
- private void setArrayParameters(Transformer transformer, Ruby runtime, RubyArray params) {
123
- int limit = params.getLength();
124
- if(limit % 2 == 1) limit--;
125
-
126
- for(int i = 0; i < limit; i+=2) {
127
- String name = params.aref(runtime.newFixnum(i)).asJavaString();
128
- String value = params.aref(runtime.newFixnum(i+1)).asJavaString();
129
- transformer.setParameter(name, unparseValue(value));
130
- }
97
+ }
98
+
99
+ private void
100
+ setArrayParameters(Transformer transformer, Ruby runtime, RubyArray params)
101
+ {
102
+ int limit = params.getLength();
103
+ if (limit % 2 == 1) { limit--; }
104
+
105
+ for (int i = 0; i < limit; i += 2) {
106
+ String name = params.aref(runtime.newFixnum(i)).asJavaString();
107
+ String value = params.aref(runtime.newFixnum(i + 1)).asJavaString();
108
+ transformer.setParameter(name, unparseValue(value));
131
109
  }
132
-
133
- private static final Pattern QUOTED = Pattern.compile("'.{1,}'");
110
+ }
134
111
 
135
- private String unparseValue(String orig) {
136
- if ((orig.startsWith("\"") && orig.endsWith("\"")) || QUOTED.matcher(orig).matches()) {
137
- orig = orig.substring(1, orig.length() - 1);
138
- }
112
+ private static final Pattern QUOTED = Pattern.compile("'.{1,}'");
139
113
 
140
- return orig;
114
+ private String
115
+ unparseValue(String orig)
116
+ {
117
+ if ((orig.startsWith("\"") && orig.endsWith("\"")) || QUOTED.matcher(orig).matches()) {
118
+ orig = orig.substring(1, orig.length() - 1);
141
119
  }
142
120
 
143
- @JRubyMethod(meta = true, rest = true)
144
- public static IRubyObject parse_stylesheet_doc(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
145
-
146
- Ruby runtime = context.getRuntime();
121
+ return orig;
122
+ }
123
+
124
+ @JRubyMethod(meta = true, rest = true)
125
+ public static IRubyObject
126
+ parse_stylesheet_doc(ThreadContext context, IRubyObject klazz, IRubyObject[] args)
127
+ {
147
128
 
148
- ensureFirstArgIsDocument(runtime, args[0]);
129
+ Ruby runtime = context.getRuntime();
149
130
 
150
- XmlDocument xmlDoc = (XmlDocument) args[0];
151
- ensureDocumentHasNoError(context, xmlDoc);
152
-
153
- Document doc = ((XmlDocument) xmlDoc.dup_implementation(context, true)).getDocument();
131
+ ensureFirstArgIsDocument(runtime, args[0]);
154
132
 
155
- XsltStylesheet xslt =
156
- (XsltStylesheet) NokogiriService.XSLT_STYLESHEET_ALLOCATOR.allocate(runtime, (RubyClass)klazz);
133
+ XmlDocument xmlDoc = (XmlDocument) args[0];
134
+ ensureDocumentHasNoError(context, xmlDoc);
157
135
 
158
- try {
159
- xslt.init(args[1], doc);
160
- } catch (TransformerConfigurationException ex) {
161
- throw runtime.newRuntimeError("could not parse xslt stylesheet");
162
- }
136
+ Document doc = ((XmlDocument) xmlDoc.dup_implementation(context, true)).getDocument();
163
137
 
164
- return xslt;
138
+ XsltStylesheet xslt =
139
+ (XsltStylesheet) NokogiriService.XSLT_STYLESHEET_ALLOCATOR.allocate(runtime, (RubyClass)klazz);
140
+
141
+ try {
142
+ xslt.init(args[1], doc);
143
+ } catch (TransformerConfigurationException ex) {
144
+ throw runtime.newRuntimeError("could not parse xslt stylesheet");
165
145
  }
166
-
167
- private void init(IRubyObject stylesheet, Document document) throws TransformerConfigurationException {
168
- this.stylesheet = stylesheet; // either RubyString or RubyFile
169
- if (factory == null) factory = TransformerFactory.newInstance();
170
- NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
171
- factory.setErrorListener(elistener);
172
- sheet = factory.newTemplates(new DOMSource(document));
146
+
147
+ return xslt;
148
+ }
149
+
150
+ private void
151
+ init(IRubyObject stylesheet, Document document) throws TransformerConfigurationException
152
+ {
153
+ this.stylesheet = stylesheet; // either RubyString or RubyFile
154
+ if (factory == null) { factory = TransformerFactory.newInstance(); }
155
+ NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
156
+ factory.setErrorListener(elistener);
157
+ sheet = factory.newTemplates(new DOMSource(document));
158
+ }
159
+
160
+ private static void
161
+ ensureFirstArgIsDocument(Ruby runtime, IRubyObject arg)
162
+ {
163
+ if (arg instanceof XmlDocument) { return; }
164
+ throw runtime.newArgumentError("doc must be a Nokogiri::XML::Document instance");
165
+ }
166
+
167
+ private static void
168
+ ensureDocumentHasNoError(ThreadContext context, XmlDocument xmlDoc)
169
+ {
170
+ Ruby runtime = context.getRuntime();
171
+ RubyArray errors_of_xmlDoc = (RubyArray) xmlDoc.getInstanceVariable("@errors");
172
+ if (!errors_of_xmlDoc.isEmpty()) {
173
+ throw runtime.newRuntimeError(errors_of_xmlDoc.first().asString().asJavaString());
173
174
  }
174
-
175
- private static void ensureFirstArgIsDocument(Ruby runtime, IRubyObject arg) {
176
- if (arg instanceof XmlDocument) return;
177
- throw runtime.newArgumentError("doc must be a Nokogiri::XML::Document instance");
175
+ }
176
+
177
+ @JRubyMethod
178
+ public IRubyObject
179
+ serialize(ThreadContext context, IRubyObject doc) throws IOException, TransformerException
180
+ {
181
+ XmlDocument xmlDoc = (XmlDocument) doc;
182
+ TransformerImpl transformer = (TransformerImpl) this.sheet.newTransformer();
183
+ ByteArrayOutputStream writer = new ByteArrayOutputStream();
184
+ StreamResult streamResult = new StreamResult(writer);
185
+ SerializationHandler serializationHandler = transformer.createSerializationHandler(streamResult);
186
+ serializationHandler.serialize(xmlDoc.getNode());
187
+ return context.getRuntime().newString(writer.toString());
188
+ }
189
+
190
+ @JRubyMethod(rest = true, required = 1, optional = 2)
191
+ public IRubyObject
192
+ transform(ThreadContext context, IRubyObject[] args)
193
+ {
194
+ Ruby runtime = context.getRuntime();
195
+
196
+ argumentTypeCheck(runtime, args[0]);
197
+
198
+ NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
199
+ DOMSource domSource = new DOMSource(((XmlDocument) args[0]).getDocument());
200
+ final DOMResult result;
201
+ String stringResult = null;
202
+ try {
203
+ result = tryXsltTransformation(context, args, domSource, elistener); // DOMResult
204
+ if (result.getNode().getFirstChild() == null) {
205
+ stringResult = retryXsltTransformation(context, args, domSource, elistener); // StreamResult
206
+ }
207
+ } catch (TransformerConfigurationException ex) {
208
+ throw runtime.newRuntimeError(ex.getMessage());
209
+ } catch (TransformerException ex) {
210
+ throw runtime.newRuntimeError(ex.getMessage());
211
+ } catch (IOException ex) {
212
+ throw runtime.newRuntimeError(ex.getMessage());
178
213
  }
179
-
180
- private static void ensureDocumentHasNoError(ThreadContext context, XmlDocument xmlDoc) {
181
- Ruby runtime = context.getRuntime();
182
- RubyArray errors_of_xmlDoc = (RubyArray) xmlDoc.getInstanceVariable("@errors");
183
- if (!errors_of_xmlDoc.isEmpty()) {
184
- throw runtime.newRuntimeError(errors_of_xmlDoc.first().asString().asJavaString());
185
- }
214
+
215
+ switch (elistener.getErrorType()) {
216
+ case ERROR:
217
+ case FATAL:
218
+ throw runtime.newRuntimeError(elistener.getErrorMessage());
219
+ case WARNING:
220
+ default:
221
+ // no-op
186
222
  }
187
223
 
188
- @JRubyMethod
189
- public IRubyObject serialize(ThreadContext context, IRubyObject doc) throws IOException, TransformerException {
190
- XmlDocument xmlDoc = (XmlDocument) doc;
191
- TransformerImpl transformer = (TransformerImpl) this.sheet.newTransformer();
192
- ByteArrayOutputStream writer = new ByteArrayOutputStream();
193
- StreamResult streamResult = new StreamResult(writer);
194
- SerializationHandler serializationHandler = transformer.createSerializationHandler(streamResult);
195
- serializationHandler.serialize(xmlDoc.getNode());
196
- return context.getRuntime().newString(writer.toString());
224
+ if (stringResult == null) {
225
+ return createDocumentFromDomResult(context, runtime, result);
226
+ } else {
227
+ return createDocumentFromString(context, runtime, stringResult);
197
228
  }
229
+ }
198
230
 
199
- @JRubyMethod(rest = true, required=1, optional=2)
200
- public IRubyObject transform(ThreadContext context, IRubyObject[] args) {
201
- Ruby runtime = context.getRuntime();
202
-
203
- argumentTypeCheck(runtime, args[0]);
204
-
205
- NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
206
- DOMSource domSource = new DOMSource(((XmlDocument) args[0]).getDocument());
207
- final DOMResult result; String stringResult = null;
208
- try{
209
- result = tryXsltTransformation(context, args, domSource, elistener); // DOMResult
210
- if (result.getNode().getFirstChild() == null) {
211
- stringResult = retryXsltTransformation(context, args, domSource, elistener); // StreamResult
212
- }
213
- } catch(TransformerConfigurationException ex) {
214
- throw runtime.newRuntimeError(ex.getMessage());
215
- } catch(TransformerException ex) {
216
- throw runtime.newRuntimeError(ex.getMessage());
217
- } catch (IOException ex) {
218
- throw runtime.newRuntimeError(ex.getMessage());
219
- }
220
-
221
- switch (elistener.getErrorType()) {
222
- case ERROR:
223
- case FATAL:
224
- throw runtime.newRuntimeError(elistener.getErrorMessage());
225
- case WARNING:
226
- default:
227
- // no-op
228
- }
229
-
230
- if (stringResult == null) {
231
- return createDocumentFromDomResult(context, runtime, result);
232
- } else {
233
- return createDocumentFromString(context, runtime, stringResult);
234
- }
231
+ private DOMResult
232
+ tryXsltTransformation(ThreadContext context, IRubyObject[] args, DOMSource domSource,
233
+ NokogiriXsltErrorListener elistener) throws TransformerException
234
+ {
235
+ Transformer transf = sheet.newTransformer();
236
+ transf.reset();
237
+ transf.setErrorListener(elistener);
238
+ if (args.length > 1) {
239
+ addParametersToTransformer(context, transf, args[1]);
235
240
  }
236
-
237
- private DOMResult tryXsltTransformation(ThreadContext context, IRubyObject[] args, DOMSource domSource, NokogiriXsltErrorListener elistener) throws TransformerException {
238
- Transformer transf = sheet.newTransformer();
239
- transf.reset();
240
- transf.setErrorListener(elistener);
241
- if (args.length > 1) {
242
- addParametersToTransformer(context, transf, args[1]);
243
- }
244
-
245
- DOMResult result = new DOMResult();
246
- transf.transform(domSource, result);
247
- return result;
241
+
242
+ DOMResult result = new DOMResult();
243
+ transf.transform(domSource, result);
244
+ return result;
245
+ }
246
+
247
+ private String
248
+ retryXsltTransformation(ThreadContext context,
249
+ IRubyObject[] args,
250
+ DOMSource domSource,
251
+ NokogiriXsltErrorListener elistener)
252
+ throws TransformerException, IOException
253
+ {
254
+ Templates templates = getTemplatesFromStreamSource();
255
+ Transformer transf = templates.newTransformer();
256
+ transf.setErrorListener(elistener);
257
+ if (args.length > 1) {
258
+ addParametersToTransformer(context, transf, args[1]);
248
259
  }
249
-
250
- private String retryXsltTransformation(ThreadContext context,
251
- IRubyObject[] args,
252
- DOMSource domSource,
253
- NokogiriXsltErrorListener elistener)
254
- throws TransformerException, IOException {
255
- Templates templates = getTemplatesFromStreamSource();
256
- Transformer transf = templates.newTransformer();
257
- transf.setErrorListener(elistener);
258
- if (args.length > 1) {
259
- addParametersToTransformer(context, transf, args[1]);
260
- }
261
- PipedWriter pwriter = new PipedWriter();
262
- PipedReader preader = new PipedReader();
263
- pwriter.connect(preader);
264
- StreamResult result = new StreamResult(pwriter);
265
- transf.transform(domSource, result);
266
-
267
- char[] cbuf = new char[1024];
268
- int len = preader.read(cbuf, 0, 1024);
269
- StringBuilder builder = new StringBuilder(len);
260
+ PipedWriter pwriter = new PipedWriter();
261
+ PipedReader preader = new PipedReader();
262
+ pwriter.connect(preader);
263
+ StreamResult result = new StreamResult(pwriter);
264
+ transf.transform(domSource, result);
265
+
266
+ char[] cbuf = new char[1024];
267
+ int len = preader.read(cbuf, 0, 1024);
268
+ StringBuilder builder = new StringBuilder(len);
269
+ builder.append(cbuf, 0, len);
270
+ htmlish = isHtml(builder); // judge from the first chunk
271
+
272
+ while (len == 1024) {
273
+ len = preader.read(cbuf, 0, 1024);
274
+ if (len > 0) {
270
275
  builder.append(cbuf, 0, len);
271
- htmlish = isHtml(builder); // judge from the first chunk
272
-
273
- while (len == 1024) {
274
- len = preader.read(cbuf, 0, 1024);
275
- if (len > 0) {
276
- builder.append(cbuf, 0, len);
277
- }
278
- }
279
-
280
- preader.close();
281
- pwriter.close();
282
-
283
- return builder.toString();
284
- }
285
-
286
- private IRubyObject createDocumentFromDomResult(ThreadContext context, Ruby runtime, DOMResult domResult) {
287
- if ("html".equals(domResult.getNode().getFirstChild().getNodeName())) {
288
- return new HtmlDocument(context.runtime, (Document) domResult.getNode());
289
- } else {
290
- return new XmlDocument(context.runtime, (Document) domResult.getNode());
291
- }
292
- }
293
-
294
- private Templates getTemplatesFromStreamSource() throws TransformerConfigurationException {
295
- if (stylesheet instanceof RubyString) {
296
- StringReader reader = new StringReader(stylesheet.asJavaString());
297
- StreamSource xsltStreamSource = new StreamSource(reader);
298
- return factory.newTemplates(xsltStreamSource);
299
- }
300
- return null;
301
- }
302
-
303
- private static final Pattern HTML_TAG = Pattern.compile("<(%s)*html", Pattern.CASE_INSENSITIVE);
304
-
305
- private static boolean isHtml(CharSequence chunk) {
306
- Matcher match = HTML_TAG.matcher(chunk);
307
- return match.find();
276
+ }
308
277
  }
309
-
310
- private IRubyObject createDocumentFromString(ThreadContext context, Ruby runtime, String stringResult) {
311
- IRubyObject[] args = new IRubyObject[4];
312
- args[0] = stringOrBlank(runtime, stringResult);
313
- args[1] = runtime.getNil(); // url
314
- args[2] = runtime.getNil(); // encoding
315
- RubyClass parse_options = (RubyClass)runtime.getClassFromPath("Nokogiri::XML::ParseOptions");
316
- if (htmlish) {
317
- args[3] = parse_options.getConstant("DEFAULT_HTML");
318
- RubyClass htmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::HTML::Document");
319
- return Helpers.invoke(context, htmlDocumentClass, "parse", args);
320
- } else {
321
- args[3] = parse_options.getConstant("DEFAULT_XML");
322
- RubyClass xmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
323
- XmlDocument xmlDocument = (XmlDocument) Helpers.invoke(context, xmlDocumentClass, "parse", args);
324
- if (((Document)xmlDocument.getNode()).getDocumentElement() == null) {
325
- RubyArray errors = (RubyArray) xmlDocument.getInstanceVariable("@errors");
326
- Helpers.invoke(context, errors, "<<", args[0]);
327
- }
328
- return xmlDocument;
329
- }
278
+
279
+ preader.close();
280
+ pwriter.close();
281
+
282
+ return builder.toString();
283
+ }
284
+
285
+ private IRubyObject
286
+ createDocumentFromDomResult(ThreadContext context, Ruby runtime, DOMResult domResult)
287
+ {
288
+ if ("html".equals(domResult.getNode().getFirstChild().getNodeName())) {
289
+ return new HtmlDocument(context.runtime, (Document) domResult.getNode());
290
+ } else {
291
+ return new XmlDocument(context.runtime, (Document) domResult.getNode());
330
292
  }
331
-
332
- private static void argumentTypeCheck(Ruby runtime, IRubyObject arg) {
333
- if (arg instanceof XmlDocument) return;
334
- throw runtime.newArgumentError("argument must be a Nokogiri::XML::Document");
293
+ }
294
+
295
+ private Templates
296
+ getTemplatesFromStreamSource() throws TransformerConfigurationException
297
+ {
298
+ if (stylesheet instanceof RubyString) {
299
+ StringReader reader = new StringReader(stylesheet.asJavaString());
300
+ StreamSource xsltStreamSource = new StreamSource(reader);
301
+ return factory.newTemplates(xsltStreamSource);
335
302
  }
336
-
337
- @JRubyMethod(name = {"registr", "register"}, meta = true)
338
- public static IRubyObject register(ThreadContext context, IRubyObject cls, IRubyObject uri, IRubyObject receiver) {
339
- throw context.getRuntime().newNotImplementedError("Nokogiri::XSLT.register method is not implemented");
340
- /* When API conflict is solved, this method should be below:
341
- // ThreadContext is used while executing xslt extension function
342
- registry.put("context", context);
343
- registry.put("receiver", receiver);
344
- return context.getRuntime().getNil();
345
- */
303
+ return null;
304
+ }
305
+
306
+ private static final Pattern HTML_TAG = Pattern.compile("<(%s)*html", Pattern.CASE_INSENSITIVE);
307
+
308
+ private static boolean
309
+ isHtml(CharSequence chunk)
310
+ {
311
+ Matcher match = HTML_TAG.matcher(chunk);
312
+ return match.find();
313
+ }
314
+
315
+ private IRubyObject
316
+ createDocumentFromString(ThreadContext context, Ruby runtime, String stringResult)
317
+ {
318
+ IRubyObject[] args = new IRubyObject[4];
319
+ args[0] = stringOrBlank(runtime, stringResult);
320
+ args[1] = runtime.getNil(); // url
321
+ args[2] = runtime.getNil(); // encoding
322
+ RubyClass parse_options = (RubyClass)runtime.getClassFromPath("Nokogiri::XML::ParseOptions");
323
+ if (htmlish) {
324
+ args[3] = parse_options.getConstant("DEFAULT_HTML");
325
+ RubyClass htmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::HTML::Document");
326
+ return Helpers.invoke(context, htmlDocumentClass, "parse", args);
327
+ } else {
328
+ args[3] = parse_options.getConstant("DEFAULT_XML");
329
+ RubyClass xmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
330
+ XmlDocument xmlDocument = (XmlDocument) Helpers.invoke(context, xmlDocumentClass, "parse", args);
331
+ if (((Document)xmlDocument.getNode()).getDocumentElement() == null) {
332
+ RubyArray errors = (RubyArray) xmlDocument.getInstanceVariable("@errors");
333
+ Helpers.invoke(context, errors, "<<", args[0]);
334
+ }
335
+ return xmlDocument;
346
336
  }
337
+ }
338
+
339
+ private static void
340
+ argumentTypeCheck(Ruby runtime, IRubyObject arg)
341
+ {
342
+ if (arg instanceof XmlDocument) { return; }
343
+ throw runtime.newArgumentError("argument must be a Nokogiri::XML::Document");
344
+ }
345
+
346
+ @JRubyMethod(name = {"registr", "register"}, meta = true)
347
+ public static IRubyObject
348
+ register(ThreadContext context, IRubyObject cls, IRubyObject uri, IRubyObject receiver)
349
+ {
350
+ throw context.getRuntime().newNotImplementedError("Nokogiri::XSLT.register method is not implemented");
351
+ /* When API conflict is solved, this method should be below:
352
+ // ThreadContext is used while executing xslt extension function
353
+ registry.put("context", context);
354
+ registry.put("receiver", receiver);
355
+ return context.getRuntime().getNil();
356
+ */
357
+ }
347
358
  }