nokogiri-backport 1.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (239) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE-DEPENDENCIES.md +1682 -0
  4. data/LICENSE.md +9 -0
  5. data/README.md +272 -0
  6. data/bin/nokogiri +118 -0
  7. data/dependencies.yml +74 -0
  8. data/ext/java/nokogiri/EncodingHandler.java +124 -0
  9. data/ext/java/nokogiri/HtmlDocument.java +178 -0
  10. data/ext/java/nokogiri/HtmlElementDescription.java +148 -0
  11. data/ext/java/nokogiri/HtmlEntityLookup.java +79 -0
  12. data/ext/java/nokogiri/HtmlSaxParserContext.java +282 -0
  13. data/ext/java/nokogiri/HtmlSaxPushParser.java +222 -0
  14. data/ext/java/nokogiri/NokogiriService.java +597 -0
  15. data/ext/java/nokogiri/XmlAttr.java +162 -0
  16. data/ext/java/nokogiri/XmlAttributeDecl.java +129 -0
  17. data/ext/java/nokogiri/XmlCdata.java +82 -0
  18. data/ext/java/nokogiri/XmlComment.java +97 -0
  19. data/ext/java/nokogiri/XmlDocument.java +633 -0
  20. data/ext/java/nokogiri/XmlDocumentFragment.java +185 -0
  21. data/ext/java/nokogiri/XmlDtd.java +481 -0
  22. data/ext/java/nokogiri/XmlElement.java +68 -0
  23. data/ext/java/nokogiri/XmlElementContent.java +382 -0
  24. data/ext/java/nokogiri/XmlElementDecl.java +147 -0
  25. data/ext/java/nokogiri/XmlEntityDecl.java +157 -0
  26. data/ext/java/nokogiri/XmlEntityReference.java +101 -0
  27. data/ext/java/nokogiri/XmlNamespace.java +199 -0
  28. data/ext/java/nokogiri/XmlNode.java +1684 -0
  29. data/ext/java/nokogiri/XmlNodeSet.java +434 -0
  30. data/ext/java/nokogiri/XmlProcessingInstruction.java +100 -0
  31. data/ext/java/nokogiri/XmlReader.java +531 -0
  32. data/ext/java/nokogiri/XmlRelaxng.java +151 -0
  33. data/ext/java/nokogiri/XmlSaxParserContext.java +374 -0
  34. data/ext/java/nokogiri/XmlSaxPushParser.java +286 -0
  35. data/ext/java/nokogiri/XmlSchema.java +388 -0
  36. data/ext/java/nokogiri/XmlSyntaxError.java +138 -0
  37. data/ext/java/nokogiri/XmlText.java +110 -0
  38. data/ext/java/nokogiri/XmlXpathContext.java +301 -0
  39. data/ext/java/nokogiri/XsltStylesheet.java +347 -0
  40. data/ext/java/nokogiri/internals/ClosedStreamException.java +10 -0
  41. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +252 -0
  42. data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +20 -0
  43. data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +151 -0
  44. data/ext/java/nokogiri/internals/NokogiriDomParser.java +116 -0
  45. data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +121 -0
  46. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +69 -0
  47. data/ext/java/nokogiri/internals/NokogiriHandler.java +327 -0
  48. data/ext/java/nokogiri/internals/NokogiriHelpers.java +734 -0
  49. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +217 -0
  50. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +127 -0
  51. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +100 -0
  52. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +121 -0
  53. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +78 -0
  54. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +180 -0
  55. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +72 -0
  56. data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +60 -0
  57. data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +87 -0
  58. data/ext/java/nokogiri/internals/ParserContext.java +259 -0
  59. data/ext/java/nokogiri/internals/ReaderNode.java +488 -0
  60. data/ext/java/nokogiri/internals/SaveContextVisitor.java +778 -0
  61. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +73 -0
  62. data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +168 -0
  63. data/ext/java/nokogiri/internals/XmlDeclHandler.java +42 -0
  64. data/ext/java/nokogiri/internals/XmlDomParserContext.java +274 -0
  65. data/ext/java/nokogiri/internals/XmlSaxParser.java +65 -0
  66. data/ext/java/nokogiri/internals/c14n/AttrCompare.java +119 -0
  67. data/ext/java/nokogiri/internals/c14n/C14nHelper.java +159 -0
  68. data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +37 -0
  69. data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +93 -0
  70. data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +252 -0
  71. data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +639 -0
  72. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +38 -0
  73. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +38 -0
  74. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +367 -0
  75. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +295 -0
  76. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +40 -0
  77. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +44 -0
  78. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +44 -0
  79. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +43 -0
  80. data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +630 -0
  81. data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +173 -0
  82. data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +76 -0
  83. data/ext/java/nokogiri/internals/c14n/Constants.java +42 -0
  84. data/ext/java/nokogiri/internals/c14n/ElementProxy.java +293 -0
  85. data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +93 -0
  86. data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +79 -0
  87. data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +166 -0
  88. data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +76 -0
  89. data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +402 -0
  90. data/ext/java/nokogiri/internals/c14n/NodeFilter.java +51 -0
  91. data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +179 -0
  92. data/ext/java/nokogiri/internals/c14n/XMLUtils.java +507 -0
  93. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1745 -0
  94. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +685 -0
  95. data/ext/nokogiri/depend +477 -0
  96. data/ext/nokogiri/extconf.rb +836 -0
  97. data/ext/nokogiri/html_document.c +171 -0
  98. data/ext/nokogiri/html_document.h +10 -0
  99. data/ext/nokogiri/html_element_description.c +279 -0
  100. data/ext/nokogiri/html_element_description.h +10 -0
  101. data/ext/nokogiri/html_entity_lookup.c +32 -0
  102. data/ext/nokogiri/html_entity_lookup.h +8 -0
  103. data/ext/nokogiri/html_sax_parser_context.c +116 -0
  104. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  105. data/ext/nokogiri/html_sax_push_parser.c +87 -0
  106. data/ext/nokogiri/html_sax_push_parser.h +9 -0
  107. data/ext/nokogiri/nokogiri.c +135 -0
  108. data/ext/nokogiri/nokogiri.h +130 -0
  109. data/ext/nokogiri/xml_attr.c +103 -0
  110. data/ext/nokogiri/xml_attr.h +9 -0
  111. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  112. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  113. data/ext/nokogiri/xml_cdata.c +62 -0
  114. data/ext/nokogiri/xml_cdata.h +9 -0
  115. data/ext/nokogiri/xml_comment.c +69 -0
  116. data/ext/nokogiri/xml_comment.h +9 -0
  117. data/ext/nokogiri/xml_document.c +622 -0
  118. data/ext/nokogiri/xml_document.h +23 -0
  119. data/ext/nokogiri/xml_document_fragment.c +48 -0
  120. data/ext/nokogiri/xml_document_fragment.h +10 -0
  121. data/ext/nokogiri/xml_dtd.c +202 -0
  122. data/ext/nokogiri/xml_dtd.h +10 -0
  123. data/ext/nokogiri/xml_element_content.c +123 -0
  124. data/ext/nokogiri/xml_element_content.h +10 -0
  125. data/ext/nokogiri/xml_element_decl.c +69 -0
  126. data/ext/nokogiri/xml_element_decl.h +9 -0
  127. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  128. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  129. data/ext/nokogiri/xml_entity_decl.c +110 -0
  130. data/ext/nokogiri/xml_entity_decl.h +10 -0
  131. data/ext/nokogiri/xml_entity_reference.c +52 -0
  132. data/ext/nokogiri/xml_entity_reference.h +9 -0
  133. data/ext/nokogiri/xml_io.c +63 -0
  134. data/ext/nokogiri/xml_io.h +11 -0
  135. data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
  136. data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
  137. data/ext/nokogiri/xml_namespace.c +111 -0
  138. data/ext/nokogiri/xml_namespace.h +14 -0
  139. data/ext/nokogiri/xml_node.c +1773 -0
  140. data/ext/nokogiri/xml_node.h +13 -0
  141. data/ext/nokogiri/xml_node_set.c +486 -0
  142. data/ext/nokogiri/xml_node_set.h +12 -0
  143. data/ext/nokogiri/xml_processing_instruction.c +56 -0
  144. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  145. data/ext/nokogiri/xml_reader.c +657 -0
  146. data/ext/nokogiri/xml_reader.h +10 -0
  147. data/ext/nokogiri/xml_relax_ng.c +179 -0
  148. data/ext/nokogiri/xml_relax_ng.h +9 -0
  149. data/ext/nokogiri/xml_sax_parser.c +305 -0
  150. data/ext/nokogiri/xml_sax_parser.h +39 -0
  151. data/ext/nokogiri/xml_sax_parser_context.c +262 -0
  152. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  153. data/ext/nokogiri/xml_sax_push_parser.c +159 -0
  154. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  155. data/ext/nokogiri/xml_schema.c +276 -0
  156. data/ext/nokogiri/xml_schema.h +9 -0
  157. data/ext/nokogiri/xml_syntax_error.c +64 -0
  158. data/ext/nokogiri/xml_syntax_error.h +13 -0
  159. data/ext/nokogiri/xml_text.c +52 -0
  160. data/ext/nokogiri/xml_text.h +9 -0
  161. data/ext/nokogiri/xml_xpath_context.c +374 -0
  162. data/ext/nokogiri/xml_xpath_context.h +10 -0
  163. data/ext/nokogiri/xslt_stylesheet.c +263 -0
  164. data/ext/nokogiri/xslt_stylesheet.h +14 -0
  165. data/lib/isorelax.jar +0 -0
  166. data/lib/jing.jar +0 -0
  167. data/lib/nekodtd.jar +0 -0
  168. data/lib/nekohtml.jar +0 -0
  169. data/lib/nokogiri/css/node.rb +53 -0
  170. data/lib/nokogiri/css/parser.rb +751 -0
  171. data/lib/nokogiri/css/parser.y +272 -0
  172. data/lib/nokogiri/css/parser_extras.rb +94 -0
  173. data/lib/nokogiri/css/syntax_error.rb +8 -0
  174. data/lib/nokogiri/css/tokenizer.rb +154 -0
  175. data/lib/nokogiri/css/tokenizer.rex +55 -0
  176. data/lib/nokogiri/css/xpath_visitor.rb +260 -0
  177. data/lib/nokogiri/css.rb +28 -0
  178. data/lib/nokogiri/decorators/slop.rb +43 -0
  179. data/lib/nokogiri/html/builder.rb +36 -0
  180. data/lib/nokogiri/html/document.rb +322 -0
  181. data/lib/nokogiri/html/document_fragment.rb +50 -0
  182. data/lib/nokogiri/html/element_description.rb +24 -0
  183. data/lib/nokogiri/html/element_description_defaults.rb +672 -0
  184. data/lib/nokogiri/html/entity_lookup.rb +14 -0
  185. data/lib/nokogiri/html/sax/parser.rb +63 -0
  186. data/lib/nokogiri/html/sax/parser_context.rb +17 -0
  187. data/lib/nokogiri/html/sax/push_parser.rb +37 -0
  188. data/lib/nokogiri/html.rb +38 -0
  189. data/lib/nokogiri/jruby/dependencies.rb +20 -0
  190. data/lib/nokogiri/syntax_error.rb +5 -0
  191. data/lib/nokogiri/version/constant.rb +5 -0
  192. data/lib/nokogiri/version/info.rb +182 -0
  193. data/lib/nokogiri/version.rb +3 -0
  194. data/lib/nokogiri/xml/attr.rb +15 -0
  195. data/lib/nokogiri/xml/attribute_decl.rb +19 -0
  196. data/lib/nokogiri/xml/builder.rb +447 -0
  197. data/lib/nokogiri/xml/cdata.rb +12 -0
  198. data/lib/nokogiri/xml/character_data.rb +8 -0
  199. data/lib/nokogiri/xml/document.rb +290 -0
  200. data/lib/nokogiri/xml/document_fragment.rb +159 -0
  201. data/lib/nokogiri/xml/dtd.rb +33 -0
  202. data/lib/nokogiri/xml/element_content.rb +37 -0
  203. data/lib/nokogiri/xml/element_decl.rb +14 -0
  204. data/lib/nokogiri/xml/entity_decl.rb +20 -0
  205. data/lib/nokogiri/xml/entity_reference.rb +19 -0
  206. data/lib/nokogiri/xml/namespace.rb +14 -0
  207. data/lib/nokogiri/xml/node/save_options.rb +62 -0
  208. data/lib/nokogiri/xml/node.rb +1240 -0
  209. data/lib/nokogiri/xml/node_set.rb +372 -0
  210. data/lib/nokogiri/xml/notation.rb +7 -0
  211. data/lib/nokogiri/xml/parse_options.rb +127 -0
  212. data/lib/nokogiri/xml/pp/character_data.rb +19 -0
  213. data/lib/nokogiri/xml/pp/node.rb +57 -0
  214. data/lib/nokogiri/xml/pp.rb +3 -0
  215. data/lib/nokogiri/xml/processing_instruction.rb +9 -0
  216. data/lib/nokogiri/xml/reader.rb +116 -0
  217. data/lib/nokogiri/xml/relax_ng.rb +37 -0
  218. data/lib/nokogiri/xml/sax/document.rb +172 -0
  219. data/lib/nokogiri/xml/sax/parser.rb +123 -0
  220. data/lib/nokogiri/xml/sax/parser_context.rb +17 -0
  221. data/lib/nokogiri/xml/sax/push_parser.rb +61 -0
  222. data/lib/nokogiri/xml/sax.rb +5 -0
  223. data/lib/nokogiri/xml/schema.rb +72 -0
  224. data/lib/nokogiri/xml/searchable.rb +239 -0
  225. data/lib/nokogiri/xml/syntax_error.rb +71 -0
  226. data/lib/nokogiri/xml/text.rb +10 -0
  227. data/lib/nokogiri/xml/xpath/syntax_error.rb +12 -0
  228. data/lib/nokogiri/xml/xpath.rb +11 -0
  229. data/lib/nokogiri/xml/xpath_context.rb +17 -0
  230. data/lib/nokogiri/xml.rb +76 -0
  231. data/lib/nokogiri/xslt/stylesheet.rb +26 -0
  232. data/lib/nokogiri/xslt.rb +57 -0
  233. data/lib/nokogiri.rb +144 -0
  234. data/lib/serializer.jar +0 -0
  235. data/lib/xalan.jar +0 -0
  236. data/lib/xercesImpl.jar +0 -0
  237. data/lib/xml-apis.jar +0 -0
  238. data/lib/xsd/xmlparser/nokogiri.rb +103 -0
  239. metadata +531 -0
