nokogiri 1.11.1-java → 1.11.6-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 (143) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE-DEPENDENCIES.md +12 -12
  3. data/LICENSE.md +1 -1
  4. data/README.md +21 -16
  5. data/dependencies.yml +12 -12
  6. data/ext/java/nokogiri/EncodingHandler.java +76 -89
  7. data/ext/java/nokogiri/HtmlDocument.java +135 -144
  8. data/ext/java/nokogiri/HtmlElementDescription.java +102 -117
  9. data/ext/java/nokogiri/HtmlEntityLookup.java +33 -60
  10. data/ext/java/nokogiri/HtmlSaxParserContext.java +218 -222
  11. data/ext/java/nokogiri/HtmlSaxPushParser.java +162 -169
  12. data/ext/java/nokogiri/NokogiriService.java +595 -556
  13. data/ext/java/nokogiri/XmlAttr.java +118 -126
  14. data/ext/java/nokogiri/XmlAttributeDecl.java +95 -106
  15. data/ext/java/nokogiri/XmlCdata.java +35 -58
  16. data/ext/java/nokogiri/XmlComment.java +46 -67
  17. data/ext/java/nokogiri/XmlDocument.java +645 -572
  18. data/ext/java/nokogiri/XmlDocumentFragment.java +125 -137
  19. data/ext/java/nokogiri/XmlDtd.java +448 -414
  20. data/ext/java/nokogiri/XmlElement.java +23 -48
  21. data/ext/java/nokogiri/XmlElementContent.java +343 -316
  22. data/ext/java/nokogiri/XmlElementDecl.java +124 -125
  23. data/ext/java/nokogiri/XmlEntityDecl.java +119 -127
  24. data/ext/java/nokogiri/XmlEntityReference.java +49 -72
  25. data/ext/java/nokogiri/XmlNamespace.java +175 -175
  26. data/ext/java/nokogiri/XmlNode.java +1843 -1620
  27. data/ext/java/nokogiri/XmlNodeSet.java +361 -331
  28. data/ext/java/nokogiri/XmlProcessingInstruction.java +47 -69
  29. data/ext/java/nokogiri/XmlReader.java +513 -450
  30. data/ext/java/nokogiri/XmlRelaxng.java +85 -104
  31. data/ext/java/nokogiri/XmlSaxParserContext.java +328 -315
  32. data/ext/java/nokogiri/XmlSaxPushParser.java +227 -220
  33. data/ext/java/nokogiri/XmlSchema.java +328 -295
  34. data/ext/java/nokogiri/XmlSyntaxError.java +113 -115
  35. data/ext/java/nokogiri/XmlText.java +55 -76
  36. data/ext/java/nokogiri/XmlXpathContext.java +240 -238
  37. data/ext/java/nokogiri/XsltStylesheet.java +280 -269
  38. data/ext/java/nokogiri/internals/ClosedStreamException.java +5 -2
  39. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +201 -202
  40. data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +17 -10
  41. data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +43 -16
  42. data/ext/java/nokogiri/internals/NokogiriDomParser.java +63 -80
  43. data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +107 -88
  44. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +27 -52
  45. data/ext/java/nokogiri/internals/NokogiriHandler.java +316 -286
  46. data/ext/java/nokogiri/internals/NokogiriHelpers.java +736 -652
  47. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +184 -173
  48. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +81 -98
  49. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +64 -79
  50. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +84 -99
  51. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +48 -65
  52. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +116 -131
  53. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -56
  54. data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +23 -46
  55. data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +55 -72
  56. data/ext/java/nokogiri/internals/ParserContext.java +206 -211
  57. data/ext/java/nokogiri/internals/ReaderNode.java +478 -403
  58. data/ext/java/nokogiri/internals/SaveContextVisitor.java +822 -739
  59. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +31 -54
  60. data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +129 -123
  61. data/ext/java/nokogiri/internals/XmlDeclHandler.java +3 -34
  62. data/ext/java/nokogiri/internals/XmlDomParserContext.java +206 -207
  63. data/ext/java/nokogiri/internals/XmlSaxParser.java +22 -47
  64. data/ext/java/nokogiri/internals/c14n/AttrCompare.java +71 -68
  65. data/ext/java/nokogiri/internals/c14n/C14nHelper.java +137 -118
  66. data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +27 -21
  67. data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +74 -61
  68. data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +230 -205
  69. data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +572 -547
  70. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +17 -10
  71. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +17 -10
  72. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +323 -302
  73. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +232 -219
  74. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +22 -15
  75. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +23 -16
  76. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +23 -16
  77. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +22 -15
  78. data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +575 -545
  79. data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +141 -120
  80. data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +39 -38
  81. data/ext/java/nokogiri/internals/c14n/Constants.java +13 -10
  82. data/ext/java/nokogiri/internals/c14n/ElementProxy.java +279 -247
  83. data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +66 -53
  84. data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +44 -37
  85. data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +135 -120
  86. data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +59 -48
  87. data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +384 -334
  88. data/ext/java/nokogiri/internals/c14n/NodeFilter.java +25 -24
  89. data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +151 -140
  90. data/ext/java/nokogiri/internals/c14n/XMLUtils.java +456 -423
  91. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1466 -1500
  92. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +626 -574
  93. data/ext/nokogiri/depend +34 -474
  94. data/ext/nokogiri/extconf.rb +253 -183
  95. data/ext/nokogiri/html_document.c +10 -15
  96. data/ext/nokogiri/html_element_description.c +84 -71
  97. data/ext/nokogiri/html_entity_lookup.c +21 -16
  98. data/ext/nokogiri/html_sax_parser_context.c +66 -65
  99. data/ext/nokogiri/html_sax_push_parser.c +29 -27
  100. data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
  101. data/ext/nokogiri/nokogiri.c +190 -63
  102. data/ext/nokogiri/test_global_handlers.c +3 -4
  103. data/ext/nokogiri/xml_attr.c +15 -15
  104. data/ext/nokogiri/xml_attribute_decl.c +18 -18
  105. data/ext/nokogiri/xml_cdata.c +13 -18
  106. data/ext/nokogiri/xml_comment.c +19 -26
  107. data/ext/nokogiri/xml_document.c +246 -188
  108. data/ext/nokogiri/xml_document_fragment.c +13 -15
  109. data/ext/nokogiri/xml_dtd.c +54 -48
  110. data/ext/nokogiri/xml_element_content.c +30 -27
  111. data/ext/nokogiri/xml_element_decl.c +22 -22
  112. data/ext/nokogiri/xml_encoding_handler.c +17 -11
  113. data/ext/nokogiri/xml_entity_decl.c +32 -30
  114. data/ext/nokogiri/xml_entity_reference.c +16 -18
  115. data/ext/nokogiri/xml_namespace.c +56 -49
  116. data/ext/nokogiri/xml_node.c +385 -326
  117. data/ext/nokogiri/xml_node_set.c +168 -156
  118. data/ext/nokogiri/xml_processing_instruction.c +17 -19
  119. data/ext/nokogiri/xml_reader.c +191 -157
  120. data/ext/nokogiri/xml_relax_ng.c +29 -23
  121. data/ext/nokogiri/xml_sax_parser.c +117 -112
  122. data/ext/nokogiri/xml_sax_parser_context.c +100 -85
  123. data/ext/nokogiri/xml_sax_push_parser.c +34 -27
  124. data/ext/nokogiri/xml_schema.c +48 -42
  125. data/ext/nokogiri/xml_syntax_error.c +21 -23
  126. data/ext/nokogiri/xml_text.c +13 -17
  127. data/ext/nokogiri/xml_xpath_context.c +134 -127
  128. data/ext/nokogiri/xslt_stylesheet.c +157 -157
  129. data/lib/nokogiri.rb +1 -22
  130. data/lib/nokogiri/css/parser.rb +1 -1
  131. data/lib/nokogiri/extension.rb +26 -0
  132. data/lib/nokogiri/html/document_fragment.rb +15 -15
  133. data/lib/nokogiri/nokogiri.jar +0 -0
  134. data/lib/nokogiri/version/constant.rb +1 -1
  135. data/lib/nokogiri/version/info.rb +32 -8
  136. data/lib/nokogiri/xml/document.rb +74 -28
  137. data/lib/nokogiri/xml/node.rb +39 -42
  138. data/lib/nokogiri/xml/reader.rb +2 -9
  139. data/lib/nokogiri/xml/xpath.rb +1 -3
  140. data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
  141. metadata +7 -8
  142. data/ext/nokogiri/xml_io.c +0 -63
  143. data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
@@ -1,35 +1,3 @@
1
- /**
2
- * (The MIT License)
3
- *
4
- * Copyright (c) 2008 - 2011:
5
- *
6
- * * {Aaron Patterson}[http://tenderlovemaking.com]
7
- * * {Mike Dalessio}[http://mike.daless.io]
8
- * * {Charles Nutter}[http://blog.headius.com]
9
- * * {Sergio Arbeo}[http://www.serabe.com]
10
- * * {Patrick Mahoney}[http://polycrystal.org]
11
- * * {Yoko Harada}[http://yokolet.blogspot.com]
12
- *
13
- * Permission is hereby granted, free of charge, to any person obtaining
14
- * a copy of this software and associated documentation files (the
15
- * 'Software'), to deal in the Software without restriction, including
16
- * without limitation the rights to use, copy, modify, merge, publish,
17
- * distribute, sublicense, and/or sell copies of the Software, and to
18
- * permit persons to whom the Software is furnished to do so, subject to
19
- * the following conditions:
20
- *
21
- * The above copyright notice and this permission notice shall be
22
- * included in all copies or substantial portions of the Software.
23
- *
24
- * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
- */
32
-
33
1
  package nokogiri;
34
2
 
35
3
  import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
@@ -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
  }