nokogiri 1.5.0.beta.4 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (107) hide show
  1. data/.gemtest +0 -0
  2. data/CHANGELOG.ja.rdoc +34 -0
  3. data/CHANGELOG.rdoc +40 -1
  4. data/Manifest.txt +11 -2
  5. data/README.rdoc +1 -1
  6. data/Rakefile +100 -104
  7. data/bin/nokogiri +1 -2
  8. data/ext/nokogiri/nokogiri.c +24 -1
  9. data/ext/nokogiri/xml_io.c +32 -7
  10. data/ext/nokogiri/xml_node.c +14 -13
  11. data/ext/nokogiri/xml_sax_parser.c +9 -4
  12. data/ext/nokogiri/xslt_stylesheet.c +7 -1
  13. data/lib/nokogiri.rb +3 -22
  14. data/lib/nokogiri/css.rb +4 -0
  15. data/lib/nokogiri/html/document.rb +10 -14
  16. data/lib/nokogiri/version.rb +76 -23
  17. data/lib/nokogiri/xml/builder.rb +7 -0
  18. data/lib/nokogiri/xml/document.rb +17 -1
  19. data/lib/nokogiri/xml/document_fragment.rb +14 -0
  20. data/lib/nokogiri/xml/node.rb +36 -28
  21. data/lib/nokogiri/xml/node/save_options.rb +17 -1
  22. data/lib/nokogiri/xml/node_set.rb +7 -0
  23. data/lib/nokogiri/xml/parse_options.rb +8 -0
  24. data/lib/nokogiri/xml/reader.rb +6 -6
  25. data/lib/nokogiri/xml/schema.rb +7 -1
  26. data/nokogiri_help_responses.md +40 -0
  27. data/tasks/cross_compile.rb +134 -159
  28. data/tasks/nokogiri.org.rb +18 -0
  29. data/tasks/test.rb +1 -1
  30. data/test/files/encoding.html +82 -0
  31. data/test/files/encoding.xhtml +84 -0
  32. data/test/files/metacharset.html +10 -0
  33. data/test/files/noencoding.html +47 -0
  34. data/test/helper.rb +2 -0
  35. data/test/html/test_document.rb +15 -0
  36. data/test/html/test_document_encoding.rb +13 -0
  37. data/test/test_memory_leak.rb +20 -0
  38. data/test/test_reader.rb +22 -0
  39. data/test/test_xslt_transforms.rb +6 -2
  40. data/test/xml/node/test_save_options.rb +10 -2
  41. data/test/xml/test_builder.rb +17 -0
  42. data/test/xml/test_document.rb +22 -0
  43. data/test/xml/test_node.rb +19 -1
  44. data/test/xml/test_node_reparenting.rb +16 -3
  45. data/test/xml/test_node_set.rb +34 -0
  46. data/test/xml/test_schema.rb +5 -0
  47. data/test/xslt/test_exception_handling.rb +37 -0
  48. metadata +185 -157
  49. data/deps.rip +0 -5
  50. data/ext/java/nokogiri/EncodingHandler.java +0 -124
  51. data/ext/java/nokogiri/HtmlDocument.java +0 -149
  52. data/ext/java/nokogiri/HtmlElementDescription.java +0 -145
  53. data/ext/java/nokogiri/HtmlEntityLookup.java +0 -79
  54. data/ext/java/nokogiri/HtmlSaxParserContext.java +0 -259
  55. data/ext/java/nokogiri/NokogiriService.java +0 -535
  56. data/ext/java/nokogiri/XmlAttr.java +0 -191
  57. data/ext/java/nokogiri/XmlAttributeDecl.java +0 -130
  58. data/ext/java/nokogiri/XmlCdata.java +0 -91
  59. data/ext/java/nokogiri/XmlComment.java +0 -86
  60. data/ext/java/nokogiri/XmlDocument.java +0 -529
  61. data/ext/java/nokogiri/XmlDocumentFragment.java +0 -217
  62. data/ext/java/nokogiri/XmlDtd.java +0 -467
  63. data/ext/java/nokogiri/XmlElement.java +0 -222
  64. data/ext/java/nokogiri/XmlElementContent.java +0 -382
  65. data/ext/java/nokogiri/XmlElementDecl.java +0 -148
  66. data/ext/java/nokogiri/XmlEntityDecl.java +0 -162
  67. data/ext/java/nokogiri/XmlEntityReference.java +0 -75
  68. data/ext/java/nokogiri/XmlNamespace.java +0 -128
  69. data/ext/java/nokogiri/XmlNode.java +0 -1399
  70. data/ext/java/nokogiri/XmlNodeSet.java +0 -311
  71. data/ext/java/nokogiri/XmlProcessingInstruction.java +0 -103
  72. data/ext/java/nokogiri/XmlReader.java +0 -411
  73. data/ext/java/nokogiri/XmlRelaxng.java +0 -144
  74. data/ext/java/nokogiri/XmlSaxParserContext.java +0 -367
  75. data/ext/java/nokogiri/XmlSaxPushParser.java +0 -184
  76. data/ext/java/nokogiri/XmlSchema.java +0 -319
  77. data/ext/java/nokogiri/XmlSyntaxError.java +0 -119
  78. data/ext/java/nokogiri/XmlText.java +0 -136
  79. data/ext/java/nokogiri/XmlXpathContext.java +0 -179
  80. data/ext/java/nokogiri/XsltStylesheet.java +0 -183
  81. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +0 -206
  82. data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +0 -73
  83. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +0 -86
  84. data/ext/java/nokogiri/internals/NokogiriHandler.java +0 -327
  85. data/ext/java/nokogiri/internals/NokogiriHelpers.java +0 -582
  86. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +0 -171
  87. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +0 -118
  88. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +0 -74
  89. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +0 -121
  90. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +0 -79
  91. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +0 -126
  92. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +0 -56
  93. data/ext/java/nokogiri/internals/ParserContext.java +0 -278
  94. data/ext/java/nokogiri/internals/PushInputStream.java +0 -411
  95. data/ext/java/nokogiri/internals/ReaderNode.java +0 -474
  96. data/ext/java/nokogiri/internals/SaveContext.java +0 -288
  97. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +0 -76
  98. data/ext/java/nokogiri/internals/XmlDeclHandler.java +0 -42
  99. data/ext/java/nokogiri/internals/XmlDomParser.java +0 -77
  100. data/ext/java/nokogiri/internals/XmlDomParserContext.java +0 -238
  101. data/ext/java/nokogiri/internals/XmlSaxParser.java +0 -65
  102. data/ext/java/nokogiri/internals/XsltExtensionFunction.java +0 -72
  103. data/lib/isorelax.jar +0 -0
  104. data/lib/jing.jar +0 -0
  105. data/lib/nekodtd.jar +0 -0
  106. data/lib/nekohtml.jar +0 -0
  107. data/lib/xercesImpl.jar +0 -0
