nokogiri 1.5.0.beta.3-java → 1.5.0.beta.4-java

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 (88) hide show
  1. data/CHANGELOG.ja.rdoc +32 -16
  2. data/CHANGELOG.rdoc +18 -0
  3. data/Manifest.txt +1 -2
  4. data/README.rdoc +27 -3
  5. data/Rakefile +39 -83
  6. data/ext/java/nokogiri/EncodingHandler.java +1 -1
  7. data/ext/java/nokogiri/HtmlDocument.java +11 -14
  8. data/ext/java/nokogiri/HtmlElementDescription.java +1 -1
  9. data/ext/java/nokogiri/HtmlEntityLookup.java +1 -1
  10. data/ext/java/nokogiri/HtmlSaxParserContext.java +13 -10
  11. data/ext/java/nokogiri/NokogiriService.java +103 -34
  12. data/ext/java/nokogiri/XmlAttr.java +14 -6
  13. data/ext/java/nokogiri/XmlAttributeDecl.java +1 -1
  14. data/ext/java/nokogiri/XmlCdata.java +3 -1
  15. data/ext/java/nokogiri/XmlComment.java +3 -1
  16. data/ext/java/nokogiri/XmlDocument.java +29 -8
  17. data/ext/java/nokogiri/XmlDocumentFragment.java +14 -13
  18. data/ext/java/nokogiri/XmlDtd.java +5 -2
  19. data/ext/java/nokogiri/XmlElement.java +2 -1
  20. data/ext/java/nokogiri/XmlElementContent.java +1 -1
  21. data/ext/java/nokogiri/XmlElementDecl.java +2 -1
  22. data/ext/java/nokogiri/XmlEntityDecl.java +2 -1
  23. data/ext/java/nokogiri/XmlEntityReference.java +1 -1
  24. data/ext/java/nokogiri/XmlNamespace.java +3 -2
  25. data/ext/java/nokogiri/XmlNode.java +17 -10
  26. data/ext/java/nokogiri/XmlNodeSet.java +40 -13
  27. data/ext/java/nokogiri/XmlProcessingInstruction.java +1 -1
  28. data/ext/java/nokogiri/XmlReader.java +3 -1
  29. data/ext/java/nokogiri/XmlRelaxng.java +37 -92
  30. data/ext/java/nokogiri/XmlSaxParserContext.java +25 -11
  31. data/ext/java/nokogiri/XmlSaxPushParser.java +6 -4
  32. data/ext/java/nokogiri/XmlSchema.java +190 -46
  33. data/ext/java/nokogiri/XmlSyntaxError.java +42 -37
  34. data/ext/java/nokogiri/XmlText.java +3 -2
  35. data/ext/java/nokogiri/XmlXpathContext.java +8 -4
  36. data/ext/java/nokogiri/XsltStylesheet.java +12 -10
  37. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +8 -7
  38. data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +1 -1
  39. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +11 -5
  40. data/ext/java/nokogiri/internals/NokogiriHandler.java +36 -9
  41. data/ext/java/nokogiri/internals/NokogiriHelpers.java +21 -22
  42. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +5 -4
  43. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +1 -1
  44. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +2 -1
  45. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +1 -1
  46. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +2 -1
  47. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +15 -9
  48. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +1 -1
  49. data/ext/java/nokogiri/internals/ParserContext.java +18 -7
  50. data/ext/java/nokogiri/internals/PushInputStream.java +1 -1
  51. data/ext/java/nokogiri/internals/ReaderNode.java +7 -6
  52. data/ext/java/nokogiri/internals/SaveContext.java +16 -10
  53. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +13 -5
  54. data/ext/java/nokogiri/internals/XmlDeclHandler.java +1 -1
  55. data/ext/java/nokogiri/internals/XmlDomParser.java +1 -1
  56. data/ext/java/nokogiri/internals/XmlDomParserContext.java +13 -8
  57. data/ext/java/nokogiri/internals/XmlSaxParser.java +1 -1
  58. data/ext/java/nokogiri/internals/XsltExtensionFunction.java +1 -1
  59. data/ext/nokogiri/extconf.rb +3 -3
  60. data/ext/nokogiri/xml_document.c +9 -0
  61. data/ext/nokogiri/xml_sax_parser.c +4 -2
  62. data/lib/nokogiri.rb +9 -6
  63. data/lib/nokogiri/css.rb +1 -3
  64. data/lib/nokogiri/css/parser.rb +665 -70
  65. data/lib/nokogiri/css/parser.y +3 -1
  66. data/lib/nokogiri/css/parser_extras.rb +91 -0
  67. data/lib/nokogiri/css/tokenizer.rb +148 -3
  68. data/lib/nokogiri/css/tokenizer.rex +1 -1
  69. data/lib/nokogiri/html/document.rb +138 -11
  70. data/lib/nokogiri/html/sax/parser.rb +6 -2
  71. data/lib/nokogiri/nokogiri.jar +0 -0
  72. data/lib/nokogiri/version.rb +1 -1
  73. data/lib/nokogiri/xml/node.rb +2 -2
  74. data/lib/nokogiri/xml/node/save_options.rb +3 -0
  75. data/lib/nokogiri/xml/node_set.rb +1 -1
  76. data/test/css/test_tokenizer.rb +8 -0
  77. data/test/helper.rb +2 -0
  78. data/test/html/sax/test_parser.rb +43 -0
  79. data/test/html/test_document.rb +59 -0
  80. data/test/html/test_document_encoding.rb +48 -0
  81. data/test/html/test_element_description.rb +1 -1
  82. data/test/xml/sax/test_parser.rb +16 -0
  83. data/test/xml/test_document.rb +3 -1
  84. data/test/xml/test_node.rb +4 -1
  85. data/test/xml/test_node_set.rb +10 -0
  86. metadata +73 -82
  87. data/lib/nokogiri/css/generated_parser.rb +0 -676
  88. data/lib/nokogiri/css/generated_tokenizer.rb +0 -145
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2010:
4
+ * Copyright (c) 2008 - 2011:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -63,6 +63,7 @@ import org.w3c.dom.NamedNodeMap;
63
63
  * Class for Nokogiri::XML::DocumentFragment