@@ -0,0 +1,734 @@
1
+ /**
2
+ * (The MIT License)
3
+ *
4
+ * Copyright (c) 2008 - 2014:
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.ByteArrayInputStream;
36
+ import java.io.File;
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.Charset;
42
+ import java.util.List;
43
+ import java.util.Set;
44
+ import java.util.regex.Matcher;
45
+ import java.util.regex.Pattern;
46
+
47
+ import org.jruby.Ruby;
48
+ import org.jruby.RubyArray;
49
+ import org.jruby.RubyClass;
50
+ import org.jruby.RubyString;
51
+ import org.jruby.runtime.ThreadContext;
52
+ import org.jruby.runtime.builtin.IRubyObject;
53
+ import org.jruby.util.ByteList;
54
+ import org.w3c.dom.Attr;
55
+ import org.w3c.dom.DOMException;
56
+ import org.w3c.dom.Document;
57
+ import org.w3c.dom.Node;
58
+ import org.w3c.dom.NodeList;
59
+
60
+ import nokogiri.HtmlDocument;
61
+ import nokogiri.NokogiriService;
62
+ import nokogiri.XmlAttr;
63
+ import nokogiri.XmlCdata;
64
+ import nokogiri.XmlComment;
65
+ import nokogiri.XmlDocument;
66
+ import nokogiri.XmlDtd;
67
+ import nokogiri.XmlElement;
68
+ import nokogiri.XmlEntityReference;
69
+ import nokogiri.XmlNamespace;
70
+ import nokogiri.XmlNode;
71
+ import nokogiri.XmlProcessingInstruction;
72
+ import nokogiri.XmlText;
73
+ import nokogiri.XmlXpathContext;
74
+
75
+ /**
76
+ * A class for various utility methods.
77
+ *
78
+ * @author serabe
79
+ * @author Patrick Mahoney <pat@polycrystal.org>
80
+ * @author Yoko Harada <yokolet@gmail.com>
81
+ */
82
+ public class NokogiriHelpers {
83
+ public static final String CACHED_NODE = "NOKOGIRI_CACHED_NODE";
84
+ public static final String ROOT_NODE_INVALID = "NOKOGIRI_ROOT_NODE_INVALID";
85
+ public static final String ENCODED_STRING = "NOKOGIRI_ENCODED_STRING";
86
+
87
+ public static XmlNode getCachedNode(Node node) {
88
+ return (XmlNode) node.getUserData(CACHED_NODE);
89
+ }
90
+
91
+ public static void clearCachedNode(Node node) {
92
+ node.setUserData(CACHED_NODE, null, null);
93
+ }
94
+
95
+ public static void clearXpathContext(Node node) {
96
+ if (node == null) return;
97
+
98
+ Node ownerDocument = node.getOwnerDocument();
99
+ if (ownerDocument == null) {
100
+ ownerDocument = node;
101
+ }
102
+ ownerDocument.setUserData(XmlXpathContext.XPATH_CONTEXT, null, null);
103
+ }
104
+
105
+ /**
106
+ * Get the XmlNode associated with the underlying
107
+ * <code>node</code>. Creates a new XmlNode (or appropriate subclass)
108
+ * or XmlNamespace wrapping <code>node</code> if there is no cached
109
+ * value.
110
+ */
111
+ public static IRubyObject getCachedNodeOrCreate(Ruby runtime, Node node) {
112
+ if (node == null) return runtime.getNil();
113
+ if (node.getNodeType() == Node.ATTRIBUTE_NODE && isNamespace(node.getNodeName())) {
114
+ XmlDocument xmlDocument = (XmlDocument) node.getOwnerDocument().getUserData(CACHED_NODE);
115
+ if (!(xmlDocument instanceof HtmlDocument)) {
116
+ String prefix = getLocalNameForNamespace(((Attr) node).getName(), null);
117
+ String href = ((Attr) node).getValue();
118
+ XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(prefix, href);
119
+ if (xmlNamespace != null) return xmlNamespace;
120
+ return XmlNamespace.createFromAttr(runtime, (Attr) node);
121
+ }
122
+ }
123
+ XmlNode xmlNode = getCachedNode(node);
124
+ if (xmlNode == null) {
125
+ xmlNode = (XmlNode) constructNode(runtime, node);
126
+ node.setUserData(CACHED_NODE, xmlNode, null);
127
+ }
128
+ return xmlNode;
129
+ }
130
+
131
+ /**
132
+ * Construct a new XmlNode wrapping <code>node</code>. The proper
133
+ * subclass of XmlNode is chosen based on the type of
134
+ * <code>node</code>.
135
+ */
136
+ public static IRubyObject constructNode(Ruby runtime, Node node) {
137
+ if (node == null) return runtime.getNil();
138
+ // this is slow; need a way to cache nokogiri classes/modules somewhere
139
+ switch (node.getNodeType()) {
140
+ case Node.ELEMENT_NODE:
141
+ XmlElement xmlElement = (XmlElement) NokogiriService.XML_ELEMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Element"));
142
+ xmlElement.setNode(runtime, node);
143
+ return xmlElement;
144
+ case Node.ATTRIBUTE_NODE:
145
+ XmlAttr xmlAttr = (XmlAttr) NokogiriService.XML_ATTR_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Attr"));
146
+ xmlAttr.setNode(runtime, node);
147
+ return xmlAttr;
148
+ case Node.TEXT_NODE:
149
+ XmlText xmlText = (XmlText) NokogiriService.XML_TEXT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Text"));
150
+ xmlText.setNode(runtime, node);
151
+ return xmlText;
152
+ case Node.COMMENT_NODE:
153
+ XmlComment xmlComment = (XmlComment) NokogiriService.XML_COMMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Comment"));
154
+ xmlComment.setNode(runtime, node);
155
+ return xmlComment;
156
+ case Node.ENTITY_NODE:
157
+ return new XmlNode(runtime, getNokogiriClass(runtime, "Nokogiri::XML::EntityDecl"), node);
158
+ case Node.ENTITY_REFERENCE_NODE:
159
+ XmlEntityReference xmlEntityRef = (XmlEntityReference) NokogiriService.XML_ENTITY_REFERENCE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::EntityReference"));
160
+ xmlEntityRef.setNode(runtime, node);
161
+ return xmlEntityRef;
162
+ case Node.PROCESSING_INSTRUCTION_NODE:
163
+ XmlProcessingInstruction xmlProcessingInstruction = (XmlProcessingInstruction) NokogiriService.XML_PROCESSING_INSTRUCTION_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::ProcessingInstruction"));
164
+ xmlProcessingInstruction.setNode(runtime, node);
165
+ return xmlProcessingInstruction;
166
+ case Node.CDATA_SECTION_NODE:
167
+ XmlCdata xmlCdata = (XmlCdata) NokogiriService.XML_CDATA_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::CDATA"));
168
+ xmlCdata.setNode(runtime, node);
169
+ return xmlCdata;
170
+ case Node.DOCUMENT_NODE:
171
+ XmlDocument xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Document"));
172
+ xmlDocument.setDocumentNode(runtime, (Document) node);
173
+ return xmlDocument;
174
+ case Node.DOCUMENT_TYPE_NODE:
175
+ XmlDtd xmlDtd = (XmlDtd) NokogiriService.XML_DTD_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::DTD"));
176
+ xmlDtd.setNode(runtime, node);
177
+ return xmlDtd;
178
+ default:
179
+ XmlNode xmlNode = (XmlNode) NokogiriService.XML_NODE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Node"));
180
+ xmlNode.setNode(runtime, node);
181
+ return xmlNode;
182
+ }
183
+ }
184
+
185
+ public static RubyClass getNokogiriClass(Ruby ruby, String name) {
186
+ return NokogiriService.getNokogiriClassCache(ruby).get(name);
187
+ }
188
+
189
+ public static IRubyObject stringOrNil(Ruby runtime, String str) {
190
+ return str == null ? runtime.getNil() : convertString(runtime, str);
191
+ }
192
+
193
+ public static IRubyObject stringOrNil(Ruby runtime, CharSequence str) {
194
+ return str == null ? runtime.getNil() : convertString(runtime, str);
195
+ }
196
+
197
+ public static IRubyObject stringOrNil(Ruby runtime, byte[] bytes) {
198
+ return bytes == null ? runtime.getNil() : RubyString.newString(runtime, bytes);
199
+ }
200
+
201
+ public static IRubyObject stringOrBlank(Ruby runtime, String str) {
202
+ return str == null ? runtime.newString() : convertString(runtime, str);
203
+ }
204
+
205
+ public static RubyString convertString(Ruby runtime, String str) {
206
+ return RubyString.newUTF8String(runtime, str);
207
+ }
208
+
209
+ public static RubyString convertString(Ruby runtime, CharSequence str) {
210
+ return RubyString.newUTF8String(runtime, str);
211
+ }
212
+
213
+ /**
214
+ * Convert <code>s</code> to a RubyString, or if s is null or
215
+ * empty return RubyNil.
216
+ */
217
+ public static IRubyObject nonEmptyStringOrNil(Ruby runtime, String s) {
218
+ if (s == null || s.length() == 0) return runtime.getNil();
219
+ return RubyString.newString(runtime, s);
220
+ }
221
+
222
+ /**
223
+ * Return the prefix of a qualified name like "prefix:local".
224
+ * Returns null if there is no prefix.
225
+ */
226
+ public static String getPrefix(String qName) {
227
+ if (qName == null) return null;
228
+
229
+ final int pos = qName.indexOf(':');
230
+ return pos > 0 ? qName.substring(0, pos) : null;
231
+ }
232
+
233
+ /**
234
+ * Return the local part of a qualified name like "prefix:local".
235
+ * Returns <code>qName</code> if there is no prefix.
236
+ */
237
+ public static String getLocalPart(String qName) {
238
+ if (qName == null) return null;
239
+
240
+ final int pos = qName.indexOf(':');
241
+ return pos > 0 ? qName.substring(pos + 1) : qName;
242
+ }
243
+
244
+ public static String getLocalNameForNamespace(String name, String defValue) {
245
+ String localName = getLocalPart(name);
246
+ return ("xmlns".equals(localName)) ? defValue : localName;
247
+ }
248
+
249
+ public static String rubyStringToString(IRubyObject str) {
250
+ if (str.isNil()) return null;
251
+ return str.convertToString().decodeString();
252
+ }
253
+
254
+ public static String rubyStringToString(RubyString str) {
255
+ return str.decodeString(); // if encoding UTF-8 will decode UTF-8
256
+ }
257
+
258
+ public static ByteArrayInputStream stringBytesToStream(final IRubyObject str) {
259
+ if (str instanceof RubyString || str.respondsTo("to_str")) {
260
+ final ByteList bytes = str.convertToString().getByteList();
261
+ return new ByteArrayInputStream(bytes.unsafeBytes(), bytes.begin(), bytes.length());
262
+ }
263
+ return null;
264
+ }
265
+
266
+ public static String getNodeCompletePath(Node node) {
267
+
268
+ Node cur, tmp, next;
269
+
270
+ String buffer = "";
271
+
272
+ cur = node;
273
+
274
+ do {
275
+ String name = "";
276
+ String sep = "?";
277
+ int occur = 0;
278
+ boolean generic = false;
279
+
280
+ if(cur.getNodeType() == Node.DOCUMENT_NODE) {
281
+ if(buffer.startsWith("/")) break;
282
+
283
+ sep = "/";
284
+ next = null;
285
+ } else if(cur.getNodeType() == Node.ELEMENT_NODE) {
286
+ generic = false;
287
+ sep = "/";
288
+
289
+ name = cur.getLocalName();
290
+ if (name == null) name = cur.getNodeName();
291
+ if(cur.getNamespaceURI() != null) {
292
+ if(cur.getPrefix() != null) {
293
+ name = cur.getPrefix() + ":" + name;
294
+ } else {
295
+ generic = true;
296
+ name = "*";
297
+ }
298
+ }
299
+
300
+ next = cur.getParentNode();
301
+
302
+ /*
303
+ * Thumbler index computation
304
+ */
305
+
306
+ tmp = cur.getPreviousSibling();
307
+
308
+ while(tmp != null) {
309
+ if((tmp.getNodeType() == Node.ELEMENT_NODE) &&
310
+ (generic || fullNamesMatch(tmp, cur))) {
311
+ occur++;
312
+ }
313
+ tmp = tmp.getPreviousSibling();
314
+ }
315
+
316
+ if(occur == 0) {
317
+ tmp = cur.getNextSibling();
318
+
319
+ while(tmp != null && occur == 0) {
320
+ if((tmp.getNodeType() == Node.ELEMENT_NODE) &&
321
+ (generic || fullNamesMatch(tmp,cur))) {
322
+ occur++;
323
+ }
324
+ tmp = tmp.getNextSibling();
325
+ }
326
+
327
+ if(occur != 0) occur = 1;
328
+
329
+ } else {
330
+ occur++;
331
+ }
332
+ } else if(cur.getNodeType() == Node.COMMENT_NODE) {
333
+ sep = "/";
334
+ name = "comment()";
335
+ next = cur.getParentNode();
336
+
337
+ /*
338
+ * Thumbler index computation.
339
+ */
340
+
341
+ tmp = cur.getPreviousSibling();
342
+
343
+ while(tmp != null) {
344
+ if(tmp.getNodeType() == Node.COMMENT_NODE) {
345
+ occur++;
346
+ }
347
+ tmp = tmp.getPreviousSibling();
348
+ }
349
+
350
+ if(occur == 0) {
351
+ tmp = cur.getNextSibling();
352
+ while(tmp != null && occur == 0) {
353
+ if(tmp.getNodeType() == Node.COMMENT_NODE) {
354
+ occur++;
355
+ }
356
+ tmp = tmp.getNextSibling();
357
+ }
358
+ if(occur != 0) occur = 1;
359
+ } else {
360
+ occur = 1;
361
+ }
362
+
363
+ } else if(cur.getNodeType() == Node.TEXT_NODE ||
364
+ cur.getNodeType() == Node.CDATA_SECTION_NODE) {
365
+ // I'm here. gist:129
366
+ // http://gist.github.com/144923
367
+
368
+ sep = "/";
369
+ name = "text()";
370
+ next = cur.getParentNode();
371
+
372
+ /*
373
+ * Thumbler index computation.
374
+ */
375
+
376
+ tmp = cur.getPreviousSibling();
377
+ while(tmp != null) {
378
+ if(tmp.getNodeType() == Node.TEXT_NODE ||
379
+ tmp.getNodeType() == Node.CDATA_SECTION_NODE) {
380
+ occur++;
381
+ }
382
+ tmp = tmp.getPreviousSibling();
383
+ }
384
+
385
+ if(occur == 0) {
386
+ tmp = cur.getNextSibling();
387
+
388
+ while(tmp != null && occur == 0) {
389
+ if(tmp.getNodeType() == Node.TEXT_NODE ||
390
+ tmp.getNodeType() == Node.CDATA_SECTION_NODE) {
391
+ occur++;
392
+ }
393
+ tmp = tmp.getNextSibling();
394
+ }
395
+ } else {
396
+ occur++;
397
+ }
398
+
399
+ } else if(cur.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
400
+ sep = "/";
401
+ name = "processing-instruction('"+cur.getLocalName()+"')";
402
+ next = cur.getParentNode();
403
+
404
+ /*
405
+ * Thumbler index computation.
406
+ */
407
+
408
+ tmp = cur.getParentNode();
409
+
410
+ while(tmp != null) {
411
+ if(tmp.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE &&
412
+ tmp.getLocalName().equals(cur.getLocalName())) {
413
+ occur++;
414
+ }
415
+ tmp = tmp.getPreviousSibling();
416
+ }
417
+
418
+ if(occur == 0) {
419
+ tmp = cur.getNextSibling();
420
+
421
+ while(tmp != null && occur == 0) {
422
+ if(tmp.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE &&
423
+ tmp.getLocalName().equals(cur.getLocalName())){
424
+ occur++;
425
+ }
426
+ tmp = tmp.getNextSibling();
427
+ }
428
+
429
+ if(occur != 0) {
430
+ occur = 1;
431
+ }
432
+
433
+ } else {
434
+ occur++;
435
+ }
436
+
437
+ } else if(cur.getNodeType() == Node.ATTRIBUTE_NODE) {
438
+ sep = "/@";
439
+ name = cur.getLocalName();
440
+
441
+ if(cur.getNamespaceURI() != null) {
442
+ if(cur.getPrefix() != null) {
443
+ name = cur.getPrefix() + ":" + name;
444
+ }
445
+ }
446
+
447
+ next = ((Attr) cur).getOwnerElement();
448
+
449
+ } else {
450
+ next = cur.getParentNode();
451
+ }
452
+
453
+ if(occur == 0){
454
+ buffer = sep+name+buffer;
455
+ } else {
456
+ buffer = sep+name+"["+occur+"]"+buffer;
457
+ }
458
+
459
+ cur = next;
460
+
461
+ } while(cur != null);
462
+
463
+ return buffer;
464
+ }
465
+
466
+ static boolean compareTwoNodes(Node m, Node n) {
467
+ return nodesAreEqual(m.getLocalName(), n.getLocalName()) &&
468
+ nodesAreEqual(m.getPrefix(), n.getPrefix());
469
+ }
470
+
471
+ private static boolean nodesAreEqual(Object a, Object b) {
472
+ return (((a == null) && (b == null)) ||
473
+ ((a != null) && (b != null) && (b.equals(a))));
474
+ }
475
+
476
+ private static boolean fullNamesMatch(Node a, Node b) {
477
+ return a.getNodeName().equals(b.getNodeName());
478
+ }
479
+
480
+ private static final Pattern encoded_pattern = Pattern.compile("&amp;|&gt;|&lt;|&#13;");
481
+ private static final String[] encoded = {"&amp;", "&gt;", "&lt;", "&#13;"};
482
+ private static final Pattern decoded_pattern = Pattern.compile("&|>|<|\r");
483
+ private static final String[] decoded = {"&", ">", "<", "\r"};
484
+
485
+ private static StringBuffer convert(Pattern ptn, CharSequence input, String[] oldChars, String[] newChars) {
486
+ Matcher matcher = ptn.matcher(input);
487
+ boolean result = matcher.find();
488
+ StringBuffer sb = new StringBuffer(input.length() + 8);
489
+ while (result) {
490
+ String matched = matcher.group();
491
+ String replacement = "";
492
+ for (int i=0; i<oldChars.length; i++) {
493
+ if (matched.contains(oldChars[i])) {
494
+ replacement = matched.replace(oldChars[i], newChars[i]);
495
+ break;
496
+ }
497
+ }
498
+ matcher.appendReplacement(sb, replacement);
499
+ result = matcher.find();
500
+ }
501
+ matcher.appendTail(sb);
502
+ return sb;
503
+ }
504
+
505
+ public static CharSequence encodeJavaString(CharSequence str) {
506
+ return convert(decoded_pattern, str, decoded, encoded);
507
+ }
508
+
509
+ public static CharSequence decodeJavaString(CharSequence str) {
510
+ return convert(encoded_pattern, str, encoded, decoded);
511
+ }
512
+
513
+ public static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
514
+ public static boolean isNamespace(Node node) {
515
+ return (XMLNS_URI.equals(node.getNamespaceURI()) || isNamespace(node.getNodeName()));
516
+ }
517
+
518
+ public static boolean isNamespace(String nodeName) {
519
+ return (nodeName.startsWith("xmlns"));
520
+ }
521
+
522
+ public static boolean isNonDefaultNamespace(Node node) {
523
+ return (isNamespace(node) && ! "xmlns".equals(node.getNodeName()));
524
+ }
525
+
526
+ public static boolean isXmlBase(String attrName) {
527
+ return "xml:base".equals(attrName) || "xlink:href".equals(attrName);
528
+ }
529
+
530
+ public static boolean isBlank(IRubyObject obj) {
531
+ if ( !(obj instanceof XmlText) ) return false;
532
+
533
+ CharSequence content = ((XmlNode) obj).getContentImpl();
534
+ return content == null || isBlank(content);
535
+ }
536
+
537
+ public static boolean isBlank(CharSequence str) {
538
+ int len = str.length(); int beg = 0;
539
+ while ((beg < len) && (str.charAt(beg) <= ' ')) beg++;
540
+ return beg == len;
541
+ }
542
+
543
+ public static boolean isBlank(String str) {
544
+ return str.isEmpty() || isBlank((CharSequence) str);
545
+ }
546
+
547
+ public static boolean isNullOrEmpty(String str) {
548
+ return str == null || str.isEmpty();
549
+ }
550
+
551
+ public static CharSequence canonicalizeWhitespace(CharSequence str) {
552
+ final int len = str.length();
553
+ StringBuilder sb = new StringBuilder(len);
554
+ boolean newline_added = false;
555
+ for ( int i = 0; i < len; i++ ) {
556
+ char c = str.charAt(i);
557
+ if ( c == '\n' ) {
558
+ if ( ! newline_added ) {
559
+ sb.append(c); newline_added = true;
560
+ }
561
+ } else {
562
+ sb.append(c);
563
+ }
564
+ }
565
+ return sb;
566
+ }
567
+
568
+ public static String newQName(String newPrefix, Node node) {
569
+ String tagName = getLocalPart(node.getNodeName());
570
+ if (newPrefix == null) return tagName;
571
+ return newPrefix + ':' + tagName;
572
+ }
573
+
574
+ public static IRubyObject[] nodeListToRubyArray(Ruby runtime, NodeList nodes) {
575
+ IRubyObject[] array = new IRubyObject[nodes.getLength()];
576
+ for (int i = 0; i < nodes.getLength(); i++) {
577
+ array[i] = NokogiriHelpers.getCachedNodeOrCreate(runtime, nodes.item(i));
578
+ }
579
+ return array;
580
+ }
581
+
582
+ public static IRubyObject[] nodeListToArray(Ruby ruby, List<Node> nodes) {
583
+ IRubyObject[] result = new IRubyObject[nodes.size()];
584
+ for (int i = 0; i < result.length; i++) {
585
+ result[i] = NokogiriHelpers.getCachedNodeOrCreate(ruby, nodes.get(i));
586
+ }
587
+ return result;
588
+ }
589
+
590
+ public static RubyArray nodeArrayToRubyArray(Ruby ruby, Node[] nodes) {
591
+ RubyArray n = RubyArray.newArray(ruby, nodes.length);
592
+ for (int i = 0; i < nodes.length; i++) {
593
+ n.append(NokogiriHelpers.getCachedNodeOrCreate(ruby, nodes[i]));
594
+ }
595
+ return n;
596
+ }
597
+
598
+ public static String getValidEncodingOrNull(IRubyObject encoding) {
599
+ if (encoding.isNil()) return null; // charsetNames does not like contains(null)
600
+ String enc = rubyStringToString(encoding.convertToString());
601
+ if (CharsetNames.contains(enc)) return enc;
602
+ return null;
603
+ }
604
+
605
+ public static String getValidEncoding(IRubyObject encoding) {
606
+ String validEncoding = getValidEncodingOrNull(encoding);
607
+ if (validEncoding != null) return validEncoding;
608
+ return Charset.defaultCharset().name();
609
+ }
610
+
611
+ private static final Set<String> CharsetNames = Charset.availableCharsets().keySet();
612
+
613
+ public static String adjustSystemIdIfNecessary(String currentDir, String scriptFileName, String baseURI, String systemId) {
614
+ if (systemId == null) return systemId;
615
+ File file = new File(systemId);
616
+ if (file.isAbsolute()) return systemId;
617
+ String path = resolveSystemId(baseURI, systemId);
618
+ if (path != null) return path;
619
+ path = resolveSystemId(currentDir, systemId);
620
+ if (path != null) return path;
621
+ return resolveSystemId(scriptFileName, systemId);
622
+ }
623
+
624
+ private static String resolveSystemId(String baseName, String systemId) {
625
+ if (baseName == null || baseName.length() < 1) return null;
626
+ String parentName;
627
+ baseName = baseName.replace("%20", " ");
628
+ File base = new File(baseName);
629
+ if (base.isDirectory()) parentName = baseName;
630
+ else parentName = base.getParent();
631
+ if (parentName == null) return null;
632
+ if (parentName.toLowerCase().startsWith("file:")) parentName = parentName.substring("file:".length());
633
+ File dtdFile = new File(parentName + "/" + systemId);
634
+ if (dtdFile.exists()) return dtdFile.getPath();
635
+ return null;
636
+ }
637
+
638
+ private static final Charset UTF8 = Charset.forName("UTF-8");
639
+
640
+ public static boolean isUTF8(String encoding) {
641
+ if (encoding == null) return true; // no need to convert encoding
642
+
643
+ if ("UTF-8".equals(encoding)) return true;
644
+ return UTF8.aliases().contains(encoding);
645
+ }
646
+
647
+ public static ByteBuffer convertEncoding(Charset output_charset, CharSequence input_string) {
648
+ return output_charset.encode(CharBuffer.wrap(input_string)); // does replace implicitly on un-mappable characters
649
+ }
650
+
651
+ public static CharSequence convertEncodingByNKFIfNecessary(ThreadContext context, XmlDocument doc, CharSequence str) {
652
+ if (!(doc instanceof HtmlDocument)) return str;
653
+ String parsed_encoding = ((HtmlDocument)doc).getPraedEncoding();
654
+ if (parsed_encoding == null) return str;
655
+ String ruby_encoding = rubyStringToString(doc.getEncoding());
656
+ if (ruby_encoding == null) return str;
657
+ Charset encoding = Charset.forName(ruby_encoding);
658
+ if (Charset.forName(parsed_encoding).compareTo(encoding) == 0) return str;
659
+ if (str.length() == 0) return str; // no need to convert
660
+ return NokogiriHelpers.nkf(context, encoding, str);
661
+ }
662
+
663
+ private static final ByteList _Sw = new ByteList(new byte[] { '-','S','w' }, false);
664
+ private static final ByteList _Jw = new ByteList(new byte[] { '-','J','w' }, false);
665
+ private static final ByteList _Ew = new ByteList(new byte[] { '-','E','w' }, false);
666
+ private static final ByteList _Ww = new ByteList(new byte[] { '-','W','w' }, false);
667
+
668
+ // This method is used from HTML documents. HTML meta tag with encoding specification
669
+ // might appear after non-ascii characters are used. For example, a title tag before
670
+ // a meta tag. In such a case, Xerces encodes characters in UTF-8 without seeing meta tag.
671
+ // Nokogiri uses NKF library to convert characters correct encoding. This means the method
672
+ // works only for JIS/Shift_JIS/EUC-JP.
673
+ private static CharSequence nkf(ThreadContext context, Charset encoding, CharSequence str) {
674
+ final Ruby runtime = context.getRuntime();
675
+ final ByteList opt;
676
+ if (NokogiriHelpers.Shift_JIS.compareTo(encoding) == 0) opt = _Sw;
677
+ else if (NokogiriHelpers.ISO_2022_JP.compareTo(encoding) == 0) opt = _Jw;
678
+ else if (NokogiriHelpers.EUC_JP.compareTo(encoding) == 0) opt = _Ew;
679
+ else opt = _Ww; // should not come here. should be treated before this method.
680
+
681
+ Class nkfClass;
682
+ try {
683
+ // JRuby 1.7 and later
684
+ nkfClass = runtime.getClassLoader().loadClass("org.jruby.ext.nkf.RubyNKF");
685
+ } catch (ClassNotFoundException e1) {
686
+ return str;
687
+ }
688
+ Method nkf_method;
689
+ try {
690
+ nkf_method = nkfClass.getMethod("nkf", ThreadContext.class, IRubyObject.class, IRubyObject.class, IRubyObject.class);
691
+ RubyString r_str =
692
+ (RubyString)nkf_method.invoke(null, context, null, runtime.newString(opt), runtime.newString(str.toString()));
693
+ return NokogiriHelpers.rubyStringToString(r_str);
694
+ } catch (SecurityException e) {
695
+ return str;
696
+ } catch (NoSuchMethodException e) {
697
+ return str;
698
+ } catch (IllegalArgumentException e) {
699
+ return str;
700
+ } catch (IllegalAccessException e) {
701
+ return str;
702
+ } catch (InvocationTargetException e) {
703
+ return str;
704
+ }
705
+ }
706
+
707
+ private static final Charset Shift_JIS = Charset.forName("Shift_JIS");
708
+ private static final Charset ISO_2022_JP = Charset.forName("ISO-2022-JP"); // JIS
709
+ private static final Charset EUC_JP = Charset.forName("EUC-JP");
710
+
711
+ public static boolean shouldEncode(Node text) {
712
+ final Boolean encoded = (Boolean) text.getUserData(NokogiriHelpers.ENCODED_STRING);
713
+ return encoded == null || ! encoded;
714
+ }
715
+
716
+ public static boolean shouldDecode(Node text) {
717
+ return !shouldEncode(text);
718
+ }
719
+
720
+ public static NokogiriNamespaceCache getNamespaceCache(Node node) {
721
+ XmlDocument xmlDoc = (XmlDocument) getCachedNode(node.getOwnerDocument());
722
+ return xmlDoc.getNamespaceCache();
723
+ }
724
+
725
+ public static Node renameNode(Node node, String namespaceURI, String qualifiedName) throws DOMException {
726
+ Document doc = node.getOwnerDocument();
727
+ NokogiriNamespaceCache nsCache = getNamespaceCache(node);
728
+ Node result = doc.renameNode(node, namespaceURI, qualifiedName);
729
+ if (result != node) {
730
+ nsCache.replaceNode(node, result);
731
+ }
732
+ return result;
733
+ }
734
+ }