smile-xml 1.0.3-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/lib/smile-xml.jar +0 -0
  2. data/lib/xml/libxml.rb +1 -0
  3. data/lib/xml.rb +5 -0
  4. data/src/main/java/smile/xml/AttrJ.java +165 -0
  5. data/src/main/java/smile/xml/AttributesJ.java +219 -0
  6. data/src/main/java/smile/xml/BaseJ.java +60 -0
  7. data/src/main/java/smile/xml/DocumentJ.java +435 -0
  8. data/src/main/java/smile/xml/EncodingJ.java +94 -0
  9. data/src/main/java/smile/xml/ErrorJ.java +155 -0
  10. data/src/main/java/smile/xml/NamespaceJ.java +132 -0
  11. data/src/main/java/smile/xml/NamespacesJ.java +158 -0
  12. data/src/main/java/smile/xml/NodeJ.java +1040 -0
  13. data/src/main/java/smile/xml/NodeSetJ.java +90 -0
  14. data/src/main/java/smile/xml/ParserContextJ.java +44 -0
  15. data/src/main/java/smile/xml/ParserJ.java +196 -0
  16. data/src/main/java/smile/xml/ParserOptionsJ.java +58 -0
  17. data/src/main/java/smile/xml/ReaderJ.java +34 -0
  18. data/src/main/java/smile/xml/SchemaJ.java +66 -0
  19. data/src/main/java/smile/xml/SmileXML.java +65 -0
  20. data/src/main/java/smile/xml/XmlJ.java +58 -0
  21. data/src/main/java/smile/xml/sax/CallbackHandler.java +113 -0
  22. data/src/main/java/smile/xml/sax/SaxParserCallbacksJ.java +71 -0
  23. data/src/main/java/smile/xml/sax/SaxParserJ.java +153 -0
  24. data/src/main/java/smile/xml/util/UtilJ.java +447 -0
  25. data/src/main/java/smile/xml/xpath/CustomNamespaceContext.java +59 -0
  26. data/src/main/java/smile/xml/xpath/XPathContextJ.java +154 -0
  27. data/src/main/java/smile/xml/xpath/XPathExpressionJ.java +62 -0
  28. data/src/main/java/smile/xml/xpath/XPathJ.java +36 -0
  29. data/src/main/java/smile/xml/xpath/XPathObjectJ.java +196 -0
  30. data/src/main/java/smile/xml/xpath/XPointerJ.java +32 -0
  31. data/src/main/ruby/xml/libxml.rb +1 -0
  32. data/src/main/ruby/xml.rb +5 -0
  33. data/src/test/ruby/etc_doc_to_s.rb +21 -0
  34. data/src/test/ruby/ets_doc_file.rb +17 -0
  35. data/src/test/ruby/ets_doc_to_s.rb +23 -0
  36. data/src/test/ruby/ets_gpx.rb +28 -0
  37. data/src/test/ruby/ets_node_gc.rb +23 -0
  38. data/src/test/ruby/ets_test.xml +2 -0
  39. data/src/test/ruby/ets_tsr.rb +11 -0
  40. data/src/test/ruby/model/atom.xml +13 -0
  41. data/src/test/ruby/model/bands.iso-8859-1.xml +5 -0
  42. data/src/test/ruby/model/bands.utf-8.xml +5 -0
  43. data/src/test/ruby/model/bands.xml +5 -0
  44. data/src/test/ruby/model/books.xml +146 -0
  45. data/src/test/ruby/model/merge_bug_data.xml +58 -0
  46. data/src/test/ruby/model/ruby-lang.html +238 -0
  47. data/src/test/ruby/model/rubynet.xml +79 -0
  48. data/src/test/ruby/model/shiporder.rnc +28 -0
  49. data/src/test/ruby/model/shiporder.rng +86 -0
  50. data/src/test/ruby/model/shiporder.xml +23 -0
  51. data/src/test/ruby/model/shiporder.xsd +31 -0
  52. data/src/test/ruby/model/soap.xml +27 -0
  53. data/src/test/ruby/model/xinclude.xml +5 -0
  54. data/src/test/ruby/smile_xml_test.rb +64 -0
  55. data/src/test/ruby/tc_attr.rb +191 -0
  56. data/src/test/ruby/tc_attr_decl.rb +133 -0
  57. data/src/test/ruby/tc_attributes.rb +135 -0
  58. data/src/test/ruby/tc_deprecated_require.rb +13 -0
  59. data/src/test/ruby/tc_document.rb +162 -0
  60. data/src/test/ruby/tc_document_write.rb +212 -0
  61. data/src/test/ruby/tc_dtd.rb +125 -0
  62. data/src/test/ruby/tc_error.rb +150 -0
  63. data/src/test/ruby/tc_html_parser.rb +140 -0
  64. data/src/test/ruby/tc_namespace.rb +62 -0
  65. data/src/test/ruby/tc_namespaces.rb +210 -0
  66. data/src/test/ruby/tc_node.rb +273 -0
  67. data/src/test/ruby/tc_node_cdata.rb +51 -0
  68. data/src/test/ruby/tc_node_comment.rb +33 -0
  69. data/src/test/ruby/tc_node_copy.rb +42 -0
  70. data/src/test/ruby/tc_node_edit.rb +178 -0
  71. data/src/test/ruby/tc_node_text.rb +73 -0
  72. data/src/test/ruby/tc_node_write.rb +108 -0
  73. data/src/test/ruby/tc_node_xlink.rb +29 -0
  74. data/src/test/ruby/tc_parser.rb +371 -0
  75. data/src/test/ruby/tc_parser_context.rb +189 -0
  76. data/src/test/ruby/tc_properties.rb +40 -0
  77. data/src/test/ruby/tc_reader.rb +306 -0
  78. data/src/test/ruby/tc_relaxng.rb +54 -0
  79. data/src/test/ruby/tc_sax_parser.rb +340 -0
  80. data/src/test/ruby/tc_schema.rb +59 -0
  81. data/src/test/ruby/tc_traversal.rb +222 -0
  82. data/src/test/ruby/tc_xinclude.rb +21 -0
  83. data/src/test/ruby/tc_xml.rb +226 -0
  84. data/src/test/ruby/tc_xpath.rb +210 -0
  85. data/src/test/ruby/tc_xpath_context.rb +80 -0
  86. data/src/test/ruby/tc_xpath_expression.rb +38 -0
  87. data/src/test/ruby/tc_xpointer.rb +74 -0
  88. data/src/test/ruby/test_helper.rb +23 -0
  89. data/src/test/ruby/test_suite.rb +41 -0
  90. metadata +142 -0
