nokogiri-backport 1.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/LICENSE-DEPENDENCIES.md +1682 -0
- data/LICENSE.md +9 -0
- data/README.md +272 -0
- data/bin/nokogiri +118 -0
- data/dependencies.yml +74 -0
- data/ext/java/nokogiri/EncodingHandler.java +124 -0
- data/ext/java/nokogiri/HtmlDocument.java +178 -0
- data/ext/java/nokogiri/HtmlElementDescription.java +148 -0
- data/ext/java/nokogiri/HtmlEntityLookup.java +79 -0
- data/ext/java/nokogiri/HtmlSaxParserContext.java +282 -0
- data/ext/java/nokogiri/HtmlSaxPushParser.java +222 -0
- data/ext/java/nokogiri/NokogiriService.java +597 -0
- data/ext/java/nokogiri/XmlAttr.java +162 -0
- data/ext/java/nokogiri/XmlAttributeDecl.java +129 -0
- data/ext/java/nokogiri/XmlCdata.java +82 -0
- data/ext/java/nokogiri/XmlComment.java +97 -0
- data/ext/java/nokogiri/XmlDocument.java +633 -0
- data/ext/java/nokogiri/XmlDocumentFragment.java +185 -0
- data/ext/java/nokogiri/XmlDtd.java +481 -0
- data/ext/java/nokogiri/XmlElement.java +68 -0
- data/ext/java/nokogiri/XmlElementContent.java +382 -0
- data/ext/java/nokogiri/XmlElementDecl.java +147 -0
- data/ext/java/nokogiri/XmlEntityDecl.java +157 -0
- data/ext/java/nokogiri/XmlEntityReference.java +101 -0
- data/ext/java/nokogiri/XmlNamespace.java +199 -0
- data/ext/java/nokogiri/XmlNode.java +1684 -0
- data/ext/java/nokogiri/XmlNodeSet.java +434 -0
- data/ext/java/nokogiri/XmlProcessingInstruction.java +100 -0
- data/ext/java/nokogiri/XmlReader.java +531 -0
- data/ext/java/nokogiri/XmlRelaxng.java +151 -0
- data/ext/java/nokogiri/XmlSaxParserContext.java +374 -0
- data/ext/java/nokogiri/XmlSaxPushParser.java +286 -0
- data/ext/java/nokogiri/XmlSchema.java +388 -0
- data/ext/java/nokogiri/XmlSyntaxError.java +138 -0
- data/ext/java/nokogiri/XmlText.java +110 -0
- data/ext/java/nokogiri/XmlXpathContext.java +301 -0
- data/ext/java/nokogiri/XsltStylesheet.java +347 -0
- data/ext/java/nokogiri/internals/ClosedStreamException.java +10 -0
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +252 -0
- data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +20 -0
- data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +151 -0
- data/ext/java/nokogiri/internals/NokogiriDomParser.java +116 -0
- data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +121 -0
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +69 -0
- data/ext/java/nokogiri/internals/NokogiriHandler.java +327 -0
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +734 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +217 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +127 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +100 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +121 -0
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +78 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +180 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +72 -0
- data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +60 -0
- data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +87 -0
- data/ext/java/nokogiri/internals/ParserContext.java +259 -0
- data/ext/java/nokogiri/internals/ReaderNode.java +488 -0
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +778 -0
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +73 -0
- data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +168 -0
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +42 -0
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +274 -0
- data/ext/java/nokogiri/internals/XmlSaxParser.java +65 -0
- data/ext/java/nokogiri/internals/c14n/AttrCompare.java +119 -0
- data/ext/java/nokogiri/internals/c14n/C14nHelper.java +159 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +37 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +93 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +252 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +639 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +38 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +38 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +367 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +295 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +40 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +44 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +44 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +43 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +630 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +173 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +76 -0
- data/ext/java/nokogiri/internals/c14n/Constants.java +42 -0
- data/ext/java/nokogiri/internals/c14n/ElementProxy.java +293 -0
- data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +93 -0
- data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +79 -0
- data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +166 -0
- data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +76 -0
- data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +402 -0
- data/ext/java/nokogiri/internals/c14n/NodeFilter.java +51 -0
- data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +179 -0
- data/ext/java/nokogiri/internals/c14n/XMLUtils.java +507 -0
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1745 -0
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +685 -0
- data/ext/nokogiri/depend +477 -0
- data/ext/nokogiri/extconf.rb +836 -0
- data/ext/nokogiri/html_document.c +171 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +279 -0
- data/ext/nokogiri/html_element_description.h +10 -0
- data/ext/nokogiri/html_entity_lookup.c +32 -0
- data/ext/nokogiri/html_entity_lookup.h +8 -0
- data/ext/nokogiri/html_sax_parser_context.c +116 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/html_sax_push_parser.c +87 -0
- data/ext/nokogiri/html_sax_push_parser.h +9 -0
- data/ext/nokogiri/nokogiri.c +135 -0
- data/ext/nokogiri/nokogiri.h +130 -0
- data/ext/nokogiri/xml_attr.c +103 -0
- data/ext/nokogiri/xml_attr.h +9 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_attribute_decl.h +9 -0
- data/ext/nokogiri/xml_cdata.c +62 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +69 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +622 -0
- data/ext/nokogiri/xml_document.h +23 -0
- data/ext/nokogiri/xml_document_fragment.c +48 -0
- data/ext/nokogiri/xml_document_fragment.h +10 -0
- data/ext/nokogiri/xml_dtd.c +202 -0
- data/ext/nokogiri/xml_dtd.h +10 -0
- data/ext/nokogiri/xml_element_content.c +123 -0
- data/ext/nokogiri/xml_element_content.h +10 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_element_decl.h +9 -0
- data/ext/nokogiri/xml_encoding_handler.c +79 -0
- data/ext/nokogiri/xml_encoding_handler.h +8 -0
- data/ext/nokogiri/xml_entity_decl.c +110 -0
- data/ext/nokogiri/xml_entity_decl.h +10 -0
- data/ext/nokogiri/xml_entity_reference.c +52 -0
- data/ext/nokogiri/xml_entity_reference.h +9 -0
- data/ext/nokogiri/xml_io.c +63 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
- data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
- data/ext/nokogiri/xml_namespace.c +111 -0
- data/ext/nokogiri/xml_namespace.h +14 -0
- data/ext/nokogiri/xml_node.c +1773 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +486 -0
- data/ext/nokogiri/xml_node_set.h +12 -0
- data/ext/nokogiri/xml_processing_instruction.c +56 -0
- data/ext/nokogiri/xml_processing_instruction.h +9 -0
- data/ext/nokogiri/xml_reader.c +657 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +179 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +305 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +262 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +159 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +276 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +64 -0
- data/ext/nokogiri/xml_syntax_error.h +13 -0
- data/ext/nokogiri/xml_text.c +52 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath_context.c +374 -0
- data/ext/nokogiri/xml_xpath_context.h +10 -0
- data/ext/nokogiri/xslt_stylesheet.c +263 -0
- data/ext/nokogiri/xslt_stylesheet.h +14 -0
- 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/nokogiri/css/node.rb +53 -0
- data/lib/nokogiri/css/parser.rb +751 -0
- data/lib/nokogiri/css/parser.y +272 -0
- data/lib/nokogiri/css/parser_extras.rb +94 -0
- data/lib/nokogiri/css/syntax_error.rb +8 -0
- data/lib/nokogiri/css/tokenizer.rb +154 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +260 -0
- data/lib/nokogiri/css.rb +28 -0
- data/lib/nokogiri/decorators/slop.rb +43 -0
- data/lib/nokogiri/html/builder.rb +36 -0
- data/lib/nokogiri/html/document.rb +322 -0
- data/lib/nokogiri/html/document_fragment.rb +50 -0
- data/lib/nokogiri/html/element_description.rb +24 -0
- data/lib/nokogiri/html/element_description_defaults.rb +672 -0
- data/lib/nokogiri/html/entity_lookup.rb +14 -0
- data/lib/nokogiri/html/sax/parser.rb +63 -0
- data/lib/nokogiri/html/sax/parser_context.rb +17 -0
- data/lib/nokogiri/html/sax/push_parser.rb +37 -0
- data/lib/nokogiri/html.rb +38 -0
- data/lib/nokogiri/jruby/dependencies.rb +20 -0
- data/lib/nokogiri/syntax_error.rb +5 -0
- data/lib/nokogiri/version/constant.rb +5 -0
- data/lib/nokogiri/version/info.rb +182 -0
- data/lib/nokogiri/version.rb +3 -0
- data/lib/nokogiri/xml/attr.rb +15 -0
- data/lib/nokogiri/xml/attribute_decl.rb +19 -0
- data/lib/nokogiri/xml/builder.rb +447 -0
- data/lib/nokogiri/xml/cdata.rb +12 -0
- data/lib/nokogiri/xml/character_data.rb +8 -0
- data/lib/nokogiri/xml/document.rb +290 -0
- data/lib/nokogiri/xml/document_fragment.rb +159 -0
- data/lib/nokogiri/xml/dtd.rb +33 -0
- data/lib/nokogiri/xml/element_content.rb +37 -0
- data/lib/nokogiri/xml/element_decl.rb +14 -0
- data/lib/nokogiri/xml/entity_decl.rb +20 -0
- data/lib/nokogiri/xml/entity_reference.rb +19 -0
- data/lib/nokogiri/xml/namespace.rb +14 -0
- data/lib/nokogiri/xml/node/save_options.rb +62 -0
- data/lib/nokogiri/xml/node.rb +1240 -0
- data/lib/nokogiri/xml/node_set.rb +372 -0
- data/lib/nokogiri/xml/notation.rb +7 -0
- data/lib/nokogiri/xml/parse_options.rb +127 -0
- data/lib/nokogiri/xml/pp/character_data.rb +19 -0
- data/lib/nokogiri/xml/pp/node.rb +57 -0
- data/lib/nokogiri/xml/pp.rb +3 -0
- data/lib/nokogiri/xml/processing_instruction.rb +9 -0
- data/lib/nokogiri/xml/reader.rb +116 -0
- data/lib/nokogiri/xml/relax_ng.rb +37 -0
- data/lib/nokogiri/xml/sax/document.rb +172 -0
- data/lib/nokogiri/xml/sax/parser.rb +123 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +17 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +61 -0
- data/lib/nokogiri/xml/sax.rb +5 -0
- data/lib/nokogiri/xml/schema.rb +72 -0
- data/lib/nokogiri/xml/searchable.rb +239 -0
- data/lib/nokogiri/xml/syntax_error.rb +71 -0
- data/lib/nokogiri/xml/text.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +12 -0
- data/lib/nokogiri/xml/xpath.rb +11 -0
- data/lib/nokogiri/xml/xpath_context.rb +17 -0
- data/lib/nokogiri/xml.rb +76 -0
- data/lib/nokogiri/xslt/stylesheet.rb +26 -0
- data/lib/nokogiri/xslt.rb +57 -0
- data/lib/nokogiri.rb +144 -0
- data/lib/serializer.jar +0 -0
- data/lib/xalan.jar +0 -0
- data/lib/xercesImpl.jar +0 -0
- data/lib/xml-apis.jar +0 -0
- data/lib/xsd/xmlparser/nokogiri.rb +103 -0
- metadata +531 -0
@@ -0,0 +1,531 @@
|
|
1
|
+
/**
|
2
|
+
* (The MIT License)
|
3
|
+
*
|
4
|
+
* Copyright (c) 2008 - 2012:
|
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 nokogiri.internals.NokogiriHelpers.stringOrBlank;
|
37
|
+
|
38
|
+
import java.io.IOException;
|
39
|
+
import java.io.InputStream;
|
40
|
+
import java.util.LinkedList;
|
41
|
+
import java.util.List;
|
42
|
+
import java.util.Stack;
|
43
|
+
|
44
|
+
import org.apache.xerces.impl.Constants;
|
45
|
+
import org.apache.xerces.impl.xs.opti.DefaultXMLDocumentHandler;
|
46
|
+
import org.apache.xerces.parsers.StandardParserConfiguration;
|
47
|
+
import org.apache.xerces.util.EntityResolver2Wrapper;
|
48
|
+
import org.apache.xerces.xni.Augmentations;
|
49
|
+
import org.apache.xerces.xni.NamespaceContext;
|
50
|
+
import org.apache.xerces.xni.QName;
|
51
|
+
import org.apache.xerces.xni.XMLAttributes;
|
52
|
+
import org.apache.xerces.xni.XMLLocator;
|
53
|
+
import org.apache.xerces.xni.XMLResourceIdentifier;
|
54
|
+
import org.apache.xerces.xni.XMLString;
|
55
|
+
import org.apache.xerces.xni.XNIException;
|
56
|
+
import org.apache.xerces.xni.parser.XMLErrorHandler;
|
57
|
+
import org.apache.xerces.xni.parser.XMLInputSource;
|
58
|
+
import org.apache.xerces.xni.parser.XMLParseException;
|
59
|
+
import org.apache.xerces.xni.parser.XMLPullParserConfiguration;
|
60
|
+
import org.jruby.Ruby;
|
61
|
+
import org.jruby.RubyArray;
|
62
|
+
import org.jruby.RubyBoolean;
|
63
|
+
import org.jruby.RubyClass;
|
64
|
+
import org.jruby.RubyFixnum;
|
65
|
+
import org.jruby.RubyObject;
|
66
|
+
import org.jruby.anno.JRubyClass;
|
67
|
+
import org.jruby.anno.JRubyMethod;
|
68
|
+
import org.jruby.exceptions.RaiseException;
|
69
|
+
import org.jruby.runtime.Block;
|
70
|
+
import org.jruby.runtime.ThreadContext;
|
71
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
72
|
+
import org.jruby.util.IOInputStream;
|
73
|
+
import org.xml.sax.InputSource;
|
74
|
+
|
75
|
+
import nokogiri.internals.NokogiriEntityResolver;
|
76
|
+
import nokogiri.internals.ParserContext;
|
77
|
+
import nokogiri.internals.ParserContext.Options;
|
78
|
+
import nokogiri.internals.ReaderNode;
|
79
|
+
import nokogiri.internals.ReaderNode.ClosingNode;
|
80
|
+
import nokogiri.internals.ReaderNode.ElementNode;
|
81
|
+
import nokogiri.internals.ReaderNode.TextNode;
|
82
|
+
|
83
|
+
/**
|
84
|
+
* Class for Nokogiri:XML::Reader
|
85
|
+
*
|
86
|
+
* @author sergio
|
87
|
+
* @author Yoko Harada <yokolet@gmail.com>
|
88
|
+
*/
|
89
|
+
@JRubyClass(name="Nokogiri::XML::Reader")
|
90
|
+
public class XmlReader extends RubyObject {
|
91
|
+
|
92
|
+
private static final int XML_TEXTREADER_MODE_INITIAL = 0;
|
93
|
+
private static final int XML_TEXTREADER_MODE_INTERACTIVE = 1;
|
94
|
+
private static final int XML_TEXTREADER_MODE_ERROR = 2;
|
95
|
+
private static final int XML_TEXTREADER_MODE_EOF = 3;
|
96
|
+
private static final int XML_TEXTREADER_MODE_CLOSED = 4;
|
97
|
+
private static final int XML_TEXTREADER_MODE_READING = 5;
|
98
|
+
|
99
|
+
List<ReaderNode> nodeQueue;
|
100
|
+
private int state;
|
101
|
+
private int position = 0;
|
102
|
+
private XMLPullParserConfiguration config;
|
103
|
+
private boolean continueParsing = true;
|
104
|
+
|
105
|
+
public XmlReader(Ruby runtime, RubyClass klazz) {
|
106
|
+
super(runtime, klazz);
|
107
|
+
}
|
108
|
+
|
109
|
+
/**
|
110
|
+
* Create and return a copy of this object.
|
111
|
+
*
|
112
|
+
* @return a clone of this object
|
113
|
+
*/
|
114
|
+
@Override
|
115
|
+
public Object clone() throws CloneNotSupportedException {
|
116
|
+
return super.clone();
|
117
|
+
}
|
118
|
+
|
119
|
+
public void init(Ruby runtime) {
|
120
|
+
nodeQueue = new LinkedList<ReaderNode>();
|
121
|
+
nodeQueue.add(new ReaderNode.EmptyNode(runtime));
|
122
|
+
}
|
123
|
+
|
124
|
+
private void setInput(ThreadContext context, InputStream in, IRubyObject url, Options options){
|
125
|
+
this.setState(XML_TEXTREADER_MODE_READING);
|
126
|
+
config = this.createReader(context.getRuntime(), options);
|
127
|
+
InputSource inputSource = new InputSource();
|
128
|
+
ParserContext.setUrl(context, inputSource, url);
|
129
|
+
XMLInputSource xmlInputSource = new XMLInputSource(inputSource.getPublicId(),
|
130
|
+
inputSource.getSystemId(), null, in, null);
|
131
|
+
try {
|
132
|
+
config.setInputSource(xmlInputSource);
|
133
|
+
} catch (IOException e) {
|
134
|
+
throw context.getRuntime().newRuntimeError(e.getMessage());
|
135
|
+
}
|
136
|
+
this.setState(XML_TEXTREADER_MODE_CLOSED);
|
137
|
+
}
|
138
|
+
|
139
|
+
private void setState(int state) { this.state = state; }
|
140
|
+
|
141
|
+
@JRubyMethod
|
142
|
+
public IRubyObject attribute(ThreadContext context, IRubyObject name) {
|
143
|
+
return currentNode().getAttributeByName(name);
|
144
|
+
}
|
145
|
+
|
146
|
+
@JRubyMethod
|
147
|
+
public IRubyObject attribute_at(ThreadContext context, IRubyObject index) {
|
148
|
+
return currentNode().getAttributeByIndex(index);
|
149
|
+
}
|
150
|
+
|
151
|
+
@JRubyMethod
|
152
|
+
public IRubyObject attribute_count(ThreadContext context) {
|
153
|
+
return currentNode().getAttributeCount();
|
154
|
+
}
|
155
|
+
|
156
|
+
@JRubyMethod
|
157
|
+
public IRubyObject attribute_nodes(ThreadContext context) {
|
158
|
+
return currentNode().getAttributesNodes();
|
159
|
+
}
|
160
|
+
|
161
|
+
@JRubyMethod
|
162
|
+
public IRubyObject attr_nodes(ThreadContext context) {
|
163
|
+
return currentNode().getAttributesNodes();
|
164
|
+
}
|
165
|
+
|
166
|
+
@JRubyMethod(name = "attributes?")
|
167
|
+
public IRubyObject attributes_p(ThreadContext context) {
|
168
|
+
return currentNode().hasAttributes();
|
169
|
+
}
|
170
|
+
|
171
|
+
@JRubyMethod
|
172
|
+
public IRubyObject base_uri(ThreadContext context) {
|
173
|
+
return currentNode().getXmlBase();
|
174
|
+
}
|
175
|
+
|
176
|
+
@JRubyMethod(name="default?")
|
177
|
+
public IRubyObject default_p(ThreadContext context){
|
178
|
+
return currentNode().isDefault();
|
179
|
+
}
|
180
|
+
|
181
|
+
@JRubyMethod
|
182
|
+
public IRubyObject depth(ThreadContext context) {
|
183
|
+
return currentNode().getDepth();
|
184
|
+
}
|
185
|
+
|
186
|
+
@JRubyMethod(name = {"empty_element?", "self_closing?"})
|
187
|
+
public IRubyObject empty_element_p(ThreadContext context) {
|
188
|
+
ReaderNode readerNode = currentNode();
|
189
|
+
ensureNodeClosed(context);
|
190
|
+
|
191
|
+
if (readerNode == null) return context.getRuntime().getNil();
|
192
|
+
if (!(readerNode instanceof ElementNode)) context.getRuntime().getFalse();
|
193
|
+
return RubyBoolean.newBoolean(context.getRuntime(), !readerNode.hasChildren);
|
194
|
+
}
|
195
|
+
|
196
|
+
@JRubyMethod(meta = true, rest = true)
|
197
|
+
public static IRubyObject from_io(ThreadContext context, IRubyObject cls, IRubyObject args[]) {
|
198
|
+
// Only to pass the source test.
|
199
|
+
Ruby runtime = context.getRuntime();
|
200
|
+
// Not nil allowed!
|
201
|
+
if(args[0].isNil()) throw runtime.newArgumentError("io cannot be nil");
|
202
|
+
|
203
|
+
XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
|
204
|
+
reader.init(runtime);
|
205
|
+
reader.setInstanceVariable("@source", args[0]);
|
206
|
+
reader.setInstanceVariable("@errors", runtime.newArray());
|
207
|
+
IRubyObject url = context.nil;
|
208
|
+
if (args.length > 1) url = args[1];
|
209
|
+
if (args.length > 2) reader.setInstanceVariable("@encoding", args[2]);
|
210
|
+
|
211
|
+
Options options;
|
212
|
+
if (args.length > 3) {
|
213
|
+
options = new ParserContext.Options((Long)args[3].toJava(Long.class));
|
214
|
+
} else {
|
215
|
+
// use the default options RECOVER | NONET
|
216
|
+
options = new ParserContext.Options(2048 | 1);
|
217
|
+
}
|
218
|
+
|
219
|
+
InputStream in = new IOInputStream(args[0]);
|
220
|
+
reader.setInput(context, in, url, options);
|
221
|
+
return reader;
|
222
|
+
}
|
223
|
+
|
224
|
+
@JRubyMethod(meta = true, rest = true)
|
225
|
+
public static IRubyObject from_memory(ThreadContext context, IRubyObject cls, IRubyObject args[]) {
|
226
|
+
// args[0]: string, args[1]: url, args[2]: encoding, args[3]: options
|
227
|
+
Ruby runtime = context.getRuntime();
|
228
|
+
// Not nil allowed!
|
229
|
+
if(args[0].isNil()) throw runtime.newArgumentError("string cannot be nil");
|
230
|
+
|
231
|
+
XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
|
232
|
+
reader.init(runtime);
|
233
|
+
reader.setInstanceVariable("@source", args[0]);
|
234
|
+
reader.setInstanceVariable("@errors", runtime.newArray());
|
235
|
+
IRubyObject url = context.nil;
|
236
|
+
if (args.length > 1) url = args[1];
|
237
|
+
if (args.length > 2) reader.setInstanceVariable("@encoding", args[2]);
|
238
|
+
|
239
|
+
Options options;
|
240
|
+
if (args.length > 3) {
|
241
|
+
options = new ParserContext.Options((Long)args[3].toJava(Long.class));
|
242
|
+
} else {
|
243
|
+
// use the default options RECOVER | NONET
|
244
|
+
options = new ParserContext.Options(2048 | 1);
|
245
|
+
}
|
246
|
+
IRubyObject stringIO = runtime.getClass("StringIO").newInstance(context, args[0], Block.NULL_BLOCK);
|
247
|
+
InputStream in = new IOInputStream(stringIO);
|
248
|
+
reader.setInput(context, in, url, options);
|
249
|
+
return reader;
|
250
|
+
}
|
251
|
+
|
252
|
+
@JRubyMethod
|
253
|
+
public IRubyObject node_type(ThreadContext context) {
|
254
|
+
IRubyObject node_type = currentNode().getNodeType();
|
255
|
+
return node_type == null ? RubyFixnum.zero(context.getRuntime()) : node_type;
|
256
|
+
}
|
257
|
+
|
258
|
+
@JRubyMethod
|
259
|
+
public IRubyObject inner_xml(ThreadContext context) {
|
260
|
+
ensureNodeClosed(context);
|
261
|
+
return stringOrBlank(context.getRuntime(), getInnerXml(currentNode()));
|
262
|
+
}
|
263
|
+
|
264
|
+
private String getInnerXml(ReaderNode current) {
|
265
|
+
if (current.depth < 0) return null;
|
266
|
+
if (!current.hasChildren) return null;
|
267
|
+
StringBuffer sb = new StringBuffer();
|
268
|
+
for (int i = current.startOffset + 1; i <= current.endOffset - 1; i++) {
|
269
|
+
sb.append(nodeQueue.get(i).getString());
|
270
|
+
}
|
271
|
+
return new String(sb);
|
272
|
+
}
|
273
|
+
|
274
|
+
@JRubyMethod
|
275
|
+
public IRubyObject outer_xml(ThreadContext context) {
|
276
|
+
ensureNodeClosed(context);
|
277
|
+
return stringOrBlank(context.getRuntime(), getOuterXml());
|
278
|
+
}
|
279
|
+
|
280
|
+
private String getOuterXml() {
|
281
|
+
ReaderNode current = currentNode();
|
282
|
+
if (current == null || current.depth < 0) return null;
|
283
|
+
|
284
|
+
if (current instanceof ClosingNode) {
|
285
|
+
return "<" + current.name + "/>";
|
286
|
+
}
|
287
|
+
|
288
|
+
StringBuilder sb = new StringBuilder();
|
289
|
+
for (int i = position; i <= current.endOffset; i++) {
|
290
|
+
sb.append(nodeQueue.get(i).getString());
|
291
|
+
}
|
292
|
+
return new String(sb);
|
293
|
+
}
|
294
|
+
|
295
|
+
@JRubyMethod
|
296
|
+
public IRubyObject lang(ThreadContext context) {
|
297
|
+
return currentNode().getLang();
|
298
|
+
}
|
299
|
+
|
300
|
+
@JRubyMethod
|
301
|
+
public IRubyObject local_name(ThreadContext context) {
|
302
|
+
return currentNode().getLocalName();
|
303
|
+
}
|
304
|
+
|
305
|
+
@JRubyMethod
|
306
|
+
public IRubyObject name(ThreadContext context) {
|
307
|
+
return currentNode().getName();
|
308
|
+
}
|
309
|
+
|
310
|
+
@JRubyMethod
|
311
|
+
public IRubyObject namespace_uri(ThreadContext context) {
|
312
|
+
return currentNode().getUri();
|
313
|
+
}
|
314
|
+
|
315
|
+
@JRubyMethod
|
316
|
+
public IRubyObject namespaces(ThreadContext context) {
|
317
|
+
return currentNode().getNamespaces(context);
|
318
|
+
}
|
319
|
+
|
320
|
+
@JRubyMethod
|
321
|
+
public IRubyObject prefix(ThreadContext context) {
|
322
|
+
return currentNode().getPrefix();
|
323
|
+
}
|
324
|
+
|
325
|
+
private void readMoreData(ThreadContext context) {
|
326
|
+
if (!continueParsing) throw context.runtime.newRuntimeError("Cannot parse more data");
|
327
|
+
try {
|
328
|
+
continueParsing = config.parse(false);
|
329
|
+
}
|
330
|
+
catch (XNIException e) {
|
331
|
+
throw XmlSyntaxError.createXMLSyntaxError(context.runtime, e).toThrowable(); // Nokogiri::XML::SyntaxError
|
332
|
+
}
|
333
|
+
catch (IOException e) {
|
334
|
+
throw context.runtime.newRuntimeError(e.toString());
|
335
|
+
}
|
336
|
+
}
|
337
|
+
|
338
|
+
private void ensureNodeClosed(ThreadContext context) {
|
339
|
+
ReaderNode node = currentNode();
|
340
|
+
if (node instanceof TextNode) return;
|
341
|
+
while (node.endOffset < 1) readMoreData(context);
|
342
|
+
}
|
343
|
+
|
344
|
+
@JRubyMethod
|
345
|
+
public IRubyObject read(ThreadContext context) {
|
346
|
+
position++;
|
347
|
+
try {
|
348
|
+
while (nodeQueue.size() <= position && continueParsing) {
|
349
|
+
readMoreData(context);
|
350
|
+
}
|
351
|
+
return setAndRaiseErrorsIfAny(context.runtime, null);
|
352
|
+
}
|
353
|
+
catch (RaiseException ex) {
|
354
|
+
return setAndRaiseErrorsIfAny(context.runtime, ex);
|
355
|
+
}
|
356
|
+
}
|
357
|
+
|
358
|
+
private IRubyObject setAndRaiseErrorsIfAny(final Ruby runtime, final RaiseException ex) throws RaiseException {
|
359
|
+
final ReaderNode currentNode = currentNode();
|
360
|
+
if (currentNode == null) return runtime.getNil();
|
361
|
+
if (currentNode.isError()) {
|
362
|
+
RubyArray errors = (RubyArray) getInstanceVariable("@errors");
|
363
|
+
IRubyObject error = currentNode.toSyntaxError();
|
364
|
+
errors.append(error);
|
365
|
+
setInstanceVariable("@errors", errors);
|
366
|
+
|
367
|
+
throw ex != null ? ex : ((XmlSyntaxError) error).toThrowable();
|
368
|
+
}
|
369
|
+
if ( ex != null ) throw ex;
|
370
|
+
return this;
|
371
|
+
}
|
372
|
+
|
373
|
+
private ReaderNode currentNode() {
|
374
|
+
if (position >= nodeQueue.size()) return null;
|
375
|
+
return nodeQueue.get(position);
|
376
|
+
}
|
377
|
+
|
378
|
+
@JRubyMethod
|
379
|
+
public IRubyObject state(ThreadContext context) {
|
380
|
+
return context.getRuntime().newFixnum(this.state);
|
381
|
+
}
|
382
|
+
|
383
|
+
@JRubyMethod
|
384
|
+
public IRubyObject value(ThreadContext context) {
|
385
|
+
return currentNode().getValue();
|
386
|
+
}
|
387
|
+
|
388
|
+
@JRubyMethod(name = "value?")
|
389
|
+
public IRubyObject value_p(ThreadContext context) {
|
390
|
+
return currentNode().hasValue();
|
391
|
+
}
|
392
|
+
|
393
|
+
@JRubyMethod
|
394
|
+
public IRubyObject xml_version(ThreadContext context) {
|
395
|
+
return currentNode().getXmlVersion();
|
396
|
+
}
|
397
|
+
|
398
|
+
protected XMLPullParserConfiguration createReader(Ruby ruby, Options options) {
|
399
|
+
StandardParserConfiguration config = new StandardParserConfiguration();
|
400
|
+
DocumentHandler handler = new DocumentHandler(ruby);
|
401
|
+
// XMLReader reader = XMLReaderFactory.createXMLReader();
|
402
|
+
config.setDocumentHandler(handler);
|
403
|
+
config.setDTDHandler(handler);
|
404
|
+
config.setErrorHandler(handler);
|
405
|
+
config.setEntityResolver(new EntityResolver2Wrapper(new NokogiriEntityResolver(ruby, null, options)));
|
406
|
+
// config.setFeature("http://xml.org/sax/features/xmlns-uris", true);
|
407
|
+
// config.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
408
|
+
config.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", options.dtdLoad || options.dtdValid);
|
409
|
+
return config;
|
410
|
+
}
|
411
|
+
|
412
|
+
private class DocumentHandler extends DefaultXMLDocumentHandler implements XMLErrorHandler {
|
413
|
+
|
414
|
+
Stack<String> langStack;
|
415
|
+
int depth;
|
416
|
+
Stack<String> xmlBaseStack;
|
417
|
+
Stack<ReaderNode.ElementNode> elementStack;
|
418
|
+
private final Ruby ruby;
|
419
|
+
|
420
|
+
public DocumentHandler(Ruby ruby) {
|
421
|
+
this.ruby = ruby;
|
422
|
+
}
|
423
|
+
|
424
|
+
@Override
|
425
|
+
public void startGeneralEntity(String name, XMLResourceIdentifier identifier,
|
426
|
+
String encoding, Augmentations augs) throws XNIException {
|
427
|
+
Object entitySkipped;
|
428
|
+
if (augs != null && (entitySkipped = augs.getItem(Constants.ENTITY_SKIPPED)) != null && ((Boolean) entitySkipped)) {
|
429
|
+
nodeQueue.add(new ReaderNode.ExceptionNode(ruby, null));
|
430
|
+
}
|
431
|
+
}
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
@Override
|
436
|
+
public void startDocument(XMLLocator locator, String encoding, NamespaceContext context, Augmentations augs) {
|
437
|
+
depth = 0;
|
438
|
+
langStack = new Stack<String>();
|
439
|
+
xmlBaseStack = new Stack<String>();
|
440
|
+
elementStack = new Stack<ReaderNode.ElementNode>();
|
441
|
+
}
|
442
|
+
|
443
|
+
@Override
|
444
|
+
public void endDocument(Augmentations augs) {
|
445
|
+
langStack = null;
|
446
|
+
xmlBaseStack = null;
|
447
|
+
elementStack = null;
|
448
|
+
}
|
449
|
+
|
450
|
+
@Override
|
451
|
+
public void startElement(QName element, XMLAttributes attrs, Augmentations augs) {
|
452
|
+
commonElement(element, attrs, false);
|
453
|
+
}
|
454
|
+
|
455
|
+
@Override
|
456
|
+
public void endElement(QName element, Augmentations augs) {
|
457
|
+
String uri = element.uri;
|
458
|
+
String localName = element.localpart;
|
459
|
+
String qName = element.rawname;
|
460
|
+
depth--;
|
461
|
+
ElementNode startElementNode = elementStack.pop();
|
462
|
+
ReaderNode node = ReaderNode.createClosingNode(ruby, uri, localName, qName, depth, langStack, xmlBaseStack);
|
463
|
+
|
464
|
+
startElementNode.endOffset = nodeQueue.size() - 1;
|
465
|
+
|
466
|
+
if (startElementNode.endOffset != startElementNode.startOffset) {
|
467
|
+
// this node isn't empty
|
468
|
+
node.attributeList = startElementNode.attributeList;
|
469
|
+
node.namespaces = startElementNode.namespaces;
|
470
|
+
node.startOffset = startElementNode.startOffset;
|
471
|
+
node.endOffset = ++startElementNode.endOffset;
|
472
|
+
node.hasChildren = startElementNode.hasChildren = true;
|
473
|
+
nodeQueue.add(node);
|
474
|
+
}
|
475
|
+
if (!langStack.isEmpty()) langStack.pop();
|
476
|
+
if (!xmlBaseStack.isEmpty()) xmlBaseStack.pop();
|
477
|
+
}
|
478
|
+
|
479
|
+
@Override
|
480
|
+
public void emptyElement(QName element, XMLAttributes attrs, Augmentations augs) {
|
481
|
+
commonElement(element, attrs, true);
|
482
|
+
}
|
483
|
+
|
484
|
+
private void commonElement(QName element, XMLAttributes attrs, boolean isEmpty) {
|
485
|
+
String qName = element.rawname;
|
486
|
+
String uri = element.uri;
|
487
|
+
String localName = element.localpart;
|
488
|
+
ReaderNode readerNode = ReaderNode.createElementNode(ruby, uri, localName, qName, attrs, depth, langStack, xmlBaseStack);
|
489
|
+
if (!elementStack.isEmpty()) {
|
490
|
+
ElementNode parent = elementStack.peek();
|
491
|
+
parent.hasChildren = true;
|
492
|
+
}
|
493
|
+
nodeQueue.add(readerNode);
|
494
|
+
readerNode.startOffset = nodeQueue.size() - 1;
|
495
|
+
if (!isEmpty) {
|
496
|
+
depth++;
|
497
|
+
if (readerNode.lang != null) langStack.push(readerNode.lang);
|
498
|
+
if (readerNode.xmlBase != null) xmlBaseStack.push(readerNode.xmlBase);
|
499
|
+
elementStack.push((ReaderNode.ElementNode)readerNode);
|
500
|
+
} else {
|
501
|
+
readerNode.endOffset = readerNode.startOffset;
|
502
|
+
readerNode.hasChildren = false;
|
503
|
+
}
|
504
|
+
}
|
505
|
+
|
506
|
+
@Override
|
507
|
+
public void characters(XMLString string, Augmentations augs) {
|
508
|
+
ReaderNode.TextNode node = ReaderNode.createTextNode(ruby, string.toString(), depth, langStack, xmlBaseStack);
|
509
|
+
nodeQueue.add(node);
|
510
|
+
node.startOffset = node.endOffset = nodeQueue.size() - 1;
|
511
|
+
}
|
512
|
+
|
513
|
+
@Override
|
514
|
+
public void error(String domain, String key, XMLParseException ex) {
|
515
|
+
nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
|
516
|
+
throw ex;
|
517
|
+
}
|
518
|
+
|
519
|
+
@Override
|
520
|
+
public void fatalError(String domain, String key, XMLParseException ex) {
|
521
|
+
nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
|
522
|
+
throw ex;
|
523
|
+
}
|
524
|
+
|
525
|
+
@Override
|
526
|
+
public void warning(String domain, String key, XMLParseException ex) {
|
527
|
+
nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
|
528
|
+
throw ex;
|
529
|
+
}
|
530
|
+
}
|
531
|
+
}
|
@@ -0,0 +1,151 @@
|
|
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.ByteArrayInputStream;
|
38
|
+
import java.io.IOException;
|
39
|
+
import java.io.InputStream;
|
40
|
+
import java.io.StringWriter;
|
41
|
+
import java.io.UnsupportedEncodingException;
|
42
|
+
|
43
|
+
import javax.xml.transform.Source;
|
44
|
+
import javax.xml.transform.TransformerConfigurationException;
|
45
|
+
import javax.xml.transform.TransformerException;
|
46
|
+
import javax.xml.transform.TransformerFactory;
|
47
|
+
import javax.xml.transform.dom.DOMSource;
|
48
|
+
import javax.xml.transform.stream.StreamResult;
|
49
|
+
import javax.xml.transform.stream.StreamSource;
|
50
|
+
|
51
|
+
import org.iso_relax.verifier.Schema;
|
52
|
+
import org.iso_relax.verifier.Verifier;
|
53
|
+
import org.iso_relax.verifier.VerifierConfigurationException;
|
54
|
+
import org.iso_relax.verifier.VerifierFactory;
|
55
|
+
import org.jruby.Ruby;
|
56
|
+
import org.jruby.RubyClass;
|
57
|
+
import org.jruby.anno.JRubyClass;
|
58
|
+
import org.jruby.runtime.ThreadContext;
|
59
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
60
|
+
import org.w3c.dom.Document;
|
61
|
+
import org.xml.sax.ErrorHandler;
|
62
|
+
import org.xml.sax.SAXException;
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Class for Nokogiri::XML::RelaxNG
|
66
|
+
*
|
67
|
+
* @author sergio
|
68
|
+
* @author Yoko Harada <yokolet@gmail.com>
|
69
|
+
*/
|
70
|
+
@JRubyClass(name="Nokogiri::XML::RelaxNG", parent="Nokogiri::XML::Schema")
|
71
|
+
public class XmlRelaxng extends XmlSchema {
|
72
|
+
private Verifier verifier;
|
73
|
+
|
74
|
+
public XmlRelaxng(Ruby ruby, RubyClass klazz) {
|
75
|
+
super(ruby, klazz);
|
76
|
+
}
|
77
|
+
|
78
|
+
private void setVerifier(Verifier verifier) {
|
79
|
+
this.verifier = verifier;
|
80
|
+
}
|
81
|
+
|
82
|
+
static XmlSchema createSchemaInstance(ThreadContext context, RubyClass klazz, Source source, IRubyObject parseOptions) {
|
83
|
+
Ruby runtime = context.getRuntime();
|
84
|
+
XmlRelaxng xmlRelaxng = (XmlRelaxng) NokogiriService.XML_RELAXNG_ALLOCATOR.allocate(runtime, klazz);
|
85
|
+
|
86
|
+
if (parseOptions == null) {
|
87
|
+
parseOptions = defaultParseOptions(context.getRuntime());
|
88
|
+
}
|
89
|
+
|
90
|
+
xmlRelaxng.setInstanceVariable("@errors", runtime.newEmptyArray());
|
91
|
+
xmlRelaxng.setInstanceVariable("@parse_options", parseOptions);
|
92
|
+
|
93
|
+
try {
|
94
|
+
Schema schema = xmlRelaxng.getSchema(source, context);
|
95
|
+
xmlRelaxng.setVerifier(schema.newVerifier());
|
96
|
+
return xmlRelaxng;
|
97
|
+
} catch (VerifierConfigurationException ex) {
|
98
|
+
throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
|
99
|
+
}
|
100
|
+
}
|
101
|
+
|
102
|
+
private Schema getSchema(Source source, ThreadContext context) {
|
103
|
+
InputStream is;
|
104
|
+
VerifierFactory factory = new com.thaiopensource.relaxng.jarv.VerifierFactoryImpl();
|
105
|
+
if (source instanceof StreamSource) {
|
106
|
+
StreamSource ss = (StreamSource)source;
|
107
|
+
is = ss.getInputStream();
|
108
|
+
} else { //if (this.source instanceof DOMSource)
|
109
|
+
DOMSource ds = (DOMSource)source;
|
110
|
+
StringWriter xmlAsWriter = new StringWriter();
|
111
|
+
StreamResult result = new StreamResult(xmlAsWriter);
|
112
|
+
try {
|
113
|
+
TransformerFactory.newInstance().newTransformer().transform(ds, result);
|
114
|
+
} catch (TransformerConfigurationException ex) {
|
115
|
+
throw context.getRuntime()
|
116
|
+
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
117
|
+
} catch (TransformerException ex) {
|
118
|
+
throw context.getRuntime()
|
119
|
+
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
120
|
+
}
|
121
|
+
try {
|
122
|
+
is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
|
123
|
+
} catch (UnsupportedEncodingException ex) {
|
124
|
+
throw context.getRuntime()
|
125
|
+
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
try {
|
130
|
+
return factory.compileSchema(is);
|
131
|
+
} catch (VerifierConfigurationException ex) {
|
132
|
+
throw context.getRuntime()
|
133
|
+
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
134
|
+
} catch (SAXException ex) {
|
135
|
+
throw context.getRuntime()
|
136
|
+
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
137
|
+
} catch (IOException ex) {
|
138
|
+
throw context.getRuntime().newIOError(ex.getClass() + ": " + ex.getMessage());
|
139
|
+
}
|
140
|
+
}
|
141
|
+
|
142
|
+
@Override
|
143
|
+
protected void setErrorHandler(ErrorHandler errorHandler) {
|
144
|
+
verifier.setErrorHandler(errorHandler);
|
145
|
+
}
|
146
|
+
|
147
|
+
@Override
|
148
|
+
protected void validate(Document document) throws SAXException, IOException {
|
149
|
+
verifier.verify(document);
|
150
|
+
}
|
151
|
+
}
|