smile-xml 1.0.0-jruby → 1.0.2-jruby

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data/lib/smile-xml.jar +0 -0
  2. data/src/main/java/smile/xml/AttrJ.java +160 -0
  3. data/src/main/java/smile/xml/AttributesJ.java +212 -0
  4. data/src/main/java/smile/xml/BaseJ.java +60 -0
  5. data/src/main/java/smile/xml/DocumentJ.java +435 -0
  6. data/src/main/java/smile/xml/EncodingJ.java +94 -0
  7. data/src/main/java/smile/xml/ErrorJ.java +162 -0
  8. data/src/main/java/smile/xml/NamespaceJ.java +67 -0
  9. data/src/main/java/smile/xml/NamespacesJ.java +62 -0
  10. data/src/main/java/smile/xml/NodeJ.java +1029 -0
  11. data/src/main/java/smile/xml/NodeSetJ.java +90 -0
  12. data/src/main/java/smile/xml/ParserContextJ.java +44 -0
  13. data/src/main/java/smile/xml/ParserJ.java +196 -0
  14. data/src/main/java/smile/xml/ParserOptionsJ.java +58 -0
  15. data/src/main/java/smile/xml/ReaderJ.java +34 -0
  16. data/src/main/java/smile/xml/SchemaJ.java +66 -0
  17. data/src/main/java/smile/xml/SmileXML.java +65 -0
  18. data/src/main/java/smile/xml/XmlJ.java +58 -0
  19. data/src/main/java/smile/xml/sax/CallbackHandler.java +113 -0
  20. data/src/main/java/smile/xml/sax/SaxParserCallbacksJ.java +71 -0
  21. data/src/main/java/smile/xml/sax/SaxParserJ.java +153 -0
  22. data/src/main/java/smile/xml/util/UtilJ.java +424 -0
  23. data/src/main/java/smile/xml/xpath/CustomNamespaceContext.java +59 -0
  24. data/src/main/java/smile/xml/xpath/XPathContextJ.java +154 -0
  25. data/src/main/java/smile/xml/xpath/XPathExpressionJ.java +62 -0
  26. data/src/main/java/smile/xml/xpath/XPathJ.java +36 -0
  27. data/src/main/java/smile/xml/xpath/XPathObjectJ.java +196 -0
  28. data/src/main/java/smile/xml/xpath/XPointerJ.java +32 -0
  29. data/src/main/ruby/xml/libxml.rb +1 -0
  30. data/src/main/ruby/xml.rb +5 -0
  31. data/src/test/ruby/etc_doc_to_s.rb +21 -0
  32. data/src/test/ruby/ets_doc_file.rb +17 -0
  33. data/src/test/ruby/ets_doc_to_s.rb +23 -0
  34. data/src/test/ruby/ets_gpx.rb +28 -0
  35. data/src/test/ruby/ets_node_gc.rb +23 -0
  36. data/src/test/ruby/ets_test.xml +2 -0
  37. data/src/test/ruby/ets_tsr.rb +11 -0
  38. data/src/test/ruby/model/atom.xml +13 -0
  39. data/src/test/ruby/model/bands.iso-8859-1.xml +5 -0
  40. data/src/test/ruby/model/bands.utf-8.xml +5 -0
  41. data/src/test/ruby/model/bands.xml +5 -0
  42. data/src/test/ruby/model/books.xml +146 -0
  43. data/src/test/ruby/model/merge_bug_data.xml +58 -0
  44. data/src/test/ruby/model/ruby-lang.html +238 -0
  45. data/src/test/ruby/model/rubynet.xml +79 -0
  46. data/src/test/ruby/model/shiporder.rnc +28 -0
  47. data/src/test/ruby/model/shiporder.rng +86 -0
  48. data/src/test/ruby/model/shiporder.xml +23 -0
  49. data/src/test/ruby/model/shiporder.xsd +31 -0
  50. data/src/test/ruby/model/soap.xml +27 -0
  51. data/src/test/ruby/model/xinclude.xml +5 -0
  52. data/src/test/ruby/smile_xml_test.rb +64 -0
  53. data/src/test/ruby/tc_attr.rb +191 -0
  54. data/src/test/ruby/tc_attr_decl.rb +133 -0
  55. data/src/test/ruby/tc_attributes.rb +135 -0
  56. data/src/test/ruby/tc_deprecated_require.rb +13 -0
  57. data/src/test/ruby/tc_document.rb +162 -0
  58. data/src/test/ruby/tc_document_write.rb +212 -0
  59. data/src/test/ruby/tc_dtd.rb +125 -0
  60. data/src/test/ruby/tc_error.rb +150 -0
  61. data/src/test/ruby/tc_html_parser.rb +140 -0
  62. data/src/test/ruby/tc_namespace.rb +62 -0
  63. data/src/test/ruby/tc_namespaces.rb +210 -0
  64. data/src/test/ruby/tc_node.rb +273 -0
  65. data/src/test/ruby/tc_node_cdata.rb +51 -0
  66. data/src/test/ruby/tc_node_comment.rb +33 -0
  67. data/src/test/ruby/tc_node_copy.rb +42 -0
  68. data/src/test/ruby/tc_node_edit.rb +178 -0
  69. data/src/test/ruby/tc_node_text.rb +73 -0
  70. data/src/test/ruby/tc_node_write.rb +108 -0
  71. data/src/test/ruby/tc_node_xlink.rb +29 -0
  72. data/src/test/ruby/tc_parser.rb +371 -0
  73. data/src/test/ruby/tc_parser_context.rb +189 -0
  74. data/src/test/ruby/tc_properties.rb +40 -0
  75. data/src/test/ruby/tc_reader.rb +306 -0
  76. data/src/test/ruby/tc_relaxng.rb +54 -0
  77. data/src/test/ruby/tc_sax_parser.rb +340 -0
  78. data/src/test/ruby/tc_schema.rb +59 -0
  79. data/src/test/ruby/tc_traversal.rb +222 -0
  80. data/src/test/ruby/tc_xinclude.rb +21 -0
  81. data/src/test/ruby/tc_xml.rb +226 -0
  82. data/src/test/ruby/tc_xpath.rb +210 -0
  83. data/src/test/ruby/tc_xpath_context.rb +80 -0
  84. data/src/test/ruby/tc_xpath_expression.rb +38 -0
  85. data/src/test/ruby/tc_xpointer.rb +74 -0
  86. data/src/test/ruby/test_helper.rb +23 -0
  87. data/src/test/ruby/test_suite.rb +41 -0
  88. metadata +91 -5
