Nokogiri_precompiled_aarch64_dedshit 1.14.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +44 -0
- data/LICENSE-DEPENDENCIES.md +2224 -0
- data/LICENSE.md +9 -0
- data/README.md +287 -0
- data/bin/nokogiri +131 -0
- data/dependencies.yml +41 -0
- data/ext/java/nokogiri/Html4Document.java +157 -0
- data/ext/java/nokogiri/Html4ElementDescription.java +133 -0
- data/ext/java/nokogiri/Html4EntityLookup.java +63 -0
- data/ext/java/nokogiri/Html4SaxParserContext.java +289 -0
- data/ext/java/nokogiri/Html4SaxPushParser.java +213 -0
- data/ext/java/nokogiri/NokogiriService.java +613 -0
- data/ext/java/nokogiri/XmlAttr.java +154 -0
- data/ext/java/nokogiri/XmlAttributeDecl.java +119 -0
- data/ext/java/nokogiri/XmlCdata.java +60 -0
- data/ext/java/nokogiri/XmlComment.java +77 -0
- data/ext/java/nokogiri/XmlDocument.java +705 -0
- data/ext/java/nokogiri/XmlDocumentFragment.java +163 -0
- data/ext/java/nokogiri/XmlDtd.java +516 -0
- data/ext/java/nokogiri/XmlElement.java +44 -0
- data/ext/java/nokogiri/XmlElementContent.java +412 -0
- data/ext/java/nokogiri/XmlElementDecl.java +148 -0
- data/ext/java/nokogiri/XmlEntityDecl.java +151 -0
- data/ext/java/nokogiri/XmlEntityReference.java +79 -0
- data/ext/java/nokogiri/XmlNamespace.java +193 -0
- data/ext/java/nokogiri/XmlNode.java +1938 -0
- data/ext/java/nokogiri/XmlNodeSet.java +463 -0
- data/ext/java/nokogiri/XmlProcessingInstruction.java +79 -0
- data/ext/java/nokogiri/XmlReader.java +615 -0
- data/ext/java/nokogiri/XmlRelaxng.java +133 -0
- data/ext/java/nokogiri/XmlSaxParserContext.java +329 -0
- data/ext/java/nokogiri/XmlSaxPushParser.java +288 -0
- data/ext/java/nokogiri/XmlSchema.java +423 -0
- data/ext/java/nokogiri/XmlSyntaxError.java +137 -0
- data/ext/java/nokogiri/XmlText.java +90 -0
- data/ext/java/nokogiri/XmlXpathContext.java +305 -0
- data/ext/java/nokogiri/XsltStylesheet.java +368 -0
- data/ext/java/nokogiri/internals/ClosedStreamException.java +13 -0
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +252 -0
- data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +27 -0
- data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +178 -0
- data/ext/java/nokogiri/internals/NokogiriDomParser.java +99 -0
- data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +140 -0
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +65 -0
- data/ext/java/nokogiri/internals/NokogiriHandler.java +339 -0
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +817 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +228 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +110 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +86 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +107 -0
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +62 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +165 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +50 -0
- data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +37 -0
- data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +70 -0
- data/ext/java/nokogiri/internals/ParserContext.java +262 -0
- data/ext/java/nokogiri/internals/ReaderNode.java +564 -0
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +865 -0
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +50 -0
- data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +174 -0
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +11 -0
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +265 -0
- data/ext/java/nokogiri/internals/XmlSaxParser.java +40 -0
- data/ext/java/nokogiri/internals/c14n/AttrCompare.java +122 -0
- data/ext/java/nokogiri/internals/c14n/C14nHelper.java +178 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +43 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +106 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +278 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +664 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +45 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +45 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +388 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +308 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +47 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +51 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +51 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +50 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +660 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +194 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +77 -0
- data/ext/java/nokogiri/internals/c14n/Constants.java +45 -0
- data/ext/java/nokogiri/internals/c14n/ElementProxy.java +325 -0
- data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +106 -0
- data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +86 -0
- data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +181 -0
- data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +87 -0
- data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +452 -0
- data/ext/java/nokogiri/internals/c14n/NodeFilter.java +52 -0
- data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +190 -0
- data/ext/java/nokogiri/internals/c14n/XMLUtils.java +540 -0
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1712 -0
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +737 -0
- data/ext/nokogiri/depend +38 -0
- data/ext/nokogiri/extconf.rb +1086 -0
- data/ext/nokogiri/gumbo.c +594 -0
- data/ext/nokogiri/html4_document.c +167 -0
- data/ext/nokogiri/html4_element_description.c +294 -0
- data/ext/nokogiri/html4_entity_lookup.c +37 -0
- data/ext/nokogiri/html4_sax_parser_context.c +116 -0
- data/ext/nokogiri/html4_sax_push_parser.c +95 -0
- data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
- data/ext/nokogiri/nokogiri.c +265 -0
- data/ext/nokogiri/nokogiri.h +235 -0
- data/ext/nokogiri/test_global_handlers.c +42 -0
- data/ext/nokogiri/xml_attr.c +103 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_cdata.c +57 -0
- data/ext/nokogiri/xml_comment.c +62 -0
- data/ext/nokogiri/xml_document.c +689 -0
- data/ext/nokogiri/xml_document_fragment.c +44 -0
- data/ext/nokogiri/xml_dtd.c +210 -0
- data/ext/nokogiri/xml_element_content.c +128 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_encoding_handler.c +104 -0
- data/ext/nokogiri/xml_entity_decl.c +112 -0
- data/ext/nokogiri/xml_entity_reference.c +50 -0
- data/ext/nokogiri/xml_namespace.c +186 -0
- data/ext/nokogiri/xml_node.c +2426 -0
- data/ext/nokogiri/xml_node_set.c +496 -0
- data/ext/nokogiri/xml_processing_instruction.c +54 -0
- data/ext/nokogiri/xml_reader.c +794 -0
- data/ext/nokogiri/xml_relax_ng.c +164 -0
- data/ext/nokogiri/xml_sax_parser.c +316 -0
- data/ext/nokogiri/xml_sax_parser_context.c +283 -0
- data/ext/nokogiri/xml_sax_push_parser.c +166 -0
- data/ext/nokogiri/xml_schema.c +260 -0
- data/ext/nokogiri/xml_syntax_error.c +85 -0
- data/ext/nokogiri/xml_text.c +48 -0
- data/ext/nokogiri/xml_xpath_context.c +415 -0
- data/ext/nokogiri/xslt_stylesheet.c +363 -0
- data/gumbo-parser/CHANGES.md +63 -0
- data/gumbo-parser/Makefile +111 -0
- data/gumbo-parser/THANKS +27 -0
- data/gumbo-parser/src/Makefile +34 -0
- data/gumbo-parser/src/README.md +41 -0
- data/gumbo-parser/src/ascii.c +75 -0
- data/gumbo-parser/src/ascii.h +115 -0
- data/gumbo-parser/src/attribute.c +42 -0
- data/gumbo-parser/src/attribute.h +17 -0
- data/gumbo-parser/src/char_ref.c +22225 -0
- data/gumbo-parser/src/char_ref.h +29 -0
- data/gumbo-parser/src/char_ref.rl +2154 -0
- data/gumbo-parser/src/error.c +626 -0
- data/gumbo-parser/src/error.h +148 -0
- data/gumbo-parser/src/foreign_attrs.c +104 -0
- data/gumbo-parser/src/foreign_attrs.gperf +27 -0
- data/gumbo-parser/src/insertion_mode.h +33 -0
- data/gumbo-parser/src/macros.h +91 -0
- data/gumbo-parser/src/nokogiri_gumbo.h +944 -0
- data/gumbo-parser/src/parser.c +4878 -0
- data/gumbo-parser/src/parser.h +41 -0
- data/gumbo-parser/src/replacement.h +33 -0
- data/gumbo-parser/src/string_buffer.c +103 -0
- data/gumbo-parser/src/string_buffer.h +68 -0
- data/gumbo-parser/src/string_piece.c +48 -0
- data/gumbo-parser/src/svg_attrs.c +174 -0
- data/gumbo-parser/src/svg_attrs.gperf +77 -0
- data/gumbo-parser/src/svg_tags.c +137 -0
- data/gumbo-parser/src/svg_tags.gperf +55 -0
- data/gumbo-parser/src/tag.c +223 -0
- data/gumbo-parser/src/tag_lookup.c +382 -0
- data/gumbo-parser/src/tag_lookup.gperf +170 -0
- data/gumbo-parser/src/tag_lookup.h +13 -0
- data/gumbo-parser/src/token_buffer.c +79 -0
- data/gumbo-parser/src/token_buffer.h +71 -0
- data/gumbo-parser/src/token_type.h +17 -0
- data/gumbo-parser/src/tokenizer.c +3463 -0
- data/gumbo-parser/src/tokenizer.h +112 -0
- data/gumbo-parser/src/tokenizer_states.h +339 -0
- data/gumbo-parser/src/utf8.c +245 -0
- data/gumbo-parser/src/utf8.h +164 -0
- data/gumbo-parser/src/util.c +66 -0
- data/gumbo-parser/src/util.h +34 -0
- data/gumbo-parser/src/vector.c +111 -0
- data/gumbo-parser/src/vector.h +45 -0
- data/lib/nokogiri/class_resolver.rb +67 -0
- data/lib/nokogiri/css/node.rb +54 -0
- data/lib/nokogiri/css/parser.rb +770 -0
- data/lib/nokogiri/css/parser.y +277 -0
- data/lib/nokogiri/css/parser_extras.rb +96 -0
- data/lib/nokogiri/css/syntax_error.rb +9 -0
- data/lib/nokogiri/css/tokenizer.rb +155 -0
- data/lib/nokogiri/css/tokenizer.rex +56 -0
- data/lib/nokogiri/css/xpath_visitor.rb +359 -0
- data/lib/nokogiri/css.rb +66 -0
- data/lib/nokogiri/decorators/slop.rb +44 -0
- data/lib/nokogiri/encoding_handler.rb +57 -0
- data/lib/nokogiri/extension.rb +32 -0
- data/lib/nokogiri/gumbo.rb +15 -0
- data/lib/nokogiri/html.rb +48 -0
- data/lib/nokogiri/html4/builder.rb +37 -0
- data/lib/nokogiri/html4/document.rb +214 -0
- data/lib/nokogiri/html4/document_fragment.rb +54 -0
- data/lib/nokogiri/html4/element_description.rb +25 -0
- data/lib/nokogiri/html4/element_description_defaults.rb +572 -0
- data/lib/nokogiri/html4/encoding_reader.rb +121 -0
- data/lib/nokogiri/html4/entity_lookup.rb +15 -0
- data/lib/nokogiri/html4/sax/parser.rb +63 -0
- data/lib/nokogiri/html4/sax/parser_context.rb +20 -0
- data/lib/nokogiri/html4/sax/push_parser.rb +37 -0
- data/lib/nokogiri/html4.rb +47 -0
- data/lib/nokogiri/html5/document.rb +168 -0
- data/lib/nokogiri/html5/document_fragment.rb +90 -0
- data/lib/nokogiri/html5/node.rb +98 -0
- data/lib/nokogiri/html5.rb +389 -0
- data/lib/nokogiri/jruby/dependencies.rb +3 -0
- data/lib/nokogiri/jruby/isorelax/isorelax/20030108/isorelax-20030108.jar +0 -0
- data/lib/nokogiri/jruby/net/sf/saxon/Saxon-HE/9.6.0-4/Saxon-HE-9.6.0-4.jar +0 -0
- data/lib/nokogiri/jruby/net/sourceforge/htmlunit/neko-htmlunit/2.63.0/neko-htmlunit-2.63.0.jar +0 -0
- data/lib/nokogiri/jruby/nokogiri_jars.rb +43 -0
- data/lib/nokogiri/jruby/nu/validator/jing/20200702VNU/jing-20200702VNU.jar +0 -0
- data/lib/nokogiri/jruby/org/nokogiri/nekodtd/0.1.11.noko2/nekodtd-0.1.11.noko2.jar +0 -0
- data/lib/nokogiri/jruby/xalan/serializer/2.7.3/serializer-2.7.3.jar +0 -0
- data/lib/nokogiri/jruby/xalan/xalan/2.7.3/xalan-2.7.3.jar +0 -0
- data/lib/nokogiri/jruby/xerces/xercesImpl/2.12.2/xercesImpl-2.12.2.jar +0 -0
- data/lib/nokogiri/jruby/xml-apis/xml-apis/1.4.01/xml-apis-1.4.01.jar +0 -0
- data/lib/nokogiri/syntax_error.rb +6 -0
- data/lib/nokogiri/version/constant.rb +6 -0
- data/lib/nokogiri/version/info.rb +223 -0
- data/lib/nokogiri/version.rb +4 -0
- data/lib/nokogiri/xml/attr.rb +66 -0
- data/lib/nokogiri/xml/attribute_decl.rb +20 -0
- data/lib/nokogiri/xml/builder.rb +487 -0
- data/lib/nokogiri/xml/cdata.rb +13 -0
- data/lib/nokogiri/xml/character_data.rb +9 -0
- data/lib/nokogiri/xml/document.rb +471 -0
- data/lib/nokogiri/xml/document_fragment.rb +205 -0
- data/lib/nokogiri/xml/dtd.rb +34 -0
- data/lib/nokogiri/xml/element_content.rb +38 -0
- data/lib/nokogiri/xml/element_decl.rb +15 -0
- data/lib/nokogiri/xml/entity_decl.rb +21 -0
- data/lib/nokogiri/xml/entity_reference.rb +20 -0
- data/lib/nokogiri/xml/namespace.rb +58 -0
- data/lib/nokogiri/xml/node/save_options.rb +68 -0
- data/lib/nokogiri/xml/node.rb +1563 -0
- data/lib/nokogiri/xml/node_set.rb +447 -0
- data/lib/nokogiri/xml/notation.rb +19 -0
- data/lib/nokogiri/xml/parse_options.rb +213 -0
- data/lib/nokogiri/xml/pp/character_data.rb +21 -0
- data/lib/nokogiri/xml/pp/node.rb +57 -0
- data/lib/nokogiri/xml/pp.rb +4 -0
- data/lib/nokogiri/xml/processing_instruction.rb +11 -0
- data/lib/nokogiri/xml/reader.rb +105 -0
- data/lib/nokogiri/xml/relax_ng.rb +38 -0
- data/lib/nokogiri/xml/sax/document.rb +167 -0
- data/lib/nokogiri/xml/sax/parser.rb +125 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +21 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +61 -0
- data/lib/nokogiri/xml/sax.rb +6 -0
- data/lib/nokogiri/xml/schema.rb +73 -0
- data/lib/nokogiri/xml/searchable.rb +270 -0
- data/lib/nokogiri/xml/syntax_error.rb +72 -0
- data/lib/nokogiri/xml/text.rb +11 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +13 -0
- data/lib/nokogiri/xml/xpath.rb +21 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xml.rb +76 -0
- data/lib/nokogiri/xslt/stylesheet.rb +27 -0
- data/lib/nokogiri/xslt.rb +65 -0
- data/lib/nokogiri.rb +120 -0
- data/lib/xsd/xmlparser/nokogiri.rb +106 -0
- metadata +391 -0
@@ -0,0 +1,705 @@
|
|
1
|
+
package nokogiri;
|
2
|
+
|
3
|
+
import static nokogiri.internals.NokogiriHelpers.clearXpathContext;
|
4
|
+
import static nokogiri.internals.NokogiriHelpers.getCachedNodeOrCreate;
|
5
|
+
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
|
6
|
+
import static nokogiri.internals.NokogiriHelpers.isNamespace;
|
7
|
+
import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
|
8
|
+
import static nokogiri.internals.NokogiriHelpers.stringOrNil;
|
9
|
+
|
10
|
+
import java.util.List;
|
11
|
+
|
12
|
+
import javax.xml.parsers.DocumentBuilderFactory;
|
13
|
+
import javax.xml.parsers.ParserConfigurationException;
|
14
|
+
|
15
|
+
import org.jcodings.specific.USASCIIEncoding;
|
16
|
+
import org.jcodings.specific.UTF8Encoding;
|
17
|
+
import org.jruby.Ruby;
|
18
|
+
import org.jruby.RubyArray;
|
19
|
+
import org.jruby.RubyClass;
|
20
|
+
import org.jruby.RubyFixnum;
|
21
|
+
import org.jruby.RubyString;
|
22
|
+
import org.jruby.anno.JRubyClass;
|
23
|
+
import org.jruby.anno.JRubyMethod;
|
24
|
+
import org.jruby.exceptions.RaiseException;
|
25
|
+
import org.jruby.javasupport.JavaUtil;
|
26
|
+
import org.jruby.runtime.Block;
|
27
|
+
import org.jruby.runtime.Helpers;
|
28
|
+
import org.jruby.runtime.ThreadContext;
|
29
|
+
import org.jruby.runtime.Visibility;
|
30
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
31
|
+
import org.jruby.util.ByteList;
|
32
|
+
import org.w3c.dom.Attr;
|
33
|
+
import org.w3c.dom.Document;
|
34
|
+
import org.w3c.dom.DocumentType;
|
35
|
+
import org.w3c.dom.NamedNodeMap;
|
36
|
+
import org.w3c.dom.Node;
|
37
|
+
import org.w3c.dom.NodeList;
|
38
|
+
|
39
|
+
import nokogiri.internals.NokogiriHelpers;
|
40
|
+
import nokogiri.internals.NokogiriNamespaceCache;
|
41
|
+
import nokogiri.internals.SaveContextVisitor;
|
42
|
+
import nokogiri.internals.XmlDomParserContext;
|
43
|
+
import nokogiri.internals.c14n.CanonicalFilter;
|
44
|
+
import nokogiri.internals.c14n.CanonicalizationException;
|
45
|
+
import nokogiri.internals.c14n.Canonicalizer;
|
46
|
+
|
47
|
+
/**
|
48
|
+
* Class for Nokogiri::XML::Document
|
49
|
+
*
|
50
|
+
* @author sergio
|
51
|
+
* @author Yoko Harada <yokolet@gmail.com>
|
52
|
+
* @author John Shahid <jvshahid@gmail.com>
|
53
|
+
*/
|
54
|
+
@JRubyClass(name = "Nokogiri::XML::Document", parent = "Nokogiri::XML::Node")
|
55
|
+
public class XmlDocument extends XmlNode
|
56
|
+
{
|
57
|
+
private static final long serialVersionUID = 1L;
|
58
|
+
|
59
|
+
private NokogiriNamespaceCache nsCache;
|
60
|
+
|
61
|
+
/* UserData keys for storing extra info in the document node. */
|
62
|
+
public final static String DTD_RAW_DOCUMENT = "DTD_RAW_DOCUMENT";
|
63
|
+
public final static String DTD_INTERNAL_SUBSET = "DTD_INTERNAL_SUBSET";
|
64
|
+
public final static String DTD_EXTERNAL_SUBSET = "DTD_EXTERNAL_SUBSET";
|
65
|
+
|
66
|
+
/* DocumentBuilderFactory implementation class name. This needs to set a classloader into it.
|
67
|
+
* Setting an appropriate classloader resolves issue 380.
|
68
|
+
*/
|
69
|
+
private static final String DOCUMENTBUILDERFACTORY_IMPLE_NAME = "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl";
|
70
|
+
|
71
|
+
private static final ByteList DOCUMENT = ByteList.create("document");
|
72
|
+
static { DOCUMENT.setEncoding(USASCIIEncoding.INSTANCE); }
|
73
|
+
|
74
|
+
private static boolean substituteEntities = false;
|
75
|
+
private static boolean loadExternalSubset = false; // TODO: Verify this.
|
76
|
+
|
77
|
+
/** cache variables */
|
78
|
+
protected IRubyObject encoding;
|
79
|
+
protected IRubyObject url;
|
80
|
+
|
81
|
+
public
|
82
|
+
XmlDocument(Ruby runtime, RubyClass klazz)
|
83
|
+
{
|
84
|
+
super(runtime, klazz, createNewDocument(runtime));
|
85
|
+
}
|
86
|
+
|
87
|
+
public
|
88
|
+
XmlDocument(Ruby runtime, Document document)
|
89
|
+
{
|
90
|
+
this(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Document"), document);
|
91
|
+
}
|
92
|
+
|
93
|
+
public
|
94
|
+
XmlDocument(Ruby runtime, RubyClass klass, Document document)
|
95
|
+
{
|
96
|
+
super(runtime, klass, document);
|
97
|
+
init(runtime, document);
|
98
|
+
}
|
99
|
+
|
100
|
+
void
|
101
|
+
init(Ruby runtime, Document document)
|
102
|
+
{
|
103
|
+
stabilizeTextContent(document);
|
104
|
+
if (document.getDocumentElement() != null) {
|
105
|
+
createAndCacheNamespaces(runtime, document.getDocumentElement());
|
106
|
+
}
|
107
|
+
setInstanceVariable("@decorators", runtime.getNil());
|
108
|
+
}
|
109
|
+
|
110
|
+
public final void
|
111
|
+
setDocumentNode(Ruby runtime, Document node)
|
112
|
+
{
|
113
|
+
super.setNode(runtime, node);
|
114
|
+
if (node != null) { init(runtime, node); }
|
115
|
+
else { setInstanceVariable("@decorators", runtime.getNil()); }
|
116
|
+
}
|
117
|
+
|
118
|
+
public void
|
119
|
+
setEncoding(IRubyObject encoding)
|
120
|
+
{
|
121
|
+
this.encoding = encoding;
|
122
|
+
}
|
123
|
+
|
124
|
+
public IRubyObject
|
125
|
+
getEncoding()
|
126
|
+
{
|
127
|
+
return encoding;
|
128
|
+
}
|
129
|
+
|
130
|
+
// not sure, but like attribute values, text value will be lost
|
131
|
+
// unless it is referred once before this document is used.
|
132
|
+
// this seems to happen only when the fragment is parsed from Node#in_context.
|
133
|
+
protected static void
|
134
|
+
stabilizeTextContent(Document document)
|
135
|
+
{
|
136
|
+
if (document.getDocumentElement() != null) { document.getDocumentElement().getTextContent(); }
|
137
|
+
}
|
138
|
+
|
139
|
+
private static void
|
140
|
+
createAndCacheNamespaces(Ruby runtime, Node node)
|
141
|
+
{
|
142
|
+
if (node.hasAttributes()) {
|
143
|
+
NamedNodeMap nodeMap = node.getAttributes();
|
144
|
+
for (int i = 0; i < nodeMap.getLength(); i++) {
|
145
|
+
Node n = nodeMap.item(i);
|
146
|
+
if (n instanceof Attr) {
|
147
|
+
Attr attr = (Attr) n;
|
148
|
+
stabilizeAttr(attr);
|
149
|
+
if (isNamespace(attr.getName())) {
|
150
|
+
// create and cache
|
151
|
+
XmlNamespace.createFromAttr(runtime, attr);
|
152
|
+
}
|
153
|
+
}
|
154
|
+
}
|
155
|
+
}
|
156
|
+
NodeList children = node.getChildNodes();
|
157
|
+
for (int i = 0; i < children.getLength(); i++) {
|
158
|
+
createAndCacheNamespaces(runtime, children.item(i));
|
159
|
+
}
|
160
|
+
}
|
161
|
+
|
162
|
+
static void
|
163
|
+
stabilizeAttr(final Attr attr)
|
164
|
+
{
|
165
|
+
// TODO not sure, but need to get value always before document is referred or lose attribute value
|
166
|
+
attr.getValue(); // don't delete this line
|
167
|
+
}
|
168
|
+
|
169
|
+
// When a document is created from fragment with a context (reference) document,
|
170
|
+
// namespace should be resolved based on the context document.
|
171
|
+
public
|
172
|
+
XmlDocument(Ruby ruby, RubyClass klass, Document document, XmlDocument contextDoc)
|
173
|
+
{
|
174
|
+
super(ruby, klass, document);
|
175
|
+
nsCache = contextDoc.getNamespaceCache();
|
176
|
+
String default_href = nsCache.getDefault().getHref();
|
177
|
+
resolveNamespaceIfNecessary(document.getDocumentElement(), default_href);
|
178
|
+
}
|
179
|
+
|
180
|
+
private void
|
181
|
+
resolveNamespaceIfNecessary(Node node, String default_href)
|
182
|
+
{
|
183
|
+
if (node == null) { return; }
|
184
|
+
String nodePrefix = node.getPrefix();
|
185
|
+
if (nodePrefix == null) { // default namespace
|
186
|
+
NokogiriHelpers.renameNode(node, default_href, node.getNodeName());
|
187
|
+
} else {
|
188
|
+
String href = getNamespaceCache().get(node, nodePrefix).getHref();
|
189
|
+
NokogiriHelpers.renameNode(node, href, node.getNodeName());
|
190
|
+
}
|
191
|
+
resolveNamespaceIfNecessary(node.getNextSibling(), default_href);
|
192
|
+
NodeList children = node.getChildNodes();
|
193
|
+
for (int i = 0; i < children.getLength(); i++) {
|
194
|
+
resolveNamespaceIfNecessary(children.item(i), default_href);
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
public NokogiriNamespaceCache
|
199
|
+
getNamespaceCache()
|
200
|
+
{
|
201
|
+
if (nsCache == null) { nsCache = new NokogiriNamespaceCache(); }
|
202
|
+
return nsCache;
|
203
|
+
}
|
204
|
+
|
205
|
+
public Document
|
206
|
+
getDocument()
|
207
|
+
{
|
208
|
+
return (Document) node;
|
209
|
+
}
|
210
|
+
|
211
|
+
@Override
|
212
|
+
protected IRubyObject
|
213
|
+
getNodeName(ThreadContext context)
|
214
|
+
{
|
215
|
+
if (name == null) { name = RubyString.newStringShared(context.runtime, DOCUMENT); }
|
216
|
+
return name;
|
217
|
+
}
|
218
|
+
|
219
|
+
public void
|
220
|
+
setUrl(IRubyObject url)
|
221
|
+
{
|
222
|
+
this.url = url;
|
223
|
+
}
|
224
|
+
|
225
|
+
protected IRubyObject
|
226
|
+
getUrl()
|
227
|
+
{
|
228
|
+
return this.url;
|
229
|
+
}
|
230
|
+
|
231
|
+
@JRubyMethod
|
232
|
+
public IRubyObject
|
233
|
+
url(ThreadContext context)
|
234
|
+
{
|
235
|
+
return getUrl();
|
236
|
+
}
|
237
|
+
|
238
|
+
public static Document
|
239
|
+
createNewDocument(final Ruby runtime)
|
240
|
+
{
|
241
|
+
try {
|
242
|
+
return DocumentBuilderFactoryHolder.INSTANCE.newDocumentBuilder().newDocument();
|
243
|
+
} catch (ParserConfigurationException e) {
|
244
|
+
throw asRuntimeError(runtime, null, e);
|
245
|
+
}
|
246
|
+
}
|
247
|
+
|
248
|
+
private static class DocumentBuilderFactoryHolder
|
249
|
+
{
|
250
|
+
static final DocumentBuilderFactory INSTANCE;
|
251
|
+
static
|
252
|
+
{
|
253
|
+
INSTANCE = DocumentBuilderFactory.newInstance(DOCUMENTBUILDERFACTORY_IMPLE_NAME,
|
254
|
+
NokogiriService.class.getClassLoader());
|
255
|
+
}
|
256
|
+
}
|
257
|
+
|
258
|
+
static RaiseException
|
259
|
+
asRuntimeError(Ruby runtime, String message, Exception cause)
|
260
|
+
{
|
261
|
+
if (cause instanceof RaiseException) { return (RaiseException) cause; }
|
262
|
+
|
263
|
+
if (message == null) { message = cause.toString(); }
|
264
|
+
else { message = message + '(' + cause.toString() + ')'; }
|
265
|
+
RaiseException ex = runtime.newRuntimeError(message);
|
266
|
+
ex.initCause(cause);
|
267
|
+
return ex;
|
268
|
+
}
|
269
|
+
|
270
|
+
/*
|
271
|
+
* call-seq:
|
272
|
+
* new(version = default)
|
273
|
+
*
|
274
|
+
* Create a new document with +version+ (defaults to "1.0")
|
275
|
+
*/
|
276
|
+
@JRubyMethod(name = "new", meta = true, rest = true, required = 0)
|
277
|
+
public static IRubyObject
|
278
|
+
rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args)
|
279
|
+
{
|
280
|
+
final Ruby runtime = context.runtime;
|
281
|
+
XmlDocument xmlDocument;
|
282
|
+
try {
|
283
|
+
Document docNode = createNewDocument(runtime);
|
284
|
+
if ("Nokogiri::HTML4::Document".equals(((RubyClass)klazz).getName())) {
|
285
|
+
xmlDocument = new Html4Document(context.runtime, (RubyClass) klazz, docNode);
|
286
|
+
} else {
|
287
|
+
xmlDocument = new XmlDocument(context.runtime, (RubyClass) klazz, docNode);
|
288
|
+
}
|
289
|
+
} catch (Exception ex) {
|
290
|
+
throw asRuntimeError(runtime, "couldn't create document: ", ex);
|
291
|
+
}
|
292
|
+
|
293
|
+
Helpers.invoke(context, xmlDocument, "initialize", args);
|
294
|
+
|
295
|
+
return xmlDocument;
|
296
|
+
}
|
297
|
+
|
298
|
+
@JRubyMethod(required = 1, optional = 4)
|
299
|
+
public IRubyObject
|
300
|
+
create_entity(ThreadContext context, IRubyObject[] argv)
|
301
|
+
{
|
302
|
+
// FIXME: Entity node should be create by some right way.
|
303
|
+
// this impl passes tests, but entity doesn't exists in DTD, which
|
304
|
+
// would cause validation failure.
|
305
|
+
if (argv.length == 0) { throw context.runtime.newRuntimeError("Could not create entity"); }
|
306
|
+
String tagName = rubyStringToString(argv[0]);
|
307
|
+
Node node = getOwnerDocument().createElement(tagName);
|
308
|
+
return XmlEntityDecl.create(context, node, argv);
|
309
|
+
}
|
310
|
+
|
311
|
+
@Override
|
312
|
+
XmlDocument
|
313
|
+
document(Ruby runtime)
|
314
|
+
{
|
315
|
+
return this;
|
316
|
+
}
|
317
|
+
|
318
|
+
@JRubyMethod(name = "encoding=")
|
319
|
+
public IRubyObject
|
320
|
+
encoding_set(IRubyObject encoding)
|
321
|
+
{
|
322
|
+
this.encoding = encoding;
|
323
|
+
return this;
|
324
|
+
}
|
325
|
+
|
326
|
+
@JRubyMethod
|
327
|
+
public IRubyObject
|
328
|
+
encoding(ThreadContext context)
|
329
|
+
{
|
330
|
+
if (this.encoding == null || this.encoding.isNil()) {
|
331
|
+
final String enc = getDocument().getXmlEncoding();
|
332
|
+
if (enc == null) {
|
333
|
+
this.encoding = context.nil;
|
334
|
+
} else {
|
335
|
+
this.encoding = context.runtime.newString(enc);
|
336
|
+
}
|
337
|
+
}
|
338
|
+
|
339
|
+
return this.encoding.isNil() ? this.encoding : this.encoding.asString().encode(context,
|
340
|
+
context.getRuntime().newString("UTF-8"));
|
341
|
+
}
|
342
|
+
|
343
|
+
@JRubyMethod(meta = true)
|
344
|
+
public static IRubyObject
|
345
|
+
load_external_subsets_set(ThreadContext context, IRubyObject cls, IRubyObject value)
|
346
|
+
{
|
347
|
+
XmlDocument.loadExternalSubset = value.isTrue();
|
348
|
+
return context.nil;
|
349
|
+
}
|
350
|
+
|
351
|
+
@JRubyMethod(meta = true, required = 4)
|
352
|
+
public static IRubyObject
|
353
|
+
read_io(ThreadContext context, IRubyObject klass, IRubyObject[] args)
|
354
|
+
{
|
355
|
+
XmlDomParserContext ctx = new XmlDomParserContext(context.runtime, args[2], args[3]);
|
356
|
+
ctx.setIOInputSource(context, args[0], args[1]);
|
357
|
+
return ctx.parse(context, (RubyClass) klass, args[1]);
|
358
|
+
}
|
359
|
+
|
360
|
+
@JRubyMethod(meta = true, required = 4)
|
361
|
+
public static IRubyObject
|
362
|
+
read_memory(ThreadContext context, IRubyObject klass, IRubyObject[] args)
|
363
|
+
{
|
364
|
+
XmlDomParserContext ctx = new XmlDomParserContext(context.runtime, args[2], args[3]);
|
365
|
+
ctx.setStringInputSource(context, args[0], args[1]);
|
366
|
+
return ctx.parse(context, (RubyClass) klass, args[1]);
|
367
|
+
}
|
368
|
+
|
369
|
+
@JRubyMethod(name = "remove_namespaces!")
|
370
|
+
public IRubyObject
|
371
|
+
remove_namespaces(ThreadContext context)
|
372
|
+
{
|
373
|
+
removeNamespaceRecursively(this);
|
374
|
+
if (nsCache != null) { nsCache.clear(); }
|
375
|
+
clearXpathContext(getNode());
|
376
|
+
return this;
|
377
|
+
}
|
378
|
+
|
379
|
+
private void
|
380
|
+
removeNamespaceRecursively(XmlNode xmlNode)
|
381
|
+
{
|
382
|
+
Node node = xmlNode.node;
|
383
|
+
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
384
|
+
node.setPrefix(null);
|
385
|
+
NokogiriHelpers.renameNode(node, null, node.getLocalName());
|
386
|
+
NamedNodeMap attrs = node.getAttributes();
|
387
|
+
for (int i = 0; i < attrs.getLength(); i++) {
|
388
|
+
Attr attr = (Attr) attrs.item(i);
|
389
|
+
if (isNamespace(attr.getNodeName())) {
|
390
|
+
((org.w3c.dom.Element) node).removeAttributeNode(attr);
|
391
|
+
} else {
|
392
|
+
attr.setPrefix(null);
|
393
|
+
NokogiriHelpers.renameNode(attr, null, attr.getLocalName());
|
394
|
+
}
|
395
|
+
}
|
396
|
+
}
|
397
|
+
IRubyObject[] nodes = xmlNode.getChildren();
|
398
|
+
for (int i = 0; i < nodes.length; i++) {
|
399
|
+
XmlNode childNode = (XmlNode) nodes[i];
|
400
|
+
removeNamespaceRecursively(childNode);
|
401
|
+
}
|
402
|
+
}
|
403
|
+
|
404
|
+
@JRubyMethod
|
405
|
+
public IRubyObject
|
406
|
+
root(ThreadContext context)
|
407
|
+
{
|
408
|
+
Node rootNode = getDocument().getDocumentElement();
|
409
|
+
if (rootNode == null) { return context.nil; }
|
410
|
+
|
411
|
+
Object invalid = rootNode.getUserData(NokogiriHelpers.ROOT_NODE_INVALID);
|
412
|
+
if (invalid != null && ((Boolean) invalid)) { return context.nil; }
|
413
|
+
|
414
|
+
return getCachedNodeOrCreate(context.runtime, rootNode);
|
415
|
+
}
|
416
|
+
|
417
|
+
protected IRubyObject
|
418
|
+
dup_implementation(Ruby runtime, boolean deep)
|
419
|
+
{
|
420
|
+
XmlDocument doc = (XmlDocument) super.dup_implementation(runtime, deep);
|
421
|
+
// Avoid creating a new XmlDocument since we cloned one
|
422
|
+
// already. Otherwise the following test will fail:
|
423
|
+
//
|
424
|
+
// dup = doc.dup
|
425
|
+
// dup.equal?(dup.children[0].document)
|
426
|
+
//
|
427
|
+
// Since `dup.children[0].document' will end up creating a new
|
428
|
+
// XmlDocument. See #1060.
|
429
|
+
doc.resetCache();
|
430
|
+
return doc;
|
431
|
+
}
|
432
|
+
|
433
|
+
@JRubyMethod(name = "root=")
|
434
|
+
public IRubyObject
|
435
|
+
root_set(ThreadContext context, IRubyObject new_root)
|
436
|
+
{
|
437
|
+
// in case of document fragment, temporary root node should be deleted.
|
438
|
+
|
439
|
+
// Java can't have a root whose value is null. Instead of setting null,
|
440
|
+
// the method sets user data so that other methods are able to know the root
|
441
|
+
// should be nil.
|
442
|
+
if (new_root == context.nil) {
|
443
|
+
getDocument().getDocumentElement().setUserData(NokogiriHelpers.ROOT_NODE_INVALID, Boolean.TRUE, null);
|
444
|
+
return new_root;
|
445
|
+
}
|
446
|
+
if (!(new_root instanceof XmlNode)) {
|
447
|
+
throw context.runtime.newArgumentError("expected Nokogiri::XML::Node but received " + new_root.getType());
|
448
|
+
}
|
449
|
+
XmlNode newRoot = asXmlNode(context, new_root);
|
450
|
+
|
451
|
+
IRubyObject root = root(context);
|
452
|
+
if (root.isNil()) {
|
453
|
+
Node newRootNode;
|
454
|
+
if (getDocument() == newRoot.getOwnerDocument()) {
|
455
|
+
newRootNode = newRoot.node;
|
456
|
+
} else {
|
457
|
+
// must copy otherwise newRoot may exist in two places
|
458
|
+
// with different owner document.
|
459
|
+
newRootNode = getDocument().importNode(newRoot.node, true);
|
460
|
+
}
|
461
|
+
add_child_node(context, getCachedNodeOrCreate(context.runtime, newRootNode));
|
462
|
+
} else {
|
463
|
+
Node rootNode = asXmlNode(context, root).node;
|
464
|
+
((XmlNode) getCachedNodeOrCreate(context.runtime, rootNode)).replace_node(context, newRoot);
|
465
|
+
}
|
466
|
+
|
467
|
+
return newRoot;
|
468
|
+
}
|
469
|
+
|
470
|
+
@JRubyMethod
|
471
|
+
public IRubyObject
|
472
|
+
version(ThreadContext context)
|
473
|
+
{
|
474
|
+
return stringOrNil(context.runtime, getDocument().getXmlVersion());
|
475
|
+
}
|
476
|
+
|
477
|
+
@JRubyMethod(meta = true)
|
478
|
+
public static IRubyObject
|
479
|
+
substitute_entities_set(ThreadContext context, IRubyObject cls, IRubyObject value)
|
480
|
+
{
|
481
|
+
XmlDocument.substituteEntities = value.isTrue();
|
482
|
+
return context.nil;
|
483
|
+
}
|
484
|
+
|
485
|
+
public IRubyObject
|
486
|
+
getInternalSubset(ThreadContext context)
|
487
|
+
{
|
488
|
+
IRubyObject dtd = (IRubyObject) node.getUserData(DTD_INTERNAL_SUBSET);
|
489
|
+
|
490
|
+
if (dtd == null) {
|
491
|
+
Document document = getDocument();
|
492
|
+
if (document.getUserData(XmlDocument.DTD_RAW_DOCUMENT) != null) {
|
493
|
+
dtd = XmlDtd.newFromInternalSubset(context.runtime, document);
|
494
|
+
} else if (document.getDoctype() != null) {
|
495
|
+
DocumentType docType = document.getDoctype();
|
496
|
+
IRubyObject name, publicId, systemId;
|
497
|
+
name = publicId = systemId = context.nil;
|
498
|
+
if (docType.getName() != null) {
|
499
|
+
name = context.runtime.newString(docType.getName());
|
500
|
+
}
|
501
|
+
if (docType.getPublicId() != null) {
|
502
|
+
publicId = context.runtime.newString(docType.getPublicId());
|
503
|
+
}
|
504
|
+
if (docType.getSystemId() != null) {
|
505
|
+
systemId = context.runtime.newString(docType.getSystemId());
|
506
|
+
}
|
507
|
+
dtd = XmlDtd.newEmpty(context.runtime, document, name, publicId, systemId);
|
508
|
+
} else {
|
509
|
+
dtd = context.nil;
|
510
|
+
}
|
511
|
+
|
512
|
+
setInternalSubset(dtd);
|
513
|
+
}
|
514
|
+
|
515
|
+
return dtd;
|
516
|
+
}
|
517
|
+
|
518
|
+
/**
|
519
|
+
* Assumes XmlNode#internal_subset() has returned nil. (i.e. there
|
520
|
+
* is not already an internal subset).
|
521
|
+
*/
|
522
|
+
public IRubyObject
|
523
|
+
createInternalSubset(ThreadContext context,
|
524
|
+
IRubyObject name,
|
525
|
+
IRubyObject external_id,
|
526
|
+
IRubyObject system_id)
|
527
|
+
{
|
528
|
+
XmlDtd dtd = XmlDtd.newEmpty(context.runtime, getDocument(), name, external_id, system_id);
|
529
|
+
setInternalSubset(dtd);
|
530
|
+
return dtd;
|
531
|
+
}
|
532
|
+
|
533
|
+
protected void
|
534
|
+
setInternalSubset(IRubyObject data)
|
535
|
+
{
|
536
|
+
node.setUserData(DTD_INTERNAL_SUBSET, data, null);
|
537
|
+
}
|
538
|
+
|
539
|
+
public IRubyObject
|
540
|
+
getExternalSubset(ThreadContext context)
|
541
|
+
{
|
542
|
+
IRubyObject dtd = (IRubyObject) node.getUserData(DTD_EXTERNAL_SUBSET);
|
543
|
+
|
544
|
+
if (dtd == null) { return context.nil; }
|
545
|
+
return dtd;
|
546
|
+
}
|
547
|
+
|
548
|
+
/**
|
549
|
+
* Assumes XmlNode#external_subset() has returned nil. (i.e. there
|
550
|
+
* is not already an external subset).
|
551
|
+
*/
|
552
|
+
public IRubyObject
|
553
|
+
createExternalSubset(ThreadContext context,
|
554
|
+
IRubyObject name,
|
555
|
+
IRubyObject external_id,
|
556
|
+
IRubyObject system_id)
|
557
|
+
{
|
558
|
+
XmlDtd dtd = XmlDtd.newEmpty(context.runtime, getDocument(), name, external_id, system_id);
|
559
|
+
setExternalSubset(dtd);
|
560
|
+
return dtd;
|
561
|
+
}
|
562
|
+
|
563
|
+
protected void
|
564
|
+
setExternalSubset(IRubyObject data)
|
565
|
+
{
|
566
|
+
node.setUserData(DTD_EXTERNAL_SUBSET, data, null);
|
567
|
+
}
|
568
|
+
|
569
|
+
@Override
|
570
|
+
public void
|
571
|
+
accept(ThreadContext context, SaveContextVisitor visitor)
|
572
|
+
{
|
573
|
+
Document document = getDocument();
|
574
|
+
visitor.enter(document);
|
575
|
+
NodeList children = document.getChildNodes();
|
576
|
+
for (int i = 0; i < children.getLength(); i++) {
|
577
|
+
Node child = children.item(i);
|
578
|
+
short type = child.getNodeType();
|
579
|
+
if (type == Node.COMMENT_NODE) {
|
580
|
+
XmlComment xmlComment = (XmlComment) getCachedNodeOrCreate(context.runtime, child);
|
581
|
+
xmlComment.accept(context, visitor);
|
582
|
+
} else if (type == Node.DOCUMENT_TYPE_NODE) {
|
583
|
+
XmlDtd xmlDtd = (XmlDtd) getCachedNodeOrCreate(context.runtime, child);
|
584
|
+
xmlDtd.accept(context, visitor);
|
585
|
+
} else if (type == Node.PROCESSING_INSTRUCTION_NODE) {
|
586
|
+
XmlProcessingInstruction xmlProcessingInstruction = (XmlProcessingInstruction) getCachedNodeOrCreate(context.runtime,
|
587
|
+
child);
|
588
|
+
xmlProcessingInstruction.accept(context, visitor);
|
589
|
+
} else if (type == Node.TEXT_NODE) {
|
590
|
+
XmlText xmlText = (XmlText) getCachedNodeOrCreate(context.runtime, child);
|
591
|
+
xmlText.accept(context, visitor);
|
592
|
+
} else if (type == Node.ELEMENT_NODE) {
|
593
|
+
XmlElement xmlElement = (XmlElement) getCachedNodeOrCreate(context.runtime, child);
|
594
|
+
xmlElement.accept(context, visitor);
|
595
|
+
}
|
596
|
+
}
|
597
|
+
visitor.leave(document);
|
598
|
+
}
|
599
|
+
|
600
|
+
@JRubyMethod(meta = true)
|
601
|
+
public static IRubyObject
|
602
|
+
wrap(ThreadContext context, IRubyObject klass, IRubyObject arg)
|
603
|
+
{
|
604
|
+
XmlDocument xmlDocument = new XmlDocument(context.runtime, (RubyClass) klass, arg.toJava(Document.class));
|
605
|
+
Helpers.invoke(context, xmlDocument, "initialize");
|
606
|
+
return xmlDocument;
|
607
|
+
}
|
608
|
+
|
609
|
+
@Deprecated
|
610
|
+
@JRubyMethod(meta = true, visibility = Visibility.PRIVATE)
|
611
|
+
public static IRubyObject
|
612
|
+
wrapJavaDocument(ThreadContext context, IRubyObject klass, IRubyObject arg)
|
613
|
+
{
|
614
|
+
return wrap(context, klass, arg);
|
615
|
+
}
|
616
|
+
|
617
|
+
@Deprecated // default to_java works (due inherited from XmlNode#toJava)
|
618
|
+
@JRubyMethod(visibility = Visibility.PRIVATE)
|
619
|
+
public IRubyObject
|
620
|
+
toJavaDocument(ThreadContext context)
|
621
|
+
{
|
622
|
+
return JavaUtil.convertJavaToUsableRubyObject(context.getRuntime(), node);
|
623
|
+
}
|
624
|
+
|
625
|
+
/* call-seq:
|
626
|
+
* doc.canonicalize(mode=XML_C14N_1_0,inclusive_namespaces=nil,with_comments=false)
|
627
|
+
* doc.canonicalize { |obj, parent| ... }
|
628
|
+
*
|
629
|
+
* Canonicalize a document and return the results. Takes an optional block
|
630
|
+
* that takes two parameters: the +obj+ and that node's +parent+.
|
631
|
+
* The +obj+ will be either a Nokogiri::XML::Node, or a Nokogiri::XML::Namespace
|
632
|
+
* The block must return a non-nil, non-false value if the +obj+ passed in
|
633
|
+
* should be included in the canonicalized document.
|
634
|
+
*/
|
635
|
+
@JRubyMethod(optional = 3)
|
636
|
+
public IRubyObject
|
637
|
+
canonicalize(ThreadContext context, IRubyObject[] args, Block block)
|
638
|
+
{
|
639
|
+
int mode = 0;
|
640
|
+
String inclusive_namespace = null;
|
641
|
+
Boolean with_comments = false;
|
642
|
+
if (args.length > 0 && !(args[0].isNil())) {
|
643
|
+
mode = RubyFixnum.fix2int(args[0]);
|
644
|
+
}
|
645
|
+
if (args.length > 1) {
|
646
|
+
if (!args[1].isNil() && !(args[1] instanceof List)) {
|
647
|
+
throw context.runtime.newTypeError("Expected array");
|
648
|
+
}
|
649
|
+
if (!args[1].isNil()) {
|
650
|
+
inclusive_namespace = ((RubyArray)args[1])
|
651
|
+
.join(context, context.runtime.newString(" "))
|
652
|
+
.asString()
|
653
|
+
.asJavaString(); // OMG I wish I knew JRuby better, this is ugly
|
654
|
+
}
|
655
|
+
}
|
656
|
+
if (args.length > 2) {
|
657
|
+
with_comments = args[2].isTrue();
|
658
|
+
}
|
659
|
+
String algorithmURI = null;
|
660
|
+
switch (mode) {
|
661
|
+
case 0: // XML_C14N_1_0
|
662
|
+
if (with_comments) { algorithmURI = Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS; }
|
663
|
+
else { algorithmURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS; }
|
664
|
+
break;
|
665
|
+
case 1: // XML_C14N_EXCLUSIVE_1_0
|
666
|
+
if (with_comments) { algorithmURI = Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS; }
|
667
|
+
else { algorithmURI = Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS; }
|
668
|
+
break;
|
669
|
+
case 2: // XML_C14N_1_1 = 2
|
670
|
+
if (with_comments) { algorithmURI = Canonicalizer.ALGO_ID_C14N11_WITH_COMMENTS; }
|
671
|
+
else { algorithmURI = Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS; }
|
672
|
+
}
|
673
|
+
try {
|
674
|
+
Canonicalizer canonicalizer = Canonicalizer.getInstance(algorithmURI);
|
675
|
+
XmlNode startingNode = getStartingNode(block);
|
676
|
+
byte[] result;
|
677
|
+
CanonicalFilter filter = new CanonicalFilter(context, block);
|
678
|
+
if (inclusive_namespace == null) {
|
679
|
+
result = canonicalizer.canonicalizeSubtree(startingNode.getNode(), filter);
|
680
|
+
} else {
|
681
|
+
result = canonicalizer.canonicalizeSubtree(startingNode.getNode(), inclusive_namespace, filter);
|
682
|
+
}
|
683
|
+
return RubyString.newString(context.runtime, new ByteList(result, UTF8Encoding.INSTANCE));
|
684
|
+
} catch (Exception e) {
|
685
|
+
throw context.getRuntime().newRuntimeError(e.getMessage());
|
686
|
+
}
|
687
|
+
}
|
688
|
+
|
689
|
+
private XmlNode
|
690
|
+
getStartingNode(Block block)
|
691
|
+
{
|
692
|
+
if (block.isGiven()) {
|
693
|
+
IRubyObject boundSelf = block.getBinding().getSelf();
|
694
|
+
if (boundSelf instanceof XmlNode) { return (XmlNode) boundSelf; }
|
695
|
+
}
|
696
|
+
return this;
|
697
|
+
}
|
698
|
+
|
699
|
+
public void
|
700
|
+
resetNamespaceCache(ThreadContext context)
|
701
|
+
{
|
702
|
+
nsCache = new NokogiriNamespaceCache();
|
703
|
+
createAndCacheNamespaces(context.runtime, node);
|
704
|
+
}
|
705
|
+
}
|