nokogiri 1.5.0.beta.2 → 1.5.0.beta.3

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/CHANGELOG.ja.rdoc +63 -0
  2. data/CHANGELOG.rdoc +44 -1
  3. data/Manifest.txt +3 -3
  4. data/README.ja.rdoc +4 -4
  5. data/README.rdoc +4 -4
  6. data/Rakefile +3 -0
  7. data/bin/nokogiri +6 -1
  8. data/ext/java/nokogiri/EncodingHandler.java +32 -0
  9. data/ext/java/nokogiri/HtmlDocument.java +36 -0
  10. data/ext/java/nokogiri/HtmlElementDescription.java +34 -0
  11. data/ext/java/nokogiri/HtmlEntityLookup.java +34 -0
  12. data/ext/java/nokogiri/HtmlSaxParserContext.java +41 -3
  13. data/ext/java/nokogiri/NokogiriService.java +109 -13
  14. data/ext/java/nokogiri/XmlAttr.java +40 -4
  15. data/ext/java/nokogiri/XmlAttributeDecl.java +32 -0
  16. data/ext/java/nokogiri/XmlCdata.java +41 -2
  17. data/ext/java/nokogiri/XmlComment.java +38 -1
  18. data/ext/java/nokogiri/XmlDocument.java +56 -11
  19. data/ext/java/nokogiri/XmlDocumentFragment.java +39 -30
  20. data/ext/java/nokogiri/XmlDtd.java +37 -0
  21. data/ext/java/nokogiri/XmlElement.java +51 -2
  22. data/ext/java/nokogiri/XmlElementContent.java +32 -0
  23. data/ext/java/nokogiri/XmlElementDecl.java +32 -0
  24. data/ext/java/nokogiri/XmlEntityDecl.java +32 -0
  25. data/ext/java/nokogiri/XmlEntityReference.java +35 -2
  26. data/ext/java/nokogiri/XmlNamespace.java +55 -5
  27. data/ext/java/nokogiri/XmlNode.java +129 -136
  28. data/ext/java/nokogiri/XmlNodeSet.java +36 -0
  29. data/ext/java/nokogiri/XmlProcessingInstruction.java +34 -1
  30. data/ext/java/nokogiri/XmlReader.java +36 -0
  31. data/ext/java/nokogiri/XmlRelaxng.java +34 -1
  32. data/ext/java/nokogiri/XmlSaxParserContext.java +52 -7
  33. data/ext/java/nokogiri/XmlSaxPushParser.java +36 -0
  34. data/ext/java/nokogiri/XmlSchema.java +34 -1
  35. data/ext/java/nokogiri/XmlSyntaxError.java +48 -18
  36. data/ext/java/nokogiri/XmlText.java +45 -6
  37. data/ext/java/nokogiri/XmlXpathContext.java +45 -0
  38. data/ext/java/nokogiri/XsltStylesheet.java +58 -3
  39. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +50 -26
  40. data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +35 -1
  41. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +51 -13
  42. data/ext/java/nokogiri/internals/NokogiriHandler.java +70 -21
  43. data/ext/java/nokogiri/internals/NokogiriHelpers.java +95 -38
  44. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +37 -3
  45. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +39 -1
  46. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +43 -7
  47. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +77 -10
  48. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +49 -20
  49. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +34 -2
  50. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -1
  51. data/ext/java/nokogiri/internals/ParserContext.java +32 -0
  52. data/ext/java/nokogiri/internals/PushInputStream.java +33 -3
  53. data/ext/java/nokogiri/internals/ReaderNode.java +50 -8
  54. data/ext/java/nokogiri/internals/SaveContext.java +35 -2
  55. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +34 -1
  56. data/ext/java/nokogiri/internals/XmlDeclHandler.java +32 -0
  57. data/ext/java/nokogiri/internals/XmlDomParser.java +32 -0
  58. data/ext/java/nokogiri/internals/XmlDomParserContext.java +43 -11
  59. data/ext/java/nokogiri/internals/XmlSaxParser.java +32 -0
  60. data/ext/java/nokogiri/internals/XsltExtensionFunction.java +72 -0
  61. data/ext/nokogiri/depend +358 -32
  62. data/ext/nokogiri/extconf.rb +1 -3
  63. data/ext/nokogiri/nokogiri.c +2 -0
  64. data/ext/nokogiri/nokogiri.h +7 -0
  65. data/ext/nokogiri/xml_dtd.c +2 -2
  66. data/ext/nokogiri/xml_io.c +2 -2
  67. data/ext/nokogiri/xml_node.c +31 -6
  68. data/ext/nokogiri/xml_node_set.c +1 -1
  69. data/ext/nokogiri/xml_sax_parser.c +1 -1
  70. data/ext/nokogiri/xml_sax_parser_context.c +40 -0
  71. data/ext/nokogiri/xml_xpath_context.c +33 -2
  72. data/ext/nokogiri/xslt_stylesheet.c +122 -6
  73. data/lib/nokogiri.rb +12 -5
  74. data/lib/nokogiri/css/generated_tokenizer.rb +1 -2
  75. data/lib/nokogiri/css/xpath_visitor.rb +15 -7
  76. data/lib/nokogiri/decorators/slop.rb +5 -3
  77. data/lib/nokogiri/html/document.rb +3 -3
  78. data/lib/nokogiri/html/document_fragment.rb +19 -17
  79. data/lib/nokogiri/version.rb +1 -1
  80. data/lib/nokogiri/xml/document.rb +26 -1
  81. data/lib/nokogiri/xml/document_fragment.rb +2 -2
  82. data/lib/nokogiri/xml/dtd.rb +11 -0
  83. data/lib/nokogiri/xml/node.rb +156 -45
  84. data/lib/nokogiri/xml/node_set.rb +2 -2
  85. data/lib/nokogiri/xml/reader.rb +36 -0
  86. data/lib/nokogiri/xml/sax/document.rb +4 -2
  87. data/lib/nokogiri/xslt.rb +9 -5
  88. data/lib/nokogiri/xslt/stylesheet.rb +1 -1
  89. data/tasks/cross_compile.rb +27 -8
  90. data/test/css/test_parser.rb +29 -18
  91. data/test/decorators/test_slop.rb +16 -0
  92. data/test/html/test_document_fragment.rb +46 -3
  93. data/test/html/test_node.rb +9 -0
  94. data/test/xml/sax/test_parser.rb +11 -3
  95. data/test/xml/sax/test_parser_context.rb +50 -0
  96. data/test/xml/sax/test_push_parser.rb +18 -1
  97. data/test/xml/test_document_fragment.rb +15 -8
  98. data/test/xml/test_dtd.rb +15 -0
  99. data/test/xml/test_node.rb +31 -2
  100. data/test/xml/test_node_reparenting.rb +59 -31
  101. data/test/xml/test_node_set.rb +13 -0
  102. data/test/xml/test_xpath.rb +32 -0
  103. data/test/xslt/test_custom_functions.rb +94 -0
  104. metadata +83 -81
  105. data/lib/nokogiri/nokogiri.jar +0 -0
  106. data/spec/helper.rb +0 -3
  107. data/spec/xml/reader_spec.rb +0 -307
