nokogiri 1.5.0.beta.4-java → 1.5.0-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 (87) 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 +96 -105
  7. data/bin/nokogiri +1 -2
  8. data/ext/java/nokogiri/HtmlDocument.java +1 -31
  9. data/ext/java/nokogiri/HtmlSaxParserContext.java +1 -1
  10. data/ext/java/nokogiri/NokogiriService.java +77 -22
  11. data/ext/java/nokogiri/XmlAttr.java +5 -16
  12. data/ext/java/nokogiri/XmlCdata.java +4 -11
  13. data/ext/java/nokogiri/XmlComment.java +5 -5
  14. data/ext/java/nokogiri/XmlDocument.java +49 -59
  15. data/ext/java/nokogiri/XmlDocumentFragment.java +14 -8
  16. data/ext/java/nokogiri/XmlDtd.java +45 -43
  17. data/ext/java/nokogiri/XmlElement.java +19 -46
  18. data/ext/java/nokogiri/XmlElementDecl.java +9 -5
  19. data/ext/java/nokogiri/XmlEntityReference.java +24 -2
  20. data/ext/java/nokogiri/XmlNamespace.java +89 -34
  21. data/ext/java/nokogiri/XmlNode.java +31 -52
  22. data/ext/java/nokogiri/XmlNodeSet.java +42 -86
  23. data/ext/java/nokogiri/XmlProcessingInstruction.java +15 -19
  24. data/ext/java/nokogiri/XmlReader.java +40 -43
  25. data/ext/java/nokogiri/XmlSaxParserContext.java +2 -2
  26. data/ext/java/nokogiri/XmlSchema.java +14 -9
  27. data/ext/java/nokogiri/XmlText.java +18 -35
  28. data/ext/java/nokogiri/XmlXpathContext.java +43 -23
  29. data/ext/java/nokogiri/XsltStylesheet.java +17 -3
  30. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +2 -4
  31. data/ext/java/nokogiri/internals/NokogiriHelpers.java +77 -20
  32. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +13 -17
  33. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +13 -1
  34. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +23 -8
  35. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +20 -3
  36. data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +67 -0
  37. data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +86 -0
  38. data/ext/java/nokogiri/internals/ParserContext.java +25 -27
  39. data/ext/java/nokogiri/internals/ReaderNode.java +58 -1
  40. data/ext/java/nokogiri/internals/SaveContextVisitor.java +567 -0
  41. data/ext/java/nokogiri/internals/XmlDomParser.java +1 -2
  42. data/ext/java/nokogiri/internals/XmlDomParserContext.java +6 -0
  43. data/ext/nokogiri/nokogiri.c +24 -1
  44. data/ext/nokogiri/xml_io.c +32 -7
  45. data/ext/nokogiri/xml_node.c +14 -13
  46. data/ext/nokogiri/xml_sax_parser.c +9 -4
  47. data/ext/nokogiri/xslt_stylesheet.c +7 -1
  48. data/lib/nokogiri.rb +3 -22
  49. data/lib/nokogiri/css.rb +4 -0
  50. data/lib/nokogiri/html/document.rb +10 -14
  51. data/lib/nokogiri/nokogiri.jar +0 -0
  52. data/lib/nokogiri/version.rb +76 -23
  53. data/lib/nokogiri/xml/builder.rb +7 -0
  54. data/lib/nokogiri/xml/document.rb +17 -1
  55. data/lib/nokogiri/xml/document_fragment.rb +14 -0
  56. data/lib/nokogiri/xml/node.rb +36 -28
  57. data/lib/nokogiri/xml/node/save_options.rb +17 -1
  58. data/lib/nokogiri/xml/node_set.rb +7 -0
  59. data/lib/nokogiri/xml/parse_options.rb +8 -0
  60. data/lib/nokogiri/xml/reader.rb +6 -6
  61. data/lib/nokogiri/xml/schema.rb +7 -1
  62. data/lib/xercesImpl.jar +0 -0
  63. data/nokogiri_help_responses.md +40 -0
  64. data/tasks/cross_compile.rb +134 -159
  65. data/tasks/nokogiri.org.rb +18 -0
  66. data/tasks/test.rb +1 -1
  67. data/test/files/encoding.html +82 -0
  68. data/test/files/encoding.xhtml +84 -0
  69. data/test/files/metacharset.html +10 -0
  70. data/test/files/noencoding.html +47 -0
  71. data/test/helper.rb +2 -0
  72. data/test/html/test_document.rb +15 -0
  73. data/test/html/test_document_encoding.rb +13 -0
  74. data/test/test_memory_leak.rb +20 -0
  75. data/test/test_reader.rb +22 -0
  76. data/test/test_xslt_transforms.rb +6 -2
  77. data/test/xml/node/test_save_options.rb +10 -2
  78. data/test/xml/test_builder.rb +17 -0
  79. data/test/xml/test_document.rb +22 -0
  80. data/test/xml/test_node.rb +19 -1
  81. data/test/xml/test_node_reparenting.rb +16 -3
  82. data/test/xml/test_node_set.rb +34 -0
  83. data/test/xml/test_schema.rb +5 -0
  84. data/test/xslt/test_exception_handling.rb +37 -0
  85. metadata +141 -107
  86. data/deps.rip +0 -5
  87. data/ext/java/nokogiri/internals/SaveContext.java +0 -288
