nokogiri 1.11.1-java → 1.11.2-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 (142) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE-DEPENDENCIES.md +12 -12
  3. data/LICENSE.md +1 -1
  4. data/README.md +20 -15
  5. data/ext/java/nokogiri/EncodingHandler.java +78 -59
  6. data/ext/java/nokogiri/HtmlDocument.java +137 -114
  7. data/ext/java/nokogiri/HtmlElementDescription.java +104 -87
  8. data/ext/java/nokogiri/HtmlEntityLookup.java +31 -26
  9. data/ext/java/nokogiri/HtmlSaxParserContext.java +220 -192
  10. data/ext/java/nokogiri/HtmlSaxPushParser.java +164 -139
  11. data/ext/java/nokogiri/NokogiriService.java +597 -526
  12. data/ext/java/nokogiri/XmlAttr.java +120 -96
  13. data/ext/java/nokogiri/XmlAttributeDecl.java +97 -76
  14. data/ext/java/nokogiri/XmlCdata.java +35 -26
  15. data/ext/java/nokogiri/XmlComment.java +48 -37
  16. data/ext/java/nokogiri/XmlDocument.java +642 -540
  17. data/ext/java/nokogiri/XmlDocumentFragment.java +127 -107
  18. data/ext/java/nokogiri/XmlDtd.java +450 -384
  19. data/ext/java/nokogiri/XmlElement.java +25 -18
  20. data/ext/java/nokogiri/XmlElementContent.java +345 -286
  21. data/ext/java/nokogiri/XmlElementDecl.java +126 -95
  22. data/ext/java/nokogiri/XmlEntityDecl.java +121 -97
  23. data/ext/java/nokogiri/XmlEntityReference.java +51 -42
  24. data/ext/java/nokogiri/XmlNamespace.java +177 -145
  25. data/ext/java/nokogiri/XmlNode.java +1843 -1588
  26. data/ext/java/nokogiri/XmlNodeSet.java +361 -299
  27. data/ext/java/nokogiri/XmlProcessingInstruction.java +49 -39
  28. data/ext/java/nokogiri/XmlReader.java +513 -418
  29. data/ext/java/nokogiri/XmlRelaxng.java +91 -78
  30. data/ext/java/nokogiri/XmlSaxParserContext.java +330 -285
  31. data/ext/java/nokogiri/XmlSaxPushParser.java +229 -190
  32. data/ext/java/nokogiri/XmlSchema.java +328 -263
  33. data/ext/java/nokogiri/XmlSyntaxError.java +113 -83
  34. data/ext/java/nokogiri/XmlText.java +57 -46
  35. data/ext/java/nokogiri/XmlXpathContext.java +240 -206
  36. data/ext/java/nokogiri/XsltStylesheet.java +282 -239
  37. data/ext/java/nokogiri/internals/ClosedStreamException.java +5 -2
  38. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +199 -168
  39. data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +17 -10
  40. data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +43 -16
  41. data/ext/java/nokogiri/internals/NokogiriDomParser.java +65 -50
  42. data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +107 -88
  43. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +25 -18
  44. data/ext/java/nokogiri/internals/NokogiriHandler.java +316 -254
  45. data/ext/java/nokogiri/internals/NokogiriHelpers.java +738 -622
  46. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +186 -143
  47. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +83 -68
  48. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +66 -49
  49. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +86 -69
  50. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +44 -29
  51. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +118 -101
  52. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -24
  53. data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +25 -17
  54. data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +57 -42
  55. data/ext/java/nokogiri/internals/ParserContext.java +206 -179
  56. data/ext/java/nokogiri/internals/ReaderNode.java +478 -371
  57. data/ext/java/nokogiri/internals/SaveContextVisitor.java +822 -707
  58. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +28 -19
  59. data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +129 -123
  60. data/ext/java/nokogiri/internals/XmlDeclHandler.java +5 -4
  61. data/ext/java/nokogiri/internals/XmlDomParserContext.java +208 -177
  62. data/ext/java/nokogiri/internals/XmlSaxParser.java +24 -17
  63. data/ext/java/nokogiri/internals/c14n/AttrCompare.java +71 -68
  64. data/ext/java/nokogiri/internals/c14n/C14nHelper.java +137 -118
  65. data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +27 -21
  66. data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +74 -61
  67. data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +230 -205
  68. data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +572 -547
  69. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +17 -10
  70. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +17 -10
  71. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +323 -302
  72. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +232 -219
  73. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +22 -15
  74. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +23 -16
  75. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +23 -16
  76. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +22 -15
  77. data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +575 -545
  78. data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +141 -120
  79. data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +39 -38
  80. data/ext/java/nokogiri/internals/c14n/Constants.java +13 -10
  81. data/ext/java/nokogiri/internals/c14n/ElementProxy.java +279 -247
  82. data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +66 -53
  83. data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +44 -37
  84. data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +135 -120
  85. data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +59 -48
  86. data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +384 -334
  87. data/ext/java/nokogiri/internals/c14n/NodeFilter.java +25 -24
  88. data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +151 -140
  89. data/ext/java/nokogiri/internals/c14n/XMLUtils.java +456 -423
  90. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1466 -1500
  91. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +626 -570
  92. data/ext/nokogiri/depend +34 -474
  93. data/ext/nokogiri/extconf.rb +253 -183
  94. data/ext/nokogiri/html_document.c +10 -15
  95. data/ext/nokogiri/html_element_description.c +84 -71
  96. data/ext/nokogiri/html_entity_lookup.c +21 -16
  97. data/ext/nokogiri/html_sax_parser_context.c +66 -65
  98. data/ext/nokogiri/html_sax_push_parser.c +29 -27
  99. data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
  100. data/ext/nokogiri/nokogiri.c +171 -63
  101. data/ext/nokogiri/test_global_handlers.c +3 -4
  102. data/ext/nokogiri/xml_attr.c +15 -15
  103. data/ext/nokogiri/xml_attribute_decl.c +18 -18
  104. data/ext/nokogiri/xml_cdata.c +13 -18
  105. data/ext/nokogiri/xml_comment.c +19 -26
  106. data/ext/nokogiri/xml_document.c +221 -164
  107. data/ext/nokogiri/xml_document_fragment.c +13 -15
  108. data/ext/nokogiri/xml_dtd.c +54 -48
  109. data/ext/nokogiri/xml_element_content.c +30 -27
  110. data/ext/nokogiri/xml_element_decl.c +22 -22
  111. data/ext/nokogiri/xml_encoding_handler.c +17 -11
  112. data/ext/nokogiri/xml_entity_decl.c +32 -30
  113. data/ext/nokogiri/xml_entity_reference.c +16 -18
  114. data/ext/nokogiri/xml_namespace.c +56 -49
  115. data/ext/nokogiri/xml_node.c +338 -286
  116. data/ext/nokogiri/xml_node_set.c +168 -156
  117. data/ext/nokogiri/xml_processing_instruction.c +17 -19
  118. data/ext/nokogiri/xml_reader.c +191 -157
  119. data/ext/nokogiri/xml_relax_ng.c +29 -23
  120. data/ext/nokogiri/xml_sax_parser.c +117 -112
  121. data/ext/nokogiri/xml_sax_parser_context.c +100 -85
  122. data/ext/nokogiri/xml_sax_push_parser.c +34 -27
  123. data/ext/nokogiri/xml_schema.c +48 -42
  124. data/ext/nokogiri/xml_syntax_error.c +21 -23
  125. data/ext/nokogiri/xml_text.c +13 -17
  126. data/ext/nokogiri/xml_xpath_context.c +134 -127
  127. data/ext/nokogiri/xslt_stylesheet.c +157 -157
  128. data/lib/nokogiri.rb +1 -22
  129. data/lib/nokogiri/css/parser.rb +1 -1
  130. data/lib/nokogiri/extension.rb +26 -0
  131. data/lib/nokogiri/html/document_fragment.rb +15 -15
  132. data/lib/nokogiri/nokogiri.jar +0 -0
  133. data/lib/nokogiri/version/constant.rb +1 -1
  134. data/lib/nokogiri/version/info.rb +31 -8
  135. data/lib/nokogiri/xml/document.rb +31 -11
  136. data/lib/nokogiri/xml/node.rb +38 -42
  137. data/lib/nokogiri/xml/reader.rb +2 -9
  138. data/lib/nokogiri/xml/xpath.rb +1 -3
  139. data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
  140. metadata +7 -8
  141. data/ext/nokogiri/xml_io.c +0 -63
  142. data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