@@ -1,15 +1,47 @@
1
+ /**
2
+ * (The MIT License)
3
+ *
4
+ * Copyright (c) 2008 - 2010:
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
+
1
33
  package nokogiri.internals;
2
34
 
3
- import java.util.Arrays;
4
- import java.util.LinkedList;
5
35
  import static nokogiri.internals.NokogiriHelpers.getLocalPart;
6
36
  import static nokogiri.internals.NokogiriHelpers.getPrefix;
7
37
  import static nokogiri.internals.NokogiriHelpers.isNamespace;
8
38
  import static nokogiri.internals.NokogiriHelpers.stringOrNil;
9
39
 
10
- import java.util.logging.Logger;
40
+ import java.util.ArrayDeque;
41
+ import java.util.LinkedList;
11
42
 
12
43
  import nokogiri.XmlSyntaxError;
44
+
13
45
  import org.jruby.Ruby;
14
46
  import org.jruby.RubyArray;
15
47
  import org.jruby.RubyClass;
@@ -18,25 +50,23 @@ import org.jruby.javasupport.util.RuntimeHelpers;
18
50
  import org.jruby.runtime.ThreadContext;
19
51
  import org.jruby.runtime.builtin.IRubyObject;
20
52
  import org.xml.sax.Attributes;
53
+ import org.xml.sax.Locator;
21
54
  import org.xml.sax.SAXException;
22
55
  import org.xml.sax.SAXParseException;
23
56
  import org.xml.sax.ext.DefaultHandler2;
24
57
 
25
58
  /**
26
- *
59
+ * A handler for SAX parsing.
60
+ *
27
61
  * @author sergio
28
62
  */
