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.
- checksums.yaml +4 -4
- data/LICENSE-DEPENDENCIES.md +12 -12
- data/LICENSE.md +1 -1
- data/README.md +20 -15
- data/ext/java/nokogiri/EncodingHandler.java +78 -59
- data/ext/java/nokogiri/HtmlDocument.java +137 -114
- data/ext/java/nokogiri/HtmlElementDescription.java +104 -87
- data/ext/java/nokogiri/HtmlEntityLookup.java +31 -26
- data/ext/java/nokogiri/HtmlSaxParserContext.java +220 -192
- data/ext/java/nokogiri/HtmlSaxPushParser.java +164 -139
- data/ext/java/nokogiri/NokogiriService.java +597 -526
- data/ext/java/nokogiri/XmlAttr.java +120 -96
- data/ext/java/nokogiri/XmlAttributeDecl.java +97 -76
- data/ext/java/nokogiri/XmlCdata.java +35 -26
- data/ext/java/nokogiri/XmlComment.java +48 -37
- data/ext/java/nokogiri/XmlDocument.java +642 -540
- data/ext/java/nokogiri/XmlDocumentFragment.java +127 -107
- data/ext/java/nokogiri/XmlDtd.java +450 -384
- data/ext/java/nokogiri/XmlElement.java +25 -18
- data/ext/java/nokogiri/XmlElementContent.java +345 -286
- data/ext/java/nokogiri/XmlElementDecl.java +126 -95
- data/ext/java/nokogiri/XmlEntityDecl.java +121 -97
- data/ext/java/nokogiri/XmlEntityReference.java +51 -42
- data/ext/java/nokogiri/XmlNamespace.java +177 -145
- data/ext/java/nokogiri/XmlNode.java +1843 -1588
- data/ext/java/nokogiri/XmlNodeSet.java +361 -299
- data/ext/java/nokogiri/XmlProcessingInstruction.java +49 -39
- data/ext/java/nokogiri/XmlReader.java +513 -418
- data/ext/java/nokogiri/XmlRelaxng.java +91 -78
- data/ext/java/nokogiri/XmlSaxParserContext.java +330 -285
- data/ext/java/nokogiri/XmlSaxPushParser.java +229 -190
- data/ext/java/nokogiri/XmlSchema.java +328 -263
- data/ext/java/nokogiri/XmlSyntaxError.java +113 -83
- data/ext/java/nokogiri/XmlText.java +57 -46
- data/ext/java/nokogiri/XmlXpathContext.java +240 -206
- data/ext/java/nokogiri/XsltStylesheet.java +282 -239
- data/ext/java/nokogiri/internals/ClosedStreamException.java +5 -2
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +199 -168
- data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +17 -10
- data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +43 -16
- data/ext/java/nokogiri/internals/NokogiriDomParser.java +65 -50
- data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +107 -88
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +25 -18
- data/ext/java/nokogiri/internals/NokogiriHandler.java +316 -254
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +738 -622
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +186 -143
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +83 -68
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +66 -49
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +86 -69
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +44 -29
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +118 -101
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -24
- data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +25 -17
- data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +57 -42
- data/ext/java/nokogiri/internals/ParserContext.java +206 -179
- data/ext/java/nokogiri/internals/ReaderNode.java +478 -371
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +822 -707
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +28 -19
- data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +129 -123
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +5 -4
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +208 -177
- data/ext/java/nokogiri/internals/XmlSaxParser.java +24 -17
- data/ext/java/nokogiri/internals/c14n/AttrCompare.java +71 -68
- data/ext/java/nokogiri/internals/c14n/C14nHelper.java +137 -118
- data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +27 -21
- data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +74 -61
- data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +230 -205
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +572 -547
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +17 -10
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +17 -10
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +323 -302
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +232 -219
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +22 -15
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +23 -16
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +23 -16
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +22 -15
- data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +575 -545
- data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +141 -120
- data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +39 -38
- data/ext/java/nokogiri/internals/c14n/Constants.java +13 -10
- data/ext/java/nokogiri/internals/c14n/ElementProxy.java +279 -247
- data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +66 -53
- data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +44 -37
- data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +135 -120
- data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +59 -48
- data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +384 -334
- data/ext/java/nokogiri/internals/c14n/NodeFilter.java +25 -24
- data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +151 -140
- data/ext/java/nokogiri/internals/c14n/XMLUtils.java +456 -423
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1466 -1500
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +626 -570
- data/ext/nokogiri/depend +34 -474
- data/ext/nokogiri/extconf.rb +253 -183
- data/ext/nokogiri/html_document.c +10 -15
- data/ext/nokogiri/html_element_description.c +84 -71
- data/ext/nokogiri/html_entity_lookup.c +21 -16
- data/ext/nokogiri/html_sax_parser_context.c +66 -65
- data/ext/nokogiri/html_sax_push_parser.c +29 -27
- data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
- data/ext/nokogiri/nokogiri.c +171 -63
- data/ext/nokogiri/test_global_handlers.c +3 -4
- data/ext/nokogiri/xml_attr.c +15 -15
- data/ext/nokogiri/xml_attribute_decl.c +18 -18
- data/ext/nokogiri/xml_cdata.c +13 -18
- data/ext/nokogiri/xml_comment.c +19 -26
- data/ext/nokogiri/xml_document.c +221 -164
- data/ext/nokogiri/xml_document_fragment.c +13 -15
- data/ext/nokogiri/xml_dtd.c +54 -48
- data/ext/nokogiri/xml_element_content.c +30 -27
- data/ext/nokogiri/xml_element_decl.c +22 -22
- data/ext/nokogiri/xml_encoding_handler.c +17 -11
- data/ext/nokogiri/xml_entity_decl.c +32 -30
- data/ext/nokogiri/xml_entity_reference.c +16 -18
- data/ext/nokogiri/xml_namespace.c +56 -49
- data/ext/nokogiri/xml_node.c +338 -286
- data/ext/nokogiri/xml_node_set.c +168 -156
- data/ext/nokogiri/xml_processing_instruction.c +17 -19
- data/ext/nokogiri/xml_reader.c +191 -157
- data/ext/nokogiri/xml_relax_ng.c +29 -23
- data/ext/nokogiri/xml_sax_parser.c +117 -112
- data/ext/nokogiri/xml_sax_parser_context.c +100 -85
- data/ext/nokogiri/xml_sax_push_parser.c +34 -27
- data/ext/nokogiri/xml_schema.c +48 -42
- data/ext/nokogiri/xml_syntax_error.c +21 -23
- data/ext/nokogiri/xml_text.c +13 -17
- data/ext/nokogiri/xml_xpath_context.c +134 -127
- data/ext/nokogiri/xslt_stylesheet.c +157 -157
- data/lib/nokogiri.rb +1 -22
- data/lib/nokogiri/css/parser.rb +1 -1
- data/lib/nokogiri/extension.rb +26 -0
- data/lib/nokogiri/html/document_fragment.rb +15 -15
- data/lib/nokogiri/nokogiri.jar +0 -0
- data/lib/nokogiri/version/constant.rb +1 -1
- data/lib/nokogiri/version/info.rb +31 -8
- data/lib/nokogiri/xml/document.rb +31 -11
- data/lib/nokogiri/xml/node.rb +38 -42
- data/lib/nokogiri/xml/reader.rb +2 -9
- data/lib/nokogiri/xml/xpath.rb +1 -3
- data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
- metadata +7 -8
- data/ext/nokogiri/xml_io.c +0 -63
- 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
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
84
|
+
private TransformerFactory factory = null;
|
85
|
+
private Templates sheet = null;
|
86
|
+
private IRubyObject stylesheet = null;
|
87
|
+
private boolean htmlish = false;
|
87
88
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
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
|
-
|
103
|
-
|
107
|
+
private void
|
108
|
+
addParametersToTransformer(ThreadContext context, Transformer transf, IRubyObject parameters)
|
109
|
+
{
|
110
|
+
Ruby runtime = context.getRuntime();
|
104
111
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
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
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
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
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
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
|
-
|
161
|
+
Ruby runtime = context.getRuntime();
|
149
162
|
|
150
|
-
|
151
|
-
ensureDocumentHasNoError(context, xmlDoc);
|
152
|
-
|
153
|
-
Document doc = ((XmlDocument) xmlDoc.dup_implementation(context, true)).getDocument();
|
163
|
+
ensureFirstArgIsDocument(runtime, args[0]);
|
154
164
|
|
155
|
-
|
156
|
-
|
165
|
+
XmlDocument xmlDoc = (XmlDocument) args[0];
|
166
|
+
ensureDocumentHasNoError(context, xmlDoc);
|
157
167
|
|
158
|
-
|
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
|
-
|
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
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
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
|
-
|
176
|
-
|
177
|
-
|
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
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
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
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
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
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
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
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
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
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
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
|
-
|
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
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
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
|
-
|
333
|
-
|
334
|
-
|
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
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
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
|
}
|