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
data/lib/smile-xml.jar CHANGED
Binary file
@@ -0,0 +1,160 @@
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
+ private NodeJ parent;
43
+
44
+ private AttrJ(Ruby ruby, RubyClass clazz) {
45
+ super(ruby, clazz);
46
+ }
47
+
48
+ public void setParent( NodeJ node ) {
49
+ this.parent = node;
50
+ }
51
+
52
+ @JRubyMethod(name = "initialize", optional = 4)
53
+ public void initialize(ThreadContext context, IRubyObject[] args) {
54
+ NodeJ node = (NodeJ) (NodeJ) (args.length > 0 ? args[0] : null);
55
+ RubyString name = (RubyString) (args.length > 1 ? args[1] : null);
56
+ RubyString value = (RubyString) (args.length > 2 ? args[2] : null);
57
+ NamespaceJ ns = (NamespaceJ) (args.length > 3 ? args[3] : null);
58
+
59
+ this.parent = node;
60
+
61
+ if (node != null) {
62
+ Document doc = ((Node) node.getJavaObject()).getOwnerDocument();
63
+ Attr attr = doc.createAttribute(name.asJavaString());
64
+ attr.setNodeValue(value.asJavaString());
65
+ setJavaObject(attr);
66
+ node.getJavaObject().getAttributes().setNamedItem( attr );
67
+ }
68
+ }
69
+
70
+ @JRubyMethod(name = "next")
71
+ public IRubyObject getNext(ThreadContext context) {
72
+
73
+ Node n = null;
74
+
75
+ if( parent != null ) {
76
+ NamedNodeMap aa = parent.getJavaObject().getAttributes();
77
+ for( int i=0; i<aa.getLength(); i++ ) {
78
+
79
+ if( aa.item(i).equals( getJavaObject() ) ) {
80
+ if( i+1 < aa.getLength() )
81
+ n = aa.item(i+1);
82
+ break;
83
+ }
84
+ }
85
+ }
86
+
87
+ if( n == null )
88
+ return context.getRuntime().getNil();
89
+
90
+ AttrJ node = newInstance(context);
91
+ node.setDocPresent( isDocPresent() );
92
+ node.setJavaObject( n );
93
+ node.setParent( parent );
94
+ return node;
95
+
96
+ }
97
+
98
+ @JRubyMethod(name = "name")
99
+ public RubyString getName(ThreadContext context) {
100
+ if( getJavaObject().getLocalName() != null )
101
+ return context.getRuntime().newString( getJavaObject().getLocalName() );
102
+ return context.getRuntime().newString( getJavaObject().getNodeName() );
103
+ }
104
+
105
+ @JRubyMethod(name = "ns")
106
+ public IRubyObject getNs(ThreadContext context) {
107
+ if( getJavaObject().getNamespaceURI() != null ) {
108
+ RubyString prefix = null;
109
+ if( getJavaObject().getPrefix() != null )
110
+ prefix = context.getRuntime().newString( getJavaObject().getPrefix() );
111
+ RubyString uri = null;
112
+ if( getJavaObject().getNamespaceURI() != null)
113
+ uri = context.getRuntime().newString( getJavaObject().getNamespaceURI() );
114
+ return NamespaceJ.newInstance(context, this, prefix, uri );
115
+ }
116
+ return context.getRuntime().getNil();
117
+ }
118
+
119
+ @JRubyMethod(name = "parent")
120
+ public IRubyObject getParent(ThreadContext context) {
121
+ return parent;
122
+ }
123
+
124
+ @JRubyMethod(name = "parent?")
125
+ public IRubyObject hasParent(ThreadContext context) {
126
+ return toRubyBoolean( context, parent != null );
127
+ }
128
+
129
+ @JRubyMethod(name = "remove!")
130
+ public IRubyObject remove(ThreadContext context) {
131
+
132
+
133
+ NamedNodeMap aa = parent.getJavaObject().getAttributes();
134
+
135
+ if( aa != null ) {
136
+ aa.removeNamedItem( getJavaObject().getNodeName() );
137
+ }
138
+
139
+ parent = null;
140
+
141
+ return this;
142
+ }
143
+
144
+
145
+ @JRubyMethod(name = "value")
146
+ public RubyString getValue(ThreadContext context) {
147
+ return context.getRuntime().newString(
148
+ ((Node) getJavaObject()).getNodeValue());
149
+ }
150
+
151
+ @JRubyMethod(name = "value=")
152
+ public void setValue(ThreadContext context, IRubyObject obj) {
153
+ ((Node) getJavaObject()).setNodeValue(obj.asJavaString());
154
+ }
155
+
156
+ @JRubyMethod(name = "node_type_name")
157
+ public RubyString getTypeName(ThreadContext context) {
158
+ return super.getTypeName(context);
159
+ }
160
+ }
@@ -0,0 +1,212 @@
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
+ if (getJavaObject().getAttributes().getLength() == 0) {
126
+ return context.getRuntime().getNil();
127
+ }
128
+ AttrJ attr = AttrJ.newInstance(context);
129
+ NamedNodeMap aa = getJavaObject().getAttributes();
130
+ attr.setJavaObject(aa.item(0));
131
+ attr.setParent(parent);
132
+ return attr;
133
+ }
134
+
135
+ @JRubyMethod(name = { "get_attribute" })
136
+ public IRubyObject getAttribute(ThreadContext context, RubyString name) {
137
+ NamedNodeMap aa = getJavaObject().getAttributes();
138
+ String string = name.asJavaString();
139
+ Node node = aa.getNamedItem(string);
140
+ if (node == null)
141
+ for (int i = 0; i < aa.getLength(); i++) {
142
+ Node a = aa.item(i);
143
+ if (a.getLocalName().equals(string)) {
144
+ node = a;
145
+ break;
146
+ }
147
+ }
148
+ if (node != null) {
149
+ AttrJ attr = AttrJ.newInstance(context);
150
+ attr.setParent(parent);
151
+ attr.setJavaObject(node);
152
+ return attr;
153
+ }
154
+
155
+ return context.getRuntime().getNil();
156
+ }
157
+
158
+ @JRubyMethod(name = { "get_attribute_ns" })
159
+ public IRubyObject getAttributeNs(ThreadContext context,
160
+ RubyString namespace, RubyString name) {
161
+ Node node = ((Node) getJavaObject()).getAttributes().getNamedItemNS(
162
+ namespace.asJavaString(), name.asJavaString());
163
+ if (node != null) {
164
+ AttrJ attr = AttrJ.newInstance(context);
165
+ attr.setParent(parent);
166
+ attr.setJavaObject(node);
167
+ return attr;
168
+ }
169
+
170
+ return context.getRuntime().getNil();
171
+ }
172
+
173
+ @JRubyMethod(name = { "length" })
174
+ public RubyFixnum getLength(ThreadContext context) {
175
+ int r = ((Node) getJavaObject()).getAttributes() == null ? 0
176
+ : ((Node) getJavaObject()).getAttributes().getLength();
177
+ return context.getRuntime().newFixnum(r);
178
+ }
179
+
180
+ @JRubyMethod(name = { "node" })
181
+ public NodeJ getNode(ThreadContext context) {
182
+ NodeJ node = NodeJ.newInstance(context);
183
+ node.setJavaObject(getJavaObject());
184
+ return node;
185
+ }
186
+
187
+ @JRubyMethod(name = { "to_h" })
188
+ public RubyHash toHash(ThreadContext context) {
189
+ Map map = new HashMap();
190
+
191
+ for (int i = 0; i < ((Node) getJavaObject()).getAttributes()
192
+ .getLength(); i++) {
193
+ Node a = ((Node) getJavaObject()).getAttributes().item(i);
194
+ map.put(context.getRuntime().newString(a.getNodeName()), context
195
+ .getRuntime().newString(a.getNodeValue()));
196
+ }
197
+ return RubyHash.newHash(context.getRuntime(), map, context.getRuntime()
198
+ .getNil());
199
+ }
200
+
201
+ 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
+ AttrJ node = AttrJ.newInstance(context);
206
+ node.setJavaObject(list.item(i));
207
+ node.setParent(parent);
208
+ array.add(node);
209
+ }
210
+ return array;
211
+ }
212
+ }
@@ -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
+ }