@@ -1,79 +0,0 @@
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 org.apache.xerces.xni.parser.XMLParseException;
36
- import org.xml.sax.SAXException;
37
- import org.xml.sax.SAXParseException;
38
-
39
- /**
40
- * Strict error handler. Even though strict is specified, Nokogiri allows to go further
41
- * when NOERROR or/both NOWARNING is/are true.
42
- *
43
- * @author sergio
44
- * @author Yoko Harada <yokolet@gmail.com>
45
- */
46
- public class NokogiriStrictErrorHandler extends NokogiriErrorHandler {
47
- public NokogiriStrictErrorHandler(boolean noerror, boolean nowarning) {
48
- super(noerror, nowarning);
49
- }
50
-
51
- public void warning(SAXParseException spex) throws SAXException {
52
- if (!nowarning) throw spex;
53
- else errors.add(spex);
54
- }
55
-
56
- public void error(SAXParseException spex) throws SAXException {
57
- if (!noerror) throw spex;
58
- else errors.add(spex);
59
- }
60
-
61
- public void fatalError(SAXParseException spex) throws SAXException {
62
- throw spex;
63
- }
64
-
65
- public void error(String domain, String key, XMLParseException e) throws XMLParseException {
66
- if (!noerror) throw e;
67
- else errors.add(e);
68
- }
69
-
70
- public void fatalError(String domain, String key, XMLParseException e) throws XMLParseException {
71
- throw e;
72
- }
73
-
74
- public void warning(String domain, String key, XMLParseException e) throws XMLParseException {
75
- if (!nowarning) throw e;
76
- if (!usesNekoHtml(domain)) errors.add(e);
77
- }
78
-
79
- }
@@ -1,126 +0,0 @@
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 static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
-
37
- import java.util.List;
38
-
39
- import javax.xml.xpath.XPathFunction;
40
- import javax.xml.xpath.XPathFunctionException;
41
-
42
- import nokogiri.NokogiriService;
43
- import nokogiri.XmlNode;
44
- import nokogiri.XmlNodeSet;
45
-
46
- import org.jruby.Ruby;
47
- import org.jruby.RubyArray;
48
- import org.jruby.RubyBoolean;
49
- import org.jruby.RubyFloat;
50
- import org.jruby.RubyString;
51
- import org.jruby.javasupport.JavaUtil;
52
- import org.jruby.javasupport.util.RuntimeHelpers;
53
- import org.jruby.runtime.ThreadContext;
54
- import org.jruby.runtime.builtin.IRubyObject;
55
- import org.w3c.dom.NodeList;
56
-
57
- /**
58
- * Xpath function handler.
59
- *
60
- * @author sergio
61
- * @author Yoko Harada <yokolet@gmail.com>
62
- */
63
- public class NokogiriXPathFunction implements XPathFunction {
64
- private final IRubyObject handler;
65
- private final String name;
66
- private final int arity;
67
-
68
- public NokogiriXPathFunction(IRubyObject handler, String name, int arity) {
69
- this.handler = handler;
70
- this.name = name;
71
- this.arity = arity;
72
- }
73
-
74
- public Object evaluate(List args) throws XPathFunctionException {
75
- if(args.size() != this.arity) {
76
- throw new XPathFunctionException("arity does not match");
77
- }
78
-
79
- Ruby ruby = this.handler.getRuntime();
80
- ThreadContext context = ruby.getCurrentContext();
81
-
82
- IRubyObject result = RuntimeHelpers.invoke(context, this.handler,
83
- this.name, fromObjectToRubyArgs(args));
84
-
85
- return fromRubyToObject(result);
86
- }
87
-
88
- private IRubyObject[] fromObjectToRubyArgs(List args) {
89
- IRubyObject[] newArgs = new IRubyObject[args.size()];
90
- for(int i = 0; i < args.size(); i++) {
91
- newArgs[i] = fromObjectToRuby(args.get(i));
92
- }
93
- return newArgs;
94
- }
95
-
96
- private IRubyObject fromObjectToRuby(Object o) {
97
- // argument object type is one of NodeList, String, Boolean, or Double.
98
- Ruby runtime = this.handler.getRuntime();
99
- if (o instanceof NodeList) {
100
- XmlNodeSet xmlNodeSet = (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"));
101
- xmlNodeSet.setNodeList((NodeList) o);
102
- return xmlNodeSet;
103
- } else {
104
- return JavaUtil.convertJavaToUsableRubyObject(runtime, o);
105
- }
106
- }
107
-
108
- private Object fromRubyToObject(IRubyObject o) {
109
- Ruby runtime = this.handler.getRuntime();
110
- if(o instanceof RubyString) {
111
- return o.toJava(String.class);
112
- } else if (o instanceof RubyFloat) {
113
- return o.toJava(Double.class);
114
- } else if (o instanceof RubyBoolean) {
115
- return o.toJava(Boolean.class);
116
- } else if (o instanceof XmlNodeSet) {
117
- return ((XmlNodeSet) o).toNodeList(runtime);
118
- } else if (o instanceof RubyArray) {
119
- XmlNodeSet xmlNodeSet = (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"));
120
- xmlNodeSet.setInitialNodes((RubyArray)o);
121
- return xmlNodeSet.toNodeList(runtime);
122
- } else /*if (o instanceof XmlNode)*/ {
123
- return ((XmlNode) o).getNode();
124
- }
125
- }
126
- }
@@ -1,56 +0,0 @@
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 javax.xml.namespace.QName;
36
- import javax.xml.xpath.XPathFunction;
37
- import javax.xml.xpath.XPathFunctionResolver;
38
- import org.jruby.runtime.builtin.IRubyObject;
39
-
40
- /**
41
- * Xpath function resolver class, which is used in XmlXpathContext.
42
- *
43
- * @author sergio
44
- */
45
- public class NokogiriXPathFunctionResolver implements XPathFunctionResolver {
46
-
47
- private IRubyObject handler;
48
-
49
- public NokogiriXPathFunctionResolver(IRubyObject handler) {
50
- this.handler = handler;
51
- }
52
-
53
- public XPathFunction resolveFunction(QName name, int arity) {
54
- return new NokogiriXPathFunction(this.handler, name.getLocalPart(), arity);
55
- }
56
- }
@@ -1,278 +0,0 @@
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 static nokogiri.internals.NokogiriHelpers.rubyStringToString;
36
- import static org.jruby.javasupport.util.RuntimeHelpers.invoke;
37
-
38
- import java.io.ByteArrayInputStream;
39
- import java.io.File;
40
- import java.io.FileInputStream;
41
- import java.io.IOException;
42
- import java.io.InputStream;
43
-
44
- import org.jruby.Ruby;
45
- import org.jruby.RubyClass;
46
- import org.jruby.RubyIO;
47
- import org.jruby.RubyObject;
48
- import org.jruby.RubyString;
49
- import org.jruby.RubyStringIO;
50
- import org.jruby.exceptions.RaiseException;
51
- import org.jruby.runtime.ThreadContext;
52
- import org.jruby.runtime.builtin.IRubyObject;
53
- import org.jruby.util.ByteList;
54
- import org.jruby.util.TypeConverter;
55
- import org.xml.sax.InputSource;
56
- import org.xml.sax.SAXException;
57
- import org.xml.sax.ext.EntityResolver2;
58
-
59
- /**
60
- * Base class for the various parser contexts. Handles converting
61
- * Ruby objects to InputSource objects.
62
- *
63
- * @author Patrick Mahoney <pat@polycrystal.org>
64
- */
65
- public class ParserContext extends RubyObject {
66
- protected InputSource source = null;
67
-
68
- /**
69
- * Create a file base input source taking into account the current
70
- * directory of <code>runtime</code>.
71
- */
72
- public static InputSource resolveEntity(Ruby runtime,
73
- String publicId,
74
- String baseURI,
75
- String systemId)
76
- throws IOException {
77
- String path;
78
-
79
- if ((new File(systemId)).isAbsolute()) {
80
- path = systemId;
81
- } else if (baseURI != null) {
82
- path = (new File(baseURI, systemId)).getAbsolutePath();
83
- } else {
84
- String rubyDir = runtime.getCurrentDirectory();
85
- path = (new File(rubyDir, systemId)).getAbsolutePath();
86
- }
87
-
88
- InputSource s = new InputSource(new FileInputStream(path));
89
- s.setSystemId(systemId);
90
- s.setPublicId(publicId);
91
- return s;
92
- }
93
-
94
- public ParserContext(Ruby runtime) {
95
- // default to class 'Object' because this class isn't exposed to Ruby
96
- super(runtime, runtime.getObject());
97
- }
98
-
99
- public ParserContext(Ruby runtime, RubyClass klass) {
100
- super(runtime, klass);
101
- }
102
-
103
- protected InputSource getInputSource() {
104
- return source;
105
- }
106
-
107
- /**
108
- * Set the InputSource from <code>data</code> which may be an IO
109
- * object, a String, or a StringIO.
110
- */
111
- public void setInputSource(ThreadContext context,
112
- IRubyObject data) {
113
- Ruby ruby = context.getRuntime();
114
- RubyString stringData = null;
115
- if (invoke(context, data, "respond_to?",
116
- ruby.newSymbol("to_io").to_sym()).isTrue()) {
117
- /* IO or other object that responds to :to_io */
118
- RubyIO io =
119
- (RubyIO) TypeConverter.convertToType(data,
120
- ruby.getIO(),
121
- "to_io");
122
- source = new InputSource(io.getInStream());
123
- } else if (((RubyObject)data).getInstanceVariable("@io") != null) {
124
- // in case of EncodingReader is used
125
- // since EncodingReader won't respond to :to_io
126
- RubyObject dataObject = (RubyObject) ((RubyObject)data).getInstanceVariable("@io");
127
- if (dataObject instanceof RubyIO) {
128
- RubyIO io = (RubyIO)dataObject;
129
- source = new InputSource(io.getInStream());
130
- } else if (dataObject instanceof RubyStringIO) {
131
- stringData = (RubyString)((RubyStringIO)dataObject).string();
132
- }
133
- } else {
134
- if (invoke(context, data, "respond_to?",
135
- ruby.newSymbol("string").to_sym()).isTrue()) {
136
- /* StringIO or other object that responds to :string */
137
- stringData = invoke(context, data, "string").convertToString();
138
- } else if (data instanceof RubyString) {
139
- stringData = (RubyString) data;
140
- } else {
141
- throw ruby.newArgumentError(
142
- "must be kind_of String or respond to :to_io or :string");
143
- }
144
- }
145
- if (stringData != null) {
146
- ByteList bytes = stringData.getByteList();
147
- source = new InputSource(new ByteArrayInputStream(bytes.unsafeBytes(), bytes.begin(), bytes.length()));
148
- }
149
- }
150
-
151
- /**
152
- * Set the InputSource to read from <code>file</code>, a String filename.
153
- */
154
- public void setInputSourceFile(ThreadContext context, IRubyObject file) {
155
- String filename = rubyStringToString(file);
156
-
157
- try{
158
- source = resolveEntity(context.getRuntime(),
159
- null, null, filename);
160
- } catch (Exception e) {
161
- throw RaiseException
162
- .createNativeRaiseException(context.getRuntime(), e);
163
- }
164
-
165
- }
166
-
167
- /**
168
- * Set the InputSource from <code>stream</code>.
169
- */
170
- public void setInputSource(InputStream stream) {
171
- source = new InputSource(stream);
172
- }
173
-
174
- /**
175
- * Wrap Nokogiri parser options in a utility class. This is
176
- * read-only.
177
- */
178
- public static class Options {
179
- protected static final long STRICT = 0;
180
- protected static final long RECOVER = 1;
181
- protected static final long NOENT = 2;
182
- protected static final long DTDLOAD = 4;
183
- protected static final long DTDATTR = 8;
184
- protected static final long DTDVALID = 16;
185
- protected static final long NOERROR = 32;
186
- protected static final long NOWARNING = 64;
187
- protected static final long PEDANTIC = 128;
188
- protected static final long NOBLANKS = 256;
189
- protected static final long SAX1 = 512;
190
- protected static final long XINCLUDE = 1024;
191
- protected static final long NONET = 2048;
192
- protected static final long NODICT = 4096;
193
- protected static final long NSCLEAN = 8192;
194
- protected static final long NOCDATA = 16384;
195
- protected static final long NOXINCNODE = 32768;
196
-
197
- public boolean strict;
198
- public boolean recover;
199
- public boolean noEnt;
200
- public boolean dtdLoad;
201
- public boolean dtdAttr;
202
- public boolean dtdValid;
203
- public boolean noError;
204
- public boolean noWarning;
205
- public boolean pedantic;
206
- public boolean noBlanks;
207
- public boolean sax1;
208
- public boolean xInclude;
209
- public boolean noNet;
210
- public boolean noDict;
211
- public boolean nsClean;
212
- public boolean noCdata;
213
- public boolean noXIncNode;
214
-
215
- protected static boolean test(long options, long mask) {
216
- return ((options & mask) == mask);
217
- }
218
-
219
- public Options(long options) {
220
- strict = ((options & RECOVER) == STRICT);
221
- recover = test(options, RECOVER);
222
- noEnt = test(options, NOENT);
223
- dtdLoad = test(options, DTDLOAD);
224
- dtdAttr = test(options, DTDATTR);
225
- dtdValid = test(options, DTDVALID);
226
- noError = test(options, NOERROR);
227
- noWarning = test(options, NOWARNING);
228
- pedantic = test(options, PEDANTIC);
229
- noBlanks = test(options, NOBLANKS);
230
- sax1 = test(options, SAX1);
231
- xInclude = test(options, XINCLUDE);
232
- noNet = test(options, NONET);
233
- noDict = test(options, NODICT);
234
- nsClean = test(options, NSCLEAN);
235
- noCdata = test(options, NOCDATA);
236
- noXIncNode = test(options, NOXINCNODE);
237
- }
238
- }
239
-
240
- /**
241
- * An entity resolver aware of the fact that the Ruby runtime can
242
- * change directory but the JVM cannot. Thus any file based
243
- * entity resolution that uses relative paths must be translated
244
- * to be relative to the current directory of the Ruby runtime.
245
- */
246
- public static class ChdirEntityResolver implements EntityResolver2 {
247
- protected Ruby runtime;
248
-
249
- public ChdirEntityResolver(Ruby runtime) {
250
- super();
251
- this.runtime = runtime;
252
- }
253
-
254
- @Override
255
- public InputSource getExternalSubset(String name, String baseURI)
256
- throws SAXException, IOException {
257
- return null;
258
- }
259
-
260
- @Override
261
- public InputSource resolveEntity(String publicId, String systemId)
262
- throws SAXException, IOException {
263
- return resolveEntity(null, publicId, null, systemId);
264
- }
265
-
266
- @Override
267
- public InputSource resolveEntity(String name,
268
- String publicId,
269
- String baseURI,
270
- String systemId)
271
- throws SAXException, IOException {
272
- return ParserContext
273
- .resolveEntity(runtime, publicId, baseURI, systemId);
274
- }
275
-
276
- }
277
-
278
- }