nokogiri 1.11.0.rc4-java → 1.11.5-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/Gemfile +3 -0
- data/LICENSE-DEPENDENCIES.md +12 -12
- data/LICENSE.md +1 -1
- data/README.md +168 -91
- data/dependencies.yml +12 -12
- data/ext/java/nokogiri/EncodingHandler.java +76 -89
- data/ext/java/nokogiri/HtmlDocument.java +135 -144
- data/ext/java/nokogiri/HtmlElementDescription.java +102 -117
- data/ext/java/nokogiri/HtmlEntityLookup.java +33 -60
- data/ext/java/nokogiri/HtmlSaxParserContext.java +218 -222
- data/ext/java/nokogiri/HtmlSaxPushParser.java +162 -169
- data/ext/java/nokogiri/NokogiriService.java +595 -556
- data/ext/java/nokogiri/XmlAttr.java +118 -126
- data/ext/java/nokogiri/XmlAttributeDecl.java +95 -106
- data/ext/java/nokogiri/XmlCdata.java +35 -58
- data/ext/java/nokogiri/XmlComment.java +46 -67
- data/ext/java/nokogiri/XmlDocument.java +645 -572
- data/ext/java/nokogiri/XmlDocumentFragment.java +125 -137
- data/ext/java/nokogiri/XmlDtd.java +448 -414
- data/ext/java/nokogiri/XmlElement.java +23 -48
- data/ext/java/nokogiri/XmlElementContent.java +343 -316
- data/ext/java/nokogiri/XmlElementDecl.java +124 -125
- data/ext/java/nokogiri/XmlEntityDecl.java +119 -127
- data/ext/java/nokogiri/XmlEntityReference.java +49 -72
- data/ext/java/nokogiri/XmlNamespace.java +175 -175
- data/ext/java/nokogiri/XmlNode.java +1843 -1620
- data/ext/java/nokogiri/XmlNodeSet.java +361 -331
- data/ext/java/nokogiri/XmlProcessingInstruction.java +47 -69
- data/ext/java/nokogiri/XmlReader.java +513 -450
- data/ext/java/nokogiri/XmlRelaxng.java +85 -104
- data/ext/java/nokogiri/XmlSaxParserContext.java +328 -315
- data/ext/java/nokogiri/XmlSaxPushParser.java +227 -220
- data/ext/java/nokogiri/XmlSchema.java +328 -295
- data/ext/java/nokogiri/XmlSyntaxError.java +113 -115
- data/ext/java/nokogiri/XmlText.java +55 -76
- data/ext/java/nokogiri/XmlXpathContext.java +240 -238
- data/ext/java/nokogiri/XsltStylesheet.java +280 -269
- data/ext/java/nokogiri/internals/ClosedStreamException.java +5 -2
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +201 -202
- 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 +63 -80
- data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +107 -88
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +27 -52
- data/ext/java/nokogiri/internals/NokogiriHandler.java +316 -286
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +736 -652
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +184 -173
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +81 -98
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +64 -79
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +84 -99
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +48 -65
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +116 -131
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -56
- data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +23 -46
- data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +55 -72
- data/ext/java/nokogiri/internals/ParserContext.java +206 -211
- data/ext/java/nokogiri/internals/ReaderNode.java +478 -403
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +822 -739
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +31 -54
- data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +129 -123
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +3 -34
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +206 -207
- data/ext/java/nokogiri/internals/XmlSaxParser.java +22 -47
- 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 -574
- data/ext/nokogiri/depend +34 -474
- data/ext/nokogiri/extconf.rb +270 -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 +67 -64
- data/ext/nokogiri/html_sax_push_parser.c +42 -34
- data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
- data/ext/nokogiri/nokogiri.c +190 -60
- data/ext/nokogiri/test_global_handlers.c +40 -0
- 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 +246 -188
- 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 +371 -320
- 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 +101 -84
- data/ext/nokogiri/xml_sax_push_parser.c +36 -27
- data/ext/nokogiri/xml_schema.c +48 -42
- data/ext/nokogiri/xml_syntax_error.c +42 -21
- 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 +2 -6
- 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 +32 -8
- data/lib/nokogiri/xml/document.rb +74 -28
- data/lib/nokogiri/xml/node.rb +39 -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 +62 -127
- data/ext/nokogiri/xml_io.c +0 -63
- 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
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
52
|
+
private TransformerFactory factory = null;
|
53
|
+
private Templates sheet = null;
|
54
|
+
private IRubyObject stylesheet = null;
|
55
|
+
private boolean htmlish = false;
|
87
56
|
|
88
|
-
|
89
|
-
|
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
|
-
|
103
|
-
|
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
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
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
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
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
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
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
|
-
|
129
|
+
Ruby runtime = context.getRuntime();
|
149
130
|
|
150
|
-
|
151
|
-
ensureDocumentHasNoError(context, xmlDoc);
|
152
|
-
|
153
|
-
Document doc = ((XmlDocument) xmlDoc.dup_implementation(context, true)).getDocument();
|
131
|
+
ensureFirstArgIsDocument(runtime, args[0]);
|
154
132
|
|
155
|
-
|
156
|
-
|
133
|
+
XmlDocument xmlDoc = (XmlDocument) args[0];
|
134
|
+
ensureDocumentHasNoError(context, xmlDoc);
|
157
135
|
|
158
|
-
|
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
|
-
|
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
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
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
|
-
|
176
|
-
|
177
|
-
|
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
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
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
|
-
|
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());
|
224
|
+
if (stringResult == null) {
|
225
|
+
return createDocumentFromDomResult(context, runtime, result);
|
226
|
+
} else {
|
227
|
+
return createDocumentFromString(context, runtime, stringResult);
|
197
228
|
}
|
229
|
+
}
|
198
230
|
|
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
|
-
}
|
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
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
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
|
-
|
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);
|
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
|
-
|
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
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
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
|
-
|
333
|
-
|
334
|
-
|
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
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
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
|
}
|