@@ -17,10 +17,10 @@
17
17
  * distribute, sublicense, and/or sell copies of the Software, and to
18
18
  * permit persons to whom the Software is furnished to do so, subject to
19
19
  * the following conditions:
20
- *
20
+ *
21
21
  * The above copyright notice and this permission notice shall be
22
22
  * included in all copies or substantial portions of the Software.
23
- *
23
+ *
24
24
  * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25
25
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
26
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
@@ -77,271 +77,314 @@ import nokogiri.internals.NokogiriXsltErrorListener;
77
77
  * @author sergio
78
78
  * @author Yoko Harada <yokolet@gmail.com>
79
79
  */
80
- @JRubyClass(name="Nokogiri::XSLT::Stylesheet")
81
- public class XsltStylesheet extends RubyObject {
80
+ @JRubyClass(name = "Nokogiri::XSLT::Stylesheet")
81
+ public class XsltStylesheet extends RubyObject
82
+ {
82
83
 
83
- private TransformerFactory factory = null;
84
- private Templates sheet = null;
85
- private IRubyObject stylesheet = null;
86
- private boolean htmlish = false;
84
+ private TransformerFactory factory = null;
85
+ private Templates sheet = null;
86
+ private IRubyObject stylesheet = null;
87
+ private boolean htmlish = false;
87
88
 
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
- }
89
+ public
90
+ XsltStylesheet(Ruby ruby, RubyClass rubyClass)
91
+ {
92
+ super(ruby, rubyClass);
93
+ }
94
+
95
+ /**
96
+ * Create and return a copy of this object.
97
+ *
98
+ * @return a clone of this object
99
+ */
100
+ @Override
101
+ public Object
102
+ clone() throws CloneNotSupportedException
103
+ {
104
+ return super.clone();
105
+ }
101
106
 
102
- private void addParametersToTransformer(ThreadContext context, Transformer transf, IRubyObject parameters) {
103
- Ruby runtime = context.getRuntime();
107
+ private void
108
+ addParametersToTransformer(ThreadContext context, Transformer transf, IRubyObject parameters)
109
+ {
110
+ Ruby runtime = context.getRuntime();
104
111
 
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
- }
112
+ if (parameters instanceof RubyHash) {
113
+ setHashParameters(transf, (RubyHash)parameters);
114
+ } else if (parameters instanceof RubyArray) {
115
+ setArrayParameters(transf, runtime, (RubyArray)parameters);
116
+ } else {
117
+ throw runtime.newTypeError("parameters should be given either Array or Hash");
112
118
  }
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
- }
119
+ }
120
+
121
+ private void
122
+ setHashParameters(Transformer transformer, RubyHash hash)
123
+ {
124
+ Set<String> keys = hash.keySet();
125
+ for (String key : keys) {
126
+ String value = (String)hash.get(key);
127
+ transformer.setParameter(key, unparseValue(value));
120
128
  }
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
- }
129
+ }
130
+
131
+ private void
132
+ setArrayParameters(Transformer transformer, Ruby runtime, RubyArray params)
133
+ {
134
+ int limit = params.getLength();
135
+ if (limit % 2 == 1) { limit--; }
136
+
137
+ for (int i = 0; i < limit; i += 2) {
138
+ String name = params.aref(runtime.newFixnum(i)).asJavaString();
139
+ String value = params.aref(runtime.newFixnum(i + 1)).asJavaString();
140
+ transformer.setParameter(name, unparseValue(value));
131
141
  }
132
-
133
- private static final Pattern QUOTED = Pattern.compile("'.{1,}'");
142
+ }
134
143
 
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
- }
144
+ private static final Pattern QUOTED = Pattern.compile("'.{1,}'");
139
145
 
