smile-xml 1.0.2-jruby → 1.0.3-jruby

Sign up to get free protection for your applications and to get access to all the features.
data/lib/smile-xml.jar CHANGED
Binary file
@@ -38,6 +38,11 @@ public class AttrJ extends NodeJ {
38
38
  return (AttrJ) getRubyClass(context.getRuntime()).newInstance(context,
39
39
  new IRubyObject[0], null);
40
40
  }
41
+
42
+ public static AttrJ newInstance(ThreadContext context, NodeJ node, RubyString name, RubyString value, NamespaceJ ns) {
43
+ IRubyObject[] args = { node, name, value, ns };
44
+ return (AttrJ) getRubyClass(context.getRuntime()).newInstance(context, args, null);
45
+ }
41
46
 
42
47
  private NodeJ parent;
43
48
 
@@ -51,7 +56,7 @@ public class AttrJ extends NodeJ {
51
56
 
52
57
  @JRubyMethod(name = "initialize", optional = 4)
53
58
  public void initialize(ThreadContext context, IRubyObject[] args) {
54
- NodeJ node = (NodeJ) (NodeJ) (args.length > 0 ? args[0] : null);
59
+ NodeJ node = (NodeJ) (args.length > 0 ? args[0] : null);
55
60
  RubyString name = (RubyString) (args.length > 1 ? args[1] : null);
56
61
  RubyString value = (RubyString) (args.length > 2 ? args[2] : null);
57
62
  NamespaceJ ns = (NamespaceJ) (args.length > 3 ? args[3] : null);
@@ -122,14 +122,18 @@ public class AttributesJ extends BaseJ<Node> {
122
122
 
123
123
  @JRubyMethod(name = { "first" })
124
124
  public IRubyObject first(ThreadContext context) {
125
- if (getJavaObject().getAttributes().getLength() == 0) {
126
- return context.getRuntime().getNil();
125
+ AttrJ attr = null;
126
+ NamedNodeMap attributes = getJavaObject().getAttributes();
127
+ for (int i = 0; i < attributes.getLength(); i++) {
128
+ Node item = attributes.item(i);
129
+ if (UtilJ.isAttr(item)) {
130
+ attr = AttrJ.newInstance(context);
131
+ attr.setJavaObject(item);
132
+ attr.setParent(parent);
133
+ break;
134
+ }
127
135
  }
128
- AttrJ attr = AttrJ.newInstance(context);
129
- NamedNodeMap aa = getJavaObject().getAttributes();
130
- attr.setJavaObject(aa.item(0));
131
- attr.setParent(parent);
132
- return attr;
136
+ return UtilJ.nvl(attr, context.getRuntime().getNil());
133
137
  }
134
138
 
135
139
  @JRubyMethod(name = { "get_attribute" })
@@ -172,9 +176,8 @@ public class AttributesJ extends BaseJ<Node> {
172
176
 
173
177
  @JRubyMethod(name = { "length" })
174
178
  public RubyFixnum getLength(ThreadContext context) {
175
- int r = ((Node) getJavaObject()).getAttributes() == null ? 0
176
- : ((Node) getJavaObject()).getAttributes().getLength();
177
- return context.getRuntime().newFixnum(r);
179
+ int length = attributesAsList(context).size();
180
+ return context.getRuntime().newFixnum(length);
178
181
  }
179
182
 
180
183
  @JRubyMethod(name = { "node" })
@@ -199,14 +202,18 @@ public class AttributesJ extends BaseJ<Node> {
199
202
  }
200
203
 
201
204
  private List<AttrJ> attributesAsList(ThreadContext context) {
202
- NamedNodeMap list = ((Node) getJavaObject()).getAttributes();
203
- List array = new ArrayList(list.getLength());
204
- for (int i = 0; i < list.getLength(); i++) {
205
+ NamedNodeMap attributes = ((Node) getJavaObject()).getAttributes();
206
+ List<AttrJ> list = new ArrayList<AttrJ>();
207
+ for (int i = 0; i < attributes.getLength(); i++) {
208
+ Node item = attributes.item(i);
209
+ if (! UtilJ.isAttr(item)) {
210
+ continue;
211
+ }
205
212
  AttrJ node = AttrJ.newInstance(context);
206
- node.setJavaObject(list.item(i));
213
+ node.setJavaObject(item);
207
214
  node.setParent(parent);
208
- array.add(node);
215
+ list.add(node);
209
216
  }
210
- return array;
217
+ return list;
211
218
  }
212
219
  }
@@ -321,25 +321,25 @@ public class DocumentJ extends BaseJ<Document> {
321
321
 
322
322
  @JRubyMethod(name="to_s", rest=true)
323
323
  public RubyString toString(ThreadContext context, IRubyObject[] args ) throws Exception {
324
- RubyBoolean indent;
325
- EncodingJ encoding = null;
324
+ Boolean indent = null;
325
+ EncodingJ encoding = null;
326
326
  if( args.length > 0 ) {
327
327
  if( args[0] instanceof RubyHash ) {
328
328
  RubyHash hash = (RubyHash) args[0];
329
329
  RubySymbol key = context.getRuntime().newSymbol( "indent" );
330
- indent = toRubyBool( context, hash.get( key ) );
330
+ indent = UtilJ.toJavaBoolean(context, hash.get(key));
331
331
  key = context.getRuntime().newSymbol( "encoding" );
332
332
  encoding = EncodingJ.get(context, hash.get( key ) );
333
333
  } else if( args[0] instanceof RubyBoolean ) {
334
- indent = (RubyBoolean) args[0];
334
+ indent = UtilJ.toJavaBoolean(context, args[0]);
335
335
  } else if( args[0] instanceof RubyNil ) {
336
-
336
+ // do nothing
337
337
  } else {
338
338
  throw context.getRuntime().newArgumentError("");
339
339
  }
340
340
  }
341
341
 
342
- String string = UtilJ.toString( getJavaObject(), true, encoding );
342
+ String string = UtilJ.toString( getJavaObject(), nvl(indent, true), encoding );
343
343
  return context.getRuntime().newString(string);
344
344
  }
345
345
 
@@ -5,7 +5,6 @@ import java.util.concurrent.atomic.AtomicReference;
5
5
  import org.jruby.Ruby;
6
6
  import org.jruby.RubyClass;
7
7
  import org.jruby.RubyException;
8
- import org.jruby.RubyModule;
9
8
  import org.jruby.anno.JRubyClass;
10
9
  import org.jruby.anno.JRubyConstant;
11
10
  import org.jruby.anno.JRubyMethod;
@@ -29,26 +28,6 @@ public class ErrorJ extends RubyException {
29
28
  };
30
29
 
31
30
  private static final AtomicReference<Block> handler = new AtomicReference<Block>();
32
-
33
- public static RubyClass define(Ruby runtime) {
34
- return UtilJ.defineClass(runtime, ErrorJ.class, ALLOCATOR);
35
- }
36
-
37
- private static RubyClass getRubyClass(Ruby runtime) {
38
- return UtilJ.getClass(runtime, ErrorJ.class );
39
- }
40
-
41
- public static RaiseException newRaiseException(ThreadContext context,
42
- String message) {
43
- Ruby run = context.getRuntime();
44
- return new RaiseException(run, getRubyClass(context.getRuntime()), message, true);
45
- }
46
-
47
- public static RubyException newInstance(ThreadContext context,
48
- String message) {
49
- return RubyException.newException(context.getRuntime(),getRubyClass(context.getRuntime()), message);
50
- }
51
-
52
31
 
53
32
  @JRubyConstant
54
33
  public static final IRubyObject VERBOSE_HANDLER = null;
@@ -77,8 +56,23 @@ public class ErrorJ extends RubyException {
77
56
  @JRubyConstant
78
57
  public static final IRubyObject ERROR = null;
79
58
 
59
+ public static RubyClass define(Ruby runtime) {
60
+ return UtilJ.defineClass(runtime, ErrorJ.class, ALLOCATOR);
61
+ }
80
62
 
81
-
63
+ private static RubyClass getRubyClass(Ruby runtime) {
64
+ return UtilJ.getClass(runtime, ErrorJ.class );
65
+ }
66
+
67
+ public static RaiseException newRaiseException(ThreadContext context, String message) {
68
+ Ruby run = context.getRuntime();
69
+ return new RaiseException(run, getRubyClass(run), message, true);
70
+ }
71
+
72
+ public static RubyException newInstance(ThreadContext context, String message) {
73
+ Ruby run = context.getRuntime();
74
+ return RubyException.newException(run, getRubyClass(run), message);
75
+ }
82
76
 
83
77
  private ErrorJ(Ruby runtime, RubyClass metaClass) {
84
78
  super(runtime, metaClass);
@@ -151,7 +145,6 @@ public class ErrorJ extends RubyException {
151
145
 
152
146
  @JRubyMethod(name = "reset_handler", module=true)
153
147
  public static void resetHandler(ThreadContext context, IRubyObject self) {
154
-
155
148
  handler.set(null);
156
149
  }
157
150
 
@@ -2,66 +2,131 @@ package smile.xml;
2
2
 
3
3
  import org.jruby.Ruby;
4
4
  import org.jruby.RubyClass;
5
- import org.jruby.RubyModule;
5
+ import org.jruby.RubyFixnum;
6
6
  import org.jruby.RubyObject;
7
7
  import org.jruby.RubyString;
8
+ import org.jruby.anno.JRubyClass;
8
9
  import org.jruby.anno.JRubyMethod;
9
10
  import org.jruby.runtime.ObjectAllocator;
10
11
  import org.jruby.runtime.ThreadContext;
11
12
  import org.jruby.runtime.builtin.IRubyObject;
13
+
12
14
  import smile.xml.util.UtilJ;
13
15
 
14
- public class NamespaceJ extends RubyObject
15
- {
16
- private static final long serialVersionUID = 4128551928821799987L;
17
- private static final ObjectAllocator ALLOCATOR = new ObjectAllocator()
18
- {
19
- public IRubyObject allocate(Ruby runtime, RubyClass klass)
20
- {
21
- return new NamespaceJ(runtime, klass); } } ;
22
- private NodeJ node;
23
- private RubyString prefix;
24
- private RubyString href;
16
+ @JRubyClass( name="LibXML::XML::Namespace" )
17
+ public class NamespaceJ extends RubyObject {
18
+
19
+ private static final long serialVersionUID = 4128551928821799987L;
20
+
21
+ private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
22
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
23
+ return new NamespaceJ(runtime, klass);
24
+ }
25
+ };
26
+
27
+ public static RubyClass define(Ruby runtime) {
28
+ return UtilJ.defineClass(runtime, NamespaceJ.class, ALLOCATOR);
29
+ }
30
+
31
+ private static RubyClass getRubyClass(Ruby runtime) {
32
+ return UtilJ.getClass(runtime, NamespaceJ.class);
33
+ }
34
+
35
+ public static NamespaceJ newInstance(ThreadContext context) {
36
+ return new NamespaceJ(context.getRuntime(), getRubyClass(context.getRuntime()));
37
+ }
38
+
39
+ public static NamespaceJ newInstance(ThreadContext context, IRubyObject node, IRubyObject prefix, IRubyObject href) {
40
+ IRubyObject[] args = { node, prefix, href };
41
+ return (NamespaceJ) getRubyClass(context.getRuntime()).newInstance(context, args, null);
42
+ }
43
+
44
+ private NodeJ node;
45
+ private RubyString prefix;
46
+ private RubyString href;
25
47
 
26
- public static RubyClass define(Ruby runtime) { RubyModule module = UtilJ.getModule(runtime, new String[] { "LibXML", "XML" });
27
- RubyClass result = module.defineClassUnder("Namespace", runtime.getObject(), ALLOCATOR);
28
- result.defineAnnotatedMethods(NamespaceJ.class);
29
- return result; }
48
+ public NamespaceJ(Ruby runtime, RubyClass metaClass) {
49
+ super(runtime, metaClass);
50
+ }
30
51
 
31
- private static RubyClass getRubyClass(Ruby runtime)
32
- {
33
- return UtilJ.getClass(runtime, new String[] { "LibXML", "XML", "Namespace" });
34
- }
52
+ @JRubyMethod(name = { "initialize" })
53
+ public void initialize(ThreadContext context, IRubyObject pNode, IRubyObject pPrefix, IRubyObject pHref) {
54
+ if ( ! (pNode instanceof NodeJ)) {
55
+ throw context.getRuntime().newTypeError("wrong argument type " + UtilJ.getRubyClassName(pNode) + " (expected Data)");
56
+ }
57
+
58
+ node = ((NodeJ) pNode);
59
+ if (! pPrefix.isNil()) {
60
+ prefix = (RubyString) pPrefix;
61
+ }
62
+ href = (RubyString) pHref;
63
+
64
+ // define namespace as "xmlns" attribute
65
+ String name = prefix == null ? "xmlns" : "xmlns:" + prefix.asJavaString();
66
+ String value = href.asJavaString();
67
+ AttrJ.newInstance(context, node, UtilJ.toRubyString(context, name).asString(),
68
+ UtilJ.toRubyString(context, value).asString(), this);
69
+ }
35
70
 
36
- public static NamespaceJ newInstance(ThreadContext context, IRubyObject node, IRubyObject prefix, IRubyObject uri)
37
- {
38
- IRubyObject[] args = { node, uri, prefix };
39
- return (NamespaceJ)getRubyClass(context.getRuntime()).newInstance(context, args, null);
40
- }
71
+ @JRubyMethod(name = { "prefix" })
72
+ public IRubyObject getPrefix(ThreadContext context) {
73
+ return UtilJ.nvl(this.prefix, context.getRuntime().getNil());
74
+ }
41
75
 
42
- public NamespaceJ(Ruby runtime, RubyClass metaClass)
43
- {
44
- super(runtime, metaClass);
45
- }
46
- @JRubyMethod(name={"initialize"})
47
- public void initialize(ThreadContext context, IRubyObject pNode, IRubyObject pHref, IRubyObject pPrefix) {
48
- this.node = ((NodeJ)pNode);
49
- if (!pHref.isNil())
50
- this.href = ((RubyString)pHref);
51
- if (!pPrefix.isNil())
52
- this.prefix = ((RubyString)pPrefix);
53
- }
76
+ @JRubyMethod(name = { "href" })
77
+ public IRubyObject getHref(ThreadContext context) {
78
+ return UtilJ.nvl(this.href, context.getRuntime().getNil());
79
+ }
54
80
 
55
- @JRubyMethod(name={"prefix"})
56
- public IRubyObject getPrefix(ThreadContext context) {
57
- return this.prefix == null ? context.getRuntime().getNil() : this.prefix;
58
- }
59
- @JRubyMethod(name={"href"})
60
- public IRubyObject getHref(ThreadContext context) {
61
- return this.href == null ? context.getRuntime().getNil() : this.href;
62
- }
63
- @JRubyMethod(name={"node"})
64
- public IRubyObject getNode(ThreadContext context) {
65
- return this.node;
66
- }
81
+ @JRubyMethod(name = { "node" })
82
+ public IRubyObject getNode(ThreadContext context) {
83
+ return this.node;
84
+ }
85
+
86
+ @JRubyMethod(name = { "node_type" })
87
+ public IRubyObject getNodeType(ThreadContext context) {
88
+ return new RubyFixnum(context.getRuntime(), NodeJ.NAMESPACE_DECL);
89
+ }
90
+
91
+ @JRubyMethod(name = { "next" })
92
+ public IRubyObject getNext(ThreadContext context) {
93
+ // TODO
94
+ return context.getRuntime().getNil();
95
+ }
96
+
97
+ @JRubyMethod(name = { "==" })
98
+ public IRubyObject isEqual(ThreadContext context, IRubyObject pOther) {
99
+ if (pOther instanceof NamespaceJ) {
100
+ NamespaceJ otherNS = (NamespaceJ) pOther;
101
+ if (href.equals(otherNS.href) && node.equals(otherNS.node)
102
+ && (prefix == null && otherNS.prefix == null
103
+ || prefix.equals(otherNS.prefix))) {
104
+ return context.getRuntime().getTrue();
105
+ }
106
+ }
107
+ return context.getRuntime().getFalse();
108
+ }
109
+
110
+ @JRubyMethod(name = { "to_s" })
111
+ public IRubyObject toS(ThreadContext context) {
112
+ String str;
113
+ if (prefix != null) {
114
+ str = prefix.asJavaString() + ":" + href.asJavaString();
115
+ } else {
116
+ str = href.asJavaString();
117
+ }
118
+ return context.getRuntime().newString(str);
119
+ }
120
+
121
+ public void setNode(NodeJ node) {
122
+ this.node = node;
123
+ }
124
+
125
+ public void setPrefix(RubyString prefix) {
126
+ this.prefix = prefix;
127
+ }
128
+
129
+ public void setHref(RubyString href) {
130
+ this.href = href;
131
+ }
67
132
  }
@@ -1,62 +1,158 @@
1
1
  package smile.xml;
2
2
 
3
+ import java.util.ArrayList;
4
+ import java.util.List;
5
+
3
6
  import org.jruby.Ruby;
4
7
  import org.jruby.RubyClass;
5
- import org.jruby.RubyModule;
6
8
  import org.jruby.RubyObject;
9
+ import org.jruby.anno.JRubyClass;
7
10
  import org.jruby.anno.JRubyMethod;
11
+ import org.jruby.runtime.Block;
8
12
  import org.jruby.runtime.ObjectAllocator;
9
13
  import org.jruby.runtime.ThreadContext;
10
14
  import org.jruby.runtime.builtin.IRubyObject;
15
+ import org.w3c.dom.Document;
16
+ import org.w3c.dom.NamedNodeMap;
11
17
  import org.w3c.dom.Node;
18
+
12
19
  import smile.xml.util.UtilJ;
13
20
 
14
- public class NamespacesJ extends RubyObject
15
- {
16
- private static final long serialVersionUID = 4673137342270845475L;
17
- private static final ObjectAllocator ALLOCATOR = new ObjectAllocator()
18
- {
19
- public IRubyObject allocate(Ruby runtime, RubyClass klass)
20
- {
21
- return new NamespacesJ(runtime, klass);
22
- } } ;
23
- private NodeJ node;
24
-
25
- public static RubyClass define(Ruby runtime) { RubyModule module = UtilJ.getModule(runtime, new String[] { "LibXML", "XML" });
26
- RubyClass result = module.defineClassUnder("Namespaces", runtime.getObject(), ALLOCATOR);
27
- result.defineAnnotatedMethods(NamespacesJ.class);
28
- return result; }
29
-
30
- private static RubyClass getRubyClass(Ruby runtime)
31
- {
32
- return UtilJ.getClass(runtime, new String[] { "LibXML", "XML", "Namespaces" });
33
- }
34
-
35
- public static NamespacesJ newInstance(ThreadContext context, NodeJ node)
36
- {
37
- IRubyObject[] args = { node };
38
- return (NamespacesJ)getRubyClass(context.getRuntime()).newInstance(context, args, null);
39
- }
40
-
41
- public NamespacesJ(Ruby runtime, RubyClass metaClass)
42
- {
43
- super(runtime, metaClass);
44
- }
45
- @JRubyMethod(name={"initialize"})
46
- public void initialize(ThreadContext context, IRubyObject pNode) {
47
- this.node = ((NodeJ)pNode);
48
- }
49
-
50
- @JRubyMethod(name={"namespace"})
51
- public IRubyObject getNamespace(ThreadContext context) {
52
- Ruby run = context.getRuntime();
53
-
54
- String tmp = ((Node)this.node.getJavaObject()).getPrefix();
55
- IRubyObject prefix = tmp == null ? run.getNil() : run.newString(tmp);
56
-
57
- tmp = ((Node)this.node.getJavaObject()).getNamespaceURI();
58
- IRubyObject uri = tmp == null ? run.getNil() : run.newString(tmp);
59
-
60
- return NamespaceJ.newInstance(context, this.node, prefix, uri);
61
- }
21
+ @JRubyClass( name = "LibXML::XML::Namespaces", include = "Enumerable" )
22
+ public class NamespacesJ extends RubyObject {
23
+
24
+ private static final long serialVersionUID = 4673137342270845475L;
25
+
26
+ private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
27
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
28
+ return new NamespacesJ(runtime, klass);
29
+ }
30
+ };
31
+
32
+ public static RubyClass define(Ruby runtime) {
33
+ return UtilJ.defineClass(runtime, NamespacesJ.class, ALLOCATOR);
34
+ }
35
+
36
+ private static RubyClass getRubyClass(Ruby runtime) {
37
+ return UtilJ.getClass(runtime, NamespacesJ.class);
38
+ }
39
+
40
+ public static NamespacesJ newInstance(ThreadContext context, NodeJ node) {
41
+ IRubyObject[] args = { node };
42
+ return (NamespacesJ) getRubyClass(context.getRuntime()).newInstance(context, args, null);
43
+ }
44
+
45
+ private NodeJ node;
46
+
47
+ public NamespacesJ(Ruby runtime, RubyClass metaClass) {
48
+ super(runtime, metaClass);
49
+ }
50
+
51
+ @JRubyMethod(name = { "initialize" })
52
+ public void initialize(ThreadContext context, IRubyObject pNode) {
53
+ this.node = (NodeJ) pNode;
54
+ }
55
+
56
+ @JRubyMethod(name = { "default" })
57
+ public IRubyObject getDefault(ThreadContext context) {
58
+ return findByPrefix(context, context.getRuntime().getNil());
59
+ }
60
+
61
+ @JRubyMethod(name = { "default_prefix=" })
62
+ public void setDefaultPrefix(ThreadContext context, IRubyObject pPrefix) {
63
+ // TODO
64
+ throw context.getRuntime().newRuntimeError("not yet implemented");
65
+ }
66
+
67
+ @JRubyMethod(name = { "definitions" })
68
+ public IRubyObject getDefinitions(ThreadContext context) {
69
+ // TODO
70
+ throw context.getRuntime().newRuntimeError("not yet implemented");
71
+ }
72
+
73
+ @JRubyMethod(name = { "each" })
74
+ public void iterateOver(ThreadContext context, Block block) {
75
+ UtilJ.iterateOver(context, block, namespacesAsList(context));
76
+ }
77
+
78
+ @JRubyMethod(name = { "find_by_href" })
79
+ public IRubyObject findByHref(ThreadContext context, IRubyObject pHref) {
80
+ // TODO
81
+ throw context.getRuntime().newRuntimeError("not yet implemented");
82
+ }
83
+
84
+ @JRubyMethod(name = { "find_by_prefix" }, optional = 1)
85
+ public IRubyObject findByPrefix(ThreadContext context, IRubyObject pPrefix) {
86
+ // TODO
87
+ throw context.getRuntime().newRuntimeError("not yet implemented");
88
+ }
89
+
90
+ @JRubyMethod(name = { "namespace" })
91
+ public IRubyObject getNamespace(ThreadContext context) {
92
+ Ruby run = context.getRuntime();
93
+
94
+ String prefixStr = ((Node) node.getJavaObject()).getPrefix();
95
+ IRubyObject prefix = prefixStr == null ? run.getNil() : run.newString(prefixStr);
96
+
97
+ String uriStr = ((Node) node.getJavaObject()).getNamespaceURI();
98
+ IRubyObject uri = uriStr == null ? run.getNil() : run.newString(uriStr);
99
+
100
+ if (uri.isNil()) {
101
+ return run.getNil();
102
+ } else {
103
+ return NamespaceJ.newInstance(context, node, prefix, uri);
104
+ }
105
+ }
106
+
107
+ @JRubyMethod(name = { "namespace=" })
108
+ public void setNamespace(ThreadContext context, IRubyObject pNamespace) {
109
+ if ( ! (pNamespace instanceof NamespaceJ)) {
110
+ throw context.getRuntime().newTypeError("wrong argument type " + UtilJ.getRubyClassName(pNamespace) + " (expected Data)");
111
+ }
112
+
113
+ NamespaceJ namespace = (NamespaceJ) pNamespace;
114
+ Document doc = node.getJavaObject().getOwnerDocument();
115
+
116
+ String namespaceURI = "";
117
+ String qualifiedName = node.getJavaObject().getNodeName();
118
+ if (! namespace.getHref(context).isNil()) {
119
+ namespaceURI = namespace.getHref(context).asJavaString();
120
+ }
121
+ if (! namespace.getPrefix(context).isNil()) {
122
+ qualifiedName = namespace.getPrefix(context).asJavaString() + ":" + node.getJavaObject().getNodeName();
123
+ }
124
+ node.setJavaObject(doc.renameNode(node.getJavaObject(), namespaceURI, qualifiedName));
125
+ }
126
+
127
+ @JRubyMethod(name = { "node" })
128
+ public IRubyObject getNode(ThreadContext context) {
129
+ return this.node;
130
+ }
131
+
132
+ private List<NamespaceJ> namespacesAsList(ThreadContext context) {
133
+ NamedNodeMap attributes = node.getJavaObject().getAttributes();
134
+ List<NamespaceJ> list = new ArrayList<NamespaceJ>();
135
+ for (int i = 0; i < attributes.getLength(); i++) {
136
+ Node item = attributes.item(i);
137
+ String name = item.getNodeName();
138
+ if (name.startsWith("xmlns")) {
139
+ NamespaceJ ns = NamespaceJ.newInstance(context);
140
+ String prefix;
141
+ if (name.startsWith("xmlns:")) {
142
+ prefix = name.substring("xmlns:".length());
143
+ ns.setPrefix(context.getRuntime().newString(prefix));
144
+ } else if (name.equals("xmlns")) {
145
+ prefix = null;
146
+ ns.setPrefix(null);
147
+ } else {
148
+ continue;
149
+ }
150
+ ns.setNode(node);
151
+ String href = item.getNodeValue();
152
+ ns.setHref(context.getRuntime().newString(href));
153
+ list.add(ns);
154
+ }
155
+ }
156
+ return list;
157
+ }
62
158
  }
@@ -75,11 +75,13 @@ public class NodeJ extends BaseJ<Node> {
75
75
 
76
76
  @JRubyConstant
77
77
  public static final int NOTATION_NODE = 12;
78
+
79
+ @JRubyConstant
80
+ public static final int NAMESPACE_DECL = 18;
78
81
 
79
82
  @JRubyConstant
80
83
  public static final int PROCESSING_INSTRUCTION_NODE = 7;
81
84
 
82
-
83
85
  @JRubyConstant
84
86
  public static final int FRAGMENT_NODE = 11;
85
87
 
@@ -126,16 +128,28 @@ public class NodeJ extends BaseJ<Node> {
126
128
 
127
129
  @JRubyMethod(name = { "initialize" }, optional = 3)
128
130
  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
-
131
+ RubyString name = (RubyString) (args.length > 0 && ! args[0].isNil() ? args[0] : null);
132
+ RubyString content = (RubyString) (args.length > 1 && ! args[1].isNil() ? args[1] : null);
133
+ NamespaceJ ns = (NamespaceJ) (args.length > 2 && ! args[2].isNil() ? args[2] : null);
134
+
133
135
  if (name != null) {
134
136
  Document doc = UtilJ.getBuilder().newDocument();
135
- Element elemet = doc.createElement(name.asJavaString());
137
+ Element element = doc.createElement(name.asJavaString());
136
138
  if ((content != null) && (!content.isNil()))
137
- elemet.setTextContent(content.asJavaString());
138
- setJavaObject(elemet);
139
+ element.setTextContent(content.asJavaString());
140
+ setJavaObject(element);
141
+
142
+ if (ns != null && ! ns.isNil()) {
143
+ String namespaceURI = "";
144
+ String qualifiedName = getJavaObject().getNodeName();
145
+ if (! ns.getHref(context).isNil()) {
146
+ namespaceURI = ns.getHref(context).asJavaString();
147
+ }
148
+ if (! ns.getPrefix(context).isNil()) {
149
+ qualifiedName = ns.getPrefix(context).asJavaString() + ":" + getJavaObject().getNodeName();
150
+ }
151
+ setJavaObject(doc.renameNode(getJavaObject(), namespaceURI, qualifiedName));
152
+ }
139
153
  }
140
154
  }
141
155
 
@@ -210,7 +224,7 @@ public class NodeJ extends BaseJ<Node> {
210
224
  }
211
225
 
212
226
  @JRubyMethod(name = "equal?")
213
- public RubyBoolean isEqlual(ThreadContext context, IRubyObject arg)
227
+ public RubyBoolean isEqual(ThreadContext context, IRubyObject arg)
214
228
  throws Exception {
215
229
 
216
230
  if (arg.isNil()) {
@@ -252,8 +266,7 @@ public class NodeJ extends BaseJ<Node> {
252
266
  boolean r = toString(context, new IRubyObject[0]).equals(
253
267
  other.toString(context, new IRubyObject[0]));
254
268
 
255
- return r ? context.getRuntime().getTrue() : context.getRuntime()
256
- .getFalse();
269
+ return UtilJ.toBool(context, r);
257
270
  }
258
271
 
259
272
  @JRubyMethod(name ="detect")
@@ -333,9 +346,8 @@ public class NodeJ extends BaseJ<Node> {
333
346
 
334
347
  @JRubyMethod(name = { "attribute?" })
335
348
  public RubyBoolean isAttribute(ThreadContext context) {
336
- boolean r = ((Node) getJavaObject()).getNodeType() == 2;
337
- return r ? context.getRuntime().getTrue() : context.getRuntime()
338
- .getFalse();
349
+ boolean r = ((Node) getJavaObject()).getNodeType() == ATTRIBUTE_NODE;
350
+ return UtilJ.toBool(context, r);
339
351
  }
340
352
 
341
353
  @JRubyMethod(name = { "attribute_decl?" })
@@ -354,8 +366,7 @@ public class NodeJ extends BaseJ<Node> {
354
366
  @JRubyMethod(name = { "attributes?" }, alias = { "properties?" })
355
367
  public RubyBoolean hasAttributes(ThreadContext context) {
356
368
  boolean r = ((Node) getJavaObject()).getAttributes().getLength() != 0;
357
- return r ? context.getRuntime().getTrue() : context.getRuntime()
358
- .getFalse();
369
+ return UtilJ.toBool(context, r);
359
370
  }
360
371
 
361
372
  // @JRubyMethod(name ="base_uri")
@@ -681,7 +692,7 @@ public class NodeJ extends BaseJ<Node> {
681
692
  return UtilJ.toBool(context, false);
682
693
  }
683
694
 
684
- @JRubyMethod(name = { "namespacess=" })
695
+ @JRubyMethod(name = { "namespaces" })
685
696
  public NamespacesJ getNamespaces(ThreadContext context) {
686
697
  return NamespacesJ.newInstance(context, this);
687
698
  }