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,119 +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;
34
-
35
- import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
- import static nokogiri.internals.NokogiriHelpers.stringOrNil;
37
-
38
- import org.jruby.Ruby;
39
- import org.jruby.RubyClass;
40
- import org.jruby.RubyException;
41
- import org.jruby.anno.JRubyClass;
42
- import org.jruby.anno.JRubyMethod;
43
- import org.jruby.runtime.ThreadContext;
44
- import org.jruby.runtime.builtin.IRubyObject;
45
- import org.xml.sax.SAXParseException;
46
-
47
- /**
48
- * Class for Nokogiri::XML::SyntaxError
49
- *
50
- * @author sergio
51
- * @author Yoko Harada <yokolet@gmail.com>
52
- */
53
- @JRubyClass(name="Nokogiri::XML::SyntaxError", parent="Nokogiri::SyntaxError")
54
- public class XmlSyntaxError extends RubyException {
55
- private Exception exception;
56
-
57
- public XmlSyntaxError(Ruby runtime, RubyClass klazz) {
58
- super(runtime, klazz);
59
- }
60
-
61
- /**
62
- * Create and return a copy of this object.
63
- *
64
- * @return a clone of this object
65
- */
66
- @Override
67
- public Object clone() throws CloneNotSupportedException {
68
- return super.clone();
69
- }
70
-
71
- public XmlSyntaxError(Ruby runtime, RubyClass rubyClass, Exception ex) {
72
- super(runtime, rubyClass, ex.getMessage());
73
- this.exception = ex;
74
- }
75
-
76
- public static XmlSyntaxError createWarning(Ruby runtime, SAXParseException e) {
77
- XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::SyntaxError"));
78
- xmlSyntaxError.setException(runtime, e, 1);
79
- return xmlSyntaxError;
80
- }
81
-
82
- public static XmlSyntaxError createError(Ruby runtime, SAXParseException e) {
83
- XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::SyntaxError"));
84
- xmlSyntaxError.setException(runtime, e, 2);
85
- return xmlSyntaxError;
86
- }
87
-
88
- public static XmlSyntaxError createFatalError(Ruby runtime, SAXParseException e) {
89
- XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::SyntaxError"));
90
- xmlSyntaxError.setException(runtime, e, 3);
91
- return xmlSyntaxError;
92
- }
93
-
94
- public void setException(Exception exception) {
95
- this.exception = exception;
96
- }
97
-
98
- public void setException(Ruby runtime, Exception exception, int level) {
99
- this.exception = exception;
100
- setInstanceVariable("@level", runtime.newFixnum(level));
101
- setInstanceVariable("@line", runtime.newFixnum(((SAXParseException)exception).getLineNumber()));
102
- setInstanceVariable("@column", runtime.newFixnum(((SAXParseException)exception).getColumnNumber()));
103
- setInstanceVariable("@file", stringOrNil(runtime, ((SAXParseException)exception).getSystemId()));
104
- }
105
-
106
- public static RubyException createXPathSyntaxError(Ruby runtime, Exception e) {
107
- RubyClass klazz = (RubyClass)runtime.getClassFromPath("Nokogiri::XML::XPath::SyntaxError");
108
- return new XmlSyntaxError(runtime, klazz, e);
109
- }
110
-
111
- @Override
112
- @JRubyMethod(name = "to_s")
113
- public IRubyObject to_s(ThreadContext context) {
114
- if (exception != null && exception.getMessage() != null)
115
- return context.getRuntime().newString(exception.getMessage());
116
- else
117
- return super.to_s(context);
118
- }
119
- }
@@ -1,136 +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;
34
-
35
- import static nokogiri.internals.NokogiriHelpers.isXmlEscaped;
36
- import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
37
- import static nokogiri.internals.NokogiriHelpers.stringOrNil;
38
- import nokogiri.internals.NokogiriHelpers;
39
- import nokogiri.internals.SaveContext;
40
-
41
- import org.jruby.Ruby;
42
- import org.jruby.RubyClass;
43
- import org.jruby.anno.JRubyClass;
44
- import org.jruby.anno.JRubyMethod;
45
- import org.jruby.runtime.ThreadContext;
46
- import org.jruby.runtime.builtin.IRubyObject;
47
- import org.w3c.dom.Document;
48
- import org.w3c.dom.Node;
49
-
50
- /**
51
- * Class for Nokogiri::XML::Text
52
- *
53
- * @author sergio
54
- * @author Yoko Harada <yokolet@gmail.com>
55
- */
56
- @JRubyClass(name="Nokogiri::XML::Text", parent="Nokogiri::XML::CharacterData")
57
- public class XmlText extends XmlNode {
58
-
59
- public XmlText(Ruby runtime, RubyClass rubyClass, Node node) {
60
- super(runtime, rubyClass, node);
61
- }
62
-
63
- public XmlText(Ruby runtime, RubyClass klass) {
64
- super(runtime, klass);
65
- }
66
-
67
- @Override
68
- protected void init(ThreadContext context, IRubyObject[] args) {
69
- if (args.length < 2) {
70
- throw getRuntime().newArgumentError(args.length, 2);
71
- }
72
-
73
- content = args[0];
74
- IRubyObject xNode = args[1];
75
-
76
- XmlNode xmlNode = asXmlNode(context, xNode);
77
- XmlDocument xmlDoc = (XmlDocument)xmlNode.document(context);
78
- doc = xmlDoc;
79
- Document document = xmlDoc.getDocument();
80
- // text node content should not be encoded when it is created by Text node.
81
- // while content should be encoded when it is created by Element node.
82
- Node node = document.createTextNode(rubyStringToString(content));
83
- setNode(context, node);
84
- }
85
-
86
- @Override
87
- protected IRubyObject getNodeName(ThreadContext context) {
88
- if (name == null) name = context.getRuntime().newString("text");
89
- return name;
90
- }
91
-
92
- @Override
93
- @JRubyMethod(name = {"content", "text", "inner_text"})
94
- public IRubyObject content(ThreadContext context) {
95
- if (content == null || content.isNil()) {
96
- return stringOrNil(context.getRuntime(), node.getTextContent());
97
- } else {
98
- return content;
99
- }
100
- }
101
-
102
- @Override
103
- public void saveContent(ThreadContext context, SaveContext ctx) {
104
- String textContent = node.getTextContent();
105
-
106
- if (!isXmlEscaped(textContent)) {
107
- textContent = NokogiriHelpers.encodeJavaString(textContent);
108
- }
109
- if (getEncoding(context, ctx) == null) {
110
- textContent = encodeStringToHtmlEntity(textContent);
111
- }
112
- ctx.append(textContent);
113
- }
114
-
115
- private String getEncoding(ThreadContext context, SaveContext ctx) {
116
- String encoding = ctx.getEncoding();
117
- if (encoding != null) return encoding;
118
- XmlDocument xmlDocument = (XmlDocument)document(context);
119
- IRubyObject ruby_encoding = xmlDocument.encoding(context);
120
- if (!ruby_encoding.isNil()) {
121
- encoding = rubyStringToString(ruby_encoding);
122
- }
123
- return encoding;
124
- }
125
-
126
- private String encodeStringToHtmlEntity(String text) {
127
- int last = 126; // = U+007E. No need to encode under U+007E.
128
- StringBuffer sb = new StringBuffer();
129
- for (int i=0; i<text.length(); i++) {
130
- int codePoint = text.codePointAt(i);
131
- if (codePoint > last) sb.append("&#x" + Integer.toHexString(codePoint) + ";");
132
- else sb.append(text.charAt(i));
133
- }
134
- return new String(sb);
135
- }
136
- }
@@ -1,179 +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;
34
-
35
- import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
-
37
- import java.util.Set;
38
- import java.util.regex.Matcher;
39
- import java.util.regex.Pattern;
40
-
41
- import javax.xml.xpath.XPath;
42
- import javax.xml.xpath.XPathConstants;
43
- import javax.xml.xpath.XPathExpression;
44
- import javax.xml.xpath.XPathExpressionException;
45
- import javax.xml.xpath.XPathFactory;
46
-
47
- import nokogiri.internals.NokogiriNamespaceContext;
48
- import nokogiri.internals.NokogiriXPathFunctionResolver;
49
- import nokogiri.internals.NokogiriXPathVariableResolver;
50
-
51
- import org.jruby.Ruby;
52
- import org.jruby.RubyBoolean;
53
- import org.jruby.RubyClass;
54
- import org.jruby.RubyException;
55
- import org.jruby.RubyNumeric;
56
- import org.jruby.RubyObject;
57
- import org.jruby.RubyString;
58
- import org.jruby.anno.JRubyClass;
59
- import org.jruby.anno.JRubyMethod;
60
- import org.jruby.exceptions.RaiseException;
61
- import org.jruby.runtime.ThreadContext;
62
- import org.jruby.runtime.builtin.IRubyObject;
63
- import org.w3c.dom.NodeList;
64
-
65
- /**
66
- * Class for Nokogiri::XML::XpathContext
67
- *
68
- * @author sergio
69
- * @author Yoko Harada <yokolet@gmail.com>
70
- */
71
- @JRubyClass(name="Nokogiri::XML::XPathContext")
72
- public class XmlXpathContext extends RubyObject {
73
- private XmlNode context;
74
- private XPath xpath;
75
-
76
- public XmlXpathContext(Ruby ruby, RubyClass rubyClass, XmlNode context) {
77
- super(ruby, rubyClass);
78
- this.context = context;
79
- this.xpath = XPathFactory.newInstance().newXPath();
80
- this.xpath.setNamespaceContext(new NokogiriNamespaceContext());
81
- this.xpath.setXPathVariableResolver(new NokogiriXPathVariableResolver());
82
- }
83
-
84
- @JRubyMethod(name = "new", meta = true)
85
- public static IRubyObject rbNew(ThreadContext context, IRubyObject cls, IRubyObject node) {
86
- XmlNode xmlNode = (XmlNode)node;
87
- return new XmlXpathContext(context.getRuntime(), (RubyClass)cls, xmlNode);
88
- }
89
-
90
- @JRubyMethod
91
- public IRubyObject evaluate(ThreadContext context, IRubyObject expr, IRubyObject handler) {
92
- String src = expr.convertToString().asJavaString();
93
- try {
94
- if(!handler.isNil()) {
95
- if (!isContainsPrefix(src)) {
96
- Set<String> methodNames = handler.getMetaClass().getMethods().keySet();
97
- for (String name : methodNames) {
98
- src = src.replaceAll(name, NokogiriNamespaceContext.NOKOGIRI_PREFIX+":"+name);
99
- }
100
- }
101
- xpath.setXPathFunctionResolver(new NokogiriXPathFunctionResolver(handler));
102
- }
103
- XPathExpression xpathExpression = xpath.compile(src);
104
- return node_set(context, xpathExpression);
105
- } catch (XPathExpressionException xpee) {
106
- xpee = new XPathExpressionException(src);
107
- RubyException e =
108
- XmlSyntaxError.createXPathSyntaxError(getRuntime(), xpee);
109
- throw new RaiseException(e);
110
- }
111
- }
112
-
113
- protected IRubyObject node_set(ThreadContext rbctx, XPathExpression xpathExpression) {
114
- XmlNodeSet result = null;
115
- try {
116
- result = tryGetNodeSet(xpathExpression);
117
- result.setDocument(context.document(rbctx));
118
- return result;
119
- } catch (XPathExpressionException xpee) {
120
- try {
121
- return tryGetOpaqueValue(xpathExpression);
122
- } catch (XPathExpressionException xpee_opaque) {
123
- RubyException e = XmlSyntaxError.createXPathSyntaxError(getRuntime(), xpee_opaque);
124
- throw new RaiseException(e);
125
- }
126
- }
127
- }
128
-
129
- private XmlNodeSet tryGetNodeSet(XPathExpression xpathExpression) throws XPathExpressionException {
130
- NodeList nodes = (NodeList)xpathExpression.evaluate(context.node, XPathConstants.NODESET);
131
- XmlNodeSet xmlNodeSet = (XmlNodeSet) NokogiriService.XML_NODESET_ALLOCATOR.allocate(getRuntime(), getNokogiriClass(getRuntime(), "Nokogiri::XML::NodeSet"));
132
- xmlNodeSet.setNodeList(nodes);
133
- return xmlNodeSet;
134
- }
135
-
136
- private static Pattern number_pattern = Pattern.compile("\\d.*");
137
- private static Pattern boolean_pattern = Pattern.compile("true|false");
138
-
139
- private IRubyObject tryGetOpaqueValue(XPathExpression xpathExpression) throws XPathExpressionException {
140
- String string = (String)xpathExpression.evaluate(context.node, XPathConstants.STRING);
141
- if (doesMatch(number_pattern, string)) return RubyNumeric.dbl2num(getRuntime(), Double.parseDouble(string));
142
- if (doesMatch(boolean_pattern, string)) return RubyBoolean.newBoolean(getRuntime(), Boolean.parseBoolean(string));
143
- return RubyString.newString(getRuntime(), string);
144
- }
145
-
146
- private boolean doesMatch(Pattern pattern, String string) {
147
- Matcher m = pattern.matcher(string);
148
- return m.matches();
149
- }
150
-
151
- private boolean isContainsPrefix(String str) {
152
- Set<String> prefixes = ((NokogiriNamespaceContext)xpath.getNamespaceContext()).getAllPrefixes();
153
- for (String prefix : prefixes) {
154
- if (str.contains(prefix + ":")) {
155
- return true;
156
- }
157
- }
158
- return false;
159
- }
160
-
161
-
162
- @JRubyMethod
163
- public IRubyObject evaluate(ThreadContext context, IRubyObject expr) {
164
- return this.evaluate(context, expr, context.getRuntime().getNil());
165
- }
166
-
167
- @JRubyMethod
168
- public IRubyObject register_ns(ThreadContext context, IRubyObject prefix, IRubyObject uri) {
169
- ((NokogiriNamespaceContext) this.xpath.getNamespaceContext()).registerNamespace(prefix.convertToString().asJavaString(), uri.convertToString().asJavaString());
170
- return this;
171
- }
172
-
173
- @JRubyMethod
174
- public IRubyObject register_variable(ThreadContext context, IRubyObject name, IRubyObject value) {
175
- ((NokogiriXPathVariableResolver) this.xpath.getXPathVariableResolver()).
176
- registerVariable(name.convertToString().asJavaString(), value.convertToString().asJavaString());
177
- return this;
178
- }
179
- }
@@ -1,183 +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;
34
-
35
- import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
-
37
- import java.util.HashMap;
38
- import java.util.Map;
39
- import java.util.regex.Matcher;
40
- import java.util.regex.Pattern;
41
-
42
- import javax.xml.transform.Templates;
43
- import javax.xml.transform.Transformer;
44
- import javax.xml.transform.TransformerConfigurationException;
45
- import javax.xml.transform.TransformerException;
46
- import javax.xml.transform.TransformerFactory;
47
- import javax.xml.transform.dom.DOMResult;
48
- import javax.xml.transform.dom.DOMSource;
49
-
50
- import org.jruby.Ruby;
51
- import org.jruby.RubyArray;
52
- import org.jruby.RubyClass;
53
- import org.jruby.RubyObject;
54
- import org.jruby.anno.JRubyClass;
55
- import org.jruby.anno.JRubyMethod;
56
- import org.jruby.javasupport.util.RuntimeHelpers;
57
- import org.jruby.runtime.ThreadContext;
58
- import org.jruby.runtime.builtin.IRubyObject;
59
- import org.w3c.dom.Document;
60
-
61
- /**
62
- * Class for Nokogiri::XSLT::Stylesheet
63
- *
64
- * @author sergio
65
- * @author Yoko Harada <yokolet@gmail.com>
66
- */
67
- @JRubyClass(name="Nokogiri::XSLT::Stylesheet")
68
- public class XsltStylesheet extends RubyObject {
69
- private static Map<String, Object> registry = new HashMap<String, Object>();
70
- private static TransformerFactory factory = null;
71
- private Templates sheet;
72
-
73
- public static Map<String, Object> getRegistry() {
74
- return registry;
75
- }
76
-
77
- public XsltStylesheet(Ruby ruby, RubyClass rubyClass) {
78
- super(ruby, rubyClass);
79
- }
80
-
81
- private void addParametersToTransformer(ThreadContext context, Transformer transf, IRubyObject parameters) {
82
- Ruby ruby = context.getRuntime();
83
- RubyArray params = parameters.convertToArray();
84
- int limit = params.getLength();
85
- if(limit % 2 == 1) limit--;
86
-
87
- for(int i = 0; i < limit; i+=2) {
88
- String name = params.aref(ruby.newFixnum(i)).asJavaString();
89
- String value = params.aref(ruby.newFixnum(i+1)).asJavaString();
90
- transf.setParameter(name, unparseValue(value));
91
- }
92
- }
93
-
94
- private Pattern p = Pattern.compile("'.{1,}'");
95
-
96
- private String unparseValue(String orig) {
97
- Matcher m = p.matcher(orig);
98
- if ((orig.startsWith("\"") && orig.endsWith("\"")) || m.matches()) {
99
- orig = orig.substring(1, orig.length()-1);
100
- }
101
-
102
- return orig;
103
- }
104
-
105
- @JRubyMethod(meta = true)
106
- public static IRubyObject parse_stylesheet_doc(ThreadContext context, IRubyObject cls, IRubyObject document) {
107
-
108
- Ruby ruby = context.getRuntime();
109
-
110
- if(!(document instanceof XmlDocument)) {
111
- throw ruby.newArgumentError("doc must be a Nokogiri::XML::Document instance");
112
- }
113
-
114
- XmlDocument xmlDoc = (XmlDocument) document;
115
-
116
- RubyArray errors = (RubyArray) xmlDoc.getInstanceVariable("@errors");
117
-
118
- if(!errors.isEmpty()) {
119
- throw ruby.newRuntimeError(errors.first().asJavaString());
120
- }
121
-
122
- Document doc = ((XmlDocument) xmlDoc.dup_implementation(context, true)).getDocument();
123
-
124
- XsltStylesheet xslt = new XsltStylesheet(ruby, (RubyClass) cls);
125
-
126
- try {
127
- if (factory == null) factory = TransformerFactory.newInstance();
128
- xslt.sheet = factory.newTemplates(new DOMSource(doc));
129
- } catch (TransformerConfigurationException ex) {
130
- ruby.newRuntimeError("could not parse xslt stylesheet");
131
- }
132
-
133
- return xslt;
134
- }
135
-
136
- @JRubyMethod
137
- public IRubyObject serialize(ThreadContext context, IRubyObject doc) {
138
- return RuntimeHelpers.invoke(context,
139
- RuntimeHelpers.invoke(context, doc, "root"),
140
- "to_s");
141
- }
142
-
143
- @JRubyMethod(rest = true, required=1, optional=2)
144
- public IRubyObject transform(ThreadContext context, IRubyObject[] args) {
145
- Ruby runtime = context.getRuntime();
146
-
147
- DOMSource docSource = new DOMSource(((XmlDocument) args[0]).getDocument());
148
- DOMResult result = new DOMResult();
149
-
150
- try{
151
- Transformer transf = this.sheet.newTransformer();
152
- if(args.length > 1) {
153
- addParametersToTransformer(context, transf, args[1]);
154
- }
155
- transf.transform(docSource, result);
156
- } catch(TransformerConfigurationException ex) {
157
- throw runtime.newRuntimeError("Could not transform the document.");
158
- } catch(TransformerException ex) {
159
- throw runtime.newRuntimeError("Could not transform the document.");
160
- }
161
-
162
- if ("html".equals(result.getNode().getFirstChild().getNodeName())) {
163
- HtmlDocument htmlDocument = (HtmlDocument) getNokogiriClass(runtime, "Nokogiri::HTML::Document").allocate();
164
- htmlDocument.setNode(context, (Document) result.getNode());
165
- return htmlDocument;
166
- } else {
167
- XmlDocument xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Document"));
168
- xmlDocument.setNode(context, (Document) result.getNode());
169
- return xmlDocument;
170
- }
171
- }
172
-
173
- @JRubyMethod(name = {"registr", "register"}, meta = true)
174
- public static IRubyObject register(ThreadContext context, IRubyObject cls, IRubyObject uri, IRubyObject receiver) {
175
- throw context.getRuntime().newNotImplementedError("Nokogiri::XSLT.register method is not implemented");
176
- /* When API conflict is solved, this method should be below:
177
- // ThreadContext is used while executing xslt extension function
178
- registry.put("context", context);
179
- registry.put("receiver", receiver);
180
- return context.getRuntime().getNil();
181
- */
182
- }
183
- }