nokogiri 1.5.0.beta.1-java → 1.5.0.beta.2-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 (117) hide show
  1. data/CHANGELOG.ja.rdoc +28 -8
  2. data/CHANGELOG.rdoc +23 -0
  3. data/Manifest.txt +68 -1
  4. data/README.ja.rdoc +1 -1
  5. data/README.rdoc +22 -4
  6. data/Rakefile +6 -2
  7. data/ext/java/nokogiri/EncodingHandler.java +92 -0
  8. data/ext/java/nokogiri/HtmlDocument.java +116 -0
  9. data/ext/java/nokogiri/HtmlElementDescription.java +111 -0
  10. data/ext/java/nokogiri/HtmlEntityLookup.java +45 -0
  11. data/ext/java/nokogiri/HtmlSaxParserContext.java +218 -0
  12. data/ext/java/nokogiri/NokogiriService.java +370 -0
  13. data/ext/java/nokogiri/XmlAttr.java +147 -0
  14. data/ext/java/nokogiri/XmlAttributeDecl.java +98 -0
  15. data/ext/java/nokogiri/XmlCdata.java +50 -0
  16. data/ext/java/nokogiri/XmlComment.java +47 -0
  17. data/ext/java/nokogiri/XmlDocument.java +463 -0
  18. data/ext/java/nokogiri/XmlDocumentFragment.java +207 -0
  19. data/ext/java/nokogiri/XmlDtd.java +427 -0
  20. data/ext/java/nokogiri/XmlElement.java +172 -0
  21. data/ext/java/nokogiri/XmlElementContent.java +350 -0
  22. data/ext/java/nokogiri/XmlElementDecl.java +115 -0
  23. data/ext/java/nokogiri/XmlEntityDecl.java +129 -0
  24. data/ext/java/nokogiri/XmlEntityReference.java +42 -0
  25. data/ext/java/nokogiri/XmlNamespace.java +77 -0
  26. data/ext/java/nokogiri/XmlNode.java +1399 -0
  27. data/ext/java/nokogiri/XmlNodeSet.java +248 -0
  28. data/ext/java/nokogiri/XmlProcessingInstruction.java +70 -0
  29. data/ext/java/nokogiri/XmlReader.java +373 -0
  30. data/ext/java/nokogiri/XmlRelaxng.java +166 -0
  31. data/ext/java/nokogiri/XmlSaxParserContext.java +308 -0
  32. data/ext/java/nokogiri/XmlSaxPushParser.java +146 -0
  33. data/ext/java/nokogiri/XmlSchema.java +142 -0
  34. data/ext/java/nokogiri/XmlSyntaxError.java +84 -0
  35. data/ext/java/nokogiri/XmlText.java +96 -0
  36. data/ext/java/nokogiri/XmlXpathContext.java +130 -0
  37. data/ext/java/nokogiri/XsltStylesheet.java +126 -0
  38. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +181 -0
  39. data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +39 -0
  40. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +42 -0
  41. data/ext/java/nokogiri/internals/NokogiriHandler.java +251 -0
  42. data/ext/java/nokogiri/internals/NokogiriHelpers.java +526 -0
  43. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +136 -0
  44. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +80 -0
  45. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +37 -0
  46. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +54 -0
  47. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +49 -0
  48. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +88 -0
  49. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +23 -0
  50. data/ext/java/nokogiri/internals/ParserContext.java +235 -0
  51. data/ext/java/nokogiri/internals/PushInputStream.java +381 -0
  52. data/ext/java/nokogiri/internals/ReaderNode.java +431 -0
  53. data/ext/java/nokogiri/internals/SaveContext.java +249 -0
  54. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +35 -0
  55. data/ext/java/nokogiri/internals/XmlDeclHandler.java +10 -0
  56. data/ext/java/nokogiri/internals/XmlDomParser.java +45 -0
  57. data/ext/java/nokogiri/internals/XmlDomParserContext.java +201 -0
  58. data/ext/java/nokogiri/internals/XmlSaxParser.java +33 -0
  59. data/ext/nokogiri/depend +32 -0
  60. data/ext/nokogiri/extconf.rb +61 -32
  61. data/ext/nokogiri/libcharset-1.dll +0 -0
  62. data/ext/nokogiri/libexslt.dll +0 -0
  63. data/ext/nokogiri/libiconv-2.dll +0 -0
  64. data/ext/nokogiri/libxml2.dll +0 -0
  65. data/ext/nokogiri/libxslt.dll +0 -0
  66. data/ext/nokogiri/nokogiri.c +0 -5
  67. data/ext/nokogiri/nokogiri.h +2 -2
  68. data/ext/nokogiri/xml_document.c +5 -0
  69. data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
  70. data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
  71. data/ext/nokogiri/xml_node.c +56 -16
  72. data/ext/nokogiri/xml_node_set.c +7 -7
  73. data/ext/nokogiri/xml_reader.c +20 -1
  74. data/ext/nokogiri/xml_relax_ng.c +0 -7
  75. data/ext/nokogiri/xml_xpath_context.c +2 -0
  76. data/ext/nokogiri/zlib1.dll +0 -0
  77. data/lib/nokogiri.rb +1 -2
  78. data/lib/nokogiri/css/generated_parser.rb +155 -148
  79. data/lib/nokogiri/css/generated_tokenizer.rb +2 -1
  80. data/lib/nokogiri/css/parser.y +3 -0
  81. data/lib/nokogiri/css/xpath_visitor.rb +1 -7
  82. data/lib/nokogiri/html.rb +2 -2
  83. data/lib/nokogiri/html/document_fragment.rb +7 -4
  84. data/lib/nokogiri/nokogiri.jar +0 -0
  85. data/lib/nokogiri/version.rb +3 -6
  86. data/lib/nokogiri/xml/builder.rb +1 -1
  87. data/lib/nokogiri/xml/document.rb +1 -2
  88. data/lib/nokogiri/xml/document_fragment.rb +7 -0
  89. data/lib/nokogiri/xml/node.rb +5 -3
  90. data/lib/nokogiri/xml/node_set.rb +25 -0
  91. data/lib/nokogiri/xml/reader.rb +2 -0
  92. data/lib/nokogiri/xml/sax/document.rb +3 -1
  93. data/spec/helper.rb +3 -0
  94. data/spec/xml/reader_spec.rb +307 -0
  95. data/tasks/test.rb +1 -1
  96. data/test/css/test_parser.rb +11 -1
  97. data/test/html/sax/test_parser_context.rb +2 -2
  98. data/test/html/test_document.rb +2 -2
  99. data/test/html/test_document_fragment.rb +34 -6
  100. data/test/test_memory_leak.rb +2 -2
  101. data/test/test_reader.rb +28 -6
  102. data/test/test_xslt_transforms.rb +2 -3
  103. data/test/xml/test_attr.rb +31 -4
  104. data/test/xml/test_builder.rb +5 -5
  105. data/test/xml/test_cdata.rb +3 -3
  106. data/test/xml/test_document.rb +8 -8
  107. data/test/xml/test_document_fragment.rb +4 -12
  108. data/test/xml/test_node.rb +1 -1
  109. data/test/xml/test_node_reparenting.rb +26 -11
  110. data/test/xml/test_node_set.rb +38 -2
  111. data/test/xml/test_text.rb +11 -2
  112. data/test/xml/test_unparented_node.rb +1 -1
  113. data/test/xml/test_xpath.rb +11 -7
  114. metadata +159 -100
  115. data.tar.gz.sig +0 -0
  116. data/lib/nokogiri/version_warning.rb +0 -14
  117. metadata.gz.sig +0 -0
