nokogiri 1.5.0.beta.4 → 1.5.0
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.
- data/.gemtest +0 -0
- data/CHANGELOG.ja.rdoc +34 -0
- data/CHANGELOG.rdoc +40 -1
- data/Manifest.txt +11 -2
- data/README.rdoc +1 -1
- data/Rakefile +100 -104
- data/bin/nokogiri +1 -2
- data/ext/nokogiri/nokogiri.c +24 -1
- data/ext/nokogiri/xml_io.c +32 -7
- data/ext/nokogiri/xml_node.c +14 -13
- data/ext/nokogiri/xml_sax_parser.c +9 -4
- data/ext/nokogiri/xslt_stylesheet.c +7 -1
- data/lib/nokogiri.rb +3 -22
- data/lib/nokogiri/css.rb +4 -0
- data/lib/nokogiri/html/document.rb +10 -14
- data/lib/nokogiri/version.rb +76 -23
- data/lib/nokogiri/xml/builder.rb +7 -0
- data/lib/nokogiri/xml/document.rb +17 -1
- data/lib/nokogiri/xml/document_fragment.rb +14 -0
- data/lib/nokogiri/xml/node.rb +36 -28
- data/lib/nokogiri/xml/node/save_options.rb +17 -1
- data/lib/nokogiri/xml/node_set.rb +7 -0
- data/lib/nokogiri/xml/parse_options.rb +8 -0
- data/lib/nokogiri/xml/reader.rb +6 -6
- data/lib/nokogiri/xml/schema.rb +7 -1
- data/nokogiri_help_responses.md +40 -0
- data/tasks/cross_compile.rb +134 -159
- data/tasks/nokogiri.org.rb +18 -0
- data/tasks/test.rb +1 -1
- data/test/files/encoding.html +82 -0
- data/test/files/encoding.xhtml +84 -0
- data/test/files/metacharset.html +10 -0
- data/test/files/noencoding.html +47 -0
- data/test/helper.rb +2 -0
- data/test/html/test_document.rb +15 -0
- data/test/html/test_document_encoding.rb +13 -0
- data/test/test_memory_leak.rb +20 -0
- data/test/test_reader.rb +22 -0
- data/test/test_xslt_transforms.rb +6 -2
- data/test/xml/node/test_save_options.rb +10 -2
- data/test/xml/test_builder.rb +17 -0
- data/test/xml/test_document.rb +22 -0
- data/test/xml/test_node.rb +19 -1
- data/test/xml/test_node_reparenting.rb +16 -3
- data/test/xml/test_node_set.rb +34 -0
- data/test/xml/test_schema.rb +5 -0
- data/test/xslt/test_exception_handling.rb +37 -0
- metadata +185 -157
- data/deps.rip +0 -5
- data/ext/java/nokogiri/EncodingHandler.java +0 -124
- data/ext/java/nokogiri/HtmlDocument.java +0 -149
- data/ext/java/nokogiri/HtmlElementDescription.java +0 -145
- data/ext/java/nokogiri/HtmlEntityLookup.java +0 -79
- data/ext/java/nokogiri/HtmlSaxParserContext.java +0 -259
- data/ext/java/nokogiri/NokogiriService.java +0 -535
- data/ext/java/nokogiri/XmlAttr.java +0 -191
- data/ext/java/nokogiri/XmlAttributeDecl.java +0 -130
- data/ext/java/nokogiri/XmlCdata.java +0 -91
- data/ext/java/nokogiri/XmlComment.java +0 -86
- data/ext/java/nokogiri/XmlDocument.java +0 -529
- data/ext/java/nokogiri/XmlDocumentFragment.java +0 -217
- data/ext/java/nokogiri/XmlDtd.java +0 -467
- data/ext/java/nokogiri/XmlElement.java +0 -222
- data/ext/java/nokogiri/XmlElementContent.java +0 -382
- data/ext/java/nokogiri/XmlElementDecl.java +0 -148
- data/ext/java/nokogiri/XmlEntityDecl.java +0 -162
- data/ext/java/nokogiri/XmlEntityReference.java +0 -75
- data/ext/java/nokogiri/XmlNamespace.java +0 -128
- data/ext/java/nokogiri/XmlNode.java +0 -1399
- data/ext/java/nokogiri/XmlNodeSet.java +0 -311
- data/ext/java/nokogiri/XmlProcessingInstruction.java +0 -103
- data/ext/java/nokogiri/XmlReader.java +0 -411
- data/ext/java/nokogiri/XmlRelaxng.java +0 -144
- data/ext/java/nokogiri/XmlSaxParserContext.java +0 -367
- data/ext/java/nokogiri/XmlSaxPushParser.java +0 -184
- data/ext/java/nokogiri/XmlSchema.java +0 -319
- data/ext/java/nokogiri/XmlSyntaxError.java +0 -119
- data/ext/java/nokogiri/XmlText.java +0 -136
- data/ext/java/nokogiri/XmlXpathContext.java +0 -179
- data/ext/java/nokogiri/XsltStylesheet.java +0 -183
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +0 -206
- data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +0 -73
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +0 -86
- data/ext/java/nokogiri/internals/NokogiriHandler.java +0 -327
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +0 -582
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +0 -171
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +0 -118
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +0 -74
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +0 -121
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +0 -79
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +0 -126
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +0 -56
- data/ext/java/nokogiri/internals/ParserContext.java +0 -278
- data/ext/java/nokogiri/internals/PushInputStream.java +0 -411
- data/ext/java/nokogiri/internals/ReaderNode.java +0 -474
- data/ext/java/nokogiri/internals/SaveContext.java +0 -288
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +0 -76
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +0 -42
- data/ext/java/nokogiri/internals/XmlDomParser.java +0 -77
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +0 -238
- data/ext/java/nokogiri/internals/XmlSaxParser.java +0 -65
- data/ext/java/nokogiri/internals/XsltExtensionFunction.java +0 -72
- data/lib/isorelax.jar +0 -0
- data/lib/jing.jar +0 -0
- data/lib/nekodtd.jar +0 -0
- data/lib/nekohtml.jar +0 -0
- data/lib/xercesImpl.jar +0 -0
@@ -1,184 +0,0 @@
|
|
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
|
-
package nokogiri;
|
34
|
-
|
35
|
-
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
|
36
|
-
import static org.jruby.javasupport.util.RuntimeHelpers.invoke;
|
37
|
-
|
38
|
-
import java.io.IOException;
|
39
|
-
import java.io.InputStream;
|
40
|
-
import java.nio.channels.ClosedChannelException;
|
41
|
-
|
42
|
-
import nokogiri.internals.ParserContext;
|
43
|
-
import nokogiri.internals.PushInputStream;
|
44
|
-
|
45
|
-
import org.jruby.Ruby;
|
46
|
-
import org.jruby.RubyClass;
|
47
|
-
import org.jruby.RubyException;
|
48
|
-
import org.jruby.RubyObject;
|
49
|
-
import org.jruby.RubyString;
|
50
|
-
import org.jruby.anno.JRubyClass;
|
51
|
-
import org.jruby.anno.JRubyMethod;
|
52
|
-
import org.jruby.exceptions.RaiseException;
|
53
|
-
import org.jruby.runtime.ThreadContext;
|
54
|
-
import org.jruby.runtime.builtin.IRubyObject;
|
55
|
-
|
56
|
-
/**
|
57
|
-
* Class for Nokogiri::XML::SAX::PushParser
|
58
|
-
*
|
59
|
-
* @author Patrick Mahoney <pat@polycrystal.org>
|
60
|
-
* @author Yoko Harada <yokolet@gmail.com>
|
61
|
-
*/
|
62
|
-
@JRubyClass(name="Nokogiri::XML::SAX::PushParser")
|
63
|
-
public class XmlSaxPushParser extends RubyObject {
|
64
|
-
ParserContext.Options options;
|
65
|
-
IRubyObject optionsRuby;
|
66
|
-
PushInputStream stream;
|
67
|
-
Thread reader;
|
68
|
-
Runner runner;
|
69
|
-
|
70
|
-
public XmlSaxPushParser(Ruby ruby, RubyClass rubyClass) {
|
71
|
-
super(ruby, rubyClass);
|
72
|
-
}
|
73
|
-
|
74
|
-
@JRubyMethod
|
75
|
-
public IRubyObject initialize_native(final ThreadContext context,
|
76
|
-
IRubyObject _saxParser,
|
77
|
-
IRubyObject fileName) {
|
78
|
-
optionsRuby = invoke(context,
|
79
|
-
context.getRuntime().getClassFromPath("Nokogiri::XML::ParseOptions"),
|
80
|
-
"new");
|
81
|
-
options = new ParserContext.Options(0);
|
82
|
-
stream = new PushInputStream();
|
83
|
-
|
84
|
-
runner = new Runner(context, this, stream);
|
85
|
-
reader = new Thread(runner);
|
86
|
-
reader.start();
|
87
|
-
|
88
|
-
return this;
|
89
|
-
}
|
90
|
-
|
91
|
-
/**
|
92
|
-
* Returns an integer.
|
93
|
-
*/
|
94
|
-
@JRubyMethod(name="options")
|
95
|
-
public IRubyObject getOptions(ThreadContext context) {
|
96
|
-
return invoke(context, optionsRuby, "options");
|
97
|
-
}
|
98
|
-
|
99
|
-
/**
|
100
|
-
* <code>val</code> is an integer.
|
101
|
-
*/
|
102
|
-
@JRubyMethod(name="options=")
|
103
|
-
public IRubyObject setOptions(ThreadContext context, IRubyObject val) {
|
104
|
-
invoke(context, optionsRuby, "options=", val);
|
105
|
-
options =
|
106
|
-
new ParserContext.Options(val.convertToInteger().getLongValue());
|
107
|
-
return getOptions(context);
|
108
|
-
}
|
109
|
-
|
110
|
-
@JRubyMethod
|
111
|
-
public IRubyObject native_write(ThreadContext context, IRubyObject chunk,
|
112
|
-
IRubyObject isLast) {
|
113
|
-
byte[] data = null;
|
114
|
-
if (chunk instanceof RubyString || chunk.respondsTo("to_str")) {
|
115
|
-
data = chunk.convertToString().getBytes();
|
116
|
-
} else {
|
117
|
-
XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SyntaxError"));
|
118
|
-
throw new RaiseException(xmlSyntaxError);
|
119
|
-
}
|
120
|
-
|
121
|
-
int errorCount0 = runner.getErrorCount();
|
122
|
-
|
123
|
-
try {
|
124
|
-
stream.writeAndWaitForRead(data);
|
125
|
-
} catch (ClosedChannelException e) {
|
126
|
-
// ignore
|
127
|
-
} catch (IOException e) {
|
128
|
-
throw context.getRuntime().newRuntimeError(e.toString());
|
129
|
-
}
|
130
|
-
|
131
|
-
if (isLast.isTrue()) {
|
132
|
-
try {
|
133
|
-
stream.close();
|
134
|
-
} catch (IOException e) {
|
135
|
-
// ignore
|
136
|
-
}
|
137
|
-
|
138
|
-
for (;;) {
|
139
|
-
try {
|
140
|
-
reader.join();
|
141
|
-
break;
|
142
|
-
} catch (InterruptedException e) {
|
143
|
-
// continue loop
|
144
|
-
}
|
145
|
-
}
|
146
|
-
}
|
147
|
-
|
148
|
-
if (!options.recover && runner.getErrorCount() > errorCount0) {
|
149
|
-
throw new RaiseException(runner.getLastError(), true);
|
150
|
-
}
|
151
|
-
|
152
|
-
return this;
|
153
|
-
}
|
154
|
-
|
155
|
-
protected static class Runner implements Runnable {
|
156
|
-
protected ThreadContext context;
|
157
|
-
protected IRubyObject handler;
|
158
|
-
protected XmlSaxParserContext parser;
|
159
|
-
|
160
|
-
public Runner(ThreadContext context,
|
161
|
-
IRubyObject handler,
|
162
|
-
InputStream stream) {
|
163
|
-
RubyClass klazz = getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SAX::ParserContext");
|
164
|
-
|
165
|
-
this.context = context;
|
166
|
-
this.handler = handler;
|
167
|
-
this.parser = (XmlSaxParserContext) XmlSaxParserContext.parse_stream(context, klazz, stream);
|
168
|
-
}
|
169
|
-
|
170
|
-
public void run() {
|
171
|
-
parser.parse_with(context, handler);
|
172
|
-
}
|
173
|
-
|
174
|
-
public int getErrorCount() {
|
175
|
-
// check for null because thread may nto have started yet
|
176
|
-
if (parser.getNokogiriHandler() == null) return 0;
|
177
|
-
else return parser.getNokogiriHandler().getErrorCount();
|
178
|
-
}
|
179
|
-
|
180
|
-
public RubyException getLastError() {
|
181
|
-
return (RubyException) parser.getNokogiriHandler().getLastError();
|
182
|
-
}
|
183
|
-
}
|
184
|
-
}
|
@@ -1,319 +0,0 @@
|
|
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
|
-
package nokogiri;
|
34
|
-
|
35
|
-
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
|
36
|
-
|
37
|
-
import java.io.IOException;
|
38
|
-
import java.io.InputStream;
|
39
|
-
import java.io.Reader;
|
40
|
-
import java.io.StringReader;
|
41
|
-
|
42
|
-
import javax.xml.XMLConstants;
|
43
|
-
import javax.xml.transform.Source;
|
44
|
-
import javax.xml.transform.dom.DOMSource;
|
45
|
-
import javax.xml.transform.stream.StreamSource;
|
46
|
-
import javax.xml.validation.Schema;
|
47
|
-
import javax.xml.validation.SchemaFactory;
|
48
|
-
import javax.xml.validation.Validator;
|
49
|
-
|
50
|
-
import nokogiri.internals.SchemaErrorHandler;
|
51
|
-
import nokogiri.internals.XmlDomParserContext;
|
52
|
-
|
53
|
-
import org.jruby.Ruby;
|
54
|
-
import org.jruby.RubyArray;
|
55
|
-
import org.jruby.RubyClass;
|
56
|
-
import org.jruby.RubyFixnum;
|
57
|
-
import org.jruby.RubyObject;
|
58
|
-
import org.jruby.anno.JRubyClass;
|
59
|
-
import org.jruby.anno.JRubyMethod;
|
60
|
-
import org.jruby.exceptions.RaiseException;
|
61
|
-
import org.jruby.runtime.ThreadContext;
|
62
|
-
import org.jruby.runtime.Visibility;
|
63
|
-
import org.jruby.runtime.builtin.IRubyObject;
|
64
|
-
import org.w3c.dom.Document;
|
65
|
-
import org.w3c.dom.ls.LSInput;
|
66
|
-
import org.w3c.dom.ls.LSResourceResolver;
|
67
|
-
import org.xml.sax.ErrorHandler;
|
68
|
-
import org.xml.sax.SAXException;
|
69
|
-
|
70
|
-
/**
|
71
|
-
* Class for Nokogiri::XML::Schema
|
72
|
-
*
|
73
|
-
* @author sergio
|
74
|
-
* @author Yoko Harada <yokolet@gmail.com>
|
75
|
-
*/
|
76
|
-
@JRubyClass(name="Nokogiri::XML::Schema")
|
77
|
-
public class XmlSchema extends RubyObject {
|
78
|
-
private Validator validator;
|
79
|
-
|
80
|
-
public XmlSchema(Ruby ruby, RubyClass klazz) {
|
81
|
-
super(ruby, klazz);
|
82
|
-
}
|
83
|
-
|
84
|
-
/**
|
85
|
-
* Create and return a copy of this object.
|
86
|
-
*
|
87
|
-
* @return a clone of this object
|
88
|
-
*/
|
89
|
-
@Override
|
90
|
-
public Object clone() throws CloneNotSupportedException {
|
91
|
-
return super.clone();
|
92
|
-
}
|
93
|
-
|
94
|
-
private Schema getSchema(Source source, String currentDir) throws SAXException {
|
95
|
-
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
96
|
-
SchemaResourceResolver resourceResolver = new SchemaResourceResolver(currentDir, null);
|
97
|
-
schemaFactory.setResourceResolver(resourceResolver);
|
98
|
-
return schemaFactory.newSchema(source);
|
99
|
-
}
|
100
|
-
|
101
|
-
private void setValidator(Validator validator) {
|
102
|
-
this.validator = validator;
|
103
|
-
}
|
104
|
-
|
105
|
-
static XmlSchema createSchemaInstance(ThreadContext context, RubyClass klazz, Source source) {
|
106
|
-
Ruby runtime = context.getRuntime();
|
107
|
-
XmlSchema xmlSchema = (XmlSchema) NokogiriService.XML_SCHEMA_ALLOCATOR.allocate(runtime, klazz);
|
108
|
-
xmlSchema.setInstanceVariable("@errors", runtime.newEmptyArray());
|
109
|
-
|
110
|
-
try {
|
111
|
-
Schema schema = xmlSchema.getSchema(source, context.getRuntime().getCurrentDirectory());
|
112
|
-
xmlSchema.setValidator(schema.newValidator());
|
113
|
-
return xmlSchema;
|
114
|
-
} catch (SAXException ex) {
|
115
|
-
throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
|
116
|
-
}
|
117
|
-
}
|
118
|
-
|
119
|
-
/*
|
120
|
-
* call-seq:
|
121
|
-
* from_document(doc)
|
122
|
-
*
|
123
|
-
* Create a new Schema from the Nokogiri::XML::Document +doc+
|
124
|
-
*/
|
125
|
-
@JRubyMethod(meta=true)
|
126
|
-
public static IRubyObject from_document(ThreadContext context, IRubyObject klazz, IRubyObject document) {
|
127
|
-
XmlDocument doc = ((XmlDocument) ((XmlNode) document).document(context));
|
128
|
-
|
129
|
-
RubyArray errors = (RubyArray) doc.getInstanceVariable("@errors");
|
130
|
-
if (!errors.isEmpty()) {
|
131
|
-
throw new RaiseException((XmlSyntaxError) errors.first());
|
132
|
-
}
|
133
|
-
|
134
|
-
DOMSource source = new DOMSource(doc.getDocument());
|
135
|
-
|
136
|
-
IRubyObject uri = doc.url(context);
|
137
|
-
|
138
|
-
if (!uri.isNil()) {
|
139
|
-
source.setSystemId(uri.convertToString().asJavaString());
|
140
|
-
}
|
141
|
-
|
142
|
-
return getSchema(context, (RubyClass)klazz, source);
|
143
|
-
}
|
144
|
-
|
145
|
-
private static IRubyObject getSchema(ThreadContext context, RubyClass klazz, Source source) {
|
146
|
-
String moduleName = klazz.getName();
|
147
|
-
if ("Nokogiri::XML::Schema".equals(moduleName)) {
|
148
|
-
return XmlSchema.createSchemaInstance(context, klazz, source);
|
149
|
-
} else if ("Nokogiri::XML::RelaxNG".equals(moduleName)) {
|
150
|
-
return XmlRelaxng.createSchemaInstance(context, klazz, source);
|
151
|
-
}
|
152
|
-
return context.getRuntime().getNil();
|
153
|
-
}
|
154
|
-
|
155
|
-
@JRubyMethod(meta=true)
|
156
|
-
public static IRubyObject read_memory(ThreadContext context, IRubyObject klazz, IRubyObject content) {
|
157
|
-
String data = content.convertToString().asJavaString();
|
158
|
-
return getSchema(context, (RubyClass) klazz, new StreamSource(new StringReader(data)));
|
159
|
-
}
|
160
|
-
|
161
|
-
@JRubyMethod(visibility=Visibility.PRIVATE)
|
162
|
-
public IRubyObject validate_document(ThreadContext context, IRubyObject document) {
|
163
|
-
return validate_document_or_file(context, (XmlDocument)document);
|
164
|
-
}
|
165
|
-
|
166
|
-
@JRubyMethod(visibility=Visibility.PRIVATE)
|
167
|
-
public IRubyObject validate_file(ThreadContext context, IRubyObject file) {
|
168
|
-
Ruby ruby = context.getRuntime();
|
169
|
-
|
170
|
-
XmlDomParserContext ctx = new XmlDomParserContext(ruby, RubyFixnum.newFixnum(ruby, 1L));
|
171
|
-
ctx.setInputSource(context, file);
|
172
|
-
XmlDocument xmlDocument = ctx.parse(context, getNokogiriClass(ruby, "Nokogiri::XML::Document"), ruby.getNil());
|
173
|
-
return validate_document_or_file(context, xmlDocument);
|
174
|
-
}
|
175
|
-
|
176
|
-
IRubyObject validate_document_or_file(ThreadContext context, XmlDocument xmlDocument) {
|
177
|
-
RubyArray errors = (RubyArray) this.getInstanceVariable("@errors");
|
178
|
-
ErrorHandler errorHandler = new SchemaErrorHandler(context.getRuntime(), errors);
|
179
|
-
setErrorHandler(errorHandler);
|
180
|
-
|
181
|
-
try {
|
182
|
-
validate(xmlDocument.getDocument());
|
183
|
-
} catch(SAXException ex) {
|
184
|
-
XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SyntaxError"));
|
185
|
-
xmlSyntaxError.setException(ex);
|
186
|
-
errors.append(xmlSyntaxError);
|
187
|
-
} catch (IOException ex) {
|
188
|
-
throw context.getRuntime().newIOError(ex.getMessage());
|
189
|
-
}
|
190
|
-
|
191
|
-
return errors;
|
192
|
-
}
|
193
|
-
|
194
|
-
protected void setErrorHandler(ErrorHandler errorHandler) {
|
195
|
-
validator.setErrorHandler(errorHandler);
|
196
|
-
}
|
197
|
-
|
198
|
-
protected void validate(Document document) throws SAXException, IOException {
|
199
|
-
DOMSource docSource = new DOMSource(document);
|
200
|
-
validator.validate(docSource);
|
201
|
-
}
|
202
|
-
|
203
|
-
private class SchemaResourceResolver implements LSResourceResolver {
|
204
|
-
SchemaLSInput lsInput = new SchemaLSInput();
|
205
|
-
String defaultURI;
|
206
|
-
|
207
|
-
SchemaResourceResolver(String currentDir, Object input) {
|
208
|
-
defaultURI = currentDir + "/nokogiri_default_fake.xsd";
|
209
|
-
if (input == null) return;
|
210
|
-
if (input instanceof String) {
|
211
|
-
lsInput.setStringData((String)input);
|
212
|
-
} else if (input instanceof Reader) {
|
213
|
-
lsInput.setCharacterStream((Reader)input);
|
214
|
-
} else if (input instanceof InputStream) {
|
215
|
-
lsInput.setByteStream((InputStream)input);
|
216
|
-
}
|
217
|
-
}
|
218
|
-
|
219
|
-
@Override
|
220
|
-
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
|
221
|
-
lsInput.setPublicId(publicId);
|
222
|
-
lsInput.setSystemId(systemId);
|
223
|
-
lsInput.setBaseURI(baseURI != null ? baseURI : defaultURI);
|
224
|
-
return lsInput;
|
225
|
-
}
|
226
|
-
}
|
227
|
-
|
228
|
-
private class SchemaLSInput implements LSInput {
|
229
|
-
protected String fPublicId;
|
230
|
-
protected String fSystemId;
|
231
|
-
protected String fBaseSystemId;
|
232
|
-
protected InputStream fByteStream;
|
233
|
-
protected Reader fCharStream;
|
234
|
-
protected String fData;
|
235
|
-
protected String fEncoding;
|
236
|
-
protected boolean fCertifiedText = false;
|
237
|
-
|
238
|
-
@Override
|
239
|
-
public String getBaseURI() {
|
240
|
-
return fBaseSystemId;
|
241
|
-
}
|
242
|
-
|
243
|
-
@Override
|
244
|
-
public InputStream getByteStream() {
|
245
|
-
return fByteStream;
|
246
|
-
}
|
247
|
-
|
248
|
-
@Override
|
249
|
-
public boolean getCertifiedText() {
|
250
|
-
return fCertifiedText;
|
251
|
-
}
|
252
|
-
|
253
|
-
@Override
|
254
|
-
public Reader getCharacterStream() {
|
255
|
-
return fCharStream;
|
256
|
-
}
|
257
|
-
|
258
|
-
@Override
|
259
|
-
public String getEncoding() {
|
260
|
-
return fEncoding;
|
261
|
-
}
|
262
|
-
|
263
|
-
@Override
|
264
|
-
public String getPublicId() {
|
265
|
-
return fPublicId;
|
266
|
-
}
|
267
|
-
|
268
|
-
@Override
|
269
|
-
public String getStringData() {
|
270
|
-
return fData;
|
271
|
-
}
|
272
|
-
|
273
|
-
@Override
|
274
|
-
public String getSystemId() {
|
275
|
-
return fSystemId;
|
276
|
-
}
|
277
|
-
|
278
|
-
@Override
|
279
|
-
public void setBaseURI(String baseURI) {
|
280
|
-
fBaseSystemId = baseURI;
|
281
|
-
}
|
282
|
-
|
283
|
-
@Override
|
284
|
-
public void setByteStream(InputStream byteStream) {
|
285
|
-
fByteStream = byteStream;
|
286
|
-
}
|
287
|
-
|
288
|
-
@Override
|
289
|
-
public void setCertifiedText(boolean certified) {
|
290
|
-
fCertifiedText = certified;
|
291
|
-
}
|
292
|
-
|
293
|
-
@Override
|
294
|
-
public void setCharacterStream(Reader charStream) {
|
295
|
-
fCharStream = charStream;
|
296
|
-
}
|
297
|
-
|
298
|
-
@Override
|
299
|
-
public void setEncoding(String encoding) {
|
300
|
-
fEncoding = encoding;
|
301
|
-
}
|
302
|
-
|
303
|
-
@Override
|
304
|
-
public void setPublicId(String pubId) {
|
305
|
-
fPublicId = pubId;
|
306
|
-
}
|
307
|
-
|
308
|
-
@Override
|
309
|
-
public void setStringData(String stringData) {
|
310
|
-
fData = stringData;
|
311
|
-
}
|
312
|
-
|
313
|
-
@Override
|
314
|
-
public void setSystemId(String sysId) {
|
315
|
-
fSystemId = sysId;
|
316
|
-
}
|
317
|
-
|
318
|
-
}
|
319
|
-
}
|