29
- public class NokogiriHandler extends DefaultHandler2
30
- implements XmlDeclHandler {
31
-
32
- private static Logger LOGGER = Logger.getLogger(NokogiriHandler.class.getName());
63
+ public class NokogiriHandler extends DefaultHandler2 implements XmlDeclHandler {
33
64
 
34
65
  boolean inCDATA = false;
35
66
 
36
67
  private Ruby ruby;
37
68
  private RubyClass attrClass;
38
69
  private IRubyObject object;
39
- private boolean namespaceDefined = false;
40
70
 
41
71
  /**
42
72
  * Stores parse errors with the most-recent error last.
@@ -44,14 +74,22 @@ public class NokogiriHandler extends DefaultHandler2
44
74
  * TODO: should these be stored in the document 'errors' array?
45
75
  * Currently only string messages are stored there.
46
76
  */
47
- private LinkedList<XmlSyntaxError> errors =
48
- new LinkedList<XmlSyntaxError>();
77
+ private LinkedList<XmlSyntaxError> errors = new LinkedList<XmlSyntaxError>();
78
+
79
+ private Locator locator;
80
+ private ArrayDeque<Integer> lines;
81
+ private ArrayDeque<Integer> columns;
49
82
 
50
83
  public NokogiriHandler(Ruby ruby, IRubyObject object) {
51
84
  this.ruby = ruby;
52
- this.attrClass = (RubyClass) ruby.getClassFromPath(
53
- "Nokogiri::XML::SAX::Parser::Attribute");
85
+ this.attrClass = (RubyClass) ruby.getClassFromPath("Nokogiri::XML::SAX::Parser::Attribute");
54
86
  this.object = object;
87
+ lines = new ArrayDeque<Integer>();
88
+ columns = new ArrayDeque<Integer>();
89
+ }
90
+
91
+ public void setDocumentLocator(Locator locator) {
92
+ this.locator = locator;
55
93
  }
56
94
 
57
95
  @Override
@@ -80,8 +118,7 @@ public class NokogiriHandler extends DefaultHandler2
80
118
  * passed with the other attributes.
81
119
  */
82
120
  @Override
83
- public void startElement(String uri, String localName, String qName,
84
- Attributes attrs) throws SAXException {
121
+ public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
85
122
  // for attributes other than namespace attrs
86
123
  RubyArray rubyAttr = RubyArray.newArray(ruby);
87
124
  // for namespace defining attributes
@@ -90,6 +127,9 @@ public class NokogiriHandler extends DefaultHandler2
90
127
  ThreadContext context = ruby.getCurrentContext();
91
128
  boolean fromFragmentHandler = false; // isFromFragmentHandler();
92
129
 
130
+ lines.add(locator.getLineNumber());
131
+ columns.add(locator.getColumnNumber() - 1); // libxml counts from 0 while java does from 1
132
+
93
133
  for (int i = 0; i < attrs.getLength(); i++) {
94
134
  String u = attrs.getURI(i);
95
135
  String qn = attrs.getQName(i);
@@ -116,14 +156,12 @@ public class NokogiriHandler extends DefaultHandler2
116
156
  args[2] = stringOrNil(ruby, u);
117
157
  args[3] = stringOrNil(ruby, val);
118
158
 
119
- IRubyObject attr =
120
- RuntimeHelpers.invoke(context, attrClass, "new", args);
159
+ IRubyObject attr = RuntimeHelpers.invoke(context, attrClass, "new", args);
121
160
  rubyAttr.add(attr);
122
161
  }
123
162
  }
124
163
 
125
- if (localName == null || localName.equals(""))
126
- localName = getLocalPart(qName);
164
+ if (localName == null || localName.equals("")) localName = getLocalPart(qName);
127
165
  call("start_element_namespace",
128
166
  stringOrNil(ruby, localName),
129
167
  rubyAttr,
@@ -132,6 +170,14 @@ public class NokogiriHandler extends DefaultHandler2
132
170
  rubyNSAttr);
133
171
  }
134
172
 
173
+ public Integer getLine() {
174
+ return lines.pop();
175
+ }
176
+
177
+ public Integer getColumn() {
178
+ return columns.pop();
179
+ }
180
+
135
181
  private boolean isFromFragmentHandler() {
136
182
  if (object != null && object instanceof RubyObject) {
137
183
  RubyObject rubyObj = (RubyObject)object;
@@ -244,8 +290,11 @@ public class NokogiriHandler extends DefaultHandler2
244
290
  RuntimeHelpers.invoke(context, document(context), methodName, args);
245
291
  }
246
292
 
247
- private IRubyObject document(ThreadContext context){
248
- return RuntimeHelpers.invoke(context, this.object, "document");
293
+ private IRubyObject document(ThreadContext context) {
294
+ if (object instanceof RubyObject) {
295
+ return ((RubyObject)object).fastGetInstanceVariable("@document");
296
+ }
297
+ return context.getRuntime().getNil();
249
298
  }
250
299
 
251
300
  }
@@ -1,5 +1,38 @@
1
+ /**
2
+ * (The MIT License)
3
+ *
4
+ * Copyright (c) 2008 - 2010:
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
+
1
33
  package nokogiri.internals;
2
34
 
35
+ import java.io.UnsupportedEncodingException;
3
36
  import java.nio.ByteBuffer;
4
37
  import java.nio.charset.Charset;
5
38
 
@@ -19,17 +52,16 @@ import org.jruby.RubyClass;
19
52
  import org.jruby.RubyEncoding;
20
53
  import org.jruby.RubyHash;
21
54
  import org.jruby.RubyString;
22
- import org.jruby.javasupport.JavaUtil;
23
55
  import org.jruby.runtime.builtin.IRubyObject;
24
56
  import org.jruby.util.ByteList;
25
57
  import org.w3c.dom.Attr;
26
- import org.w3c.dom.Document;
27
58
  import org.w3c.dom.NamedNodeMap;
28
59
  import org.w3c.dom.Node;
29
60
  import org.w3c.dom.NodeList;
30
61
 
31
62
  /**
32
- *
63
+ * A class for various utility methods.
64
+ *
33
65
  * @author serabe
34
66
  */
35
67
  public class NokogiriHelpers {
@@ -50,10 +82,11 @@ public class NokogiriHelpers {
50
82
  if(node == null) return ruby.getNil();
51
83
  if (node.getNodeType() == Node.ATTRIBUTE_NODE && isNamespace(node.getNodeName())) {
52
84
  XmlDocument xmlDocument = (XmlDocument)node.getOwnerDocument().getUserData(CACHED_NODE);
53
- XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(node.getNodeName(), node.getNodeName());
85
+ String prefix = getLocalNameForNamespace(((Attr)node).getName());
86
+ prefix = prefix != null ? prefix : "";
87
+ String href = ((Attr)node).getValue();
88
+ XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(prefix, href);
54
89
  if (xmlNamespace == null) {
55
- String prefix = getLocalNameForNamespace(((Attr)node).getName());
56
- prefix = prefix != null ? prefix : "";
57
90
  return xmlDocument.getNamespaceCache().put(ruby, prefix, ((Attr)node).getValue(), node, xmlDocument);
58
91
  }
59
92
  }
@@ -75,51 +108,61 @@ public class NokogiriHelpers {
75
108
  // this is slow; need a way to cache nokogiri classes/modules somewhere
76
109
  switch (node.getNodeType()) {
77
110
  case Node.ELEMENT_NODE:
78
- return new XmlElement(ruby, getNokogiriClass(ruby, "Nokogiri::XML::Element"), node);
111
+ XmlElement xmlElement = (XmlElement) getNokogiriClass(ruby, "Nokogiri::XML::Element").allocate();
112
+ xmlElement.setNode(ruby.getCurrentContext(), node);
113
+ return xmlElement;
79
114
  case Node.ATTRIBUTE_NODE:
80
- return new XmlAttr(ruby, getNokogiriClass(ruby, "Nokogiri::XML::Attr"), node);
115
+ XmlAttr xmlAttr = (XmlAttr) getNokogiriClass(ruby, "Nokogiri::XML::Attr").allocate();
116
+ xmlAttr.setNode(ruby.getCurrentContext(), node);
117
+ return xmlAttr;
81
118
  case Node.TEXT_NODE:
82
- return new XmlText(ruby, getNokogiriClass(ruby, "Nokogiri::XML::Text"), node);
119
+ XmlText xmlText = (XmlText) getNokogiriClass(ruby, "Nokogiri::XML::Text").allocate();
120
+ xmlText.setNode(ruby.getCurrentContext(), node);
121
+ return xmlText;
83
122
  case Node.COMMENT_NODE:
84
- return new XmlComment(ruby, getNokogiriClass(ruby, "Nokogiri::XML::Comment"), node);
123
+ XmlComment xmlComment = (XmlComment) getNokogiriClass(ruby, "Nokogiri::XML::Comment").allocate();
124
+ xmlComment.setNode(ruby.getCurrentContext(), node);
125
+ return xmlComment;
85
126
  case Node.ENTITY_NODE:
86
127
  return new XmlNode(ruby, getNokogiriClass(ruby, "Nokogiri::XML::EntityDecl"), node);
87
128
  case Node.CDATA_SECTION_NODE:
88
- return new XmlCdata(ruby, getNokogiriClass(ruby, "Nokogiri::XML::CDATA"), node);
129
+ XmlCdata xmlCdata = (XmlCdata) getNokogiriClass(ruby, "Nokogiri::XML::CDATA").allocate();
130
+ xmlCdata.setNode(ruby.getCurrentContext(), node);
131
+ return xmlCdata;
89
132
  case Node.DOCUMENT_NODE:
90
- return new XmlDocument(ruby, getNokogiriClass(ruby, "Nokogiri::XML::Document"), (Document) node);
133
+ XmlDocument xmlDocument = (XmlDocument) getNokogiriClass(ruby, "Nokogiri::XML::Document").allocate();
134
+ xmlDocument.setNode(ruby.getCurrentContext(), node);
135
+ return xmlDocument;
91
136
  default:
92
- return new XmlNode(ruby, getNokogiriClass(ruby, "Nokogiri::XML::Node"), node);
137
+ XmlNode xmlNode = (XmlNode) getNokogiriClass(ruby, "Nokogiri::XML::Node").allocate();
138
+ xmlNode.setNode(ruby.getCurrentContext(), node);
139
+ return xmlNode;
93
140
  }
94
141
  }
95
142
 
96
143
  public static RubyClass getNokogiriClass(Ruby ruby, String name) {
97
144
  RubyHash classCache = (RubyHash) ruby.getGlobalVariables().get(NokogiriService.nokogiriClassCacheGvarName);
98
- IRubyObject rubyName = JavaUtil.convertJavaToUsableRubyObject(ruby, name);
145
+ IRubyObject rubyName = RubyString.newString(ruby, name);
99
146
  return (RubyClass)classCache.fastARef(rubyName);
100
147
  }
101
148
 
102
- public static IRubyObject stringOrNil(Ruby ruby, String s) {
103
- if (s == null)
104
- return ruby.getNil();
105
-
106
- return JavaUtil.convertJavaToUsableRubyObject(ruby, s);
149
+ public static IRubyObject stringOrNil(Ruby runtime, String s) {
150
+ if (s == null) return runtime.getNil();
151
+ return RubyString.newString(runtime, s);
107
152
  }
108
153
 
109
- public static IRubyObject stringOrBlank(Ruby ruby, String s) {
110
- if (s == null) return ruby.newString();
111
- return ruby.newString(s);
154
+ public static IRubyObject stringOrBlank(Ruby runtime, String s) {
155
+ if (s == null) return runtime.newString();
156
+ return RubyString.newString(runtime, s);
112
157
  }
113
158
 
114
159
  /**
115
160
  * Convert <code>s</code> to a RubyString, or if s is null or
116
161
  * empty return RubyNil.
117
162
  */
118
- public static IRubyObject nonEmptyStringOrNil(Ruby ruby, String s) {
119
- if (s == null || s.length() == 0)
120
- return ruby.getNil();
121
-
122
- return ruby.newString(s);
163
+ public static IRubyObject nonEmptyStringOrNil(Ruby runtime, String s) {
164
+ if (s == null || s.length() == 0) return runtime.getNil();
165
+ return RubyString.newString(runtime, s);
123
166
  }
124
167
 
125
168
  /**
@@ -155,12 +198,10 @@ public class NokogiriHelpers {
155
198
  return ("xmlns".equals(localName)) ? null : localName;
156
199
  }
157
200
 
158
- protected static Charset utf8 = null;
159
- protected static Charset getCharsetUTF8() {
160
- if (utf8 == null) {
161
- utf8 = Charset.forName("UTF-8");
162
- }
163
-
201
+ private static Charset utf8 = null;
202
+
203
+ private static Charset getCharsetUTF8() {
204
+ if (utf8 == null) utf8 = Charset.forName("UTF-8");
164
205
  return utf8;
165
206
  }
166
207
 
@@ -182,7 +223,20 @@ public class NokogiriHelpers {
182
223
  * this for us.
183
224
  */
184
225
  public static String rubyStringToString(IRubyObject str) {
185
- return rubyStringToString(str.convertToString());
226
+ //return rubyStringToString(str.convertToString());
227
+ return toJavaString(str.convertToString());
228
+ }
229
+
230
+ private static String toJavaString(RubyString str) {
231
+ ByteList value = str.getByteList();
232
+ try {
233
+ if (str.getRuntime().is1_9()) {
234
+ return new String(value.getUnsafeBytes(), value.begin(), value.length(), str.getEncoding().toString());
235
+ }
236
+ return RubyEncoding.decodeUTF8(value.getUnsafeBytes(), value.begin(), value.length());
237
+ } catch (UnsupportedEncodingException uee) {
238
+ return str.toString();
239
+ }
186
240
  }
187
241
 
188
242
  public static String rubyStringToString(RubyString str) {
@@ -407,7 +461,6 @@ public class NokogiriHelpers {
407
461
 
408
462
  protected static boolean fullNamesMatch(Node a, Node b) {
409
463
  return a.getNodeName().equals(b.getNodeName());
410
- //return getFullName(a).equals(getFullName(b));
411
464
  }
412
465
 
413
466
  protected static String getFullName(Node n) {
@@ -494,11 +547,15 @@ public class NokogiriHelpers {
494
547
  }
495
548
 
496
549
  public static RubyArray nodeListToRubyArray(Ruby ruby, NodeList nodes) {
497
- RubyArray n = RubyArray.newArray(ruby, nodes.getLength());
550
+ RubyArray array = RubyArray.newArray(ruby, nodes.getLength());
551
+ return nodeListToRubyArray(ruby, nodes, array);
552
+ }
553
+
554
+ public static RubyArray nodeListToRubyArray(Ruby ruby, NodeList nodes, RubyArray array) {
498
555
  for(int i = 0; i < nodes.getLength(); i++) {
499
- n.append(NokogiriHelpers.getCachedNodeOrCreate(ruby, nodes.item(i)));
556
+ array.append(NokogiriHelpers.getCachedNodeOrCreate(ruby, nodes.item(i)));
500
557
  }
501
- return n;
558
+ return array;
502
559
  }
503
560
 
504
561
  public static RubyArray nodeArrayToRubyArray(Ruby ruby, Node[] nodes) {
@@ -1,5 +1,38 @@
1
+ /**
2
+ * (The MIT License)
3
+ *
4
+ * Copyright (c) 2008 - 2010:
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
+
1
33
  package nokogiri.internals;
2
34
 
35
+ import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
3
36
  import static nokogiri.internals.NokogiriHelpers.isNamespace;
4
37
 
5
38
  import java.util.ArrayList;
@@ -11,13 +44,13 @@ import nokogiri.XmlDocument;
11
44
  import nokogiri.XmlNamespace;
12
45
 
13
46
  import org.jruby.Ruby;
14
- import org.jruby.runtime.ThreadContext;
15
47
  import org.w3c.dom.Attr;
16
48
  import org.w3c.dom.NamedNodeMap;
17
49
  import org.w3c.dom.Node;
18
50
 
19
51
  /**
20
- *
52
+ * Cache of namespages of each node. XmlDocument has one cache of this class.
53
+ *
21
54
  * @author sergio
22
55
  * @author Yoko Harada <yokolet@gmail.com>
23
56
  */
@@ -88,7 +121,8 @@ public class NokogiriNamespaceCache {
88
121
  keys.add(hash);
89
122
  index = keys.size() - 1;
90
123
  String actualPrefix = (prefix.equals("")) ? null : prefix;
91
- XmlNamespace namespace = new XmlNamespace(ruby, actualPrefix, href);
124
+ XmlNamespace namespace = (XmlNamespace) getNokogiriClass(ruby, "Nokogiri::XML::Namespace").allocate();
125
+ namespace.setDefinition(ruby, actualPrefix, href);
92
126
  namespace.setDocument(document);
93
127
  CacheEntry entry = new CacheEntry(namespace, node);
94
128
  cache.put(index, entry);