140
- return orig;
146
+ private String
147
+ unparseValue(String orig)
148
+ {
149
+ if ((orig.startsWith("\"") && orig.endsWith("\"")) || QUOTED.matcher(orig).matches()) {
150
+ orig = orig.substring(1, orig.length() - 1);
141
151
  }
142
152
 
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();
153
+ return orig;
154
+ }
155
+
156
+ @JRubyMethod(meta = true, rest = true)
157
+ public static IRubyObject
158
+ parse_stylesheet_doc(ThreadContext context, IRubyObject klazz, IRubyObject[] args)
159
+ {
147
160
 
148
- ensureFirstArgIsDocument(runtime, args[0]);
161
+ Ruby runtime = context.getRuntime();
149
162
 
150
- XmlDocument xmlDoc = (XmlDocument) args[0];
151
- ensureDocumentHasNoError(context, xmlDoc);
152
-
153
- Document doc = ((XmlDocument) xmlDoc.dup_implementation(context, true)).getDocument();
163
+ ensureFirstArgIsDocument(runtime, args[0]);
154
164
 
155
- XsltStylesheet xslt =
156
- (XsltStylesheet) NokogiriService.XSLT_STYLESHEET_ALLOCATOR.allocate(runtime, (RubyClass)klazz);
165
+ XmlDocument xmlDoc = (XmlDocument) args[0];
166
+ ensureDocumentHasNoError(context, xmlDoc);
157
167
 
158
- try {
159
- xslt.init(args[1], doc);
160
- } catch (TransformerConfigurationException ex) {
161
- throw runtime.newRuntimeError("could not parse xslt stylesheet");
162
- }
168
+ Document doc = ((XmlDocument) xmlDoc.dup_implementation(context, true)).getDocument();
163
169
 
164
- return xslt;
170
+ XsltStylesheet xslt =
171
+ (XsltStylesheet) NokogiriService.XSLT_STYLESHEET_ALLOCATOR.allocate(runtime, (RubyClass)klazz);
172
+
173
+ try {
174
+ xslt.init(args[1], doc);
175
+ } catch (TransformerConfigurationException ex) {
176
+ throw runtime.newRuntimeError("could not parse xslt stylesheet");
165
177
  }
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));
178
+
179
+ return xslt;
180
+ }
181
+
182
+ private void
183
+ init(IRubyObject stylesheet, Document document) throws TransformerConfigurationException
184
+ {
185
+ this.stylesheet = stylesheet; // either RubyString or RubyFile
186
+ if (factory == null) { factory = TransformerFactory.newInstance(); }
187
+ NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
188
+ factory.setErrorListener(elistener);
189
+ sheet = factory.newTemplates(new DOMSource(document));
190
+ }
191
+
192
+ private static void
193
+ ensureFirstArgIsDocument(Ruby runtime, IRubyObject arg)
194
+ {
195
+ if (arg instanceof XmlDocument) { return; }
196
+ throw runtime.newArgumentError("doc must be a Nokogiri::XML::Document instance");
197
+ }
198
+
199
+ private static void
200
+ ensureDocumentHasNoError(ThreadContext context, XmlDocument xmlDoc)
201
+ {
202
+ Ruby runtime = context.getRuntime();
203
+ RubyArray errors_of_xmlDoc = (RubyArray) xmlDoc.getInstanceVariable("@errors");
204
+ if (!errors_of_xmlDoc.isEmpty()) {
205
+ throw runtime.newRuntimeError(errors_of_xmlDoc.first().asString().asJavaString());
173
206
  }
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");
207
+ }
208
+
209
+ @JRubyMethod
210
+ public IRubyObject
211
+ serialize(ThreadContext context, IRubyObject doc) throws IOException, TransformerException
212
+ {
213
+ XmlDocument xmlDoc = (XmlDocument) doc;
214
+ TransformerImpl transformer = (TransformerImpl) this.sheet.newTransformer();
215
+ ByteArrayOutputStream writer = new ByteArrayOutputStream();
216
+ StreamResult streamResult = new StreamResult(writer);
217
+ SerializationHandler serializationHandler = transformer.createSerializationHandler(streamResult);
218
+ serializationHandler.serialize(xmlDoc.getNode());
219
+ return context.getRuntime().newString(writer.toString());
220
+ }
221
+
222
+ @JRubyMethod(rest = true, required = 1, optional = 2)
223
+ public IRubyObject
224
+ transform(ThreadContext context, IRubyObject[] args)
225
+ {
226
+ Ruby runtime = context.getRuntime();
227
+
228
+ argumentTypeCheck(runtime, args[0]);
229
+
230
+ NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
231
+ DOMSource domSource = new DOMSource(((XmlDocument) args[0]).getDocument());
232
+ final DOMResult result;
233
+ String stringResult = null;
234
+ try {
235
+ result = tryXsltTransformation(context, args, domSource, elistener); // DOMResult
236
+ if (result.getNode().getFirstChild() == null) {
237
+ stringResult = retryXsltTransformation(context, args, domSource, elistener); // StreamResult
238
+ }
239
+ } catch (TransformerConfigurationException ex) {
240
+ throw runtime.newRuntimeError(ex.getMessage());
241
+ } catch (TransformerException ex) {
242
+ throw runtime.newRuntimeError(ex.getMessage());
243
+ } catch (IOException ex) {
244
+ throw runtime.newRuntimeError(ex.getMessage());
178
245
  }
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
- }
246
+
247
+ switch (elistener.getErrorType()) {
248
+ case ERROR:
249
+ case FATAL:
250
+ throw runtime.newRuntimeError(elistener.getErrorMessage());
251
+ case WARNING:
252
+ default:
253
+ // no-op
186
254
  }
