nokogiri-fitzsimmons 1.5.5.3-java
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +26 -0
- data/.gemtest +0 -0
- data/CHANGELOG.ja.rdoc +666 -0
- data/CHANGELOG.rdoc +659 -0
- data/C_CODING_STYLE.rdoc +33 -0
- data/Manifest.txt +295 -0
- data/README.ja.rdoc +106 -0
- data/README.rdoc +178 -0
- data/ROADMAP.md +86 -0
- data/Rakefile +194 -0
- data/STANDARD_RESPONSES.md +47 -0
- data/Y_U_NO_GEMSPEC.md +155 -0
- data/bin/nokogiri +63 -0
- data/build_all +58 -0
- data/ext/java/nokogiri/EncodingHandler.java +124 -0
- data/ext/java/nokogiri/HtmlDocument.java +163 -0
- data/ext/java/nokogiri/HtmlElementDescription.java +145 -0
- data/ext/java/nokogiri/HtmlEntityLookup.java +79 -0
- data/ext/java/nokogiri/HtmlSaxParserContext.java +259 -0
- data/ext/java/nokogiri/NokogiriService.java +598 -0
- data/ext/java/nokogiri/XmlAttr.java +190 -0
- data/ext/java/nokogiri/XmlAttributeDecl.java +130 -0
- data/ext/java/nokogiri/XmlCdata.java +84 -0
- data/ext/java/nokogiri/XmlComment.java +95 -0
- data/ext/java/nokogiri/XmlDocument.java +580 -0
- data/ext/java/nokogiri/XmlDocumentFragment.java +234 -0
- data/ext/java/nokogiri/XmlDtd.java +469 -0
- data/ext/java/nokogiri/XmlElement.java +97 -0
- data/ext/java/nokogiri/XmlElementContent.java +382 -0
- data/ext/java/nokogiri/XmlElementDecl.java +152 -0
- data/ext/java/nokogiri/XmlEntityDecl.java +162 -0
- data/ext/java/nokogiri/XmlEntityReference.java +97 -0
- data/ext/java/nokogiri/XmlNamespace.java +215 -0
- data/ext/java/nokogiri/XmlNode.java +1534 -0
- data/ext/java/nokogiri/XmlNodeSet.java +270 -0
- data/ext/java/nokogiri/XmlProcessingInstruction.java +99 -0
- data/ext/java/nokogiri/XmlReader.java +456 -0
- data/ext/java/nokogiri/XmlRelaxng.java +144 -0
- data/ext/java/nokogiri/XmlSaxParserContext.java +356 -0
- data/ext/java/nokogiri/XmlSaxPushParser.java +215 -0
- data/ext/java/nokogiri/XmlSchema.java +324 -0
- data/ext/java/nokogiri/XmlSyntaxError.java +136 -0
- data/ext/java/nokogiri/XmlText.java +119 -0
- data/ext/java/nokogiri/XmlXpathContext.java +203 -0
- data/ext/java/nokogiri/XsltStylesheet.java +360 -0
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +243 -0
- data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +73 -0
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +86 -0
- data/ext/java/nokogiri/internals/NokogiriHandler.java +333 -0
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +800 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +163 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +130 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +74 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +121 -0
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +79 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +141 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +73 -0
- data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +67 -0
- data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +87 -0
- data/ext/java/nokogiri/internals/ParserContext.java +288 -0
- data/ext/java/nokogiri/internals/ReaderNode.java +531 -0
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +775 -0
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +76 -0
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +42 -0
- data/ext/java/nokogiri/internals/XmlDomParser.java +105 -0
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +266 -0
- data/ext/java/nokogiri/internals/XmlSaxParser.java +65 -0
- data/ext/java/nokogiri/internals/XsltExtensionFunction.java +72 -0
- data/ext/nokogiri/depend +358 -0
- data/ext/nokogiri/extconf.rb +142 -0
- data/ext/nokogiri/html_document.c +170 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +276 -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 +133 -0
- data/ext/nokogiri/nokogiri.h +160 -0
- data/ext/nokogiri/xml_attr.c +94 -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 +56 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +54 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +576 -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 +56 -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 +78 -0
- data/ext/nokogiri/xml_namespace.h +13 -0
- data/ext/nokogiri/xml_node.c +1480 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +467 -0
- data/ext/nokogiri/xml_node_set.h +14 -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 +684 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +161 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +293 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +222 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +115 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +205 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +58 -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 +319 -0
- data/ext/nokogiri/xml_xpath_context.h +10 -0
- data/ext/nokogiri/xslt_stylesheet.c +270 -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.rb +127 -0
- data/lib/nokogiri/css.rb +27 -0
- data/lib/nokogiri/css/node.rb +102 -0
- data/lib/nokogiri/css/parser.rb +720 -0
- data/lib/nokogiri/css/parser.y +258 -0
- data/lib/nokogiri/css/parser_extras.rb +91 -0
- data/lib/nokogiri/css/syntax_error.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rb +152 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +171 -0
- data/lib/nokogiri/decorators/slop.rb +35 -0
- data/lib/nokogiri/html.rb +37 -0
- data/lib/nokogiri/html/builder.rb +35 -0
- data/lib/nokogiri/html/document.rb +254 -0
- data/lib/nokogiri/html/document_fragment.rb +41 -0
- data/lib/nokogiri/html/element_description.rb +23 -0
- data/lib/nokogiri/html/element_description_defaults.rb +671 -0
- data/lib/nokogiri/html/entity_lookup.rb +13 -0
- data/lib/nokogiri/html/sax/parser.rb +52 -0
- data/lib/nokogiri/html/sax/parser_context.rb +16 -0
- data/lib/nokogiri/html/sax/push_parser.rb +16 -0
- data/lib/nokogiri/nokogiri.jar +0 -0
- data/lib/nokogiri/syntax_error.rb +4 -0
- data/lib/nokogiri/version.rb +88 -0
- data/lib/nokogiri/xml.rb +73 -0
- data/lib/nokogiri/xml/attr.rb +14 -0
- data/lib/nokogiri/xml/attribute_decl.rb +18 -0
- data/lib/nokogiri/xml/builder.rb +431 -0
- data/lib/nokogiri/xml/cdata.rb +11 -0
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +267 -0
- data/lib/nokogiri/xml/document_fragment.rb +103 -0
- data/lib/nokogiri/xml/dtd.rb +22 -0
- data/lib/nokogiri/xml/element_content.rb +36 -0
- data/lib/nokogiri/xml/element_decl.rb +13 -0
- data/lib/nokogiri/xml/entity_decl.rb +19 -0
- data/lib/nokogiri/xml/namespace.rb +13 -0
- data/lib/nokogiri/xml/node.rb +946 -0
- data/lib/nokogiri/xml/node/save_options.rb +61 -0
- data/lib/nokogiri/xml/node_set.rb +357 -0
- data/lib/nokogiri/xml/notation.rb +6 -0
- data/lib/nokogiri/xml/parse_options.rb +98 -0
- data/lib/nokogiri/xml/pp.rb +2 -0
- data/lib/nokogiri/xml/pp/character_data.rb +18 -0
- data/lib/nokogiri/xml/pp/node.rb +56 -0
- data/lib/nokogiri/xml/processing_instruction.rb +8 -0
- data/lib/nokogiri/xml/reader.rb +112 -0
- data/lib/nokogiri/xml/relax_ng.rb +32 -0
- data/lib/nokogiri/xml/sax.rb +4 -0
- data/lib/nokogiri/xml/sax/document.rb +164 -0
- data/lib/nokogiri/xml/sax/parser.rb +115 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
- data/lib/nokogiri/xml/schema.rb +63 -0
- data/lib/nokogiri/xml/syntax_error.rb +47 -0
- data/lib/nokogiri/xml/text.rb +9 -0
- data/lib/nokogiri/xml/xpath.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xslt.rb +56 -0
- data/lib/nokogiri/xslt/stylesheet.rb +25 -0
- data/lib/xercesImpl.jar +0 -0
- data/lib/xsd/xmlparser/nokogiri.rb +90 -0
- data/tasks/cross_compile.rb +153 -0
- data/tasks/nokogiri.org.rb +24 -0
- data/tasks/test.rb +95 -0
- data/test/css/test_nthiness.rb +159 -0
- data/test/css/test_parser.rb +341 -0
- data/test/css/test_tokenizer.rb +198 -0
- data/test/css/test_xpath_visitor.rb +91 -0
- data/test/decorators/test_slop.rb +16 -0
- data/test/files/2ch.html +108 -0
- data/test/files/address_book.rlx +12 -0
- data/test/files/address_book.xml +10 -0
- data/test/files/bar/bar.xsd +4 -0
- data/test/files/dont_hurt_em_why.xml +422 -0
- data/test/files/encoding.html +82 -0
- data/test/files/encoding.xhtml +84 -0
- data/test/files/exslt.xml +8 -0
- data/test/files/exslt.xslt +35 -0
- data/test/files/foo/foo.xsd +4 -0
- data/test/files/metacharset.html +10 -0
- data/test/files/noencoding.html +47 -0
- data/test/files/po.xml +32 -0
- data/test/files/po.xsd +66 -0
- data/test/files/shift_jis.html +10 -0
- data/test/files/shift_jis.xml +5 -0
- data/test/files/snuggles.xml +3 -0
- data/test/files/staff.dtd +10 -0
- data/test/files/staff.xml +59 -0
- data/test/files/staff.xslt +32 -0
- data/test/files/tlm.html +850 -0
- data/test/files/to_be_xincluded.xml +2 -0
- data/test/files/valid_bar.xml +2 -0
- data/test/files/xinclude.xml +4 -0
- data/test/helper.rb +147 -0
- data/test/html/sax/test_parser.rb +138 -0
- data/test/html/sax/test_parser_context.rb +46 -0
- data/test/html/test_builder.rb +164 -0
- data/test/html/test_document.rb +529 -0
- data/test/html/test_document_encoding.rb +138 -0
- data/test/html/test_document_fragment.rb +254 -0
- data/test/html/test_element_description.rb +100 -0
- data/test/html/test_named_characters.rb +14 -0
- data/test/html/test_node.rb +188 -0
- data/test/html/test_node_encoding.rb +27 -0
- data/test/test_convert_xpath.rb +135 -0
- data/test/test_css_cache.rb +45 -0
- data/test/test_encoding_handler.rb +46 -0
- data/test/test_memory_leak.rb +152 -0
- data/test/test_nokogiri.rb +132 -0
- data/test/test_reader.rb +488 -0
- data/test/test_soap4r_sax.rb +52 -0
- data/test/test_xslt_transforms.rb +254 -0
- data/test/xml/node/test_save_options.rb +28 -0
- data/test/xml/node/test_subclass.rb +44 -0
- data/test/xml/sax/test_parser.rb +338 -0
- data/test/xml/sax/test_parser_context.rb +106 -0
- data/test/xml/sax/test_push_parser.rb +157 -0
- data/test/xml/test_attr.rb +64 -0
- data/test/xml/test_attribute_decl.rb +86 -0
- data/test/xml/test_builder.rb +248 -0
- data/test/xml/test_c14n.rb +151 -0
- data/test/xml/test_cdata.rb +48 -0
- data/test/xml/test_comment.rb +29 -0
- data/test/xml/test_document.rb +742 -0
- data/test/xml/test_document_encoding.rb +28 -0
- data/test/xml/test_document_fragment.rb +216 -0
- data/test/xml/test_dtd.rb +103 -0
- data/test/xml/test_dtd_encoding.rb +33 -0
- data/test/xml/test_element_content.rb +56 -0
- data/test/xml/test_element_decl.rb +73 -0
- data/test/xml/test_entity_decl.rb +122 -0
- data/test/xml/test_entity_reference.rb +235 -0
- data/test/xml/test_namespace.rb +75 -0
- data/test/xml/test_node.rb +1029 -0
- data/test/xml/test_node_attributes.rb +53 -0
- data/test/xml/test_node_encoding.rb +107 -0
- data/test/xml/test_node_inheritance.rb +32 -0
- data/test/xml/test_node_reparenting.rb +374 -0
- data/test/xml/test_node_set.rb +755 -0
- data/test/xml/test_parse_options.rb +64 -0
- data/test/xml/test_processing_instruction.rb +30 -0
- data/test/xml/test_reader_encoding.rb +142 -0
- data/test/xml/test_relax_ng.rb +60 -0
- data/test/xml/test_schema.rb +94 -0
- data/test/xml/test_syntax_error.rb +12 -0
- data/test/xml/test_text.rb +45 -0
- data/test/xml/test_unparented_node.rb +413 -0
- data/test/xml/test_xinclude.rb +83 -0
- data/test/xml/test_xpath.rb +295 -0
- data/test/xslt/test_custom_functions.rb +129 -0
- data/test/xslt/test_exception_handling.rb +37 -0
- data/test_all +84 -0
- metadata +571 -0
@@ -0,0 +1,800 @@
|
|
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.internals;
|
34
|
+
|
35
|
+
import java.io.File;
|
36
|
+
import java.io.UnsupportedEncodingException;
|
37
|
+
import java.lang.reflect.InvocationTargetException;
|
38
|
+
import java.lang.reflect.Method;
|
39
|
+
import java.nio.ByteBuffer;
|
40
|
+
import java.nio.CharBuffer;
|
41
|
+
import java.nio.charset.CharacterCodingException;
|
42
|
+
import java.nio.charset.Charset;
|
43
|
+
import java.nio.charset.CharsetDecoder;
|
44
|
+
import java.nio.charset.CharsetEncoder;
|
45
|
+
import java.util.ArrayList;
|
46
|
+
import java.util.List;
|
47
|
+
import java.util.Set;
|
48
|
+
import java.util.SortedMap;
|
49
|
+
import java.util.regex.Matcher;
|
50
|
+
import java.util.regex.Pattern;
|
51
|
+
|
52
|
+
import nokogiri.HtmlDocument;
|
53
|
+
import nokogiri.NokogiriService;
|
54
|
+
import nokogiri.XmlAttr;
|
55
|
+
import nokogiri.XmlCdata;
|
56
|
+
import nokogiri.XmlComment;
|
57
|
+
import nokogiri.XmlDocument;
|
58
|
+
import nokogiri.XmlDtd;
|
59
|
+
import nokogiri.XmlElement;
|
60
|
+
import nokogiri.XmlEntityReference;
|
61
|
+
import nokogiri.XmlNamespace;
|
62
|
+
import nokogiri.XmlNode;
|
63
|
+
import nokogiri.XmlProcessingInstruction;
|
64
|
+
import nokogiri.XmlText;
|
65
|
+
|
66
|
+
import org.jruby.Ruby;
|
67
|
+
import org.jruby.RubyArray;
|
68
|
+
import org.jruby.RubyClass;
|
69
|
+
import org.jruby.RubyEncoding;
|
70
|
+
import org.jruby.RubyString;
|
71
|
+
import org.jruby.runtime.ThreadContext;
|
72
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
73
|
+
import org.jruby.util.ByteList;
|
74
|
+
import org.w3c.dom.Attr;
|
75
|
+
import org.w3c.dom.NamedNodeMap;
|
76
|
+
import org.w3c.dom.Node;
|
77
|
+
import org.w3c.dom.NodeList;
|
78
|
+
|
79
|
+
/**
|
80
|
+
* A class for various utility methods.
|
81
|
+
*
|
82
|
+
* @author serabe
|
83
|
+
* @author Patrick Mahoney <pat@polycrystal.org>
|
84
|
+
* @author Yoko Harada <yokolet@gmail.com>
|
85
|
+
*/
|
86
|
+
public class NokogiriHelpers {
|
87
|
+
public static final String CACHED_NODE = "NOKOGIRI_CACHED_NODE";
|
88
|
+
public static final String VALID_ROOT_NODE = "NOKOGIRI_VALIDE_ROOT_NODE";
|
89
|
+
public static final String ENCODED_STRING = "NOKOGIRI_ENCODED_STRING";
|
90
|
+
|
91
|
+
public static XmlNode getCachedNode(Node node) {
|
92
|
+
return (XmlNode) node.getUserData(CACHED_NODE);
|
93
|
+
}
|
94
|
+
|
95
|
+
/**
|
96
|
+
* Get the XmlNode associated with the underlying
|
97
|
+
* <code>node</code>. Creates a new XmlNode (or appropriate subclass)
|
98
|
+
* or XmlNamespace wrapping <code>node</code> if there is no cached
|
99
|
+
* value.
|
100
|
+
*/
|
101
|
+
public static IRubyObject getCachedNodeOrCreate(Ruby ruby, Node node) {
|
102
|
+
if(node == null) return ruby.getNil();
|
103
|
+
if (node.getNodeType() == Node.ATTRIBUTE_NODE && isNamespace(node.getNodeName())) {
|
104
|
+
XmlDocument xmlDocument = (XmlDocument)node.getOwnerDocument().getUserData(CACHED_NODE);
|
105
|
+
if (!(xmlDocument instanceof HtmlDocument)) {
|
106
|
+
String prefix = getLocalNameForNamespace(((Attr)node).getName());
|
107
|
+
prefix = prefix != null ? prefix : "";
|
108
|
+
String href = ((Attr)node).getValue();
|
109
|
+
XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(prefix, href);
|
110
|
+
if (xmlNamespace != null) return xmlNamespace;
|
111
|
+
else return XmlNamespace.createFromAttr(ruby, (Attr)node);
|
112
|
+
}
|
113
|
+
}
|
114
|
+
XmlNode xmlNode = getCachedNode(node);
|
115
|
+
if(xmlNode == null) {
|
116
|
+
xmlNode = (XmlNode)constructNode(ruby, node);
|
117
|
+
node.setUserData(CACHED_NODE, xmlNode, null);
|
118
|
+
}
|
119
|
+
return xmlNode;
|
120
|
+
}
|
121
|
+
|
122
|
+
/**
|
123
|
+
* Construct a new XmlNode wrapping <code>node</code>. The proper
|
124
|
+
* subclass of XmlNode is chosen based on the type of
|
125
|
+
* <code>node</code>.
|
126
|
+
*/
|
127
|
+
public static IRubyObject constructNode(Ruby runtime, Node node) {
|
128
|
+
if (node == null) return runtime.getNil();
|
129
|
+
// this is slow; need a way to cache nokogiri classes/modules somewhere
|
130
|
+
switch (node.getNodeType()) {
|
131
|
+
case Node.ELEMENT_NODE:
|
132
|
+
XmlElement xmlElement = (XmlElement) NokogiriService.XML_ELEMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Element"));
|
133
|
+
xmlElement.setNode(runtime.getCurrentContext(), node);
|
134
|
+
return xmlElement;
|
135
|
+
case Node.ATTRIBUTE_NODE:
|
136
|
+
XmlAttr xmlAttr = (XmlAttr) NokogiriService.XML_ATTR_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Attr"));
|
137
|
+
xmlAttr.setNode(runtime.getCurrentContext(), node);
|
138
|
+
return xmlAttr;
|
139
|
+
case Node.TEXT_NODE:
|
140
|
+
XmlText xmlText = (XmlText) NokogiriService.XML_TEXT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Text"));
|
141
|
+
xmlText.setNode(runtime.getCurrentContext(), node);
|
142
|
+
return xmlText;
|
143
|
+
case Node.COMMENT_NODE:
|
144
|
+
XmlComment xmlComment = (XmlComment) NokogiriService.XML_COMMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Comment"));
|
145
|
+
xmlComment.setNode(runtime.getCurrentContext(), node);
|
146
|
+
return xmlComment;
|
147
|
+
case Node.ENTITY_NODE:
|
148
|
+
return new XmlNode(runtime, getNokogiriClass(runtime, "Nokogiri::XML::EntityDecl"), node);
|
149
|
+
case Node.ENTITY_REFERENCE_NODE:
|
150
|
+
XmlEntityReference xmlEntityRef = (XmlEntityReference) NokogiriService.XML_ENTITY_REFERENCE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::EntityReference"));
|
151
|
+
xmlEntityRef.setNode(runtime.getCurrentContext(), node);
|
152
|
+
return xmlEntityRef;
|
153
|
+
case Node.PROCESSING_INSTRUCTION_NODE:
|
154
|
+
XmlProcessingInstruction xmlProcessingInstruction = (XmlProcessingInstruction) NokogiriService.XML_PROCESSING_INSTRUCTION_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::ProcessingInstruction"));
|
155
|
+
xmlProcessingInstruction.setNode(runtime.getCurrentContext(), node);
|
156
|
+
return xmlProcessingInstruction;
|
157
|
+
case Node.CDATA_SECTION_NODE:
|
158
|
+
XmlCdata xmlCdata = (XmlCdata) NokogiriService.XML_CDATA_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::CDATA"));
|
159
|
+
xmlCdata.setNode(runtime.getCurrentContext(), node);
|
160
|
+
return xmlCdata;
|
161
|
+
case Node.DOCUMENT_NODE:
|
162
|
+
XmlDocument xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Document"));
|
163
|
+
xmlDocument.setDocumentNode(runtime.getCurrentContext(), node);
|
164
|
+
return xmlDocument;
|
165
|
+
case Node.DOCUMENT_TYPE_NODE:
|
166
|
+
XmlDtd xmlDtd = (XmlDtd) NokogiriService.XML_DTD_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::DTD"));
|
167
|
+
xmlDtd.setNode(runtime, node);
|
168
|
+
return xmlDtd;
|
169
|
+
default:
|
170
|
+
XmlNode xmlNode = (XmlNode) NokogiriService.XML_NODE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Node"));
|
171
|
+
xmlNode.setNode(runtime.getCurrentContext(), node);
|
172
|
+
return xmlNode;
|
173
|
+
}
|
174
|
+
}
|
175
|
+
|
176
|
+
public static RubyClass getNokogiriClass(Ruby ruby, String name) {
|
177
|
+
return NokogiriService.nokogiriClassCache.get(name);
|
178
|
+
}
|
179
|
+
|
180
|
+
public static IRubyObject stringOrNil(Ruby runtime, String s) {
|
181
|
+
if (s == null) return runtime.getNil();
|
182
|
+
return RubyString.newString(runtime, s);
|
183
|
+
}
|
184
|
+
|
185
|
+
public static IRubyObject stringOrNil(Ruby runtime, byte[] bytes) {
|
186
|
+
if (bytes == null) return runtime.getNil();
|
187
|
+
return RubyString.newString(runtime, bytes);
|
188
|
+
}
|
189
|
+
|
190
|
+
public static IRubyObject stringOrBlank(Ruby runtime, String s) {
|
191
|
+
if (s == null) return runtime.newString();
|
192
|
+
return RubyString.newString(runtime, s);
|
193
|
+
}
|
194
|
+
|
195
|
+
/**
|
196
|
+
* Convert <code>s</code> to a RubyString, or if s is null or
|
197
|
+
* empty return RubyNil.
|
198
|
+
*/
|
199
|
+
public static IRubyObject nonEmptyStringOrNil(Ruby runtime, String s) {
|
200
|
+
if (s == null || s.length() == 0) return runtime.getNil();
|
201
|
+
return RubyString.newString(runtime, s);
|
202
|
+
}
|
203
|
+
|
204
|
+
/**
|
205
|
+
* Return the prefix of a qualified name like "prefix:local".
|
206
|
+
* Returns null if there is no prefix.
|
207
|
+
*/
|
208
|
+
public static String getPrefix(String qName) {
|
209
|
+
if (qName == null) return null;
|
210
|
+
|
211
|
+
int pos = qName.indexOf(':');
|
212
|
+
if (pos > 0)
|
213
|
+
return qName.substring(0, pos);
|
214
|
+
else
|
215
|
+
return null;
|
216
|
+
}
|
217
|
+
|
218
|
+
/**
|
219
|
+
* Return the local part of a qualified name like "prefix:local".
|
220
|
+
* Returns <code>qName</code> if there is no prefix.
|
221
|
+
*/
|
222
|
+
public static String getLocalPart(String qName) {
|
223
|
+
if (qName == null) return null;
|
224
|
+
|
225
|
+
int pos = qName.indexOf(':');
|
226
|
+
if (pos > 0)
|
227
|
+
return qName.substring(pos + 1);
|
228
|
+
else
|
229
|
+
return qName;
|
230
|
+
}
|
231
|
+
|
232
|
+
public static String getLocalNameForNamespace(String name) {
|
233
|
+
String localName = getLocalPart(name);
|
234
|
+
return ("xmlns".equals(localName)) ? null : localName;
|
235
|
+
}
|
236
|
+
|
237
|
+
private static Charset utf8 = null;
|
238
|
+
|
239
|
+
private static Charset getCharsetUTF8() {
|
240
|
+
if (utf8 == null) utf8 = Charset.forName("UTF-8");
|
241
|
+
return utf8;
|
242
|
+
}
|
243
|
+
|
244
|
+
/**
|
245
|
+
* Converts a RubyString in to a Java String. Assumes the
|
246
|
+
* RubyString is encoded as UTF-8. This is generally the case for
|
247
|
+
* RubyStrings created with getRuntime().newString("java string").
|
248
|
+
* It also seems to be the case for strings created within Ruby
|
249
|
+
* where $KCODE has not been set.
|
250
|
+
*
|
251
|
+
* Note that RubyString#toString() decodes the string data as
|
252
|
+
* ISO-8859-1 (See org.jruby.util.ByteList.java). This is not
|
253
|
+
* what you want if you have any multibyte characters in your
|
254
|
+
* UTF-8 string.
|
255
|
+
*
|
256
|
+
* FIXME: This really needs to be more robust in terms of
|
257
|
+
* detecting the encoding and properly converting to a Java
|
258
|
+
* String. It's unfortunate that RubyString#toString() doesn't do
|
259
|
+
* this for us.
|
260
|
+
*/
|
261
|
+
public static String rubyStringToString(IRubyObject str) {
|
262
|
+
if (str.isNil()) return null;
|
263
|
+
//return rubyStringToString(str.convertToString());
|
264
|
+
return toJavaString(str.convertToString());
|
265
|
+
}
|
266
|
+
|
267
|
+
private static String toJavaString(RubyString str) {
|
268
|
+
ByteList value = str.getByteList();
|
269
|
+
try {
|
270
|
+
if (str.getRuntime().is1_9()) {
|
271
|
+
return new String(value.getUnsafeBytes(), value.begin(), value.length(), str.getEncoding().toString());
|
272
|
+
}
|
273
|
+
return RubyEncoding.decodeUTF8(value.getUnsafeBytes(), value.begin(), value.length());
|
274
|
+
} catch (UnsupportedEncodingException uee) {
|
275
|
+
return str.toString();
|
276
|
+
}
|
277
|
+
}
|
278
|
+
|
279
|
+
public static String rubyStringToString(RubyString str) {
|
280
|
+
ByteList byteList = str.getByteList();
|
281
|
+
byte[] data = byteList.unsafeBytes();
|
282
|
+
int offset = byteList.begin();
|
283
|
+
int len = byteList.length();
|
284
|
+
ByteBuffer buf = ByteBuffer.wrap(data, offset, len);
|
285
|
+
return getCharsetUTF8().decode(buf).toString();
|
286
|
+
}
|
287
|
+
|
288
|
+
public static List<String> rubyStringArrayToJavaList(RubyArray ary) {
|
289
|
+
List<String> list = new ArrayList<String>();
|
290
|
+
for (int i=0; i < ary.getLength(); i++) {
|
291
|
+
Object obj = ary.get(i);
|
292
|
+
if (obj != null) list.add(obj.toString());
|
293
|
+
}
|
294
|
+
return list;
|
295
|
+
}
|
296
|
+
|
297
|
+
public static String getNodeCompletePath(Node node) {
|
298
|
+
|
299
|
+
Node cur, tmp, next;
|
300
|
+
|
301
|
+
// TODO: Rename buffer to path.
|
302
|
+
String buffer = "";
|
303
|
+
String sep;
|
304
|
+
String name;
|
305
|
+
|
306
|
+
int occur = 0;
|
307
|
+
boolean generic;
|
308
|
+
|
309
|
+
cur = node;
|
310
|
+
|
311
|
+
do {
|
312
|
+
name = "";
|
313
|
+
sep = "?";
|
314
|
+
occur = 0;
|
315
|
+
generic = false;
|
316
|
+
|
317
|
+
if(cur.getNodeType() == Node.DOCUMENT_NODE) {
|
318
|
+
if(buffer.startsWith("/")) break;
|
319
|
+
|
320
|
+
sep = "/";
|
321
|
+
next = null;
|
322
|
+
} else if(cur.getNodeType() == Node.ELEMENT_NODE) {
|
323
|
+
generic = false;
|
324
|
+
sep = "/";
|
325
|
+
|
326
|
+
name = cur.getLocalName();
|
327
|
+
if (name == null) name = cur.getNodeName();
|
328
|
+
if(cur.getNamespaceURI() != null) {
|
329
|
+
if(cur.getPrefix() != null) {
|
330
|
+
name = cur.getPrefix() + ":" + name;
|
331
|
+
} else {
|
332
|
+
generic = true;
|
333
|
+
name = "*";
|
334
|
+
}
|
335
|
+
}
|
336
|
+
|
337
|
+
next = cur.getParentNode();
|
338
|
+
|
339
|
+
/*
|
340
|
+
* Thumbler index computation
|
341
|
+
*/
|
342
|
+
|
343
|
+
tmp = cur.getPreviousSibling();
|
344
|
+
|
345
|
+
while(tmp != null) {
|
346
|
+
if((tmp.getNodeType() == Node.ELEMENT_NODE) &&
|
347
|
+
(generic || fullNamesMatch(tmp, cur))) {
|
348
|
+
occur++;
|
349
|
+
}
|
350
|
+
tmp = tmp.getPreviousSibling();
|
351
|
+
}
|
352
|
+
|
353
|
+
if(occur == 0) {
|
354
|
+
tmp = cur.getNextSibling();
|
355
|
+
|
356
|
+
while(tmp != null && occur == 0) {
|
357
|
+
if((tmp.getNodeType() == Node.ELEMENT_NODE) &&
|
358
|
+
(generic || fullNamesMatch(tmp,cur))) {
|
359
|
+
occur++;
|
360
|
+
}
|
361
|
+
tmp = tmp.getNextSibling();
|
362
|
+
}
|
363
|
+
|
364
|
+
if(occur != 0) occur = 1;
|
365
|
+
|
366
|
+
} else {
|
367
|
+
occur++;
|
368
|
+
}
|
369
|
+
} else if(cur.getNodeType() == Node.COMMENT_NODE) {
|
370
|
+
sep = "/";
|
371
|
+
name = "comment()";
|
372
|
+
next = cur.getParentNode();
|
373
|
+
|
374
|
+
/*
|
375
|
+
* Thumbler index computation.
|
376
|
+
*/
|
377
|
+
|
378
|
+
tmp = cur.getPreviousSibling();
|
379
|
+
|
380
|
+
while(tmp != null) {
|
381
|
+
if(tmp.getNodeType() == Node.COMMENT_NODE) {
|
382
|
+
occur++;
|
383
|
+
}
|
384
|
+
tmp = tmp.getPreviousSibling();
|
385
|
+
}
|
386
|
+
|
387
|
+
if(occur == 0) {
|
388
|
+
tmp = cur.getNextSibling();
|
389
|
+
while(tmp != null && occur == 0) {
|
390
|
+
if(tmp.getNodeType() == Node.COMMENT_NODE) {
|
391
|
+
occur++;
|
392
|
+
}
|
393
|
+
tmp = tmp.getNextSibling();
|
394
|
+
}
|
395
|
+
if(occur != 0) occur = 1;
|
396
|
+
} else {
|
397
|
+
occur = 1;
|
398
|
+
}
|
399
|
+
|
400
|
+
} else if(cur.getNodeType() == Node.TEXT_NODE ||
|
401
|
+
cur.getNodeType() == Node.CDATA_SECTION_NODE) {
|
402
|
+
// I'm here. gist:129
|
403
|
+
// http://gist.github.com/144923
|
404
|
+
|
405
|
+
sep = "/";
|
406
|
+
name = "text()";
|
407
|
+
next = cur.getParentNode();
|
408
|
+
|
409
|
+
/*
|
410
|
+
* Thumbler index computation.
|
411
|
+
*/
|
412
|
+
|
413
|
+
tmp = cur.getPreviousSibling();
|
414
|
+
while(tmp != null) {
|
415
|
+
if(tmp.getNodeType() == Node.TEXT_NODE ||
|
416
|
+
tmp.getNodeType() == Node.CDATA_SECTION_NODE) {
|
417
|
+
occur++;
|
418
|
+
}
|
419
|
+
tmp = tmp.getPreviousSibling();
|
420
|
+
}
|
421
|
+
|
422
|
+
if(occur == 0) {
|
423
|
+
tmp = cur.getNextSibling();
|
424
|
+
|
425
|
+
while(tmp != null && occur == 0) {
|
426
|
+
if(tmp.getNodeType() == Node.TEXT_NODE ||
|
427
|
+
tmp.getNodeType() == Node.CDATA_SECTION_NODE) {
|
428
|
+
occur++;
|
429
|
+
}
|
430
|
+
tmp = tmp.getNextSibling();
|
431
|
+
}
|
432
|
+
} else {
|
433
|
+
occur++;
|
434
|
+
}
|
435
|
+
|
436
|
+
} else if(cur.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
|
437
|
+
sep = "/";
|
438
|
+
name = "processing-instruction('"+cur.getLocalName()+"')";
|
439
|
+
next = cur.getParentNode();
|
440
|
+
|
441
|
+
/*
|
442
|
+
* Thumbler index computation.
|
443
|
+
*/
|
444
|
+
|
445
|
+
tmp = cur.getParentNode();
|
446
|
+
|
447
|
+
while(tmp != null) {
|
448
|
+
if(tmp.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE &&
|
449
|
+
tmp.getLocalName().equals(cur.getLocalName())) {
|
450
|
+
occur++;
|
451
|
+
}
|
452
|
+
tmp = tmp.getPreviousSibling();
|
453
|
+
}
|
454
|
+
|
455
|
+
if(occur == 0) {
|
456
|
+
tmp = cur.getNextSibling();
|
457
|
+
|
458
|
+
while(tmp != null && occur == 0) {
|
459
|
+
if(tmp.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE &&
|
460
|
+
tmp.getLocalName().equals(cur.getLocalName())){
|
461
|
+
occur++;
|
462
|
+
}
|
463
|
+
tmp = tmp.getNextSibling();
|
464
|
+
}
|
465
|
+
|
466
|
+
if(occur != 0) {
|
467
|
+
occur = 1;
|
468
|
+
}
|
469
|
+
|
470
|
+
} else {
|
471
|
+
occur++;
|
472
|
+
}
|
473
|
+
|
474
|
+
} else if(cur.getNodeType() == Node.ATTRIBUTE_NODE) {
|
475
|
+
sep = "/@";
|
476
|
+
name = cur.getLocalName();
|
477
|
+
|
478
|
+
if(cur.getNamespaceURI() != null) {
|
479
|
+
if(cur.getPrefix() != null) {
|
480
|
+
name = cur.getPrefix() + ":" + name;
|
481
|
+
}
|
482
|
+
}
|
483
|
+
|
484
|
+
next = ((Attr) cur).getOwnerElement();
|
485
|
+
|
486
|
+
} else {
|
487
|
+
next = cur.getParentNode();
|
488
|
+
}
|
489
|
+
|
490
|
+
if(occur == 0){
|
491
|
+
buffer = sep+name+buffer;
|
492
|
+
} else {
|
493
|
+
buffer = sep+name+"["+occur+"]"+buffer;
|
494
|
+
}
|
495
|
+
|
496
|
+
cur = next;
|
497
|
+
|
498
|
+
} while(cur != null);
|
499
|
+
|
500
|
+
return buffer;
|
501
|
+
}
|
502
|
+
|
503
|
+
protected static boolean compareTwoNodes(Node m, Node n) {
|
504
|
+
return nodesAreEqual(m.getLocalName(), n.getLocalName()) &&
|
505
|
+
nodesAreEqual(m.getPrefix(), n.getPrefix());
|
506
|
+
}
|
507
|
+
|
508
|
+
protected static boolean fullNamesMatch(Node a, Node b) {
|
509
|
+
return a.getNodeName().equals(b.getNodeName());
|
510
|
+
}
|
511
|
+
|
512
|
+
protected static String getFullName(Node n) {
|
513
|
+
String lname = n.getLocalName();
|
514
|
+
String prefix = n.getPrefix();
|
515
|
+
if (lname != null) {
|
516
|
+
if (prefix != null)
|
517
|
+
return prefix + ":" + lname;
|
518
|
+
else
|
519
|
+
return lname;
|
520
|
+
} else {
|
521
|
+
return n.getNodeName();
|
522
|
+
}
|
523
|
+
}
|
524
|
+
|
525
|
+
private static boolean nodesAreEqual(Object a, Object b) {
|
526
|
+
return (((a == null) && (a == null)) ||
|
527
|
+
(a != null) && (b != null) &&
|
528
|
+
(b.equals(a)));
|
529
|
+
}
|
530
|
+
|
531
|
+
private static Pattern encoded_pattern = Pattern.compile("&|>|<| ");
|
532
|
+
private static Pattern decoded_pattern = Pattern.compile("&|>|<|\r");
|
533
|
+
private static String[] encoded = {"&", ">", "<", " "};
|
534
|
+
private static String[] decoded = {"&", ">", "<", "\r"};
|
535
|
+
|
536
|
+
private static String convert(Pattern ptn, String input, String[] oldChars, String[] newChars) {
|
537
|
+
Matcher matcher = ptn.matcher(input);
|
538
|
+
boolean result = matcher.find();
|
539
|
+
StringBuffer sb = new StringBuffer();
|
540
|
+
while(result) {
|
541
|
+
String matched = matcher.group();
|
542
|
+
String replacement = "";
|
543
|
+
for (int i=0; i<oldChars.length; i++) {
|
544
|
+
if (matched.contains(oldChars[i])) {
|
545
|
+
replacement = matched.replace(oldChars[i], newChars[i]);
|
546
|
+
break;
|
547
|
+
}
|
548
|
+
}
|
549
|
+
matcher.appendReplacement(sb, replacement);
|
550
|
+
result = matcher.find();
|
551
|
+
}
|
552
|
+
matcher.appendTail(sb);
|
553
|
+
return sb.toString();
|
554
|
+
}
|
555
|
+
|
556
|
+
public static String encodeJavaString(String s) {
|
557
|
+
return convert(decoded_pattern, s, decoded, encoded);
|
558
|
+
}
|
559
|
+
|
560
|
+
public static String decodeJavaString(String s) {
|
561
|
+
return convert(encoded_pattern, s, encoded, decoded);
|
562
|
+
}
|
563
|
+
|
564
|
+
public static String getNodeName(Node node) {
|
565
|
+
if(node == null) { System.out.println("node is null"); return ""; }
|
566
|
+
String name = node.getNodeName();
|
567
|
+
if(name == null) { System.out.println("name is null"); return ""; }
|
568
|
+
if(name.equals("#document")) {
|
569
|
+
return "document";
|
570
|
+
} else if(name.equals("#text")) {
|
571
|
+
return "text";
|
572
|
+
} else {
|
573
|
+
name = getLocalPart(name);
|
574
|
+
return (name == null) ? "" : name;
|
575
|
+
}
|
576
|
+
}
|
577
|
+
|
578
|
+
public static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
|
579
|
+
public static boolean isNamespace(Node node) {
|
580
|
+
return (XMLNS_URI.equals(node.getNamespaceURI()) ||
|
581
|
+
isNamespace(node.getNodeName()));
|
582
|
+
}
|
583
|
+
|
584
|
+
public static boolean isNamespace(String nodeName) {
|
585
|
+
return (nodeName.equals("xmlns") || nodeName.startsWith("xmlns:"));
|
586
|
+
}
|
587
|
+
|
588
|
+
public static boolean isNonDefaultNamespace(Node node) {
|
589
|
+
return (isNamespace(node) && ! "xmlns".equals(node.getNodeName()));
|
590
|
+
}
|
591
|
+
|
592
|
+
public static boolean isXmlBase(String attrName) {
|
593
|
+
return "xml:base".equals(attrName) || "xlink:href".equals(attrName);
|
594
|
+
}
|
595
|
+
|
596
|
+
public static boolean isWhitespaceText(ThreadContext context, IRubyObject obj) {
|
597
|
+
if (obj == null || obj.isNil()) return false;
|
598
|
+
|
599
|
+
XmlNode node = (XmlNode) obj;
|
600
|
+
if (!(node instanceof XmlText))
|
601
|
+
return false;
|
602
|
+
|
603
|
+
String content = rubyStringToString(node.content(context));
|
604
|
+
return content.trim().length() == 0;
|
605
|
+
}
|
606
|
+
|
607
|
+
public static boolean isWhitespaceText(String s) {
|
608
|
+
return s.trim().length() == 0;
|
609
|
+
}
|
610
|
+
|
611
|
+
public static String canonicalizeWhitespce(String s) {
|
612
|
+
StringBuilder sb = new StringBuilder();
|
613
|
+
char[] chars = s.toCharArray();
|
614
|
+
boolean newline_added = false;
|
615
|
+
for (int i=0; i<chars.length; i++) {
|
616
|
+
if (chars[i] == '\n') {
|
617
|
+
if (!newline_added) {
|
618
|
+
sb.append(chars[i]);
|
619
|
+
newline_added = true;
|
620
|
+
}
|
621
|
+
} else {
|
622
|
+
sb.append(chars[i]);
|
623
|
+
}
|
624
|
+
}
|
625
|
+
return sb.toString();
|
626
|
+
}
|
627
|
+
|
628
|
+
public static String newQName(String newPrefix, Node node) {
|
629
|
+
if(newPrefix == null) {
|
630
|
+
return node.getLocalName();
|
631
|
+
} else {
|
632
|
+
return newPrefix + ":" + node.getLocalName();
|
633
|
+
}
|
634
|
+
}
|
635
|
+
|
636
|
+
public static RubyArray nodeListToRubyArray(Ruby ruby, NodeList nodes) {
|
637
|
+
RubyArray array = RubyArray.newArray(ruby, nodes.getLength());
|
638
|
+
return nodeListToRubyArray(ruby, nodes, array);
|
639
|
+
}
|
640
|
+
|
641
|
+
public static RubyArray nodeListToRubyArray(Ruby ruby, NodeList nodes, RubyArray array) {
|
642
|
+
for(int i = 0; i < nodes.getLength(); i++) {
|
643
|
+
array.append(NokogiriHelpers.getCachedNodeOrCreate(ruby, nodes.item(i)));
|
644
|
+
}
|
645
|
+
return array;
|
646
|
+
}
|
647
|
+
|
648
|
+
public static RubyArray nodeArrayToRubyArray(Ruby ruby, Node[] nodes) {
|
649
|
+
RubyArray n = RubyArray.newArray(ruby, nodes.length);
|
650
|
+
for(int i = 0; i < nodes.length; i++) {
|
651
|
+
n.append(NokogiriHelpers.getCachedNodeOrCreate(ruby, nodes[i]));
|
652
|
+
}
|
653
|
+
return n;
|
654
|
+
}
|
655
|
+
|
656
|
+
public static RubyArray namedNodeMapToRubyArray(Ruby ruby, NamedNodeMap map) {
|
657
|
+
RubyArray n = RubyArray.newArray(ruby, map.getLength());
|
658
|
+
for(int i = 0; i < map.getLength(); i++) {
|
659
|
+
n.append(NokogiriHelpers.getCachedNodeOrCreate(ruby, map.item(i)));
|
660
|
+
}
|
661
|
+
return n;
|
662
|
+
}
|
663
|
+
|
664
|
+
public static String getValidEncoding(Ruby runtime, IRubyObject encoding) {
|
665
|
+
if (encoding.isNil()) {
|
666
|
+
return guessEncoding();
|
667
|
+
} else {
|
668
|
+
return ignoreInvalidEncoding(runtime, encoding);
|
669
|
+
}
|
670
|
+
}
|
671
|
+
|
672
|
+
private static String guessEncoding() {
|
673
|
+
String name = null;
|
674
|
+
if (name == null) name = System.getProperty("file.encoding");
|
675
|
+
if (name == null) name = "UTF-8";
|
676
|
+
return name;
|
677
|
+
}
|
678
|
+
|
679
|
+
private static Set<String> charsetNames = ((SortedMap<String, Charset>)Charset.availableCharsets()).keySet();
|
680
|
+
|
681
|
+
private static String ignoreInvalidEncoding(Ruby runtime, IRubyObject encoding) {
|
682
|
+
String givenEncoding = rubyStringToString(encoding);
|
683
|
+
if (charsetNames.contains(givenEncoding)) return givenEncoding;
|
684
|
+
else return guessEncoding();
|
685
|
+
}
|
686
|
+
|
687
|
+
public static String adjustSystemIdIfNecessary(String currentDir, String scriptFileName, String baseURI, String systemId) {
|
688
|
+
if (systemId == null) return systemId;
|
689
|
+
File file = new File(systemId);
|
690
|
+
if (file.isAbsolute()) return systemId;
|
691
|
+
String path = resolveSystemId(baseURI, systemId);
|
692
|
+
if (path != null) return path;
|
693
|
+
path = resolveSystemId(currentDir, systemId);
|
694
|
+
if (path != null) return path;
|
695
|
+
return resolveSystemId(scriptFileName, systemId);
|
696
|
+
}
|
697
|
+
|
698
|
+
private static String resolveSystemId(String baseName, String systemId) {
|
699
|
+
if (baseName == null || baseName.length() < 1) return null;
|
700
|
+
String parentName = null;
|
701
|
+
baseName = baseName.replaceAll("%20", " ");
|
702
|
+
File base = new File(baseName);
|
703
|
+
if (base.isDirectory()) parentName = baseName;
|
704
|
+
else parentName = base.getParent();
|
705
|
+
if (parentName.toLowerCase().startsWith("file:")) parentName = parentName.substring("file:".length());
|
706
|
+
File dtdFile = new File(parentName + "/" + systemId);
|
707
|
+
if (dtdFile.exists()) return dtdFile.getPath();
|
708
|
+
return null;
|
709
|
+
}
|
710
|
+
|
711
|
+
public static boolean isUTF8(String encoding) {
|
712
|
+
if (encoding == null) return true; // no need to convert encoding
|
713
|
+
int ret = Charset.forName(encoding).compareTo(Charset.forName("UTF-8"));
|
714
|
+
return ret == 0;
|
715
|
+
}
|
716
|
+
|
717
|
+
public static byte[] convertEncoding(Charset output_charset, String input_string) throws CharacterCodingException {
|
718
|
+
Charset input = Charset.forName("UTF-8");
|
719
|
+
CharsetDecoder decoder = input.newDecoder();
|
720
|
+
CharsetEncoder encoder = output_charset.newEncoder();
|
721
|
+
decoder.reset();
|
722
|
+
encoder.reset();
|
723
|
+
ByteBuffer bbuf = ByteBuffer.wrap(input_string.getBytes());
|
724
|
+
CharBuffer cbuf = decoder.decode(bbuf);
|
725
|
+
bbuf.clear();
|
726
|
+
encoder.encode(cbuf, bbuf, true);
|
727
|
+
int length = bbuf.position();
|
728
|
+
byte[] bytes = new byte[length];
|
729
|
+
System.arraycopy(bbuf.array(), 0, bytes, 0, length);
|
730
|
+
return bytes;
|
731
|
+
}
|
732
|
+
|
733
|
+
public static String convertEncodingByNKFIfNecessary(Ruby runtime, XmlDocument doc, String thing) {
|
734
|
+
if (!(doc instanceof HtmlDocument)) return thing;
|
735
|
+
String parsed_encoding = ((HtmlDocument)doc).getPraedEncoding();
|
736
|
+
if (parsed_encoding == null) return thing;
|
737
|
+
String ruby_encoding = rubyStringToString(doc.getEncoding());
|
738
|
+
if (ruby_encoding == null) return thing;
|
739
|
+
if (Charset.forName(parsed_encoding).compareTo(Charset.forName(ruby_encoding)) == 0) {
|
740
|
+
return thing;
|
741
|
+
} else {
|
742
|
+
return NokogiriHelpers.nkf(runtime, ruby_encoding, thing);
|
743
|
+
}
|
744
|
+
|
745
|
+
}
|
746
|
+
|
747
|
+
// This method is used from HTML documents. HTML meta tag with encoding specification
|
748
|
+
// might appear after non-ascii characters are used. For example, a title tag before
|
749
|
+
// a meta tag. In such a case, Xerces encodes characters in UTF-8 without seeing meta tag.
|
750
|
+
// Nokogiri uses NKF library to convert characters correct encoding. This means the method
|
751
|
+
// works only for JIS/Shift_JIS/EUC-JP.
|
752
|
+
public static String nkf(Ruby runtime, String ruby_encoding, String thing) {
|
753
|
+
StringBuffer sb = new StringBuffer("-");
|
754
|
+
Charset that = Charset.forName(ruby_encoding);
|
755
|
+
if (NokogiriHelpers.shift_jis.compareTo(that) == 0) {
|
756
|
+
sb.append("S");
|
757
|
+
} else if (NokogiriHelpers.jis.compareTo(that) == 0) {
|
758
|
+
sb.append("J");
|
759
|
+
} else if (NokogiriHelpers.euc_jp.compareTo(that) == 0) {
|
760
|
+
sb.append("E");
|
761
|
+
} else {
|
762
|
+
// should not come here. should be treated before this method.
|
763
|
+
sb.append("W");
|
764
|
+
}
|
765
|
+
sb.append("w");
|
766
|
+
Class nkfClass = null;
|
767
|
+
try {
|
768
|
+
// JRuby 1.7 and later
|
769
|
+
nkfClass = runtime.getClassLoader().loadClass("org.jruby.ext.nkf.RubyNKF");
|
770
|
+
} catch (ClassNotFoundException e1) {
|
771
|
+
try {
|
772
|
+
// Before JRuby 1.7
|
773
|
+
nkfClass = runtime.getClassLoader().loadClass("org.jruby.RubyNKF");
|
774
|
+
} catch (ClassNotFoundException e2) {
|
775
|
+
return thing;
|
776
|
+
}
|
777
|
+
}
|
778
|
+
Method nkf_method;
|
779
|
+
try {
|
780
|
+
nkf_method = nkfClass.getMethod("nkf", ThreadContext.class, IRubyObject.class, IRubyObject.class, IRubyObject.class);
|
781
|
+
RubyString r_str =
|
782
|
+
(RubyString)nkf_method.invoke(null, runtime.getCurrentContext(), null, runtime.newString(new String(sb)), runtime.newString(thing));
|
783
|
+
return NokogiriHelpers.rubyStringToString(r_str);
|
784
|
+
} catch (SecurityException e) {
|
785
|
+
return thing;
|
786
|
+
} catch (NoSuchMethodException e) {
|
787
|
+
return thing;
|
788
|
+
} catch (IllegalArgumentException e) {
|
789
|
+
return thing;
|
790
|
+
} catch (IllegalAccessException e) {
|
791
|
+
return thing;
|
792
|
+
} catch (InvocationTargetException e) {
|
793
|
+
return thing;
|
794
|
+
}
|
795
|
+
}
|
796
|
+
|
797
|
+
private static Charset shift_jis = Charset.forName("Shift_JIS");
|
798
|
+
private static Charset jis = Charset.forName("ISO-2022-JP");
|
799
|
+
private static Charset euc_jp = Charset.forName("EUC-JP");
|
800
|
+
}
|