64
64
  *
65
65
  * @author sergio
66
+ * @author Yoko Harada <yokolet@gmail.com>
66
67
  */
67
68
  @JRubyClass(name="Nokogiri::XML::DocumentFragment", parent="Nokogiri::XML::Node")
68
69
  public class XmlDocumentFragment extends XmlNode {
@@ -77,33 +78,33 @@ public class XmlDocumentFragment extends XmlNode {
77
78
  }
78
79
 
79
80
  @JRubyMethod(name="new", meta = true, required=1, optional=2)
80
- public static IRubyObject rbNew(ThreadContext context, IRubyObject cls, IRubyObject[] argc) {
81
+ public static IRubyObject rbNew(ThreadContext context, IRubyObject cls, IRubyObject[] args) {
81
82
 
82
- if(argc.length < 1) {
83
- throw context.getRuntime().newArgumentError(argc.length, 1);
83
+ if(args.length < 1) {
84
+ throw context.getRuntime().newArgumentError(args.length, 1);
84
85
  }
85
86
 
86
- if(!(argc[0] instanceof XmlDocument)){
87
+ if(!(args[0] instanceof XmlDocument)){
87
88
  throw context.getRuntime().newArgumentError("first parameter must be a Nokogiri::XML::Document instance");
88
89
  }
89
90
 
90
- XmlDocument doc = (XmlDocument) argc[0];
91
+ XmlDocument doc = (XmlDocument) args[0];
91
92
 
92
93
  // make wellformed fragment, ignore invalid namespace, or add appropriate namespace to parse
93
- if (argc.length > 1 && argc[1] instanceof RubyString) {
94
- argc[1] = RubyString.newString(context.getRuntime(), ignoreNamespaceIfNeeded(doc, rubyStringToString(argc[1])));
95
- argc[1] = RubyString.newString(context.getRuntime(), addNamespaceDeclIfNeeded(doc, rubyStringToString(argc[1])));
94
+ if (args.length > 1 && args[1] instanceof RubyString) {
95
+ args[1] = RubyString.newString(context.getRuntime(), ignoreNamespaceIfNeeded(doc, rubyStringToString(args[1])));
96
+ args[1] = RubyString.newString(context.getRuntime(), addNamespaceDeclIfNeeded(doc, rubyStringToString(args[1])));
96
97
  }
97
98
 
98
- XmlDocumentFragment fragment = (XmlDocumentFragment) ((RubyClass)cls).allocate();
99
+ XmlDocumentFragment fragment = (XmlDocumentFragment) NokogiriService.XML_DOCUMENT_FRAGMENT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass)cls);
99
100
  fragment.setDocument(context, doc);
100
101
  fragment.setNode(context, doc.getDocument().createDocumentFragment());
101
102
 
102
103
  //TODO: Get namespace definitions from doc.
103
- if (argc.length == 3 && argc[2] != null && argc[2] instanceof XmlElement) {
104
- fragment.fragmentContext = (XmlElement)argc[2];
104
+ if (args.length == 3 && args[2] != null && args[2] instanceof XmlElement) {
105
+ fragment.fragmentContext = (XmlElement)args[2];
105
106
  }
106
- RuntimeHelpers.invoke(context, fragment, "initialize", argc);
107
+ RuntimeHelpers.invoke(context, fragment, "initialize", args);
107
108
  return fragment;
108
109
  }
109
110
 
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2010:
4
+ * Copyright (c) 2008 - 2011:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -56,7 +56,10 @@ import org.w3c.dom.Node;
56
56
 
57
57
  /**
58
58
  * Class for Nokogiri::XML::DTD
59
- *
59
+ *
60
+ * @author sergio
61
+ * @author Patrick Mahoney <pat@polycrystal.org>
62
+ * @author Yoko Harada <yokolet@gmail.com>
60
63
  */
61
64
 
62
65
  @JRubyClass(name="Nokogiri::XML::DTD", parent="Nokogiri::XML::Node")
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2010:
4
+ * Copyright (c) 2008 - 2011:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -54,6 +54,7 @@ import org.w3c.dom.Node;
54
54
  * Class for Nokogiri::XML::Element
55
55
  *
56
56
  * @author sergio
57
+ * @author Yoko Harada <yokolet@gamil.com>
57
58
  */
58
59
  @JRubyClass(name="Nokogiri::XML::Element", parent="Nokogiri::XML::Node")
59
60
  public class XmlElement extends XmlNode {
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2010:
4
+ * Copyright (c) 2008 - 2011:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2010:
4
+ * Copyright (c) 2008 - 2011:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -49,6 +49,7 @@ import org.w3c.dom.Node;
49
49
  * DTD element declaration.
50
50
  *
51
51
  * @author Patrick Mahoney <pat@polycrystal.org>
52
+ * @author Yoko Harada <yokolet@gmail.com>
52
53
  */
53
54
  @JRubyClass(name="Nokogiri::XML::ElementDecl", parent="Nokogiri::XML::Node")
54
55
  public class XmlElementDecl extends XmlNode {
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2010:
4
+ * Copyright (c) 2008 - 2011:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -48,6 +48,7 @@ import org.w3c.dom.Node;
48
48
  * DTD entity declaration.
49
49
  *
50
50
  * @author Patrick Mahoney <pat@polycrystal.org>
51
+ * @author Yoko Harada <yokolet@gmail.com>
51
52
  */
52
53
  @JRubyClass(name="Nokogiri::XML::EntityDecl", parent="Nokogiri::XML::Node")
53
54
  public class XmlEntityDecl extends XmlNode {
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2010:
4
+ * Copyright (c) 2008 - 2011:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2010:
4
+ * Copyright (c) 2008 - 2011:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -50,6 +50,7 @@ import org.w3c.dom.Node;
50
50
  * Class for Nokogiri::XML::Namespace
51
51
  *
52
52
  * @author serabe
53
+ * @author Yoko Harada <yokolet@gmail.com>
53
54
  */
54
55
  @JRubyClass(name="Nokogiri::XML::Namespace")
55
56
  public class XmlNamespace extends RubyObject {
@@ -98,7 +99,7 @@ public class XmlNamespace extends RubyObject {
98
99
 
99
100
  public static XmlNamespace fromNode(Ruby ruby, Node node) {
100
101
  String localName = getLocalNameForNamespace(node.getNodeName());
101
- XmlNamespace namespace = (XmlNamespace) getNokogiriClass(ruby, "Nokogiri::XML::Namespace").allocate();
102
+ XmlNamespace namespace = (XmlNamespace) NokogiriService.XML_NAMESPACE_ALLOCATOR.allocate(ruby, getNokogiriClass(ruby, "Nokogiri::XML::Namespace"));
102
103
  namespace.setDefinition(ruby, localName, node.getNodeValue());
103
104
  return namespace;
104
105
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2010:
4
+ * Copyright (c) 2008 - 2011:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -77,6 +77,9 @@ import org.w3c.dom.Text;
77
77
  /**
78
78
  * Class for Nokogiri::XML::Node
79
79
  *
80
+ * @author sergio
81
+ * @author Patrick Mahoney <pat@polycrystal.org>
82
+ * @author Yoko Harada <yokolet@gmail.com>
80
83
  */
81
84
  @JRubyClass(name="Nokogiri::XML::Node")
82
85
  public class XmlNode extends RubyObject {
@@ -427,8 +430,7 @@ public class XmlNode extends RubyObject {
427
430
  //this should delegate to subclasses' implementation
428
431
  }
429
432
 
430
- public void saveContent(ThreadContext context, SaveContext ctx) {
431
- }
433
+ public void saveContent(ThreadContext context, SaveContext ctx) {}
432
434
 
433
435
  public void setName(IRubyObject name) {
434
436
  this.name = name;
@@ -592,8 +594,9 @@ public class XmlNode extends RubyObject {
592
594
 
593
595
  @JRubyMethod
594
596
  public IRubyObject children(ThreadContext context) {
595
- XmlNodeSet result = new XmlNodeSet(context.getRuntime(), node.getChildNodes());
596
- return result;
597
+ XmlNodeSet xmlNodeSet = (XmlNodeSet) NokogiriService.XML_NODESET_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet"));
598
+ xmlNodeSet.setNodeList(node.getChildNodes());
599
+ return xmlNodeSet;
597
600
  }
598
601
 
599
602
  @JRubyMethod
@@ -618,8 +621,9 @@ public class XmlNode extends RubyObject {
618
621
  addElements(node, elementNodes, false);
619
622
  if (elementNodes.size() == 0) return XmlNodeSet.newEmptyNodeSet(context);
620
623
  RubyArray array = NokogiriHelpers.nodeArrayToRubyArray(context.getRuntime(), elementNodes.toArray(new Node[0]));
621
- XmlNodeSet result = new XmlNodeSet(context.getRuntime(), array);
622
- return result;
624
+ XmlNodeSet xmlNodeSet = (XmlNodeSet) NokogiriService.XML_NODESET_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(getRuntime(), "Nokogiri::XML::NodeSet"));
625
+ xmlNodeSet.setInitialNodes(array);
626
+ return xmlNodeSet;
623
627
  }
624
628
 
625
629
  private void addElements(Node n, List<Node> nodes, boolean isFirstOnly) {
@@ -716,7 +720,9 @@ public class XmlNode extends RubyObject {
716
720
  documentErrors.add(docErrors.get(i));
717
721
  }
718
722
  document.setInstanceVariable("@errors", documentErrors);
719
- return new XmlNodeSet(getRuntime(), RubyArray.newArray(runtime));
723
+ XmlNodeSet xmlNodeSet = (XmlNodeSet) NokogiriService.XML_NODESET_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"));
724
+ xmlNodeSet.setInitialNodes(RubyArray.newArray(runtime));
725
+ return xmlNodeSet;
720
726
  }
721
727
 
722
728
  // The first child might be document type node (dtd declaration).
@@ -731,8 +737,9 @@ public class XmlNode extends RubyObject {
731
737
  nodeArray.add(NokogiriHelpers.getCachedNodeOrCreate(runtime, first));
732
738
 
733
739
  NokogiriHelpers.nodeListToRubyArray(runtime, first.getChildNodes(), nodeArray);
734
- XmlNodeSet nodes = new XmlNodeSet(getRuntime(), nodeArray);
735
- return nodes;
740
+ XmlNodeSet xmlNodeSet = (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"));
741
+ xmlNodeSet.setInitialNodes(nodeArray);
742
+ return xmlNodeSet;
736
743
  }
737
744
 
738
745
  private RubyArray getErrorArray(XmlDocument document) {
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2010:
4
+ * Copyright (c) 2008 - 2011:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -53,12 +53,28 @@ import org.w3c.dom.NodeList;
53
53
  /**
54
54
  * Class for Nokogiri::XML::NodeSet
55
55
  *
56
+ * @author sergio
57
+ * @author Yoko Harada <yokolet@gmail.com>
56
58
  */
57
59
  @JRubyClass(name="Nokogiri::XML::NodeSet")
58
60
  public class XmlNodeSet extends RubyObject {
59
- private NodeList nodeList = null;
61
+ private NodeList nodeList;
60
62
  private RubyArray nodes;
61
63
  private IRubyObject doc;
64
+
65
+ public XmlNodeSet(Ruby ruby, RubyClass klazz) {
66
+ super(ruby, klazz);
67
+ }
68
+
69
+ /**
70
+ * Create and return a copy of this object.
71
+ *
72
+ * @return a clone of this object
73
+ */
74
+ @Override
75
+ public Object clone() throws CloneNotSupportedException {
76
+ return super.clone();
77
+ }
62
78
 
63
79
  public XmlNodeSet(Ruby ruby, NodeList nodes) {
64
80
  this(ruby, getNokogiriClass(ruby, "Nokogiri::XML::NodeSet"), nodes);
@@ -94,6 +110,17 @@ public class XmlNodeSet extends RubyObject {
94
110
  nodeList = null;
95
111
  }
96
112
 
113
+ public void setInitialNodes(RubyArray nodes) {
114
+ this.nodes = nodes;
115
+ IRubyObject first = nodes.first();
116
+ initialize(getRuntime(), first);
117
+ }
118
+
119
+ public void setNodeList(NodeList nodeList) {
120
+ setInitialNodes(nodeListToRubyArray(getRuntime(), nodeList));
121
+ this.nodeList = nodeList;
122
+ }
123
+
97
124
  private void initialize(Ruby ruby, IRubyObject refNode) {
98
125
  if (refNode instanceof XmlNode) {
99
126
  XmlNode n = (XmlNode)refNode;
@@ -106,17 +133,11 @@ public class XmlNodeSet extends RubyObject {
106
133
  }
107
134
 
108
135
  public static IRubyObject newEmptyNodeSet(ThreadContext context) {
109
- Ruby ruby = context.getRuntime();
110
- return new XmlNodeSet(ruby,
111
- getNokogiriClass(ruby, "Nokogiri::XML::NodeSet"),
112
- ruby.newEmptyArray());
113
- }
114
-
115
- public boolean isEmpty() {
116
- return nodes.isEmpty();
136
+ return (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet"));
117
137
  }
118
138
 
119
139
  public long length() {
140
+ if (nodes == null) return 0L;
120
141
  return nodes.length().getLongValue();
121
142
  }
122
143
 
@@ -165,6 +186,7 @@ public class XmlNodeSet extends RubyObject {
165
186
 
166
187
  @JRubyMethod(name = {"length", "size"})
167
188
  public IRubyObject length(ThreadContext context) {
189
+ if (nodes == null) setInitialNodes(RubyArray.newEmptyArray(context.getRuntime()));
168
190
  return nodes.length();
169
191
  }
170
192
 
@@ -179,12 +201,14 @@ public class XmlNodeSet extends RubyObject {
179
201
  @JRubyMethod(name={"|", "+"})
180
202
  public IRubyObject op_or(ThreadContext context, IRubyObject nodeSet){
181
203
  nodeList = null;
204
+ if (nodes == null) setInitialNodes(RubyArray.newEmptyArray(context.getRuntime()));
182
205
  return newXmlNodeSet(context, (RubyArray) nodes.op_or(asXmlNodeSet(context, nodeSet).nodes));
183
206
  }
184
207
 
185
208
  @JRubyMethod(name = {"push", "<<"})
186
209
  public IRubyObject push(ThreadContext context, IRubyObject node_or_namespace) {
187
210
  nodeList = null;
211
+ if (nodes == null) setInitialNodes(RubyArray.newEmptyArray(context.getRuntime()));
188
212
  nodes.append(asXmlNodeOrNamespace(context, node_or_namespace));
189
213
  return this;
190
214
  }
@@ -235,8 +259,9 @@ public class XmlNodeSet extends RubyObject {
235
259
  }
236
260
 
237
261
  private XmlNodeSet newXmlNodeSet(ThreadContext context, RubyArray array) {
238
- XmlNodeSet result = new XmlNodeSet(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet"), array);
239
- return result;
262
+ XmlNodeSet xmlNodeSet = (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet"));
263
+ xmlNodeSet.setInitialNodes(array);
264
+ return xmlNodeSet;
240
265
  }
241
266
 
242
267
  private XmlNodeSet newXmlNodeSet(ThreadContext context, XmlNodeSet reference) {
@@ -258,7 +283,9 @@ public class XmlNodeSet extends RubyObject {
258
283
  getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet")).isTrue()) {
259
284
  throw context.getRuntime().newArgumentError("node must be a Nokogiri::XML::NodeSet");
260
285
  }
261
- return (XmlNodeSet) possibleNodeSet;
286
+ XmlNodeSet xmlNodeSet = (XmlNodeSet)possibleNodeSet;
287
+ if (xmlNodeSet.nodes == null) xmlNodeSet.setInitialNodes(RubyArray.newEmptyArray(context.getRuntime()));
288
+ return xmlNodeSet;
262
289
  }
263
290
 
264
291
  class NokogiriNodeList implements NodeList{
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2010:
4
+ * Copyright (c) 2008 - 2011:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2010:
4
+ * Copyright (c) 2008 - 2011:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -69,6 +69,8 @@ import org.xml.sax.helpers.XMLReaderFactory;
69
69
  /**
70
70
  * Class for Nokogiri:XML::Reader
71
71
  *
72
+ * @author sergio
73
+ * @author Yoko Harada <yokolet@gmail.com>
72
74
  */
73
75
  @JRubyClass(name="Nokogiri::XML::Reader")
74
76
  public class XmlReader extends RubyObject {
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2010:
4
+ * Copyright (c) 2008 - 2011:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -32,12 +32,15 @@
32
32
 
33
33
  package nokogiri;
34
34
 
35
+ import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
+
35
37
  import java.io.ByteArrayInputStream;
36
38
  import java.io.IOException;
37
39
  import java.io.InputStream;
38
40
  import java.io.StringWriter;
39
41
  import java.io.UnsupportedEncodingException;
40
42
 
43
+ import javax.xml.transform.Source;
41
44
  import javax.xml.transform.TransformerConfigurationException;
42
45
  import javax.xml.transform.TransformerException;
43
46
  import javax.xml.transform.TransformerFactory;
@@ -45,20 +48,14 @@ import javax.xml.transform.dom.DOMSource;
45
48
  import javax.xml.transform.stream.StreamResult;
46
49
  import javax.xml.transform.stream.StreamSource;
47
50
 
48
- import nokogiri.internals.SchemaErrorHandler;
49
-
50
51
  import org.iso_relax.verifier.Schema;
51
52
  import org.iso_relax.verifier.Verifier;
52
53
  import org.iso_relax.verifier.VerifierConfigurationException;
53
54
  import org.iso_relax.verifier.VerifierFactory;
54
55
  import org.jruby.Ruby;
55
- import org.jruby.RubyArray;
56
56
  import org.jruby.RubyClass;
57
57
  import org.jruby.anno.JRubyClass;
58
- import org.jruby.anno.JRubyMethod;
59
58
  import org.jruby.runtime.ThreadContext;
60
- import org.jruby.runtime.Visibility;
61
- import org.jruby.runtime.builtin.IRubyObject;
62
59
  import org.w3c.dom.Document;
63
60
  import org.xml.sax.ErrorHandler;
64
61
  import org.xml.sax.SAXException;
@@ -67,22 +64,42 @@ import org.xml.sax.SAXException;
67
64
  * Class for Nokogiri::XML::RelaxNG
68
65
  *
69
66
  * @author sergio
67
+ * @author Yoko Harada <yokolet@gmail.com>
70
68
  */
71
69
  @JRubyClass(name="Nokogiri::XML::RelaxNG", parent="Nokogiri::XML::Schema")
72
- public class XmlRelaxng extends XmlSchema{
70
+ public class XmlRelaxng extends XmlSchema {
71
+ private Verifier verifier;
73
72
 
74
73
  public XmlRelaxng(Ruby ruby, RubyClass klazz) {
75
74
  super(ruby, klazz);
76
75
  }
76
+
77
+ private void setVerifier(Verifier verifier) {
78
+ this.verifier = verifier;
79
+ }
80
+
81
+ static XmlSchema createSchemaInstance(ThreadContext context, RubyClass klazz, Source source) {
82
+ Ruby runtime = context.getRuntime();
83
+ XmlRelaxng xmlRelaxng = (XmlRelaxng) NokogiriService.XML_RELAXNG_ALLOCATOR.allocate(runtime, klazz);
84
+ xmlRelaxng.setInstanceVariable("@errors", runtime.newEmptyArray());
85
+
86
+ try {
87
+ Schema schema = xmlRelaxng.getSchema(source, context);
88
+ xmlRelaxng.setVerifier(schema.newVerifier());
89
+ return xmlRelaxng;
90
+ } catch (VerifierConfigurationException ex) {
91
+ throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
92
+ }
93
+ }
77
94
 
78
- private Schema getSchema(ThreadContext context) {
95
+ private Schema getSchema(Source source, ThreadContext context) {
79
96
  InputStream is = null;
80
97
  VerifierFactory factory = new com.thaiopensource.relaxng.jarv.VerifierFactoryImpl();
81
- if(this.source instanceof StreamSource) {
82
- StreamSource ss = (StreamSource) this.source;
98
+ if (source instanceof StreamSource) {
99
+ StreamSource ss = (StreamSource)source;
83
100
  is = ss.getInputStream();
84
- } else /*if (this.source instanceof DOMSource)*/{
85
- DOMSource ds = (DOMSource) this.source;
101
+ } else { //if (this.source instanceof DOMSource)
102
+ DOMSource ds = (DOMSource)source;
86
103
  StringWriter xmlAsWriter = new StringWriter();
87
104
  StreamResult result = new StreamResult(xmlAsWriter);
88
105
  try {
@@ -114,86 +131,14 @@ public class XmlRelaxng extends XmlSchema{
114
131
  throw context.getRuntime().newIOError(ex.getMessage());
115
132
  }
116
133
  }
117
-
118
- //
119
- // protected static XmlSchema createSchemaFromSource(ThreadContext context,
120
- // IRubyObject klazz, Source source) {
121
- //
122
- // Ruby ruby = context.getRuntime();
123
- //
124
- // XmlSchema schema = new XmlSchema(ruby, (RubyClass) klazz);
125
- //
126
- // try {
127
- // schema.schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
128
- // .newSchema(source);
129
- // } catch(SAXException ex) {
130
- // throw ruby.newRuntimeError("Could not parse document: "+ex.getMessage());
131
- // }
132
- //
133
- // schema.setInstanceVariable("@errors", ruby.newEmptyArray());
134
- //
135
- // return schema;
136
- // }
137
- //
138
- // @JRubyMethod(meta=true)
139
- // public static IRubyObject from_document(ThreadContext context,
140
- // IRubyObject klazz, IRubyObject document) {
141
- // XmlDocument doc = ((XmlDocument) ((XmlNode) document).document(context));
142
- //
143
- // RubyArray errors = (RubyArray) doc.getInstanceVariable("@errors");
144
- //
145
- // if(!errors.isEmpty()) {
146
- // throw new RaiseException((XmlSyntaxError) errors.first());
147
- // }
148
- //
149
- // DOMSource source = new DOMSource(doc.getDocument());
150
- //
151
- // IRubyObject uri = doc.url(context);
152
- //
153
- // if(!uri.isNil()) {
154
- // source.setSystemId(uri.convertToString().asJavaString());
155
- // }
156
- //
157
- // return createSchemaFromSource(context, klazz, source);
158
- // }
159
- //
160
- // @JRubyMethod(meta=true)
161
- // public static IRubyObject read_memory(ThreadContext context,
162
- // IRubyObject klazz, IRubyObject content) {
163
- //
164
- // String data = content.convertToString().asJavaString();
165
- //
166
- // return createSchemaFromSource(context, klazz,
167
- // new StreamSource(new StringReader(data)));
168
- // }
169
- //
134
+
170
135
  @Override
171
- @JRubyMethod(visibility=Visibility.PRIVATE)
172
- public IRubyObject validate_document(ThreadContext context, IRubyObject document) {
173
- Ruby ruby = context.getRuntime();
174
-
175
- Document doc = ((XmlDocument) document).getDocument();
176
-
177
- Schema schema = this.getSchema(context);
178
-
179
- Verifier verifier;
180
- try {
181
- verifier = schema.newVerifier();
182
- } catch (VerifierConfigurationException ex) {
183
- throw context.getRuntime()
184
- .newRuntimeError("Could not parse document: "+ex.getMessage());
185
- }
186
-
187
- RubyArray errors = (RubyArray) this.getInstanceVariable("@errors");
188
- ErrorHandler errorHandler = new SchemaErrorHandler(ruby, errors);
189
-
136
+ protected void setErrorHandler(ErrorHandler errorHandler) {
190
137
  verifier.setErrorHandler(errorHandler);
191
- try {
192
- verifier.verify(doc);
193
- } catch (SAXException ex) {
194
- errors.append(new XmlSyntaxError(ruby, ex));
195
- }
196
-
197
- return errors;
138
+ }
139
+
140
+ @Override
141
+ protected void validate(Document document) throws SAXException, IOException {
142
+ verifier.verify(document);
198
143
  }
199
144
  }