@@ -0,0 +1,166 @@
1
+ package nokogiri;
2
+
3
+ import java.io.ByteArrayInputStream;
4
+ import java.io.IOException;
5
+ import java.io.InputStream;
6
+ import java.io.StringWriter;
7
+ import java.io.UnsupportedEncodingException;
8
+
9
+ import javax.xml.transform.TransformerConfigurationException;
10
+ import javax.xml.transform.TransformerException;
11
+ import javax.xml.transform.TransformerFactory;
12
+ import javax.xml.transform.dom.DOMSource;
13
+ import javax.xml.transform.stream.StreamResult;
14
+ import javax.xml.transform.stream.StreamSource;
15
+
16
+ import nokogiri.internals.SchemaErrorHandler;
17
+
18
+ import org.iso_relax.verifier.Schema;
19
+ import org.iso_relax.verifier.Verifier;
20
+ import org.iso_relax.verifier.VerifierConfigurationException;
21
+ import org.iso_relax.verifier.VerifierFactory;
22
+ import org.jruby.Ruby;
23
+ import org.jruby.RubyArray;
24
+ import org.jruby.RubyClass;
25
+ import org.jruby.anno.JRubyClass;
26
+ import org.jruby.anno.JRubyMethod;
27
+ import org.jruby.runtime.ThreadContext;
28
+ import org.jruby.runtime.Visibility;
29
+ import org.jruby.runtime.builtin.IRubyObject;
30
+ import org.w3c.dom.Document;
31
+ import org.xml.sax.ErrorHandler;
32
+ import org.xml.sax.SAXException;
33
+
34
+ /**
35
+ *
36
+ * @author sergio
37
+ */
38
+ @JRubyClass(name="Nokogiri::XML::RelaxNG", parent="Nokogiri::XML::Schema")
39
+ public class XmlRelaxng extends XmlSchema{
40
+
41
+ public XmlRelaxng(Ruby ruby, RubyClass klazz) {
42
+ super(ruby, klazz);
43
+ }
44
+
45
+ private Schema getSchema(ThreadContext context) {
46
+ InputStream is = null;
47
+ VerifierFactory factory = new com.thaiopensource.relaxng.jarv.VerifierFactoryImpl();
48
+ if(this.source instanceof StreamSource) {
49
+ StreamSource ss = (StreamSource) this.source;
50
+ is = ss.getInputStream();
51
+ } else /*if (this.source instanceof DOMSource)*/{
52
+ DOMSource ds = (DOMSource) this.source;
53
+ StringWriter xmlAsWriter = new StringWriter();
54
+ StreamResult result = new StreamResult(xmlAsWriter);
55
+ try {
56
+ TransformerFactory.newInstance().newTransformer().transform(ds, result);
57
+ } catch (TransformerConfigurationException ex) {
58
+ throw context.getRuntime()
59
+ .newRuntimeError("Could not parse document: "+ex.getMessage());
60
+ } catch (TransformerException ex) {
61
+ throw context.getRuntime()
62
+ .newRuntimeError("Could not parse document: "+ex.getMessage());
63
+ }
64
+ try {
65
+ is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
66
+ } catch (UnsupportedEncodingException ex) {
67
+ throw context.getRuntime()
68
+ .newRuntimeError("Could not parse document: "+ex.getMessage());
69
+ }
70
+ }
71
+
72
+ try {
73
+ return factory.compileSchema(is);
74
+ } catch (VerifierConfigurationException ex) {
75
+ throw context.getRuntime()
76
+ .newRuntimeError("Could not parse document: "+ex.getMessage());
77
+ } catch (SAXException ex) {
78
+ throw context.getRuntime()
79
+ .newRuntimeError("Could not parse document: "+ex.getMessage());
80
+ } catch (IOException ex) {
81
+ throw context.getRuntime().newIOError(ex.getMessage());
82
+ }
83
+ }
84
+
85
+ //
86
+ // protected static XmlSchema createSchemaFromSource(ThreadContext context,
87
+ // IRubyObject klazz, Source source) {
88
+ //
89
+ // Ruby ruby = context.getRuntime();
90
+ //
91
+ // XmlSchema schema = new XmlSchema(ruby, (RubyClass) klazz);
92
+ //
93
+ // try {
94
+ // schema.schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
95
+ // .newSchema(source);
96
+ // } catch(SAXException ex) {
97
+ // throw ruby.newRuntimeError("Could not parse document: "+ex.getMessage());
98
+ // }
99
+ //
100
+ // schema.setInstanceVariable("@errors", ruby.newEmptyArray());
101
+ //
102
+ // return schema;
103
+ // }
104
+ //
105
+ // @JRubyMethod(meta=true)
106
+ // public static IRubyObject from_document(ThreadContext context,
107
+ // IRubyObject klazz, IRubyObject document) {
108
+ // XmlDocument doc = ((XmlDocument) ((XmlNode) document).document(context));
109
+ //
110
+ // RubyArray errors = (RubyArray) doc.getInstanceVariable("@errors");
111
+ //
112
+ // if(!errors.isEmpty()) {
113
+ // throw new RaiseException((XmlSyntaxError) errors.first());
114
+ // }
115
+ //
116
+ // DOMSource source = new DOMSource(doc.getDocument());
117
+ //
118
+ // IRubyObject uri = doc.url(context);
119
+ //
120
+ // if(!uri.isNil()) {
121
+ // source.setSystemId(uri.convertToString().asJavaString());
122
+ // }
123
+ //
124
+ // return createSchemaFromSource(context, klazz, source);
125
+ // }
126
+ //
127
+ // @JRubyMethod(meta=true)
128
+ // public static IRubyObject read_memory(ThreadContext context,
129
+ // IRubyObject klazz, IRubyObject content) {
130
+ //
131
+ // String data = content.convertToString().asJavaString();
132
+ //
133
+ // return createSchemaFromSource(context, klazz,
134
+ // new StreamSource(new StringReader(data)));
135
+ // }
136
+ //
137
+ @Override
138
+ @JRubyMethod(visibility=Visibility.PRIVATE)
139
+ public IRubyObject validate_document(ThreadContext context, IRubyObject document) {
140
+ Ruby ruby = context.getRuntime();
141
+
142
+ Document doc = ((XmlDocument) document).getDocument();
143
+
144
+ Schema schema = this.getSchema(context);
145
+
146
+ Verifier verifier;
147
+ try {
148
+ verifier = schema.newVerifier();
149
+ } catch (VerifierConfigurationException ex) {
150
+ throw context.getRuntime()
151
+ .newRuntimeError("Could not parse document: "+ex.getMessage());
152
+ }
153
+
154
+ RubyArray errors = (RubyArray) this.getInstanceVariable("@errors");
155
+ ErrorHandler errorHandler = new SchemaErrorHandler(ruby, errors);
156
+
157
+ verifier.setErrorHandler(errorHandler);
158
+ try {
159
+ verifier.verify(doc);
160
+ } catch (SAXException ex) {
161
+ errors.append(new XmlSyntaxError(ruby, ex));
162
+ }
163
+
164
+ return errors;
165
+ }
166
+ }
@@ -0,0 +1,308 @@
1
+ package nokogiri;
2
+
3
+ import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
4
+ import static org.jruby.javasupport.util.RuntimeHelpers.invoke;
5
+
6
+ import java.io.IOException;
7
+ import java.io.InputStream;
8
+
9
+ import nokogiri.internals.NokogiriHandler;
10
+ import nokogiri.internals.ParserContext;
11
+ import nokogiri.internals.XmlSaxParser;
12
+
13
+ import org.apache.xerces.parsers.AbstractSAXParser;
14
+ import org.jruby.Ruby;
15
+ import org.jruby.RubyClass;
16
+ import org.jruby.RubyModule;
17
+ import org.jruby.RubyObjectAdapter;
18
+ import org.jruby.anno.JRubyClass;
19
+ import org.jruby.anno.JRubyMethod;
20
+ import org.jruby.exceptions.RaiseException;
21
+ import org.jruby.javasupport.JavaEmbedUtils;
22
+ import org.jruby.runtime.ThreadContext;
23
+ import org.jruby.runtime.builtin.IRubyObject;
24
+ import org.xml.sax.ContentHandler;
25
+ import org.xml.sax.ErrorHandler;
26
+ import org.xml.sax.SAXException;
27
+ import org.xml.sax.SAXNotRecognizedException;
28
+ import org.xml.sax.SAXNotSupportedException;
29
+ import org.xml.sax.SAXParseException;
30
+
31
+ /**
32
+ * Base class for the SAX parsers.
33
+ *
34
+ * @author Patrick Mahoney <pat@polycrystal.org>
35
+ */
36
+ @JRubyClass(name="Nokogiri::XML::SAX::ParserContext")
37
+ public class XmlSaxParserContext extends ParserContext {
38
+ protected static final String FEATURE_NAMESPACES =
39
+ "http://xml.org/sax/features/namespaces";
40
+ protected static final String FEATURE_NAMESPACE_PREFIXES =
41
+ "http://xml.org/sax/features/namespace-prefixes";
42
+ protected static final String FEATURE_LOAD_EXTERNAL_DTD =
43
+ "http://apache.org/xml/features/nonvalidating/load-external-dtd";
44
+
45
+ protected AbstractSAXParser parser;
46
+
47
+ protected NokogiriHandler handler = null;
48
+
49
+ public XmlSaxParserContext(final Ruby ruby, RubyClass rubyClass) {
50
+ super(ruby, rubyClass);
51
+ try {
52
+ parser = createParser();
53
+ } catch (SAXException se) {
54
+ throw RaiseException.createNativeRaiseException(ruby, se);
55
+ }
56
+ }
57
+
58
+ protected AbstractSAXParser createParser() throws SAXException {
59
+ XmlSaxParser parser = new XmlSaxParser();
60
+ parser.setFeature(FEATURE_NAMESPACE_PREFIXES, true);
61
+ parser.setFeature(FEATURE_LOAD_EXTERNAL_DTD, false);
62
+ return parser;
63
+ }
64
+
65
+ /**
66
+ * Create a new parser context that will parse the string
67
+ * <code>data</code>.
68
+ */
69
+ @JRubyMethod(name="memory", meta=true)
70
+ public static IRubyObject parse_memory(ThreadContext context,
71
+ IRubyObject klazz,
72
+ IRubyObject data) {
73
+ XmlSaxParserContext ctx = new XmlSaxParserContext(context.getRuntime(),
74
+ (RubyClass) klazz);
75
+ ctx.setInputSource(context, data);
76
+ return ctx;
77
+ }
78
+
79
+ /**
80
+ * Create a new parser context that will read from the file
81
+ * <code>data</code> and parse.
82
+ */
83
+ @JRubyMethod(name="file", meta=true)
84
+ public static IRubyObject parse_file(ThreadContext context,
85
+ IRubyObject klazz,
86
+ IRubyObject data) {
87
+ XmlSaxParserContext ctx = new XmlSaxParserContext(context.getRuntime(),
88
+ (RubyClass) klazz);
89
+ ctx.setInputSourceFile(context, data);
90
+ return ctx;
91
+ }
92
+
93
+ /**
94
+ * Create a new parser context that will read from the IO or
95
+ * StringIO <code>data</code> and parse.
96
+ *
97
+ * TODO: Currently ignores encoding <code>enc</code>.
98
+ */
99
+ @JRubyMethod(name="io", meta=true)
100
+ public static IRubyObject parse_io(ThreadContext context,
101
+ IRubyObject klazz,
102
+ IRubyObject data,
103
+ IRubyObject enc) {
104
+ //int encoding = (int)enc.convertToInteger().getLongValue();
105
+ XmlSaxParserContext ctx = new XmlSaxParserContext(context.getRuntime(),
106
+ (RubyClass) klazz);
107
+ ctx.setInputSource(context, data);
108
+ return ctx;
109
+ }
110
+
111
+ /**
112
+ * Create a new parser context that will read from a raw input
113
+ * stream. Not a JRuby method. Meant to be run in a separate
114
+ * thread by XmlSaxPushParser.
115
+ */
116
+ public static IRubyObject parse_stream(ThreadContext context,
117
+ IRubyObject klazz,
118
+ InputStream stream) {
119
+ XmlSaxParserContext ctx =
120
+ new XmlSaxParserContext(context.getRuntime(), (RubyClass)klazz);
121
+ ctx.setInputSource(stream);
122
+ return ctx;
123
+ }
124
+
125
+ /**
126
+ * Set a property of the underlying parser.
127
+ */
128
+ protected void setProperty(String key, Object val)
129
+ throws SAXNotRecognizedException, SAXNotSupportedException {
130
+ parser.setProperty(key, val);
131
+ }
132
+
133
+ protected void setContentHandler(ContentHandler handler) {
134
+ parser.setContentHandler(handler);
135
+ }
136
+
137
+ protected void setErrorHandler(ErrorHandler handler) {
138
+ parser.setErrorHandler(handler);
139
+ }
140
+
141
+ public NokogiriHandler getNokogiriHandler() {
142
+ return handler;
143
+ }
144
+
145
+ /**
146
+ * Perform any initialization prior to parsing with the handler
147
+ * <code>handlerRuby</code>. Convenience hook for subclasses.
148
+ */
149
+ protected void preParse(ThreadContext context,
150
+ IRubyObject handlerRuby,
151
+ NokogiriHandler handler) {
152
+ ((XmlSaxParser) parser).setXmlDeclHandler(handler);
153
+ }
154
+
155
+ protected void postParse(ThreadContext context,
156
+ IRubyObject handlerRuby,
157
+ NokogiriHandler handler) {
158
+ // noop
159
+ }
160
+
161
+ protected void do_parse() throws SAXException, IOException {
162
+ parser.parse(getInputSource());
163
+ }
164
+
165
+ @JRubyMethod
166
+ public IRubyObject parse_with(ThreadContext context,
167
+ IRubyObject handlerRuby) {
168
+ Ruby ruby = context.getRuntime();
169
+
170
+ if(!invoke(context, handlerRuby, "respond_to?",
171
+ ruby.newSymbol("document")).isTrue()) {
172
+ String msg = "argument must respond_to document";
173
+ throw ruby.newArgumentError(msg);
174
+ }
175
+
176
+ handler = new NokogiriHandler(ruby, handlerRuby);
177
+ preParse(context, handlerRuby, handler);
178
+
179
+ setContentHandler(handler);
180
+ setErrorHandler(handler);
181
+
182
+ try{
183
+ setProperty("http://xml.org/sax/properties/lexical-handler",
184
+ handler);
185
+ } catch(Exception ex) {
186
+ throw ruby.newRuntimeError(
187
+ "Problem while creating XML SAX Parser: " + ex.toString());
188
+ }
189
+
190
+ try{
191
+ try {
192
+ do_parse();
193
+ } catch(SAXParseException spe) {
194
+ // A bad document (<foo><bar></foo>) should call the
195
+ // error handler instead of raising a SAX exception.
196
+
197
+ // However, an EMPTY document should raise a
198
+ // RuntimeError. This is a bit kludgy, but AFAIK SAX
199
+ // doesn't distinguish between empty and bad whereas
200
+ // Nokogiri does.
201
+ String message = spe.getMessage();
202
+ if ("Premature end of file.".matches(message)) {
203
+ throw ruby.newRuntimeError(
204
+ "couldn't parse document: " + message);
205
+ } else {
206
+ handler.error(spe);
207
+ }
208
+
209
+ }
210
+ } catch(SAXException se) {
211
+ throw RaiseException.createNativeRaiseException(ruby, se);
212
+ } catch(IOException ioe) {
213
+ throw ruby.newIOErrorFromException(ioe);
214
+ }
215
+
216
+ postParse(context, handlerRuby, handler);
217
+
218
+ //maybeTrimLeadingAndTrailingWhitespace(context, handlerRuby);
219
+
220
+ return ruby.getNil();
221
+ }
222
+
223
+ /**
224
+ * Can take a boolean assignment.
225
+ *
226
+ * @param context
227
+ * @param value
228
+ * @return
229
+ */
230
+ @JRubyMethod(name = "replace_entities=")
231
+ public IRubyObject set_replace_entities(ThreadContext context,
232
+ IRubyObject value) {
233
+ if (!value.isTrue()) {
234
+ throw context.getRuntime()
235
+ .newRuntimeError("Not replacing entities is unsupported");
236
+ }
237
+
238
+ return this;
239
+ }
240
+
241
+ @JRubyMethod(name="replace_entities")
242
+ public IRubyObject get_replace_entities(ThreadContext context,
243
+ IRubyObject value) {
244
+ return context.getRuntime().getTrue();
245
+ }
246
+
247
+
248
+ /**
249
+ * If the handler's document is a FragmentHandler, attempt to trim
250
+ * leading and trailing whitespace.
251
+ *
252
+ * This is a bit hackish and depends heavily on the internals of
253
+ * FragmentHandler.
254
+ */
255
+ protected void maybeTrimLeadingAndTrailingWhitespace(ThreadContext context,
256
+ IRubyObject parser) {
257
+ final String path = "Nokogiri::XML::FragmentHandler";
258
+ RubyObjectAdapter adapter = JavaEmbedUtils.newObjectAdapter();
259
+ RubyModule mod =
260
+ context.getRuntime().getClassFromPath(path);
261
+
262
+ IRubyObject handler = adapter.getInstanceVariable(parser, "@document");
263
+ if (handler == null || handler.isNil() || !adapter.isKindOf(handler, mod))
264
+ return;
265
+ IRubyObject stack = adapter.getInstanceVariable(handler, "@stack");
266
+ if (stack == null || stack.isNil())
267
+ return;
268
+ // doc is finally a DocumentFragment whose nodes we can check
269
+ IRubyObject doc = adapter.callMethod(stack, "first");
270
+ if (doc == null || doc.isNil())
271
+ return;
272
+
273
+ IRubyObject children;
274
+
275
+ for (;;) {
276
+ children = adapter.callMethod(doc, "children");
277
+ IRubyObject first = adapter.callMethod(children, "first");
278
+ if (isWhitespaceText(context, first))
279
+ adapter.callMethod(first, "unlink");
280
+ else
281
+ break;
282
+ }
283
+
284
+ for (;;) {
285
+ children = adapter.callMethod(doc, "children");
286
+ IRubyObject last = adapter.callMethod(children, "last");
287
+ if (isWhitespaceText(context, last))
288
+ adapter.callMethod(last, "unlink");
289
+ else
290
+ break;
291
+ }
292
+
293
+ // While we have a document, normalize it.
294
+ ((XmlNode) doc).normalize();
295
+ }
296
+
297
+ protected boolean isWhitespaceText(ThreadContext context, IRubyObject obj) {
298
+ if (obj == null || obj.isNil()) return false;
299
+
300
+ XmlNode node = (XmlNode) obj;
301
+ if (!(node instanceof XmlText))
302
+ return false;
303
+
304
+ String content = rubyStringToString(node.content(context));
305
+ return content.trim().length() == 0;
306
+ }
307
+
308
+ }