@@ -32,9 +32,7 @@
32
32
 
33
33
  package nokogiri;
34
34
 
35
- import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
35
  import nokogiri.internals.HtmlDomParserContext;
37
- import nokogiri.internals.SaveContext;
38
36
 
39
37
  import org.jruby.Ruby;
40
38
  import org.jruby.RubyClass;
@@ -45,7 +43,6 @@ import org.jruby.runtime.Arity;
45
43
  import org.jruby.runtime.ThreadContext;
46
44
  import org.jruby.runtime.builtin.IRubyObject;
47
45
  import org.w3c.dom.Document;
48
- import org.w3c.dom.DocumentType;
49
46
 
50
47
  /**
51
48
  * Class for Nokogiri::HTML::Document.
@@ -88,7 +85,7 @@ public class HtmlDocument extends XmlDocument {
88
85
  Arity.checkArgumentCount(ruby, args, 4, 4);
89
86
  HtmlDomParserContext ctx =
90
87
  new HtmlDomParserContext(ruby, args[2], args[3]);
91
- ctx.setInputSource(context, args[0]);
88
+ ctx.setInputSource(context, args[0], args[1]);
92
89
  return ctx.parse(context, klass, args[1]);
93
90
  }
94
91
 
@@ -119,31 +116,4 @@ public class HtmlDocument extends XmlDocument {
119
116
  IRubyObject[] args) {
120
117
  return do_parse(context, cls, args);
121
118
  }
122
-
123
- @Override
124
- public void saveContent(ThreadContext context, SaveContext ctx) {
125
- Document doc = getDocument();
126
- DocumentType dtd = doc.getDoctype();
127
-
128
- if(dtd != null) {
129
- ctx.append("<!DOCTYPE ");
130
- ctx.append(dtd.getName());
131
- if(dtd.getPublicId() != null) {
132
- ctx.append(" PUBLIC ");
133
- ctx.appendQuoted(dtd.getPublicId());
134
- if(dtd.getSystemId() != null) {
135
- ctx.append(" ");
136
- ctx.appendQuoted(dtd.getSystemId());
137
- }
138
- } else if(dtd.getSystemId() != null) {
139
- ctx.append(" SYSTEM ");
140
- ctx.appendQuoted(dtd.getSystemId());
141
- }
142
- ctx.append(">\n");
143
- }
144
-
145
- this.saveNodeListContent(context,
146
- (XmlNodeSet) this.children(context), ctx);
147
- ctx.append("\n");
148
- }
149
119
  }
@@ -215,7 +215,7 @@ public class HtmlSaxParserContext extends XmlSaxParserContext {
215
215
  IRubyObject encoding) {
216
216
  HtmlSaxParserContext ctx = (HtmlSaxParserContext) NokogiriService.HTML_SAXPARSER_CONTEXT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass)klazz);
217
217
  ctx.initialize(context.getRuntime());
218
- ctx.setInputSource(context, data);
218
+ ctx.setInputSource(context, data, context.getRuntime().getNil());
219
219
  String javaEncoding = findEncoding(context, encoding);
220
220
  if (javaEncoding != null) {
221
221
  ctx.getInputSource().setEncoding(javaEncoding);
@@ -77,13 +77,17 @@ public class NokogiriService implements BasicLibraryService {
77
77
  nokogiriClassCache.put("Nokogiri::XML::ElementContent", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::ElementContent"));
78
78
  nokogiriClassCache.put("Nokogiri::XML::ElementDecl", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::ElementDecl"));
79
79
  nokogiriClassCache.put("Nokogiri::XML::EntityDecl", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::EntityDecl"));
80
+ nokogiriClassCache.put("Nokogiri::XML::EntityReference", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::EntityReference"));
81
+ nokogiriClassCache.put("Nokogiri::XML::ProcessingInstruction", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::ProcessingInstruction"));
80
82
  nokogiriClassCache.put("Nokogiri::XML::CDATA", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::CDATA"));
81
83
  nokogiriClassCache.put("Nokogiri::XML::Node", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::Node"));
82
84
  nokogiriClassCache.put("Nokogiri::XML::NodeSet", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::NodeSet"));
83
85
  nokogiriClassCache.put("Nokogiri::XML::Namespace", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::Namespace"));
84
86
  nokogiriClassCache.put("Nokogiri::XML::SyntaxError", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::SyntaxError"));
87
+ nokogiriClassCache.put("Nokogiri::XML::Reader", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::Reader"));
85
88
  nokogiriClassCache.put("Nokogiri::XML::RelaxNG", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::RelaxNG"));
86
89
  nokogiriClassCache.put("Nokogiri::XML::Schema", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::Schema"));
90
+ nokogiriClassCache.put("Nokogiri::XML::XPathContext", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::XPathContext"));
87
91
  nokogiriClassCache.put("Nokogiri::XML::AttributeDecl", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::AttributeDecl"));
88
92
  nokogiriClassCache.put("Nokogiri::XML::SAX::ParserContext", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::SAX::ParserContext"));
89
93
  }
@@ -154,6 +158,7 @@ public class NokogiriService implements BasicLibraryService {
154
158
 
155
159
  RubyClass entityDecl = xmlModule.defineClassUnder("EntityDecl", node, XML_ENTITY_DECL_ALLOCATOR);
156
160
  entityDecl.defineAnnotatedMethods(XmlEntityDecl.class);
161
+
157
162
  entityDecl.defineConstant("INTERNAL_GENERAL", RubyFixnum.newFixnum(ruby, XmlEntityDecl.INTERNAL_GENERAL));
158
163
  entityDecl.defineConstant("EXTERNAL_GENERAL_PARSED", RubyFixnum.newFixnum(ruby, XmlEntityDecl.EXTERNAL_GENERAL_PARSED));
159
164
  entityDecl.defineConstant("EXTERNAL_GENERAL_UNPARSED", RubyFixnum.newFixnum(ruby, XmlEntityDecl.EXTERNAL_GENERAL_UNPARSED));
@@ -312,13 +317,6 @@ public class NokogiriService implements BasicLibraryService {
312
317
  }
313
318
  };
314
319
 
315
- private static ObjectAllocator XML_PROCESSING_INSTRUCTION_ALLOCATOR =
316
- new ObjectAllocator() {
317
- public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
318
- throw runtime.newNotImplementedError("not implemented");
319
- }
320
- };
321
-
322
320
  public static final ObjectAllocator XML_DOCUMENT_ALLOCATOR = new ObjectAllocator() {
323
321
  private XmlDocument xmlDocument = null;
324
322
  public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
@@ -347,9 +345,17 @@ public class NokogiriService implements BasicLibraryService {
347
345
  }
348
346
  };
349
347
 
350
- private static ObjectAllocator XML_DTD_ALLOCATOR = new ObjectAllocator() {
348
+ public static final ObjectAllocator XML_DTD_ALLOCATOR = new ObjectAllocator() {
349
+ private XmlDtd xmlDtd = null;
351
350
  public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
352
- return new XmlDtd(runtime, klazz);
351
+ if (xmlDtd == null) xmlDtd = new XmlDtd(runtime, klazz);
352
+ try {
353
+ XmlDtd clone = (XmlDtd)xmlDtd.clone();
354
+ clone.setMetaClass(klazz);
355
+ return clone;
356
+ } catch (CloneNotSupportedException e) {
357
+ return new XmlDtd(runtime, klazz);
358
+ }
353
359
  }
354
360
  };
355
361
 
@@ -366,13 +372,35 @@ public class NokogiriService implements BasicLibraryService {
366
372
  }
367
373
  }
368
374
  };
369
-
370
- private static ObjectAllocator XML_ENTITY_REFERENCE_ALLOCATOR = new ObjectAllocator() {
375
+
376
+ public static ObjectAllocator XML_ELEMENT_DECL_ALLOCATOR = new ObjectAllocator() {
377
+ private XmlElementDecl xmlElementDecl = null;
371
378
  public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
372
- return new XmlEntityReference(runtime, klazz);
379
+ if (xmlElementDecl == null) xmlElementDecl = new XmlElementDecl(runtime, klazz);
380
+ try {
381
+ XmlElementDecl clone = (XmlElementDecl)xmlElementDecl.clone();
382
+ clone.setMetaClass(klazz);
383
+ return clone;
384
+ } catch (CloneNotSupportedException e) {
385
+ return new XmlElementDecl(runtime, klazz);
386
+ }
373
387
  }
374
388
  };
375
389
 
390
+ public static ObjectAllocator XML_ENTITY_REFERENCE_ALLOCATOR = new ObjectAllocator() {
391
+ private XmlEntityReference xmlEntityRef = null;
392
+ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
393
+ if (xmlEntityRef == null) xmlEntityRef = new XmlEntityReference(runtime, klazz);
394
+ try {
395
+ XmlEntityReference clone = (XmlEntityReference)xmlEntityRef.clone();
396
+ clone.setMetaClass(klazz);
397
+ return clone;
398
+ } catch (CloneNotSupportedException e) {
399
+ return new XmlEntityReference(runtime, klazz);
400
+ }
401
+ }
402
+ };
403
+
376
404
  public static final ObjectAllocator XML_NAMESPACE_ALLOCATOR = new ObjectAllocator() {
377
405
  private XmlNamespace xmlNamespace = null;
378
406
  public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
@@ -410,26 +438,45 @@ public class NokogiriService implements BasicLibraryService {
410
438
  clone.setMetaClass(klazz);
411
439
  return clone;
412
440
  } catch (CloneNotSupportedException e) {
413
- return new XmlNodeSet(runtime, klazz, RubyArray.newEmptyArray(runtime));
441
+ xmlNodeSet = new XmlNodeSet(runtime, klazz);
442
+ xmlNodeSet.setNodes(RubyArray.newEmptyArray(runtime));
443
+ return xmlNodeSet;
414
444
  }
415
445
  }
416
446
  };
417
-
418
- private static ObjectAllocator XML_READER_ALLOCATOR = new ObjectAllocator() {
447
+
448
+ public static ObjectAllocator XML_PROCESSING_INSTRUCTION_ALLOCATOR = new ObjectAllocator() {
449
+ private XmlProcessingInstruction xmlProcessingInstruction = null;
419
450
  public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
420
- return new XmlReader(runtime, klazz);
451
+ if (xmlProcessingInstruction == null) xmlProcessingInstruction = new XmlProcessingInstruction(runtime, klazz);
452
+ try {
453
+ XmlProcessingInstruction clone = (XmlProcessingInstruction)xmlProcessingInstruction.clone();
454
+ clone.setMetaClass(klazz);
455
+ return clone;
456
+ } catch (CloneNotSupportedException e) {
457
+ return new XmlProcessingInstruction(runtime, klazz);
458
+ }
421
459
  }
422
460
  };
423
461
 
424
- private static ObjectAllocator XML_ATTRIBUTE_DECL_ALLOCATOR = new ObjectAllocator() {
462
+ public static ObjectAllocator XML_READER_ALLOCATOR = new ObjectAllocator() {
463
+ private XmlReader xmlReader = null;
425
464
  public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
426
- return new XmlAttributeDecl(runtime, klazz);
465
+ if (xmlReader == null) xmlReader = new XmlReader(runtime, klazz);
466
+ try {
467
+ XmlReader clone = (XmlReader) xmlReader.clone();
468
+ clone.setMetaClass(klazz);
469
+ return clone;
470
+ } catch (CloneNotSupportedException e) {
471
+ xmlReader = new XmlReader(runtime, klazz);
472
+ return xmlReader;
473
+ }
427
474
  }
428
475
  };
429
476
 
430
- private static ObjectAllocator XML_ELEMENT_DECL_ALLOCATOR = new ObjectAllocator() {
477
+ private static ObjectAllocator XML_ATTRIBUTE_DECL_ALLOCATOR = new ObjectAllocator() {
431
478
  public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
432
- return new XmlElementDecl(runtime, klazz);
479
+ return new XmlAttributeDecl(runtime, klazz);
433
480
  }
434
481
  };
435
482
 
@@ -521,9 +568,17 @@ public class NokogiriService implements BasicLibraryService {
521
568
  }
522
569
  };
523
570
 
524
- private static ObjectAllocator XML_XPATHCONTEXT_ALLOCATOR = new ObjectAllocator() {
571
+ public static ObjectAllocator XML_XPATHCONTEXT_ALLOCATOR = new ObjectAllocator() {
572
+ private XmlXpathContext xmlXpathContext = null;
525
573
  public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
526
- throw runtime.newNotImplementedError("not implemented");
574
+ if (xmlXpathContext == null) xmlXpathContext = new XmlXpathContext(runtime, klazz);
575
+ try {
576
+ XmlXpathContext clone = (XmlXpathContext) xmlXpathContext.clone();
577
+ clone.setMetaClass(klazz);
578
+ return clone;
579
+ } catch (CloneNotSupportedException e) {
580
+ return new XmlXpathContext(runtime, klazz);
581
+ }
527
582
  }
528
583
  };
529
584
 
@@ -35,10 +35,9 @@ package nokogiri;
35
35
  import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
36
  import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
37
37
  import nokogiri.internals.NokogiriHelpers;
38
- import nokogiri.internals.SaveContext;
38
+ import nokogiri.internals.SaveContextVisitor;
39
39
 
40
40
  import org.jruby.Ruby;
41
- import org.jruby.RubyBoolean;
42
41
  import org.jruby.RubyClass;
43
42
  import org.jruby.RubyString;
44
43
  import org.jruby.anno.JRubyClass;
@@ -98,7 +97,7 @@ public class XmlAttr extends XmlNode{
98
97
  setNode(context, attr);
99
98
  }
100
99
 
101
- public boolean isHtmlBooleanAttr() {
100
+ private boolean isHtmlBooleanAttr() {
102
101
  String name = node.getNodeName().toLowerCase();
103
102
 
104
103
  for(String s : HTML_BOOLEAN_ATTRS) {
@@ -160,19 +159,9 @@ public class XmlAttr extends XmlNode{
160
159
  }
161
160
 
162
161
  @Override
163
- public void saveContent(ThreadContext context, SaveContext ctx) {
164
- boolean docType = isHtml(context);
165
- Attr attr = (Attr) node;
166
-
167
- ctx.maybeSpace();
168
- ctx.append(attr.getName());
169
-
170
- if (!ctx.asHtml() || !isHtmlBooleanAttr()) {
171
- ctx.append("=");
172
- ctx.append("\"");
173
- ctx.append(serializeAttrTextContent(attr.getValue(), docType));
174
- ctx.append("\"");
175
- }
162
+ public void accept(ThreadContext context, SaveContextVisitor visitor) {
163
+ visitor.enter((Attr)node);
164
+ visitor.leave((Attr)node);
176
165
  }
177
166
 
178
167
  private boolean isHtml(ThreadContext context) {
@@ -34,7 +34,7 @@ package nokogiri;
34
34
 
35
35
  import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
36
36
 
37
- import nokogiri.internals.SaveContext;
37
+ import nokogiri.internals.SaveContextVisitor;
38
38
 
39
39
  import org.jruby.Ruby;
40
40
  import org.jruby.RubyClass;
@@ -77,15 +77,8 @@ public class XmlCdata extends XmlText {
77
77
  }
78
78
 
79
79
  @Override
80
- public void saveContent(ThreadContext context, SaveContext ctx) {
81
- CDATASection cdata = (CDATASection) node;
82
-
83
- if(cdata.getData().length() == 0) {
84
- ctx.append("<![CDATA[]]>");
85
- } else {
86
- ctx.append("<![CDATA[");
87
- ctx.append(cdata.getData());
88
- ctx.append("]]>");
89
- }
80
+ public void accept(ThreadContext context, SaveContextVisitor visitor) {
81
+ visitor.enter((CDATASection)node);
82
+ visitor.leave((CDATASection)node);
90
83
  }
91
84
  }
@@ -33,13 +33,14 @@
33
33
  package nokogiri;
34
34
 
35
35
  import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
36
- import nokogiri.internals.SaveContext;
36
+ import nokogiri.internals.SaveContextVisitor;
37
37
 
38
38
  import org.jruby.Ruby;
39
39
  import org.jruby.RubyClass;
40
40
  import org.jruby.anno.JRubyClass;
41
41
  import org.jruby.runtime.ThreadContext;
42
42
  import org.jruby.runtime.builtin.IRubyObject;
43
+ import org.w3c.dom.Comment;
43
44
  import org.w3c.dom.Document;
44
45
  import org.w3c.dom.Node;
45
46
 
@@ -78,9 +79,8 @@ public class XmlComment extends XmlNode {
78
79
  public boolean isComment() { return true; }
79
80
 
80
81
  @Override
81
- public void saveContent(ThreadContext context, SaveContext ctx) {
82
- ctx.append("<!--");
83
- ctx.append(content(context).convertToString().asJavaString());
84
- ctx.append("-->");
82
+ public void accept(ThreadContext context, SaveContextVisitor visitor) {
83
+ visitor.enter((Comment)node);
84
+ visitor.leave((Comment)node);
85
85
  }
86
86
  }
@@ -44,7 +44,7 @@ import javax.xml.parsers.ParserConfigurationException;
44
44
 
45
45
  import nokogiri.internals.NokogiriHelpers;
46
46
  import nokogiri.internals.NokogiriNamespaceCache;
47
- import nokogiri.internals.SaveContext;
47
+ import nokogiri.internals.SaveContextVisitor;
48
48
  import nokogiri.internals.XmlDomParserContext;
49
49
 
50
50
  import org.jruby.Ruby;
@@ -53,6 +53,7 @@ import org.jruby.RubyFixnum;
53
53
  import org.jruby.RubyNil;
54
54
  import org.jruby.anno.JRubyClass;
55
55
  import org.jruby.anno.JRubyMethod;
56
+ import org.jruby.javasupport.JavaUtil;
56
57
  import org.jruby.javasupport.util.RuntimeHelpers;
57
58
  import org.jruby.runtime.Arity;
58
59
  import org.jruby.runtime.ThreadContext;
@@ -76,8 +77,8 @@ public class XmlDocument extends XmlNode {
76
77
 
77
78
  /* UserData keys for storing extra info in the document node. */