187
255
 
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());
256
+ if (stringResult == null) {
257
+ return createDocumentFromDomResult(context, runtime, result);
258
+ } else {
259
+ return createDocumentFromString(context, runtime, stringResult);
197
260
  }
261
+ }
198
262
 
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
- }
263
+ private DOMResult
264
+ tryXsltTransformation(ThreadContext context, IRubyObject[] args, DOMSource domSource,
265
+ NokogiriXsltErrorListener elistener) throws TransformerException
266
+ {
267
+ Transformer transf = sheet.newTransformer();
268
+ transf.reset();
269
+ transf.setErrorListener(elistener);
270
+ if (args.length > 1) {
271
+ addParametersToTransformer(context, transf, args[1]);
235
272
  }
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;
273
+
274
+ DOMResult result = new DOMResult();
275
+ transf.transform(domSource, result);
276
+ return result;
277
+ }
278
+
279
+ private String
280
+ retryXsltTransformation(ThreadContext context,
281
+ IRubyObject[] args,
282
+ DOMSource domSource,
283
+ NokogiriXsltErrorListener elistener)
284
+ throws TransformerException, IOException
285
+ {
286
+ Templates templates = getTemplatesFromStreamSource();
287
+ Transformer transf = templates.newTransformer();
288
+ transf.setErrorListener(elistener);
289
+ if (args.length > 1) {
290
+ addParametersToTransformer(context, transf, args[1]);
248
291
  }
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);
292
+ PipedWriter pwriter = new PipedWriter();
293
+ PipedReader preader = new PipedReader();
294
+ pwriter.connect(preader);
295
+ StreamResult result = new StreamResult(pwriter);
296
+ transf.transform(domSource, result);
297
+
298
+ char[] cbuf = new char[1024];
299
+ int len = preader.read(cbuf, 0, 1024);
300
+ StringBuilder builder = new StringBuilder(len);
301
+ builder.append(cbuf, 0, len);
302
+ htmlish = isHtml(builder); // judge from the first chunk
303
+
304
+ while (len == 1024) {
305
+ len = preader.read(cbuf, 0, 1024);
306
+ if (len > 0) {
270
307
  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();
308
+ }
284
309
  }
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();
308
- }
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
- }
310
+
311
+ preader.close();
312
+ pwriter.close();
313
+
314
+ return builder.toString();
315
+ }
316
+
317
+ private IRubyObject
318
+ createDocumentFromDomResult(ThreadContext context, Ruby runtime, DOMResult domResult)
319
+ {
320
+ if ("html".equals(domResult.getNode().getFirstChild().getNodeName())) {
321
+ return new HtmlDocument(context.runtime, (Document) domResult.getNode());
322
+ } else {
323
+ return new XmlDocument(context.runtime, (Document) domResult.getNode());
330
324
  }
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");
325
+ }
326
+
327
+ private Templates
328
+ getTemplatesFromStreamSource() throws TransformerConfigurationException
329
+ {
330
+ if (stylesheet instanceof RubyString) {
331
+ StringReader reader = new StringReader(stylesheet.asJavaString());
332
+ StreamSource xsltStreamSource = new StreamSource(reader);
333
+ return factory.newTemplates(xsltStreamSource);
335
334
  }
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
- */
335
+ return null;
336
+ }
337
+
338
+ private static final Pattern HTML_TAG = Pattern.compile("<(%s)*html", Pattern.CASE_INSENSITIVE);
339
+
340
+ private static boolean
341
+ isHtml(CharSequence chunk)
342
+ {
343
+ Matcher match = HTML_TAG.matcher(chunk);
344
+ return match.find();
345
+ }
346
+
347
+ private IRubyObject
348
+ createDocumentFromString(ThreadContext context, Ruby runtime, String stringResult)
349
+ {
350
+ IRubyObject[] args = new IRubyObject[4];
351
+ args[0] = stringOrBlank(runtime, stringResult);
352
+ args[1] = runtime.getNil(); // url
353
+ args[2] = runtime.getNil(); // encoding
354
+ RubyClass parse_options = (RubyClass)runtime.getClassFromPath("Nokogiri::XML::ParseOptions");
355
+ if (htmlish) {
356
+ args[3] = parse_options.getConstant("DEFAULT_HTML");
357
+ RubyClass htmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::HTML::Document");
358
+ return Helpers.invoke(context, htmlDocumentClass, "parse", args);
359
+ } else {
360
+ args[3] = parse_options.getConstant("DEFAULT_XML");
361
+ RubyClass xmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
362
+ XmlDocument xmlDocument = (XmlDocument) Helpers.invoke(context, xmlDocumentClass, "parse", args);
363
+ if (((Document)xmlDocument.getNode()).getDocumentElement() == null) {
364
+ RubyArray errors = (RubyArray) xmlDocument.getInstanceVariable("@errors");
365
+ Helpers.invoke(context, errors, "<<", args[0]);
366
+ }
367
+ return xmlDocument;
346
368
  }
369
+ }
370
+
371
+ private static void
372
+ argumentTypeCheck(Ruby runtime, IRubyObject arg)
373
+ {
374
+ if (arg instanceof XmlDocument) { return; }
375
+ throw runtime.newArgumentError("argument must be a Nokogiri::XML::Document");
376
+ }
377
+
378
+ @JRubyMethod(name = {"registr", "register"}, meta = true)
379
+ public static IRubyObject
380
+ register(ThreadContext context, IRubyObject cls, IRubyObject uri, IRubyObject receiver)
381
+ {
382
+ throw context.getRuntime().newNotImplementedError("Nokogiri::XSLT.register method is not implemented");
383
+ /* When API conflict is solved, this method should be below:
384
+ // ThreadContext is used while executing xslt extension function
385
+ registry.put("context", context);
386
+ registry.put("receiver", receiver);
387
+ return context.getRuntime().getNil();
388
+ */
389
+ }
347
390
  }