@@ -0,0 +1,1029 @@
1
+ package smile.xml;
2
+
3
+ import java.util.ArrayList;
4
+ import java.util.List;
5
+
6
+ import org.jruby.Ruby;
7
+ import org.jruby.RubyArray;
8
+ import org.jruby.RubyBoolean;
9
+ import org.jruby.RubyClass;
10
+ import org.jruby.RubyFixnum;
11
+ import org.jruby.RubyHash;
12
+ import org.jruby.RubyNil;
13
+ import org.jruby.RubyString;
14
+ import org.jruby.RubySymbol;
15
+ import org.jruby.anno.JRubyClass;
16
+ import org.jruby.anno.JRubyConstant;
17
+ import org.jruby.anno.JRubyMethod;
18
+ import org.jruby.runtime.Block;
19
+ import org.jruby.runtime.ObjectAllocator;
20
+ import org.jruby.runtime.ThreadContext;
21
+ import org.jruby.runtime.builtin.IRubyObject;
22
+ import org.w3c.dom.Attr;
23
+ import org.w3c.dom.Document;
24
+ import org.w3c.dom.Element;
25
+ import org.w3c.dom.NamedNodeMap;
26
+ import org.w3c.dom.Node;
27
+ import org.w3c.dom.NodeList;
28
+ import org.w3c.dom.Text;
29
+
30
+ import smile.xml.util.UtilJ;
31
+ import smile.xml.xpath.XPathContextJ;
32
+ import smile.xml.xpath.XPathObjectJ;
33
+ import smile.xml.xpath.XPointerJ;
34
+
35
+ @JRubyClass(name = "LibXML::XML::Node")
36
+ public class NodeJ extends BaseJ<Node> {
37
+
38
+ private static final long serialVersionUID = -7355585454179020932L;
39
+
40
+ private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
41
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
42
+ return new NodeJ(runtime, klass);
43
+ }
44
+ };
45
+
46
+ @JRubyConstant
47
+ public static final int ATTRIBUTE_NODE = 2;
48
+
49
+ @JRubyConstant
50
+ public static final int TEXT_NODE = 3;
51
+
52
+ @JRubyConstant
53
+ public static final int CDATA_SECTION_NODE = Node.CDATA_SECTION_NODE;
54
+
55
+ @JRubyConstant
56
+ public static final int COMMENT_NODE = 8;
57
+
58
+ @JRubyConstant
59
+ public static final int DOCUMENT_FRAG_NODE = 11;
60
+
61
+ @JRubyConstant
62
+ public static final int DOCUMENT_NODE = 9;
63
+
64
+ @JRubyConstant
65
+ public static final int DOCUMENT_TYPE_NODE = 10;
66
+
67
+ @JRubyConstant
68
+ public static final int ELEMENT_NODE = 1;
69
+
70
+ @JRubyConstant
71
+ public static final int ENTITY_NODE = 6;
72
+
73
+ @JRubyConstant
74
+ public static final int ENTITY_REFERENCE_NODE = 5;
75
+
76
+ @JRubyConstant
77
+ public static final int NOTATION_NODE = 12;
78
+
79
+ @JRubyConstant
80
+ public static final int PROCESSING_INSTRUCTION_NODE = 7;
81
+
82
+
83
+ @JRubyConstant
84
+ public static final int FRAGMENT_NODE = 11;
85
+
86
+ // TODO
87
+ @JRubyConstant
88
+ public static final int SPACE_DEFAULT = -1;
89
+
90
+ // TODO
91
+ @JRubyConstant
92
+ public static final int SPACE_PRESERVE = -1;
93
+
94
+ public static RubyClass define(Ruby runtime) {
95
+ return UtilJ.defineClass(runtime, NodeJ.class, ALLOCATOR);
96
+ }
97
+
98
+ private static RubyClass getRubyClass(Ruby runtime) {
99
+ return UtilJ.getClass(runtime, NodeJ.class );
100
+ }
101
+
102
+ public static NodeJ newInstance(ThreadContext context) {
103
+ return (NodeJ) getRubyClass(context.getRuntime()).newInstance(context,
104
+ new IRubyObject[0], null);
105
+ }
106
+
107
+ public static NodeJ newInstance(ThreadContext context, IRubyObject name,
108
+ IRubyObject content, IRubyObject namespace) {
109
+ return (NodeJ) getRubyClass(context.getRuntime()).newInstance(context,
110
+ new IRubyObject[] { name, content, namespace }, null);
111
+ }
112
+
113
+ private boolean docPresent = false;
114
+
115
+ private boolean outputEscaping = true;
116
+
117
+ private boolean noCData = false;
118
+
119
+ protected NodeJ(Ruby ruby, RubyClass clazz) {
120
+ super(ruby, clazz);
121
+ }
122
+
123
+ public boolean isDocPresent() {
124
+ return docPresent;
125
+ }
126
+
127
+ @JRubyMethod(name = { "initialize" }, optional = 3)
128
+ public void initialize(ThreadContext context, IRubyObject[] args) {
129
+ RubyString name = (RubyString) (RubyString) (args.length > 0 ? args[0] : null);
130
+ RubyString content = (RubyString) (RubyString) (args.length > 1 ? args[1] : null);
131
+ NamespaceJ namespace = (NamespaceJ) (NamespaceJ) (args.length > 2 ? args[2] : null);
132
+
133
+ if (name != null) {
134
+ Document doc = UtilJ.getBuilder().newDocument();
135
+ Element elemet = doc.createElement(name.asJavaString());
136
+ if ((content != null) && (!content.isNil()))
137
+ elemet.setTextContent(content.asJavaString());
138
+ setJavaObject(elemet);
139
+ }
140
+ }
141
+
142
+ @JRubyMethod(name = { "new_cdata" }, module = true)
143
+ public static NodeJ newCdata(ThreadContext context, IRubyObject klass,
144
+ IRubyObject pContent) {
145
+ RubyString content = (RubyString) pContent;
146
+ Document doc = UtilJ.getBuilder().newDocument();
147
+ NodeJ node = newInstance(context);
148
+ node.setJavaObject(doc.createCDATASection(content.asJavaString()));
149
+ return node;
150
+ }
151
+
152
+ @JRubyMethod(name = { "new_comment" }, module = true)
153
+ public static NodeJ newComment(ThreadContext context, IRubyObject klass,
154
+ IRubyObject pContent) {
155
+ RubyString content = (RubyString) pContent;
156
+ Document doc = UtilJ.getBuilder().newDocument();
157
+ NodeJ node = newInstance(context);
158
+ node.setJavaObject(doc.createComment(content.asJavaString()));
159
+ return node;
160
+ }
161
+
162
+ @JRubyMethod(name="new_text", module = true)
163
+ public static NodeJ newText(ThreadContext context, IRubyObject klass,
164
+ IRubyObject pContent) {
165
+
166
+ if( (pContent instanceof RubyString) == false )
167
+ throw context.getRuntime().newTypeError("wrong argument type nil (expected String)");
168
+
169
+ RubyString content = (RubyString) pContent;
170
+ Document doc = UtilJ.getBuilder().newDocument();
171
+ NodeJ node = newInstance(context);
172
+ node.setJavaObject(doc.createTextNode(content.asJavaString()));
173
+ return node;
174
+ }
175
+
176
+ @JRubyMethod(name ="<<", alias = { "child_add" })
177
+ public NodeJ add(ThreadContext context, IRubyObject arg) throws Exception {
178
+ if(arg instanceof NodeJ) {
179
+ NodeJ other = (NodeJ) arg;
180
+ Node node = other.getJavaObject();
181
+
182
+ if( isDocPresent() && other.isDocPresent() && getJavaObject().getOwnerDocument().equals( node.getOwnerDocument()) == false )
183
+ throw ErrorJ.newRaiseException(context," Nodes belong to different documents. You must first import the node by calling XML::Document.import.");
184
+
185
+ node = getJavaObject().getOwnerDocument().adoptNode(node);
186
+ getJavaObject().appendChild(node);
187
+
188
+ } else if ((arg instanceof RubyString)) {
189
+ String s = getJavaObject().getNodeValue() == null ? "" : getJavaObject().getNodeValue();
190
+
191
+ s = s + arg.asJavaString();
192
+
193
+ System.out.println( getJavaObject().getNodeType() );
194
+ short t = getJavaObject().getNodeType();
195
+ if( t == Node.TEXT_NODE || t == Node.CDATA_SECTION_NODE || t == Node.COMMENT_NODE ) {
196
+ getJavaObject().setTextContent(s);
197
+ } else {
198
+ Text node = getJavaObject().getOwnerDocument().createTextNode( s );
199
+ getJavaObject().appendChild(node);
200
+ }
201
+ //getJavaObject().setTextContent(s);
202
+ } else {
203
+ throw context.getRuntime().newArgumentError("unsupported argument");
204
+ }
205
+
206
+ // System.out.println("NodeJ.add()");
207
+ // System.out.println( toString(context, new IRubyObject[0]).asJavaString() );
208
+
209
+ return this;
210
+ }
211
+
212
+ @JRubyMethod(name = "equal?")
213
+ public RubyBoolean isEqlual(ThreadContext context, IRubyObject arg)
214
+ throws Exception {
215
+
216
+ if (arg.isNil()) {
217
+ return context.getRuntime().getFalse();
218
+ }
219
+
220
+ NodeJ other = (NodeJ) arg;
221
+
222
+ if (((Node) other.getJavaObject()).equals(getJavaObject())) {
223
+ return context.getRuntime().getTrue();
224
+ }
225
+
226
+ return context.getRuntime().getFalse();
227
+ }
228
+
229
+ @JRubyMethod(name = { "eql?" }, alias = { "==" })
230
+ public RubyBoolean isEql(ThreadContext context, IRubyObject arg)
231
+ throws Exception {
232
+
233
+ if (arg.isNil()) {
234
+ return context.getRuntime().getFalse();
235
+ }
236
+
237
+ if (!(arg instanceof NodeJ)) {
238
+ throw context.getRuntime().newTypeError("");
239
+ }
240
+
241
+ NodeJ other = (NodeJ) arg;
242
+
243
+ if (((Node) other.getJavaObject()).equals(getJavaObject())) {
244
+ return context.getRuntime().getTrue();
245
+ }
246
+
247
+ // if ( other.getJavaObject().getOwnerDocument().equals(
248
+ // getJavaObject().getOwnerDocument() ) == false ) {
249
+ // return context.getRuntime().getFalse();
250
+ // }
251
+
252
+ boolean r = toString(context, new IRubyObject[0]).equals(
253
+ other.toString(context, new IRubyObject[0]));
254
+
255
+ return r ? context.getRuntime().getTrue() : context.getRuntime()
256
+ .getFalse();
257
+ }
258
+
259
+ @JRubyMethod(name ="detect")
260
+ public IRubyObject detect(ThreadContext context, Block block) {
261
+ if( block.isGiven() == false )
262
+ throw context.getRuntime().newArgumentError("");
263
+
264
+ NodeList childs = getJavaObject().getChildNodes();
265
+ for( int i=0; i<childs.getLength(); i++ ) {
266
+ Node item = childs.item(i);
267
+ if( item.getNodeType() != Node.ELEMENT_NODE )
268
+ continue;
269
+ NodeJ node = newInstance(context);
270
+ node.setDocPresent( isDocPresent() );
271
+ node.noCData = noCData;
272
+ node.outputEscaping = outputEscaping;
273
+ node.setJavaObject( item );
274
+ IRubyObject[] args = new IRubyObject[]{node};
275
+ IRubyObject result = block.call( context, args, null );
276
+ if( result.isTrue() )
277
+ return node;
278
+ }
279
+ return context.getRuntime().getNil();
280
+ }
281
+
282
+ @JRubyMethod(name ="base=", alias={"base_uri="})
283
+ public void setBase(ThreadContext context, IRubyObject pBase) {
284
+ if( getJavaObject().getAttributes() != null ) {
285
+ Document doc = getJavaObject().getOwnerDocument();
286
+ Attr attr = doc.createAttribute( "xml:base");
287
+ attr.setNodeValue( pBase.asJavaString() );
288
+ getJavaObject().getAttributes().setNamedItem(attr);
289
+
290
+ } else {
291
+ Document doc = getJavaObject().getOwnerDocument();
292
+ Attr attr = doc.createAttributeNS("xml", "base");
293
+ getJavaObject().appendChild(attr);
294
+ }
295
+ }
296
+
297
+ @JRubyMethod(name ="[]", alias = { "property" })
298
+ public IRubyObject getProperty(ThreadContext context, IRubyObject pName) {
299
+ return getAttributes(context).getValue(context, pName);
300
+ }
301
+
302
+ @JRubyMethod(name = { "[]=" })
303
+ public void setProperty(ThreadContext context, IRubyObject pName,
304
+ IRubyObject pValue) {
305
+ if ((pName instanceof RubySymbol)) {
306
+ pName = ((RubySymbol) pName).asString();
307
+ }
308
+ RubyString name = (RubyString) pName;
309
+ RubyString value = (RubyString) pValue;
310
+
311
+ if ((getJavaObject() instanceof Element)) {
312
+ ((Element) getJavaObject()).setAttribute(name.asJavaString(),
313
+ value.asJavaString());
314
+ } else {
315
+ throw context.getRuntime().newArgumentError("");
316
+ }
317
+ }
318
+
319
+ @JRubyMethod(name="base", alias={"base_uri"} )
320
+ public IRubyObject getBase(ThreadContext context) {
321
+ // Node item = getJavaObject().getAttributes().getNamedItemNS("xml", "base");
322
+ //
323
+ // if (item == null)
324
+ // return context.getRuntime().getNil();
325
+ // AttrJ attr = AttrJ.newInstance(context);
326
+ // attr.setParent( this );
327
+ // attr.setJavaObject(item);
328
+ // return attr;
329
+ if( getJavaObject().getBaseURI() == null )
330
+ return context.getRuntime().getNil();
331
+ return context.getRuntime().newString( getJavaObject().getBaseURI() );
332
+ }
333
+
334
+ @JRubyMethod(name = { "attribute?" })
335
+ public RubyBoolean isAttribute(ThreadContext context) {
336
+ boolean r = ((Node) getJavaObject()).getNodeType() == 2;
337
+ return r ? context.getRuntime().getTrue() : context.getRuntime()
338
+ .getFalse();
339
+ }
340
+
341
+ @JRubyMethod(name = { "attribute_decl?" })
342
+ public RubyBoolean isAttributeDecl(ThreadContext context) {
343
+ throw context.getRuntime().newArgumentError("unsupported");
344
+ }
345
+
346
+ @JRubyMethod(name = { "attributes" }, alias = { "properties" })
347
+ public AttributesJ getAttributes(ThreadContext context) {
348
+ AttributesJ attributes = AttributesJ.newInstance(context);
349
+ attributes.setParent( this );
350
+ attributes.setJavaObject(getJavaObject());
351
+ return attributes;
352
+ }
353
+
354
+ @JRubyMethod(name = { "attributes?" }, alias = { "properties?" })
355
+ public RubyBoolean hasAttributes(ThreadContext context) {
356
+ boolean r = ((Node) getJavaObject()).getAttributes().getLength() != 0;
357
+ return r ? context.getRuntime().getTrue() : context.getRuntime()
358
+ .getFalse();
359
+ }
360
+
361
+ // @JRubyMethod(name ="base_uri")
362
+ // public IRubyObject getBaseUri(ThreadContext context) {
363
+ // String uri = getJavaObject().getBaseURI();
364
+ // if (uri == null)
365
+ // return context.getRuntime().getNil();
366
+ // return context.getRuntime().newString(uri );
367
+ // }
368
+ //
369
+ // @JRubyMethod(name = { "base_uri=" })
370
+ // public void setBaseUri(ThreadContext context, RubyString uri) {
371
+ // throw context.getRuntime().newArgumentError("unsupported");
372
+ // }
373
+
374
+ @JRubyMethod(name = "empty?")
375
+ public RubyBoolean isEmpty(ThreadContext context) throws Exception {
376
+
377
+ boolean r = getJavaObject().getTextContent() == null
378
+ || getJavaObject().getTextContent().isEmpty();
379
+ if (r)
380
+ r = getChild(context).isNil();
381
+ return UtilJ.toBool(context, r);
382
+ }
383
+
384
+ @JRubyMethod(name = { "entity_ref?" })
385
+ public RubyBoolean isEntryRef(ThreadContext context) {
386
+ boolean r = ((Node) getJavaObject()).getNodeType() == 5;
387
+ return r ? context.getRuntime().getTrue() : context.getRuntime()
388
+ .getFalse();
389
+ }
390
+
391
+ @JRubyMethod(name = { "cdata?" })
392
+ public RubyBoolean isCdata(ThreadContext context) {
393
+ boolean r = ((Node) getJavaObject()).getNodeType() == 4;
394
+ return r ? context.getRuntime().getTrue() : context.getRuntime()
395
+ .getFalse();
396
+ }
397
+
398
+ @JRubyMethod(name = { "child=" })
399
+ public void setChild(ThreadContext context, IRubyObject pChild) {
400
+ NodeJ child = (NodeJ) pChild;
401
+ Node old = ((Node) getJavaObject()).getFirstChild();
402
+ Node n = ((Node) getJavaObject()).getOwnerDocument().adoptNode(
403
+ (Node) child.getJavaObject());
404
+ if (old == null)
405
+ ((Node) getJavaObject()).appendChild(n);
406
+ else
407
+ ((Node) getJavaObject()).insertBefore(n, old);
408
+ }
409
+
410
+ @JRubyMethod(name = { "first" }, alias = { "child" })
411
+ public IRubyObject getChild(ThreadContext context) {
412
+ NodeList list = getJavaObject().getChildNodes();
413
+ for (int i = 0; i < list.getLength(); i++) {
414
+ NodeJ node;
415
+ switch (list.item(i).getNodeType()) {
416
+ case Node.ELEMENT_NODE:
417
+ case Node.CDATA_SECTION_NODE:
418
+ case Node.COMMENT_NODE:
419
+ node = newInstance(context);
420
+ node.setDocPresent(isDocPresent());
421
+ node.setJavaObject(list.item(i));
422
+ return node;
423
+ }
424
+ }
425
+ return context.getRuntime().getNil();
426
+ }
427
+
428
+ @JRubyMethod(name = { "child?" }, alias = { "first", "children?" })
429
+ public RubyBoolean hasChild(ThreadContext context) {
430
+ boolean r = ((Node) getJavaObject()).getFirstChild() != null;
431
+ return r ? context.getRuntime().getTrue() : context.getRuntime()
432
+ .getFalse();
433
+ }
434
+
435
+ @JRubyMethod(name = { "last" })
436
+ public NodeJ getLast(ThreadContext context) {
437
+ NodeJ node = newInstance(context);
438
+ node.setDocPresent(isDocPresent());
439
+ node.setJavaObject(((Node) getJavaObject()).getLastChild());
440
+ return node;
441
+ }
442
+
443
+ @JRubyMethod(name = { "last?" })
444
+ public RubyBoolean hastLast(ThreadContext context) {
445
+ return UtilJ.toBool(context,
446
+ ((Node) getJavaObject()).getLastChild() != null);
447
+ }
448
+
449
+ @JRubyMethod(name = { "children" })
450
+ public RubyArray getChildren(ThreadContext context) {
451
+ NodeList list = ((Node) getJavaObject()).getChildNodes();
452
+ List array = new ArrayList(list.getLength());
453
+ for (int i = 0; i < list.getLength(); i++) {
454
+ NodeJ node = newInstance(context);
455
+ node.setDocPresent(isDocPresent());
456
+ node.setJavaObject(list.item(i));
457
+ array.add(node);
458
+ }
459
+ return context.getRuntime().newArray(array);
460
+ }
461
+
462
+ @JRubyMethod(name = "clone", alias = { "copy", "dup" }, rest=true)
463
+ public NodeJ clone(ThreadContext context, IRubyObject[] args) {
464
+ RubyBoolean deep;
465
+ if( args.length > 0 && args[0] instanceof RubyBoolean )
466
+ deep = (RubyBoolean) args[0];
467
+ else
468
+ deep = context.getRuntime().getFalse();
469
+
470
+ NodeJ node = newInstance(context);
471
+ node.setDocPresent(isDocPresent());
472
+ node.setJavaObject(((Node) getJavaObject()).cloneNode(deep.isNil() ? false
473
+ : deep.isTrue()));
474
+
475
+ return node;
476
+ }
477
+
478
+ @JRubyMethod(name = { "comment?" })
479
+ public RubyBoolean isComment(ThreadContext context) {
480
+ boolean r = ((Node) getJavaObject()).getNodeType() == 8;
481
+ return r ? context.getRuntime().getTrue() : context.getRuntime()
482
+ .getFalse();
483
+ }
484
+
485
+ @JRubyMethod(name = { "content" })
486
+ public RubyString getContent(ThreadContext context) {
487
+ return context.getRuntime().newString(
488
+ ((Node) getJavaObject()).getTextContent());
489
+ }
490
+
491
+ @JRubyMethod(name = { "content=" })
492
+ public void setContent(ThreadContext context, IRubyObject pContent) {
493
+ RubyString content = (RubyString) pContent;
494
+ ((Node) getJavaObject()).setTextContent(content.asJavaString());
495
+ }
496
+
497
+ @JRubyMethod(name = { "content_stripped" })
498
+ public RubyString getContentStripped(ThreadContext context) {
499
+ return context.getRuntime().newString(
500
+ ((Node) getJavaObject()).getTextContent().trim());
501
+ }
502
+
503
+ @JRubyMethod(name = "context", rest = true)
504
+ public IRubyObject getContext(ThreadContext context, IRubyObject[] args) {
505
+
506
+ XPathContextJ result = XPathContextJ.newInstance(context,
507
+ getDoc(context));
508
+ result.setNode(context, this);
509
+
510
+ for (IRubyObject arg : args) {
511
+ if (arg instanceof RubyString) {
512
+ IRubyObject[] array = { arg };
513
+ result.registerNamespaces(context, array);
514
+ } else {
515
+
516
+ throw context.getRuntime().newArgumentError("unsupported");
517
+ }
518
+ }
519
+ return result;
520
+ }
521
+
522
+ @JRubyMethod(name = { "debug" })
523
+ public RubyBoolean debug(ThreadContext context) {
524
+ return context.getRuntime().getFalse();
525
+ }
526
+
527
+ @JRubyMethod(name = { "doc" })
528
+ public DocumentJ getDoc(ThreadContext context) {
529
+ DocumentJ doc = DocumentJ.newInstance(context);
530
+ doc.setJavaObject(((Node) getJavaObject()).getOwnerDocument());
531
+ return doc;
532
+ }
533
+
534
+ @JRubyMethod(name = { "document?" })
535
+ public RubyBoolean isDocument(ThreadContext context) {
536
+ return context.getRuntime().getFalse();
537
+ }
538
+
539
+ @JRubyMethod(name = { "docbook_doc?" })
540
+ public RubyBoolean isDocbookDoc(ThreadContext context) {
541
+ return context.getRuntime().getFalse();
542
+ }
543
+
544
+ @JRubyMethod(name = { "doctype?" })
545
+ public RubyBoolean isDoctype(ThreadContext context) {
546
+ boolean r = ((Node) getJavaObject()).getNodeType() == 10;
547
+ return r ? context.getRuntime().getTrue() : context.getRuntime()
548
+ .getFalse();
549
+ }
550
+
551
+ @JRubyMethod(name = { "dtd?" })
552
+ public RubyBoolean isDtd(ThreadContext context) {
553
+ return context.getRuntime().getFalse();
554
+ }
555
+
556
+ @JRubyMethod(name = { "element?" })
557
+ public RubyBoolean isElement(ThreadContext context) {
558
+ boolean r = ((Node) getJavaObject()).getNodeType() == 1;
559
+ return r ? context.getRuntime().getTrue() : context.getRuntime()
560
+ .getFalse();
561
+ }
562
+
563
+ @JRubyMethod(name = { "element_decl?" })
564
+ public RubyBoolean isElementDecl(ThreadContext context) {
565
+ return context.getRuntime().getFalse();
566
+ }
567
+
568
+ @JRubyMethod(name = { "each" }, alias = { "each_child" })
569
+ public void each(ThreadContext context, Block block) {
570
+ for (NodeJ node : childrenAsList(context)) {
571
+ block.yield(context, node);
572
+ if (block.isEscaped())
573
+ break;
574
+ }
575
+ }
576
+
577
+ @JRubyMethod(name = { "each_attr" })
578
+ public void eachAttr(ThreadContext context, Block block) {
579
+ for (NodeJ node : attributesAsList(context)) {
580
+ block.yield(context, node);
581
+ if (block.isEscaped())
582
+ break;
583
+ }
584
+ }
585
+
586
+ @JRubyMethod(name = { "each_element" })
587
+ public void iterateOverElements(ThreadContext context, Block block) {
588
+ for (NodeJ node : elementsAsList(context)) {
589
+ block.yield(context, node);
590
+ if (block.isEscaped())
591
+ break;
592
+ }
593
+ }
594
+
595
+ @JRubyMethod(name = { "find" }, required = 1, optional = 1)
596
+ public XPathObjectJ find(ThreadContext context, IRubyObject[] args) {
597
+ RubyString expression = (RubyString) args[0];
598
+
599
+ if (args.length > 1)
600
+ ;
601
+ return XPathObjectJ.newInstance(context, expression, this,
602
+ new IRubyObject[0]);
603
+ }
604
+
605
+ @JRubyMethod(name = { "find_first" }, required = 1, optional = 1)
606
+ public IRubyObject findFirst(ThreadContext context, IRubyObject[] args)
607
+ throws Exception {
608
+ RubyString expression = (RubyString) args[0];
609
+
610
+ if (args.length > 1)
611
+ ;
612
+ return XPathObjectJ.newInstance(context, expression, this,
613
+ new IRubyObject[0]).getFirst(context);
614
+ }
615
+
616
+ @JRubyMethod(name = { "fragment?" })
617
+ public RubyBoolean isFragment(ThreadContext context) {
618
+ return isNodeType(context, 11);
619
+ }
620
+
621
+ @JRubyMethod(name = { "text?" })
622
+ public RubyBoolean isText(ThreadContext context) {
623
+ return isNodeType(context, 3);
624
+ }
625
+
626
+ @JRubyMethod(name = { "html_doc?" })
627
+ public RubyBoolean isHtmlDoc(ThreadContext context) {
628
+ return context.getRuntime().getFalse();
629
+ }
630
+
631
+ @JRubyMethod(name = { "to_s" }, optional = 1)
632
+ public RubyString toString(ThreadContext context, IRubyObject[] args)
633
+ throws Exception {
634
+ RubyHash hash;
635
+ if (args.length != 0) {
636
+ hash = (RubyHash) args[0];
637
+ }
638
+
639
+ String string = UtilJ.toString(getJavaObject(), outputEscaping);
640
+ return context.getRuntime().newString(string);
641
+ }
642
+
643
+ @JRubyMethod(name = { "inner_xml" })
644
+ public RubyString getInnerXml(ThreadContext context, IRubyObject pHash)
645
+ throws Exception {
646
+ RubyHash hash = (RubyHash) pHash;
647
+
648
+ return context.getRuntime().newString(
649
+ UtilJ.toString(getJavaObject(), outputEscaping));
650
+ }
651
+
652
+ @JRubyMethod(name = { "lang" })
653
+ public IRubyObject getLang(ThreadContext context) {
654
+ Node attr = ((Node) getJavaObject()).getAttributes().getNamedItem(
655
+ "xml:lang");
656
+
657
+ if (attr == null) {
658
+ return context.getRuntime().getNil();
659
+ }
660
+ return context.getRuntime().newString(attr.getNodeValue());
661
+ }
662
+
663
+ @JRubyMethod(name = { "lang=" })
664
+ public void setLang(ThreadContext context, IRubyObject pString) {
665
+ RubyString string = (RubyString) pString;
666
+
667
+ Node attr = ((Node) getJavaObject()).getAttributes().getNamedItem(
668
+ "xml:lang");
669
+
670
+ if (attr == null) {
671
+ attr = ((Node) getJavaObject()).getOwnerDocument().createAttribute(
672
+ "xml:lang");
673
+
674
+ ((Node) getJavaObject()).getAttributes().setNamedItem(attr);
675
+ }
676
+ attr.setNodeValue(string.asJavaString());
677
+ }
678
+
679
+ @JRubyMethod(name = { "namespace?" })
680
+ public RubyBoolean isNamespace(ThreadContext context) {
681
+ return UtilJ.toBool(context, false);
682
+ }
683
+
684
+ @JRubyMethod(name = { "namespacess=" })
685
+ public NamespacesJ getNamespaces(ThreadContext context) {
686
+ return NamespacesJ.newInstance(context, this);
687
+ }
688
+
689
+ @JRubyMethod(name = { "next" })
690
+ public IRubyObject getNext(ThreadContext context) {
691
+
692
+ Node n = getJavaObject().getNextSibling();
693
+ while (n != null && n.getNodeType() != Node.ELEMENT_NODE) {
694
+ n = n.getNextSibling();
695
+ }
696
+
697
+ if (n == null || n.getNodeType() != Node.ELEMENT_NODE) {
698
+ return context.getRuntime().getNil();
699
+ }
700
+
701
+ NodeJ node = newInstance(context);
702
+ node.setDocPresent(isDocPresent());
703
+ node.setJavaObject(n);
704
+ return node;
705
+ }
706
+
707
+ @JRubyMethod(name="next=")
708
+ public void setNext(ThreadContext context, IRubyObject pNode) {
709
+
710
+ NodeJ node = (NodeJ) pNode;
711
+
712
+ Node parent = getJavaObject().getParentNode();
713
+ NodeList children = parent.getChildNodes();
714
+ List<Node> list = new ArrayList<Node>();
715
+ List<Node> toRemove = new ArrayList<Node>();
716
+
717
+ for( int i=0; i<children.getLength(); i++ ) {
718
+ Node n = children.item(i);
719
+ list.add( n );
720
+ toRemove.add(n);
721
+ if( n.equals( getJavaObject() ) ) {
722
+ list.add( getJavaObject().getOwnerDocument().adoptNode( node.getJavaObject() ) );
723
+ }
724
+ }
725
+
726
+ for( Node n : toRemove )
727
+ parent.removeChild( n );
728
+
729
+
730
+ for( Node n : list )
731
+ parent.appendChild( n );
732
+ }
733
+
734
+ @JRubyMethod(name = { "next?" })
735
+ public RubyBoolean hasNext(ThreadContext context) {
736
+ return UtilJ.toBool(context,
737
+ ((Node) getJavaObject()).getNextSibling() != null);
738
+ }
739
+
740
+ @JRubyMethod(name = { "type" })
741
+ public RubyFixnum getType(ThreadContext context) {
742
+ return context.getRuntime().newFixnum(
743
+ ((Node) getJavaObject()).getNodeType());
744
+ }
745
+
746
+ @JRubyMethod(name = { "sibling=" })
747
+ public void addSibling(ThreadContext context, IRubyObject pNode) {
748
+ NodeJ node = (NodeJ) pNode;
749
+
750
+ throw context.getRuntime().newArgumentError("unsupported");
751
+ }
752
+
753
+ @JRubyMethod(name = { "remove!" })
754
+ public IRubyObject remove(ThreadContext context) {
755
+ if( getJavaObject().getParentNode() != null)
756
+ getJavaObject().getParentNode().removeChild( getJavaObject() );
757
+ return this;
758
+ }
759
+
760
+ @JRubyMethod(name = { "node_type" })
761
+ public RubyFixnum getNodeType(ThreadContext context) {
762
+ int result = getJavaObject().getNodeType();
763
+ return context.getRuntime().newFixnum( result );
764
+ }
765
+
766
+ @JRubyMethod(name = { "node_type_name" })
767
+ public RubyString getTypeName(ThreadContext context) {
768
+ switch (((Node) getJavaObject()).getNodeType()) {
769
+ case 2:
770
+ return context.getRuntime().newString("attribute");
771
+ case 9:
772
+ return context.getRuntime().newString("document_xml");
773
+ case 1:
774
+ return context.getRuntime().newString("element");
775
+ case 3:
776
+ return context.getRuntime().newString("text");
777
+ case 8:
778
+ return context.getRuntime().newString("comment");
779
+ case 4:
780
+ return context.getRuntime().newString("cdata");
781
+ case 5:
782
+ case 6:
783
+ case 7:
784
+ }
785
+ return context.getRuntime().newString("what ever");
786
+ }
787
+
788
+ @JRubyMethod(name = { "find?" })
789
+ public RubyBoolean hasFirst(ThreadContext context) {
790
+ return UtilJ.toBool(context,
791
+ ((Node) getJavaObject()).getFirstChild() != null);
792
+ }
793
+
794
+ @JRubyMethod(name = { "notation?" })
795
+ public RubyBoolean isNotation(ThreadContext context) {
796
+ return isNodeType(context, 12);
797
+ }
798
+
799
+ @JRubyMethod(name = { "output_escaping=" })
800
+ public void setOutputEscaping(ThreadContext context, IRubyObject pValue) {
801
+ if( pValue instanceof RubyBoolean )
802
+ this.outputEscaping = ((RubyBoolean) pValue).isTrue();
803
+ else if( pValue instanceof RubyNil )
804
+ this.outputEscaping = false;
805
+ }
806
+
807
+ @JRubyMethod(name = { "output_escaping?" })
808
+ public RubyBoolean isOutputEscaping(ThreadContext context) {
809
+ return context.getRuntime().newBoolean(outputEscaping);
810
+ }
811
+
812
+ @JRubyMethod(name = { "parent?" })
813
+ public IRubyObject hasParent(ThreadContext context) {
814
+ return UtilJ.toBool(context,
815
+ ((Node) getJavaObject()).getParentNode() != null);
816
+ }
817
+
818
+ @JRubyMethod(name = { "parent" })
819
+ public IRubyObject getParent(ThreadContext context) {
820
+ if (((Node) getJavaObject()).getParentNode() == null) {
821
+ return context.getRuntime().getNil();
822
+ }
823
+ NodeJ node = newInstance(context);
824
+ node.setDocPresent(isDocPresent());
825
+ node.setJavaObject(((Node) getJavaObject()).getParentNode());
826
+ return node;
827
+ }
828
+
829
+ // @Override
830
+ // public void setJavaObject( Node node ) {
831
+ // NodeList childs = node.getChildNodes();
832
+ // for( int i=0; i<childs.getLength(); i++ ) {
833
+ // Node item = childs.item(i);
834
+ // if( item.getNodeType() == Node.CDATA_SECTION_NODE ) {
835
+ // super.setJavaObject( item );
836
+ // return;
837
+ // }
838
+ // }
839
+ // super.setJavaObject( node );
840
+ // }
841
+
842
+ @JRubyMethod(name = { "path" })
843
+ public IRubyObject getPath(ThreadContext context) {
844
+ throw context.getRuntime().newArgumentError("unsupported");
845
+ }
846
+
847
+ @JRubyMethod(name = { "pi?" })
848
+ public RubyBoolean isPi(ThreadContext context) {
849
+ return isNodeType(context, 7);
850
+ }
851
+
852
+ @JRubyMethod(name = { "pointer" })
853
+ public XPointerJ getPointer(ThreadContext context) {
854
+ return null;
855
+ }
856
+
857
+ @JRubyMethod(name = { "xinclude_start?" })
858
+ public RubyBoolean isXincludeStart(ThreadContext context) {
859
+ return context.getRuntime().getFalse();
860
+ }
861
+
862
+ @JRubyMethod(name = { "xinclude_end?" })
863
+ public RubyBoolean isXincludeEnd(ThreadContext context) {
864
+ return context.getRuntime().getFalse();
865
+ }
866
+
867
+ @JRubyMethod(name = { "xlink?" })
868
+ public RubyBoolean isXlink(ThreadContext context) throws Exception {
869
+
870
+ //System.out.println( getJavaObject().getOwnerDocument().get );
871
+
872
+ //http://www.w3.org/1999/xlink/namespace/
873
+
874
+ // System.out.println(toString(context, new IRubyObject[0]));
875
+ // System.out.println(((Node) getJavaObject()).getTextContent());
876
+
877
+ return context.getRuntime().getFalse();
878
+ }
879
+
880
+ @JRubyMethod(name = { "xlink_type" })
881
+ public IRubyObject getXlinkType(ThreadContext context) {
882
+ return context.getRuntime().getNil();
883
+ }
884
+
885
+ @JRubyMethod(name = { "xlink_type_name" })
886
+ public IRubyObject getXlinkTypeName(ThreadContext context) {
887
+ return context.getRuntime().getNil();
888
+ }
889
+
890
+ @JRubyMethod(name = { "prev" })
891
+ public IRubyObject getPrev(ThreadContext context) {
892
+ if (((Node) getJavaObject()).getPreviousSibling() == null) {
893
+ return context.getRuntime().getNil();
894
+ }
895
+ NodeJ node = newInstance(context);
896
+ node.setDocPresent(isDocPresent());
897
+ node.setJavaObject(((Node) getJavaObject()).getPreviousSibling());
898
+ return node;
899
+ }
900
+
901
+ @JRubyMethod(name = { "prev?" })
902
+ public RubyBoolean hasPrev(ThreadContext context) {
903
+ return UtilJ.toBool(context,
904
+ ((Node) getJavaObject()).getPreviousSibling() != null);
905
+ }
906
+
907
+ @JRubyMethod(name = { "prev=" })
908
+ public void setPrev(ThreadContext context, IRubyObject pNode) {
909
+ NodeJ node = (NodeJ) pNode;
910
+
911
+ Node parent = getJavaObject().getParentNode();
912
+ NodeList children = parent.getChildNodes();
913
+ List<Node> list = new ArrayList<Node>();
914
+ List<Node> toRemove = new ArrayList<Node>();
915
+
916
+ for( int i=0; i<children.getLength(); i++ ) {
917
+ Node n = children.item(i);
918
+ if( n.equals( getJavaObject() ) ) {
919
+ list.add( getJavaObject().getOwnerDocument().adoptNode( node.getJavaObject() ) );
920
+ }
921
+ list.add( n );
922
+ toRemove.add(n);
923
+ }
924
+
925
+ for( Node n : toRemove )
926
+ parent.removeChild( n );
927
+
928
+
929
+ for( Node n : list )
930
+ parent.appendChild( n );
931
+ }
932
+
933
+ @JRubyMethod(name = { "line_num" })
934
+ public RubyFixnum getLineNumber(ThreadContext context) {
935
+ return context.getRuntime().newFixnum(-1);
936
+ }
937
+
938
+ @JRubyMethod(name = "space_preserve=")
939
+ public void setSpacePreserve(ThreadContext context, IRubyObject pValue) {
940
+ // TODO
941
+ }
942
+
943
+ @JRubyMethod(name = "space_preserve")
944
+ public IRubyObject getSpacePreserve(ThreadContext context) {
945
+ // TODO
946
+ return context.getRuntime().newFixnum(SPACE_DEFAULT);
947
+ }
948
+
949
+ @JRubyMethod(name = "name")
950
+ public RubyString getName(ThreadContext context) {
951
+ Node n = getJavaObject();
952
+ String string;
953
+ switch( n.getNodeType() ) {
954
+ case Node.TEXT_NODE:
955
+ string = outputEscaping ? "text" : "textnoenc";
956
+ break;
957
+ default:
958
+ string = n.getNodeName();
959
+ break;
960
+ }
961
+
962
+ return context.getRuntime().newString(string);
963
+ }
964
+
965
+ @JRubyMethod(name = { "name=" })
966
+ public void setName(ThreadContext context, RubyString name) {
967
+ throw context.getRuntime().newArgumentError("unsupported");
968
+ }
969
+
970
+ @JRubyMethod(name = { "value" })
971
+ public IRubyObject getValue(ThreadContext context) {
972
+ return context.getRuntime().newString(
973
+ ((Node) getJavaObject()).getNodeValue());
974
+ }
975
+
976
+ private RubyBoolean isNodeType(ThreadContext context, int type) {
977
+ return UtilJ.toBool(context,
978
+ ((Node) getJavaObject()).getNodeType() == type);
979
+ }
980
+
981
+ private List<NodeJ> childrenAsList(ThreadContext context) {
982
+ NodeList list = ((Node) getJavaObject()).getChildNodes();
983
+ List array = new ArrayList(list.getLength());
984
+ for (int i = 0; i < list.getLength(); i++) {
985
+ NodeJ node = newInstance(context);
986
+ node.setDocPresent(isDocPresent());
987
+ node.setJavaObject(list.item(i));
988
+ array.add(node);
989
+ }
990
+ return array;
991
+ }
992
+
993
+ private List<NodeJ> attributesAsList(ThreadContext context) {
994
+ NamedNodeMap list = ((Node) getJavaObject()).getAttributes();
995
+ List array = new ArrayList(list.getLength());
996
+ for (int i = 0; i < list.getLength(); i++) {
997
+ NodeJ node = newInstance(context);
998
+ node.setDocPresent(isDocPresent());
999
+ node.setJavaObject(list.item(i));
1000
+ array.add(node);
1001
+ }
1002
+ return array;
1003
+ }
1004
+
1005
+ private List<NodeJ> elementsAsList(ThreadContext context) {
1006
+ NodeList list = ((Node) getJavaObject()).getChildNodes();
1007
+ List array = new ArrayList(list.getLength());
1008
+ for (int i = 0; i < list.getLength(); i++) {
1009
+ Node obj = list.item(i);
1010
+ if (obj.getNodeType() != 1)
1011
+ continue;
1012
+ NodeJ node = newInstance(context);
1013
+ node.setDocPresent(isDocPresent());
1014
+ node.setJavaObject(obj);
1015
+ array.add(node);
1016
+ }
1017
+ return array;
1018
+ }
1019
+
1020
+ public void setDocPresent(boolean b) {
1021
+ this.docPresent = b;
1022
+ }
1023
+
1024
+ @Override
1025
+ public String toString() {
1026
+ return UtilJ.toString(getJavaObject(), true);
1027
+ }
1028
+
1029
+ }