78
79
  public final static String DTD_RAW_DOCUMENT = "DTD_RAW_DOCUMENT";
79
- protected final static String DTD_INTERNAL_SUBSET = "DTD_INTERNAL_SUBSET";
80
- protected final static String DTD_EXTERNAL_SUBSET = "DTD_EXTERNAL_SUBSET";
80
+ public final static String DTD_INTERNAL_SUBSET = "DTD_INTERNAL_SUBSET";
81
+ public final static String DTD_EXTERNAL_SUBSET = "DTD_EXTERNAL_SUBSET";
81
82
 
82
83
  /* DocumentBuilderFactory implementation class name. This needs to set a classloader into it.
83
84
  * Setting an appropriate classloader resolves issue 380.
@@ -142,11 +143,10 @@ public class XmlDocument extends XmlNode {
142
143
  String attrName = attr.getName();
143
144
  // not sure, but need to get value always before document is referred.
144
145
  // or lose attribute value
145
- String attrValue = attr.getValue();
146
+ String attrValue = attr.getValue(); // don't delete this line
146
147
  if (isNamespace(attrName)) {
147
- String prefix = getLocalNameForNamespace(attrName);
148
- prefix = prefix != null ? prefix : "";
149
- nsCache.put(ruby, prefix, attrValue, node, this);
148
+ // create and cache
149
+ XmlNamespace.createFromAttr(ruby, attr);
150
150
  }
151
151
  }
152
152
  }
@@ -309,7 +309,7 @@ public class XmlDocument extends XmlNode {
309
309
  Arity.checkArgumentCount(ruby, args, 4, 4);
310
310
  XmlDomParserContext ctx =
311
311
  new XmlDomParserContext(ruby, args[2], args[3]);
312
- ctx.setInputSource(context, args[0]);
312
+ ctx.setInputSource(context, args[0], args[1]);
313
313
  return ctx.parse(context, klass, args[1]);
314
314
  }
315
315
 
@@ -420,16 +420,11 @@ public class XmlDocument extends XmlNode {
420
420
  }
421
421
 
422
422
  public IRubyObject getInternalSubset(ThreadContext context) {
423
- IRubyObject dtd =
424
- (IRubyObject) node.getUserData(DTD_INTERNAL_SUBSET);
423
+ IRubyObject dtd = (IRubyObject) node.getUserData(DTD_INTERNAL_SUBSET);
425
424
 
426
425
  if (dtd == null) {
427
- if (getDocument().getDoctype() == null)
428
- dtd = context.getRuntime().getNil();
429
- else
430
- dtd = XmlDtd.newFromInternalSubset(context.getRuntime(),
431
- getDocument());
432
-
426
+ if (getDocument().getDoctype() == null) dtd = context.getRuntime().getNil();
427
+ else dtd = XmlDtd.newFromInternalSubset(context.getRuntime(), getDocument());
433
428
  setInternalSubset(dtd);
434
429
  }
435
430
 
@@ -456,15 +451,9 @@ public class XmlDocument extends XmlNode {
456
451
  }
457
452
 
458
453
  public IRubyObject getExternalSubset(ThreadContext context) {
459
- IRubyObject dtd = (IRubyObject)
460
- node.getUserData(DTD_EXTERNAL_SUBSET);
461
-
462
- if (dtd == null) {
463
- dtd = XmlDtd.newFromExternalSubset(context.getRuntime(),
464
- getDocument());
465
- setExternalSubset(dtd);
466
- }
454
+ IRubyObject dtd = (IRubyObject) node.getUserData(DTD_EXTERNAL_SUBSET);
467
455
 
456
+ if (dtd == null) return context.getRuntime().getNil();
468
457
  return dtd;
469
458
  }
470
459
 
@@ -487,43 +476,44 @@ public class XmlDocument extends XmlNode {
487
476
  node.setUserData(DTD_EXTERNAL_SUBSET, data, null);
488
477
  }
489
478
 
490
- //public IRubyObject createE
491
-
492
479
  @Override
493
- public void saveContent(ThreadContext context, SaveContext ctx) {
494
- if(!ctx.noDecl()) {
495
- ctx.append("<?xml version=\"");
496
- ctx.append(getDocument().getXmlVersion());
497
- ctx.append("\"");
498
- // if(!cur.encoding(context).isNil()) {
499
- // ctx.append(" encoding=");
500
- // ctx.append(cur.encoding(context).asJavaString());
501
- // }
502
-
503
- String encoding = ctx.getEncoding();
504
-
505
- if(encoding == null &&
506
- !encoding(context).isNil()) {
507
- encoding = encoding(context).convertToString().asJavaString();
508
- }
509
-
510
- if(encoding != null) {
511
- ctx.append(" encoding=\"");
512
- ctx.append(encoding);
513
- ctx.append("\"");
480
+ public void accept(ThreadContext context, SaveContextVisitor visitor) {
481
+ Document document = getDocument();
482
+ visitor.enter(document);
483
+ NodeList children = document.getChildNodes();
484
+ for (int i=0; i<children.getLength(); i++) {
485
+ Node child = children.item(i);
486
+ short type = child.getNodeType();
487
+ if (type == Node.COMMENT_NODE) {
488
+ XmlComment xmlComment = (XmlComment) getCachedNodeOrCreate(context.getRuntime(), child);
489
+ xmlComment.accept(context, visitor);
490
+ } else if (type == Node.DOCUMENT_TYPE_NODE) {
491
+ XmlDtd xmlDtd = (XmlDtd) getCachedNodeOrCreate(context.getRuntime(), child);
492
+ xmlDtd.accept(context, visitor);
493
+ } else if (type == Node.PROCESSING_INSTRUCTION_NODE) {
494
+ XmlProcessingInstruction xmlProcessingInstruction = (XmlProcessingInstruction) getCachedNodeOrCreate(context.getRuntime(), child);
495
+ xmlProcessingInstruction.accept(context, visitor);
496
+ } else if (type == Node.TEXT_NODE) {
497
+ XmlText xmlText = (XmlText) getCachedNodeOrCreate(context.getRuntime(), child);
498
+ xmlText.accept(context, visitor);
499
+ } else if (type == Node.ELEMENT_NODE) {
500
+ XmlElement xmlElement = (XmlElement) getCachedNodeOrCreate(context.getRuntime(), child);
501
+ xmlElement.accept(context, visitor);
514
502
  }
515
-
516
- //ctx.append(" standalone=\"");
517
- //ctx.append(getDocument().getXmlStandalone() ? "yes" : "no");
518
- ctx.append("?>\n");
519
503
  }
520
-
521
- IRubyObject maybeRoot = root(context);
522
- if (maybeRoot.isNil())
523
- throw context.getRuntime().newRuntimeError("no root document");
524
-
525
- XmlNode root = (XmlNode) maybeRoot;
526
- root.saveContent(context, ctx);
527
- ctx.append("\n");
504
+ visitor.leave(document);
505
+ }
506
+
507
+ @JRubyMethod(meta=true)
508
+ public static IRubyObject wrapJavaDocument(ThreadContext context, IRubyObject klazz, IRubyObject arg) {
509
+ XmlDocument xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::Document"));
510
+ Document document = (Document)arg.toJava(Document.class);
511
+ xmlDocument.setNode(context, document);
512
+ return xmlDocument;
513
+ }
514
+
515
+ @JRubyMethod
516
+ public IRubyObject toJavaDocument(ThreadContext context) {
517
+ return JavaUtil.convertJavaToUsableRubyObject(context.getRuntime(), (org.w3c.dom.Document)node);
528
518
  }
529
519
  }