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,252 @@
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 static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
+ import static nokogiri.internals.NokogiriHelpers.isNamespace;
37
+ import static nokogiri.internals.NokogiriHelpers.stringOrNil;
38
+
39
+ import nokogiri.HtmlDocument;
40
+ import nokogiri.NokogiriService;
41
+ import nokogiri.XmlDocument;
42
+ import nokogiri.XmlSyntaxError;
43
+
44
+ import org.apache.xerces.xni.Augmentations;
45
+ import org.apache.xerces.xni.QName;
46
+ import org.apache.xerces.xni.XMLAttributes;
47
+ import org.apache.xerces.xni.XNIException;
48
+ import org.apache.xerces.xni.parser.XMLDocumentFilter;
49
+ import org.apache.xerces.xni.parser.XMLParserConfiguration;
50
+ import org.cyberneko.html.HTMLConfiguration;
51
+ import org.cyberneko.html.filters.DefaultFilter;
52
+ import org.jruby.Ruby;
53
+ import org.jruby.RubyClass;
54
+ import org.jruby.runtime.ThreadContext;
55
+ import org.jruby.runtime.builtin.IRubyObject;
56
+ import org.w3c.dom.Document;
57
+ import org.w3c.dom.NamedNodeMap;
58
+ import org.w3c.dom.Node;
59
+ import org.w3c.dom.NodeList;
60
+
61
+ /**
62
+ * Parser for HtmlDocument. This class actually parses HtmlDocument using NekoHtml.
63
+ *
64
+ * @author sergio
65
+ * @author Patrick Mahoney <pat@polycrystal.org>
66
+ * @author Yoko Harada <yokolet@gmail.com>
67
+ */
68
+ public class HtmlDomParserContext extends XmlDomParserContext {
69
+
70
+ public HtmlDomParserContext(Ruby runtime, IRubyObject options) {
71
+ this(runtime, runtime.getNil(), options);
72
+ }
73
+
74
+ public HtmlDomParserContext(Ruby runtime, IRubyObject encoding, IRubyObject options) {
75
+ super(runtime, encoding, options);
76
+ java_encoding = NokogiriHelpers.getValidEncoding(encoding);
77
+ }
78
+
79
+ @Override
80
+ protected void initParser(Ruby runtime) {
81
+ XMLParserConfiguration config = new HTMLConfiguration();
82
+ //XMLDocumentFilter removeNSAttrsFilter = new RemoveNSAttrsFilter();
83
+ XMLDocumentFilter elementValidityCheckFilter = new ElementValidityCheckFilter(errorHandler);
84
+ //XMLDocumentFilter[] filters = { removeNSAttrsFilter, elementValidityCheckFilter};
85
+ XMLDocumentFilter[] filters = { elementValidityCheckFilter};
86
+
87
+ config.setErrorHandler(this.errorHandler);
88
+
89
+ parser = new NokogiriDomParser(config);
90
+
91
+ // see http://nekohtml.sourceforge.net/settings.html for details
92
+ setProperty("http://cyberneko.org/html/properties/default-encoding", java_encoding);
93
+ setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
94
+ setProperty("http://cyberneko.org/html/properties/names/attrs", "lower");
95
+ setProperty("http://cyberneko.org/html/properties/filters", filters);
96
+ setFeature("http://cyberneko.org/html/features/report-errors", true);
97
+ setFeature("http://xml.org/sax/features/namespaces", false);
98
+ }
99
+
100
+ @Override
101
+ public void setEncoding(String encoding) {
102
+ super.setEncoding(encoding);
103
+ }
104
+
105
+ /**
106
+ * Enable NekoHTML feature for balancing tags in a document fragment.
107
+ *
108
+ * This method is used in XmlNode#in_context method.
109
+ */
110
+ public void enableDocumentFragment() {
111
+ setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment", true);
112
+ }
113
+
114
+ @Override
115
+ public XmlDocument parse(ThreadContext context, RubyClass klass, IRubyObject url) {
116
+ XmlDocument xmlDoc = super.parse(context, klass, url);
117
+
118
+ // let's be consistent in how we handle RECOVER and NORECOVER (a.k.a. STRICT)
119
+ // https://github.com/sparklemotion/nokogiri/issues/2130
120
+ if (!options.recover && errorHandler.getErrors().size() > 0) {
121
+ XmlSyntaxError xmlSyntaxError = XmlSyntaxError.createXMLSyntaxError(context.runtime);
122
+ String exceptionMsg = String.format("%s: '%s'",
123
+ "Parser without recover option encountered error or warning",
124
+ errorHandler.getErrors().get(0));
125
+ xmlSyntaxError.setException(new Exception(exceptionMsg));
126
+ throw xmlSyntaxError.toThrowable();
127
+ }
128
+
129
+ return xmlDoc;
130
+ }
131
+
132
+ @Override
133
+ protected XmlDocument wrapDocument(ThreadContext context, RubyClass klass, Document document) {
134
+ HtmlDocument htmlDocument = new HtmlDocument(context.runtime, klass, document);
135
+ htmlDocument.setDocumentNode(context.runtime, document);
136
+ if (ruby_encoding.isNil()) {
137
+ // ruby_encoding might have detected by HtmlDocument::EncodingReader
138
+ if (detected_encoding != null && !detected_encoding.isNil()) {
139
+ ruby_encoding = detected_encoding;
140
+ } else {
141
+ // no encoding given & no encoding detected, then try to get it
142
+ String charset = tryGetCharsetFromHtml5MetaTag(document);
143
+ ruby_encoding = stringOrNil(context.runtime, charset);
144
+ }
145
+ }
146
+ htmlDocument.setEncoding(ruby_encoding);
147
+ htmlDocument.setParsedEncoding(java_encoding);
148
+ return htmlDocument;
149
+ }
150
+
151
+ // NekoHtml doesn't understand HTML5 meta tag format. This fails to detect charset
152
+ // from an HTML5 style meta tag. Luckily, the meta tag and charset exists in DOM tree
153
+ // so, this method attempts to find the charset.
154
+ private static String tryGetCharsetFromHtml5MetaTag(Document document) {
155
+ if (!"html".equalsIgnoreCase(document.getDocumentElement().getNodeName())) return null;
156
+ NodeList list = document.getDocumentElement().getChildNodes(); Node item;
157
+ for (int i = 0; i < list.getLength(); i++) {
158
+ if ("head".equalsIgnoreCase((item = list.item(i)).getNodeName())) {
159
+ NodeList headers = item.getChildNodes();
160
+ for (int j = 0; j < headers.getLength(); j++) {
161
+ if ("meta".equalsIgnoreCase((item = headers.item(j)).getNodeName())) {
162
+ NamedNodeMap nodeMap = item.getAttributes();
163
+ for (int k = 0; k < nodeMap.getLength(); k++) {
164
+ if ("charset".equalsIgnoreCase((item = nodeMap.item(k)).getNodeName())) {
165
+ return item.getNodeValue();
166
+ }
167
+ }
168
+ }
169
+ }
170
+ }
171
+ }
172
+ return null;
173
+ }
174
+
175
+ /**
176
+ * Filter to strip out attributes that pertain to XML namespaces.
177
+ */
178
+ public static class RemoveNSAttrsFilter extends DefaultFilter {
179
+ @Override
180
+ public void startElement(QName element, XMLAttributes attrs,
181
+ Augmentations augs) throws XNIException {
182
+ int i;
183
+ for (i = 0; i < attrs.getLength(); ++i) {
184
+ if (isNamespace(attrs.getQName(i))) {
185
+ attrs.removeAttributeAt(i);
186
+ --i;
187
+ }
188
+ }
189
+
190
+ element.uri = null;
191
+ super.startElement(element, attrs, augs);
192
+ }
193
+ }
194
+
195
+ public static class ElementValidityCheckFilter extends DefaultFilter {
196
+ private NokogiriErrorHandler errorHandler;
197
+
198
+ private ElementValidityCheckFilter(NokogiriErrorHandler errorHandler) {
199
+ this.errorHandler = errorHandler;
200
+ }
201
+
202
+ // element names from xhtml1-strict.dtd
203
+ private static String[][] element_names = {
204
+ {"a", "abbr", "acronym", "address", "area"},
205
+ {"b", "base", "basefont", "bdo", "big", "blockquote", "body", "br", "button"},
206
+ {"caption", "cite", "code", "col", "colgroup"},
207
+ {"dd", "del", "dfn", "div", "dl", "dt"},
208
+ {"em"},
209
+ {"fieldset", "font", "form", "frame", "frameset"},
210
+ {}, // g
211
+ {"h1", "h2", "h3", "h4", "h5", "h6", "head", "hr", "html"},
212
+ {"i", "iframe", "img", "input", "ins"},
213
+ {}, // j
214
+ {"kbd"},
215
+ {"label", "legend", "li", "link"},
216
+ {"map", "meta"},
217
+ {"noframes", "noscript"},
218
+ {"object", "ol", "optgroup", "option"},
219
+ {"p", "param", "pre"},
220
+ {"q"},
221
+ {}, // r
222
+ {"s", "samp", "script", "select", "small", "span", "strike", "strong", "style", "sub", "sup"},
223
+ {"table", "tbody", "td", "textarea", "tfoot", "th", "thead", "title", "tr", "tt"},
224
+ {"u", "ul"},
225
+ {"var"},
226
+ {}, // w
227
+ {}, // x
228
+ {}, // y
229
+ {} // z
230
+ };
231
+
232
+ private static boolean isValid(final String name) {
233
+ int index = name.charAt(0) - 97;
234
+ if (index >= element_names.length) return false;
235
+ String[] elementNames = element_names[index];
236
+ for (int i=0; i<elementNames.length; i++) {
237
+ if (name.equals(elementNames[i])) {
238
+ return true;
239
+ }
240
+ }
241
+ return false;
242
+ }
243
+
244
+ @Override
245
+ public void startElement(QName name, XMLAttributes attrs, Augmentations augs) throws XNIException {
246
+ if (!isValid(name.rawname)) {
247
+ errorHandler.addError(new Exception("Tag " + name.rawname + " invalid"));
248
+ }
249
+ super.startElement(name, attrs, augs);
250
+ }
251
+ }
252
+ }
@@ -0,0 +1,20 @@
1
+ package nokogiri.internals;
2
+
3
+ import org.xml.sax.ErrorHandler;
4
+ import org.xml.sax.SAXException;
5
+ import org.xml.sax.SAXParseException;
6
+
7
+ public class IgnoreSchemaErrorsErrorHandler implements ErrorHandler {
8
+
9
+ @Override
10
+ public void warning(SAXParseException exception) throws SAXException {
11
+ }
12
+
13
+ @Override
14
+ public void error(SAXParseException exception) throws SAXException {
15
+ }
16
+
17
+ @Override
18
+ public void fatalError(SAXParseException exception) throws SAXException {
19
+ }
20
+ }
@@ -0,0 +1,151 @@
1
+ /**
2
+ *
3
+ */
4
+ package nokogiri.internals;
5
+
6
+ import java.io.ByteArrayInputStream;
7
+ import java.io.IOException;
8
+ import java.io.InputStream;
9
+ import java.util.LinkedList;
10
+ import java.util.List;
11
+ import java.util.concurrent.Callable;
12
+ import java.util.concurrent.Future;
13
+ import java.util.concurrent.FutureTask;
14
+ import java.util.concurrent.LinkedBlockingQueue;
15
+
16
+ import nokogiri.XmlSaxPushParser;
17
+
18
+ /**
19
+ * A smart input stream that signals the caller when a chunk of data is consumed
20
+ * from the stream. The main use of this stream is to synchronize the
21
+ * {@link XmlSaxPushParser} and the {@link XmlSaxParser} which runs in a
22
+ * different thread.
23
+ *
24
+ * @author John Shahid <jvshahid@gmail.com>
25
+ */
26
+ public class NokogiriBlockingQueueInputStream extends InputStream {
27
+ private final LinkedBlockingQueue<Task> queue;
28
+ protected Task currentTask;
29
+ protected ByteArrayInputStream currentStream;
30
+ protected int position;
31
+ protected boolean closed = false;
32
+
33
+ public static final ByteArrayInputStream END = new ByteArrayInputStream(new byte[0]);
34
+
35
+ private static class Task extends FutureTask<Void> {
36
+ private final ByteArrayInputStream stream;
37
+
38
+ public Task(ByteArrayInputStream stream) {
39
+ super(new Callable<Void>() {
40
+ @Override
41
+ public Void call() throws Exception {
42
+ // TODO Auto-generated method stub
43
+ return null;
44
+ }
45
+ });
46
+ this.stream = stream;
47
+ }
48
+
49
+ public ByteArrayInputStream getStream() {
50
+ return stream;
51
+ }
52
+
53
+ @Override
54
+ public void run() {
55
+ // don't do anything
56
+ }
57
+
58
+ @Override
59
+ public boolean runAndReset() {
60
+ // don't do anything
61
+ return true;
62
+ }
63
+
64
+ @Override
65
+ public void set(Void v) {
66
+ super.set(v);
67
+ }
68
+ }
69
+
70
+ public NokogiriBlockingQueueInputStream() {
71
+ queue = new LinkedBlockingQueue<Task>();
72
+ }
73
+
74
+ /**
75
+ * This method shouldn't be called unless the parser has finished parsing or
76
+ * threw an exception while doing so, otherwise, there'll be the protential
77
+ * that the read method will block indefinitely.
78
+ */
79
+ @Override
80
+ public synchronized void close() {
81
+ closed = true;
82
+ List<Task> tasks = new LinkedList<Task>();
83
+ queue.drainTo(tasks);
84
+ tasks.add(currentTask);
85
+ for (Task task : tasks) {
86
+ task.set(null);
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Add @param stream to the end of the queue of data that will be returned by
92
+ * {@link #read()} and its variants. The method will @return a future whose
93
+ * {@link Future#get()} will block until the data in @param stream is read.
94
+ *
95
+ * Passing the special stream {@link #END} to this method, will cause
96
+ * {@link #read()} to return an eof indicator (i.e. -1) to the caller, after
97
+ * all the data inserted before {@link #END} is processed.
98
+ *
99
+ * @return
100
+ */
101
+ public synchronized Future<Void> addChunk(ByteArrayInputStream stream) throws ClosedStreamException {
102
+ if (closed)
103
+ throw new ClosedStreamException("Cannot add a chunk to a closed stream");
104
+ Task task = new Task(stream);
105
+ queue.add(task);
106
+ return task;
107
+ }
108
+
109
+ /*
110
+ * (non-Javadoc)
111
+ *
112
+ * @see java.io.InputStream#read()
113
+ */
114
+ @Override
115
+ public int read() throws IOException {
116
+ if (currentTask == null || currentStream.available() == 0)
117
+ if (getNextTask() == -1)
118
+ return -1;
119
+ return currentStream.read();
120
+ }
121
+
122
+ /*
123
+ * (non-Javadoc)
124
+ *
125
+ * @see java.io.InputStream#read(byte[], int, int)
126
+ */
127
+ @Override
128
+ public int read(byte[] bytes, int off, int len) {
129
+ if (currentTask == null || currentStream.available() == 0) {
130
+ if (getNextTask() == -1) {
131
+ currentTask.set(null);
132
+ return -1;
133
+ }
134
+ }
135
+ return currentStream.read(bytes, off, len);
136
+ }
137
+
138
+ protected int getNextTask() {
139
+ while (true) {
140
+ try {
141
+ if (currentTask != null)
142
+ currentTask.set(null);
143
+ currentTask = queue.take();
144
+ currentStream = currentTask.getStream();
145
+ return currentStream.available() == 0 ? -1 : currentStream.available();
146
+ } catch (InterruptedException ex) {
147
+ // keep retrying to read
148
+ }
149
+ }
150
+ }
151
+ }
@@ -0,0 +1,116 @@
1
+ /**
2
+ * (The MIT License)
3
+ *
4
+ * Copyright (c) 2008 - 2011:
5
+ *
6
+ * * {Aaron Patterson}[http://tenderlovemaking.com]
7
+ * * {Mike Dalessio}[http://mike.daless.io]
8
+ * * {Charles Nutter}[http://blog.headius.com]
9
+ * * {Sergio Arbeo}[http://www.serabe.com]
10
+ * * {Patrick Mahoney}[http://polycrystal.org]
11
+ * * {Yoko Harada}[http://yokolet.blogspot.com]
12
+ *
13
+ * Permission is hereby granted, free of charge, to any person obtaining
14
+ * a copy of this software and associated documentation files (the
15
+ * 'Software'), to deal in the Software without restriction, including
16
+ * without limitation the rights to use, copy, modify, merge, publish,
17
+ * distribute, sublicense, and/or sell copies of the Software, and to
18
+ * permit persons to whom the Software is furnished to do so, subject to
19
+ * the following conditions:
20
+ *
21
+ * The above copyright notice and this permission notice shall be
22
+ * included in all copies or substantial portions of the Software.
23
+ *
24
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
+ */
32
+
33
+ package nokogiri.internals;
34
+
35
+ import java.io.IOException;
36
+
37
+ import nokogiri.XmlDocument;
38
+
39
+ import org.apache.xerces.parsers.DOMParser;
40
+ import org.apache.xerces.parsers.XIncludeParserConfiguration;
41
+ import org.apache.xerces.xni.parser.XMLParserConfiguration;
42
+ import org.cyberneko.dtd.DTDConfiguration;
43
+ import org.w3c.dom.Document;
44
+ import org.xml.sax.Attributes;
45
+ import org.xml.sax.ContentHandler;
46
+ import org.xml.sax.InputSource;
47
+ import org.xml.sax.Locator;
48
+ import org.xml.sax.SAXException;
49
+
50
+ /**
51
+ * Sets up a Xerces/XNI DOM Parser for use with Nokogiri. Uses
52
+ * NekoDTD to parse the DTD into a tree of Nodes.
53
+ *
54
+ * @author Patrick Mahoney <pat@polycrystal.org>
55
+ */
56
+ public class NokogiriDomParser extends DOMParser {
57
+ protected DOMParser dtd;
58
+ protected boolean xInclude;
59
+ protected XMLParserConfiguration config;
60
+
61
+ public NokogiriDomParser(XMLParserConfiguration config) {
62
+ super(config);
63
+ this.config = config;
64
+ initialize();
65
+ }
66
+
67
+ public NokogiriDomParser(ParserContext.Options options) {
68
+ xInclude = options.xInclude;
69
+ initialize();
70
+ }
71
+
72
+ protected void initialize() {
73
+ if (config == null) {
74
+ if (xInclude) {
75
+ config = new XIncludeParserConfiguration();
76
+ } else {
77
+ config = getXMLParserConfiguration();
78
+ }
79
+ }
80
+
81
+ DTDConfiguration dtdConfig = new DTDConfiguration();
82
+ dtd = new DOMParser(dtdConfig);
83
+
84
+ config.setDTDHandler(dtdConfig);
85
+ config.setDTDContentModelHandler(dtdConfig);
86
+ }
87
+
88
+ @Override
89
+ public void parse(InputSource source) throws SAXException, IOException {
90
+ dtd.reset();
91
+ if (xInclude) {
92
+ setEntityResolver(new NokogiriXInlcudeEntityResolver(source));
93
+ }
94
+ super.parse(source);
95
+ Document doc = getDocument();
96
+ if (doc == null)
97
+ throw new RuntimeException("null document");
98
+
99
+ doc.setUserData(XmlDocument.DTD_RAW_DOCUMENT, dtd.getDocument(), null);
100
+ }
101
+
102
+ private static class NokogiriXInlcudeEntityResolver implements org.xml.sax.EntityResolver {
103
+ InputSource source;
104
+ private NokogiriXInlcudeEntityResolver(InputSource source) {
105
+ this.source = source;
106
+ }
107
+
108
+ @Override
109
+ public InputSource resolveEntity(String publicId, String systemId)
110
+ throws SAXException, IOException {
111
+ if (systemId != null) source.setSystemId(systemId);
112
+ if (publicId != null) source.setPublicId(publicId);
113
+ return source;
114
+ }
115
+ }
116
+ }
@@ -0,0 +1,121 @@
1
+ package nokogiri.internals;
2
+
3
+ import java.io.ByteArrayInputStream;
4
+ import java.io.File;
5
+ import java.io.IOException;
6
+ import java.net.URI;
7
+
8
+ import nokogiri.internals.ParserContext.Options;
9
+
10
+ import org.jruby.Ruby;
11
+ import org.xml.sax.InputSource;
12
+ import org.xml.sax.SAXException;
13
+ import org.xml.sax.ext.EntityResolver2;
14
+
15
+ /**
16
+ * An entity resolver aware of the fact that the Ruby runtime can
17
+ * change directory but the JVM cannot. Thus any file based
18
+ * entity resolution that uses relative paths must be translated
19
+ * to be relative to the current directory of the Ruby runtime.
20
+ */
21
+ public class NokogiriEntityResolver implements EntityResolver2 {
22
+ protected final Ruby runtime;
23
+ private final NokogiriErrorHandler handler;
24
+ private final Options options;
25
+
26
+ public NokogiriEntityResolver(Ruby runtime, NokogiriErrorHandler handler, Options options) {
27
+ super();
28
+ this.runtime = runtime;
29
+ this.handler = handler;
30
+ this.options = options;
31
+ }
32
+
33
+ @Override
34
+ public InputSource getExternalSubset(String name, String baseURI)
35
+ throws SAXException, IOException {
36
+ return null;
37
+ }
38
+
39
+ @Override
40
+ public InputSource resolveEntity(String publicId, String systemId)
41
+ throws SAXException, IOException {
42
+ return resolveEntity(runtime, null, publicId, null, systemId);
43
+ }
44
+
45
+ @Override
46
+ public InputSource resolveEntity(String name,
47
+ String publicId,
48
+ String baseURI,
49
+ String systemId)
50
+ throws SAXException, IOException {
51
+ return resolveEntity(runtime, name, publicId, baseURI, systemId);
52
+ }
53
+
54
+ private static File join(String parent, String child) {
55
+ if (new File(parent).isFile()) {
56
+ parent = new File(parent).getParent();
57
+ }
58
+ return new File(parent, child);
59
+ }
60
+
61
+ private static InputSource emptyInputSource(InputSource source) {
62
+ source.setByteStream(new ByteArrayInputStream(new byte[0]));
63
+ return source;
64
+ }
65
+
66
+ private boolean shouldLoadDtd() {
67
+ return options.dtdLoad || options.dtdValid;
68
+ }
69
+
70
+ private void addError(String errorMessage) {
71
+ if (handler != null) handler.errors.add(new Exception(errorMessage));
72
+ }
73
+
74
+ /**
75
+ * Create a file base input source taking into account the current
76
+ * directory of <code>runtime</code>.
77
+ * @throws SAXException
78
+ */
79
+ protected InputSource resolveEntity(Ruby runtime, String name, String publicId, String baseURI, String systemId)
80
+ throws IOException, SAXException {
81
+ InputSource s = new InputSource();
82
+ if (name.equals("[dtd]") && !shouldLoadDtd()) {
83
+ return emptyInputSource(s);
84
+ } else if (!name.equals("[dtd]") && !options.noEnt) {
85
+ return emptyInputSource(s);
86
+ }
87
+ String adjustedSystemId;
88
+ URI uri = URI.create(systemId);
89
+ if (options.noNet && uri.getHost() != null) {
90
+ addError("Attempt to load network entity " + systemId);
91
+ return emptyInputSource(s);
92
+ }
93
+ // if this is a url or absolute file name then use it
94
+ if (uri.isAbsolute() && !uri.isOpaque()) {
95
+ adjustedSystemId = uri.toURL().toString();
96
+ } else if (new File(uri.getPath()).isAbsolute()) {
97
+ adjustedSystemId = uri.getPath();
98
+ } else if (baseURI != null) {
99
+ URI baseuri = URI.create(baseURI);
100
+ if (options.noNet && baseuri.getHost() != null) {
101
+ addError("Attempt to load network entity " + systemId);
102
+ return emptyInputSource(s);
103
+ }
104
+ if (baseuri.getHost() == null) {
105
+ // this is a local file
106
+ adjustedSystemId = join(baseuri.getPath(), uri.getPath()).getCanonicalPath();
107
+ } else {
108
+ // this is a url, then resolve uri using baseuri
109
+ adjustedSystemId = baseuri.resolve(systemId).toURL().toString();
110
+ }
111
+ } else {
112
+ // baseURI is null we have to use the current working directory to resolve the entity
113
+ String pwd = runtime.getCurrentDirectory();
114
+ adjustedSystemId = join(pwd, uri.getPath()).getCanonicalPath();
115
+ }
116
+ s.setSystemId(adjustedSystemId);
117
+ s.setPublicId(publicId);
118
+ return s;
119
+ }
120
+
121
+ }