data/lib/smile-xml.jar ADDED
Binary file
data/lib/xml/libxml.rb ADDED
@@ -0,0 +1 @@
1
+ require 'xml'
data/lib/xml.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'java'
2
+ require File.join( File.dirname( __FILE__), 'smile-xml.jar' )
3
+
4
+ ruby = JRuby.runtime
5
+ Java::smile.xml.SmileXML.define(ruby);
@@ -0,0 +1,165 @@
1
+ package smile.xml;
2
+
3
+ import org.jruby.Ruby;
4
+ import org.jruby.RubyClass;
5
+ import org.jruby.RubyString;
6
+ import org.jruby.anno.JRubyClass;
7
+ import org.jruby.anno.JRubyMethod;
8
+ import org.jruby.runtime.ObjectAllocator;
9
+ import org.jruby.runtime.ThreadContext;
10
+ import org.jruby.runtime.builtin.IRubyObject;
11
+ import org.w3c.dom.Attr;
12
+ import org.w3c.dom.Document;
13
+ import org.w3c.dom.NamedNodeMap;
14
+ import org.w3c.dom.Node;
15
+
16
+ import smile.xml.util.UtilJ;
17
+
18
+ @JRubyClass( name="LibXML::XML::Attr" )
19
+ public class AttrJ extends NodeJ {
20
+
21
+ private static final long serialVersionUID = -8068663140926376117L;
22
+
23
+ private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
24
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
25
+ return new AttrJ(runtime, klass);
26
+ }
27
+ };
28
+
29
+ public static RubyClass define(Ruby runtime) {
30
+ return UtilJ.defineClass( runtime, AttrJ.class, ALLOCATOR );
31
+ }
32
+
33
+ private static RubyClass getRubyClass(Ruby runtime) {
34
+ return UtilJ.getClass(runtime, AttrJ.class );
35
+ }
36
+
37
+ public static AttrJ newInstance(ThreadContext context) {
38
+ return (AttrJ) getRubyClass(context.getRuntime()).newInstance(context,
39
+ new IRubyObject[0], null);
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
+ }
46
+
47
+ private NodeJ parent;
48
+
49
+ private AttrJ(Ruby ruby, RubyClass clazz) {
50
+ super(ruby, clazz);
51
+ }
52
+
53
+ public void setParent( NodeJ node ) {
54
+ this.parent = node;
55
+ }
56
+
57
+ @JRubyMethod(name = "initialize", optional = 4)
58
+ public void initialize(ThreadContext context, IRubyObject[] args) {
59
+ NodeJ node = (NodeJ) (args.length > 0 ? args[0] : null);
60
+ RubyString name = (RubyString) (args.length > 1 ? args[1] : null);
61
+ RubyString value = (RubyString) (args.length > 2 ? args[2] : null);
62
+ NamespaceJ ns = (NamespaceJ) (args.length > 3 ? args[3] : null);
63
+
64
+ this.parent = node;
65
+
66
+ if (node != null) {
67
+ Document doc = ((Node) node.getJavaObject()).getOwnerDocument();
68
+ Attr attr = doc.createAttribute(name.asJavaString());
69
+ attr.setNodeValue(value.asJavaString());
70
+ setJavaObject(attr);
71
+ node.getJavaObject().getAttributes().setNamedItem( attr );
72
+ }
73
+ }
74
+
75
+ @JRubyMethod(name = "next")
76
+ public IRubyObject getNext(ThreadContext context) {
77
+
78
+ Node n = null;
79
+
80
+ if( parent != null ) {
81
+ NamedNodeMap aa = parent.getJavaObject().getAttributes();
82
+ for( int i=0; i<aa.getLength(); i++ ) {
83
+
84
+ if( aa.item(i).equals( getJavaObject() ) ) {
85
+ if( i+1 < aa.getLength() )
86
+ n = aa.item(i+1);
87
+ break;
88
+ }
89
+ }
90
+ }
91
+
92
+ if( n == null )
93
+ return context.getRuntime().getNil();
94
+
95
+ AttrJ node = newInstance(context);
96
+ node.setDocPresent( isDocPresent() );
97
+ node.setJavaObject( n );
98
+ node.setParent( parent );
99
+ return node;
100
+
101
+ }
102
+
103
+ @JRubyMethod(name = "name")
104
+ public RubyString getName(ThreadContext context) {
105
+ if( getJavaObject().getLocalName() != null )
106
+ return context.getRuntime().newString( getJavaObject().getLocalName() );
107
+ return context.getRuntime().newString( getJavaObject().getNodeName() );
108
+ }
109
+
110
+ @JRubyMethod(name = "ns")
111
+ public IRubyObject getNs(ThreadContext context) {
112
+ if( getJavaObject().getNamespaceURI() != null ) {
113
+ RubyString prefix = null;
114
+ if( getJavaObject().getPrefix() != null )
115
+ prefix = context.getRuntime().newString( getJavaObject().getPrefix() );
116
+ RubyString uri = null;
117
+ if( getJavaObject().getNamespaceURI() != null)
118
+ uri = context.getRuntime().newString( getJavaObject().getNamespaceURI() );
119
+ return NamespaceJ.newInstance(context, this, prefix, uri );
120
+ }
121
+ return context.getRuntime().getNil();
122
+ }
123
+
124
+ @JRubyMethod(name = "parent")
125
+ public IRubyObject getParent(ThreadContext context) {
126
+ return parent;
127
+ }
128
+
129
+ @JRubyMethod(name = "parent?")
130
+ public IRubyObject hasParent(ThreadContext context) {
131
+ return toRubyBoolean( context, parent != null );
132
+ }
133
+
134
+ @JRubyMethod(name = "remove!")
135
+ public IRubyObject remove(ThreadContext context) {
136
+
137
+
138
+ NamedNodeMap aa = parent.getJavaObject().getAttributes();
139
+
140
+ if( aa != null ) {
141
+ aa.removeNamedItem( getJavaObject().getNodeName() );
142
+ }
143
+
144
+ parent = null;
145
+
146
+ return this;
147
+ }
148
+
149
+
150
+ @JRubyMethod(name = "value")
151
+ public RubyString getValue(ThreadContext context) {
152
+ return context.getRuntime().newString(
153
+ ((Node) getJavaObject()).getNodeValue());
154
+ }
155
+
156
+ @JRubyMethod(name = "value=")
157
+ public void setValue(ThreadContext context, IRubyObject obj) {
158
+ ((Node) getJavaObject()).setNodeValue(obj.asJavaString());
159
+ }
160
+
161
+ @JRubyMethod(name = "node_type_name")
162
+ public RubyString getTypeName(ThreadContext context) {
163
+ return super.getTypeName(context);
164
+ }
165
+ }
@@ -0,0 +1,219 @@
1
+ package smile.xml;
2
+
3
+ import java.util.ArrayList;
4
+ import java.util.HashMap;
5
+ import java.util.List;
6
+ import java.util.Map;
7
+
8
+ import org.jruby.Ruby;
9
+ import org.jruby.RubyClass;
10
+ import org.jruby.RubyFixnum;
11
+ import org.jruby.RubyHash;
12
+ import org.jruby.RubyString;
13
+ import org.jruby.RubySymbol;
14
+ import org.jruby.anno.JRubyClass;
15
+ import org.jruby.anno.JRubyMethod;
16
+ import org.jruby.runtime.Block;
17
+ import org.jruby.runtime.ObjectAllocator;
18
+ import org.jruby.runtime.ThreadContext;
19
+ import org.jruby.runtime.builtin.IRubyObject;
20
+ import org.w3c.dom.NamedNodeMap;
21
+ import org.w3c.dom.Node;
22
+
23
+ import smile.xml.util.UtilJ;
24
+
25
+ @JRubyClass(name = "LibXML::XML::Attributes", include = "Enumerable")
26
+ public class AttributesJ extends BaseJ<Node> {
27
+
28
+ private static final long serialVersionUID = -2519312417704775509L;
29
+
30
+ private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
31
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
32
+ return new AttributesJ(runtime, klass);
33
+ }
34
+ };
35
+
36
+ public static RubyClass define(Ruby runtime) {
37
+ return UtilJ.defineClass(runtime, AttributesJ.class, ALLOCATOR);
38
+ }
39
+
40
+ private NodeJ parent;
41
+
42
+ public void setParent(NodeJ node) {
43
+ this.parent = node;
44
+ }
45
+
46
+ @JRubyMethod(name = { "initialize" })
47
+ public IRubyObject initialize(ThreadContext context) {
48
+ return this;
49
+ }
50
+
51
+ private static RubyClass getRubyClass(Ruby runtime) {
52
+ return UtilJ.getClass(runtime, new String[] { "LibXML", "XML",
53
+ "Attributes" });
54
+ }
55
+
56
+ public static AttributesJ newInstance(ThreadContext context) {
57
+ return (AttributesJ) getRubyClass(context.getRuntime()).newInstance(
58
+ context, new IRubyObject[0], null);
59
+ }
60
+
61
+ public AttributesJ(Ruby ruby, RubyClass clazz) {
62
+ super(ruby, clazz);
63
+ }
64
+
65
+ @JRubyMethod(name = "[]")
66
+ public IRubyObject getValue(ThreadContext context, IRubyObject pName) {
67
+
68
+ String name = toJavaString(context, pName );
69
+
70
+ NamedNodeMap aa = getJavaObject().getAttributes();
71
+ if ( aa == null ) {
72
+
73
+ return context.getRuntime().getNil();
74
+ }
75
+
76
+ Node node = aa.getNamedItem( name );
77
+
78
+
79
+
80
+ if (node == null) {
81
+
82
+ for( int i=0;i<aa.getLength(); i++ ) {
83
+ Node item = aa.item(i);
84
+ // System.out.println( item.get);
85
+
86
+ if( item.getLocalName().equals( name ) ) {
87
+ node = item;
88
+ break;
89
+ }
90
+ }
91
+ }
92
+
93
+ return context.getRuntime().newString(node.getTextContent());
94
+ }
95
+
96
+ @JRubyMethod(name = { "[]=" })
97
+ public void setValue(ThreadContext context, IRubyObject pName,
98
+ IRubyObject pValue) {
99
+ if ((pName instanceof RubySymbol)) {
100
+ pName = pName.asString();
101
+ }
102
+
103
+ RubyString name = (RubyString) pName;
104
+
105
+ RubyString value = (RubyString) pValue;
106
+
107
+ Node node = ((Node) getJavaObject()).getAttributes().getNamedItem(
108
+ name.asJavaString());
109
+ if (node == null) {
110
+ node = ((Node) getJavaObject()).getOwnerDocument().createAttribute(
111
+ name.asJavaString());
112
+ ((Node) getJavaObject()).getAttributes().setNamedItem(node);
113
+ }
114
+
115
+ node.setNodeValue(value.asJavaString());
116
+ }
117
+
118
+ @JRubyMethod(name = { "each" })
119
+ public void iterateOver(ThreadContext context, Block block) {
120
+ UtilJ.iterateOver(context, block, attributesAsList(context));
121
+ }
122
+
123
+ @JRubyMethod(name = { "first" })
124
+ public IRubyObject first(ThreadContext context) {
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
+ }
135
+ }
136
+ return UtilJ.nvl(attr, context.getRuntime().getNil());
137
+ }
138
+
139
+ @JRubyMethod(name = { "get_attribute" })
140
+ public IRubyObject getAttribute(ThreadContext context, RubyString name) {
141
+ NamedNodeMap aa = getJavaObject().getAttributes();
142
+ String string = name.asJavaString();
143
+ Node node = aa.getNamedItem(string);
144
+ if (node == null)
145
+ for (int i = 0; i < aa.getLength(); i++) {
146
+ Node a = aa.item(i);
147
+ if (a.getLocalName().equals(string)) {
148
+ node = a;
149
+ break;
150
+ }
151
+ }
152
+ if (node != null) {
153
+ AttrJ attr = AttrJ.newInstance(context);
154
+ attr.setParent(parent);
155
+ attr.setJavaObject(node);
156
+ return attr;
157
+ }
158
+
159
+ return context.getRuntime().getNil();
160
+ }
161
+
162
+ @JRubyMethod(name = { "get_attribute_ns" })
163
+ public IRubyObject getAttributeNs(ThreadContext context,
164
+ RubyString namespace, RubyString name) {
165
+ Node node = ((Node) getJavaObject()).getAttributes().getNamedItemNS(
166
+ namespace.asJavaString(), name.asJavaString());
167
+ if (node != null) {
168
+ AttrJ attr = AttrJ.newInstance(context);
169
+ attr.setParent(parent);
170
+ attr.setJavaObject(node);
171
+ return attr;
172
+ }
173
+
174
+ return context.getRuntime().getNil();
175
+ }
176
+
177
+ @JRubyMethod(name = { "length" })
178
+ public RubyFixnum getLength(ThreadContext context) {
179
+ int length = attributesAsList(context).size();
180
+ return context.getRuntime().newFixnum(length);
181
+ }
182
+
183
+ @JRubyMethod(name = { "node" })
184
+ public NodeJ getNode(ThreadContext context) {
185
+ NodeJ node = NodeJ.newInstance(context);
186
+ node.setJavaObject(getJavaObject());
187
+ return node;
188
+ }
189
+
190
+ @JRubyMethod(name = { "to_h" })
191
+ public RubyHash toHash(ThreadContext context) {
192
+ Map map = new HashMap();
193
+
194
+ for (int i = 0; i < ((Node) getJavaObject()).getAttributes()
195
+ .getLength(); i++) {
196
+ Node a = ((Node) getJavaObject()).getAttributes().item(i);
197
+ map.put(context.getRuntime().newString(a.getNodeName()), context
198
+ .getRuntime().newString(a.getNodeValue()));
199
+ }
200
+ return RubyHash.newHash(context.getRuntime(), map, context.getRuntime()
201
+ .getNil());
202
+ }
203
+
204
+ private List<AttrJ> attributesAsList(ThreadContext context) {
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
+ }
212
+ AttrJ node = AttrJ.newInstance(context);
213
+ node.setJavaObject(item);
214
+ node.setParent(parent);
215
+ list.add(node);
216
+ }
217
+ return list;
218
+ }
219
+ }
@@ -0,0 +1,60 @@
1
+ package smile.xml;
2
+
3
+ import org.jruby.Ruby;
4
+ import org.jruby.RubyBoolean;
5
+ import org.jruby.RubyClass;
6
+ import org.jruby.RubyNil;
7
+ import org.jruby.RubyObject;
8
+ import org.jruby.runtime.ThreadContext;
9
+ import org.jruby.runtime.builtin.IRubyObject;
10
+
11
+ import smile.xml.util.UtilJ;
12
+
13
+ public class BaseJ<T> extends RubyObject {
14
+
15
+ private static final long serialVersionUID = 4634403693609027673L;
16
+
17
+ private T javaObject;
18
+
19
+ public BaseJ(Ruby ruby, RubyClass clazz) {
20
+ super(ruby, clazz);
21
+ }
22
+
23
+ public T getJavaObject() {
24
+ return this.javaObject;
25
+ }
26
+
27
+ public void setJavaObject(T javaObject) {
28
+ this.javaObject = javaObject;
29
+ }
30
+
31
+ public static <T> T nvl( T obj, T def ) {
32
+ return obj == null ? def : obj;
33
+ }
34
+
35
+ public static IRubyObject toRubyBoolean(ThreadContext context, Object obj) {
36
+
37
+ if( obj == null || obj instanceof RubyNil )
38
+ return context.getRuntime().getNil();
39
+
40
+ if( obj instanceof RubyBoolean )
41
+ return (IRubyObject) obj;
42
+
43
+ if( obj instanceof Boolean )
44
+ return ((Boolean)obj) ? context.getRuntime().getTrue() : context.getRuntime().getFalse();
45
+
46
+ throw context.getRuntime().newArgumentError("");
47
+ }
48
+
49
+ public static String toJavaString(ThreadContext context, Object obj) {
50
+
51
+ return UtilJ.toJavaString(context, obj);
52
+ }
53
+
54
+ public static IRubyObject toRubyString(ThreadContext context, Object obj) {
55
+
56
+ return UtilJ.toRubyString( context, obj);
57
+ }
58
+
59
+
60
+ }