public
Description: ruby libxml library targetting speed and ease of use. provides an hpricot-like interface to xml
Homepage: http://trac.hasno.info/fastxml
Clone URL: git://github.com/segfault/fastxml.git
added attribute read/write support


git-svn-id: svn://hasno.info/fastxml/trunk@44 
b3082176-f867-4bde-be85-e3c57d66f029
segfault (author)
Sun Nov 25 21:54:07 -0800 2007
commit  b716d85f2d7ec615331d142f862273d78fd994c4
tree    32800d72f62b39a04ab5c2d82164f82f80b13c2d
parent  f854b2a8eed8ebe62bbe5e6c9bd8932e6631bac6
...
6
7
8
 
9
10
11
12
 
13
14
15
...
33
34
35
 
36
37
38
...
56
57
58
59
 
60
61
62
...
71
72
73
 
 
 
 
 
 
74
75
76
...
6
7
8
9
10
11
12
13
14
15
16
17
...
35
36
37
38
39
40
41
...
59
60
61
 
62
63
64
65
...
74
75
76
77
78
79
80
81
82
83
84
85
0
@@ -6,10 +6,12 @@
0
 #include "fastxml_node.h"
0
 #include "fastxml_doc.h"
0
 #include "fastxml_nodelist.h"
0
+#include "fastxml_attrlist.h"
0
 
0
 VALUE rb_cFastXmlDoc;
0
 VALUE rb_cFastXmlNode;
0
 VALUE rb_cFastXmlNodeList;
0
+VALUE rb_cFastXmlAttrList;
0
 VALUE rb_sValidateDtd;
0
 VALUE rb_sForgivingParse;
0
 ID s_readlines;
0
@@ -33,6 +35,7 @@ void Init_fastxml()
0
     rb_cFastXmlDoc = rb_define_class_under( rb_mFastXml, "Doc", rb_cObject );
0
     rb_cFastXmlNode = rb_define_class_under( rb_mFastXml, "Node", rb_cObject );
0
     rb_cFastXmlNodeList = rb_define_class_under( rb_mFastXml, "NodeList", rb_cObject );
0
+ rb_cFastXmlAttrList = rb_define_class_under( rb_mFastXml, "AttrList", rb_cObject );
0
 
0
     /* Doc */
0
     rb_sValidateDtd = ID2SYM( rb_intern("validate") );
0
@@ -56,7 +59,7 @@ void Init_fastxml()
0
     rb_define_method( rb_cFastXmlNode, "content=", fastxml_node_value_set, 1 );
0
     rb_define_method( rb_cFastXmlNode, "inner_xml", fastxml_node_innerxml, 0 );
0
   rb_define_method( rb_cFastXmlNode, "xpath", fastxml_node_xpath, 0 );
0
- rb_define_method( rb_cFastXmlNode, "attr", fastxml_node_attr, 1 );
0
+ rb_define_method( rb_cFastXmlNode, "attr", fastxml_node_attr, 0 );
0
   rb_define_method( rb_cFastXmlNode, "children", fastxml_node_children, 0 );
0
   rb_define_method( rb_cFastXmlNode, "next", fastxml_node_next, 0 );  
0
   rb_define_method( rb_cFastXmlNode, "prev", fastxml_node_prev, 0 );  
0
@@ -71,6 +74,12 @@ void Init_fastxml()
0
     rb_define_method( rb_cFastXmlNodeList, "entry", fastxml_nodelist_entry, 1 );
0
   rb_define_method( rb_cFastXmlNodeList, "to_ary", fastxml_nodelist_entry, 0 );
0
   
0
+ /* AttrList */
0
+ rb_include_module( rb_cFastXmlAttrList, rb_mEnumerable );
0
+ rb_define_method( rb_cFastXmlAttrList, "initialize", fastxml_attrlist_initialize, 0 );
0
+ rb_define_method( rb_cFastXmlAttrList, "[]", fastxml_attrlist_indexer, 1 );
0
+ rb_define_method( rb_cFastXmlAttrList, "[]=", fastxml_attrlist_indexer_set, 2 );
0
+
0
   rb_require( "lib/fastxml_lib" );
0
 
0
 }
...
37
38
39
 
40
41
42
...
37
38
39
40
41
42
43
0
@@ -37,6 +37,7 @@ typedef struct {
0
 #ifndef fastxml_c
0
 RUBY_EXTERN VALUE rb_cFastXmlDoc;
0
 RUBY_EXTERN VALUE rb_cFastXmlNode;
0
+RUBY_EXTERN VALUE rb_cFastXmlAttrList;
0
 
0
 RUBY_EXTERN VALUE rb_sValidateDtd;
0
 RUBY_EXTERN VALUE rb_sForgivingParse;
...
132
133
134
135
 
 
136
137
 
138
139
140
141
142
143
144
145
146
147
148
149
150
151
 
 
 
 
 
 
 
 
 
 
 
 
152
153
154
 
155
156
157
...
132
133
134
 
135
136
137
 
138
139
 
 
 
 
 
 
 
 
 
 
 
 
 
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
0
@@ -132,26 +132,27 @@ VALUE fastxml_node_name(VALUE self)
0
     return ret;
0
 }
0
 
0
-VALUE fastxml_node_attr(VALUE self, VALUE attr_name)
0
+
0
+VALUE fastxml_node_attr(VALUE self)
0
 {
0
- VALUE ret, dv;
0
+ VALUE self_dv, ret;
0
   fxml_data_t *data;
0
- xmlChar *raw_ret, *name_str;
0
-
0
- dv = rb_iv_get( self, "@lxml_doc" );
0
- Data_Get_Struct( dv, fxml_data_t, data );
0
-
0
- name_str = (xmlChar*)StringValuePtr( attr_name );
0
- raw_ret = xmlGetProp( data->node, name_str );
0
- if (raw_ret == NULL)
0
- return Qnil;
0
-
0
- ret = rb_str_new2( (const char*)raw_ret );
0
- xmlFree( raw_ret );
0
-
0
+ xmlChar *raw_ret;
0
+
0
+ ret = rb_iv_get( self, "@attrs" );
0
+ if (ret == Qnil) {
0
+ self_dv = rb_iv_get( self, "@lxml_doc" );
0
+ Data_Get_Struct( self_dv, fxml_data_t, data );
0
+ ret = rb_class_new_instance( 0, 0, rb_cFastXmlAttrList );
0
+
0
+ rb_iv_set( ret, "@lxml_doc", self_dv );
0
+ rb_iv_set( self, "@attrs", ret );
0
+ }
0
+
0
   return ret;
0
 }
0
 
0
+
0
 VALUE fastxml_node_xpath(VALUE self)
0
 {
0
   VALUE ret, dv;
...
12
13
14
15
 
16
17
18
...
12
13
14
 
15
16
17
18
0
@@ -12,7 +12,7 @@ RUBY_EXTERN VALUE fastxml_node_value_set(VALUE self, VALUE new_val);
0
 RUBY_EXTERN VALUE fastxml_node_innerxml(VALUE self);
0
 RUBY_EXTERN VALUE fastxml_node_to_s(VALUE self);
0
 RUBY_EXTERN VALUE fastxml_node_xpath(VALUE self);
0
-RUBY_EXTERN VALUE fastxml_node_attr(VALUE self, VALUE attr_name);
0
+RUBY_EXTERN VALUE fastxml_node_attr(VALUE self);
0
 RUBY_EXTERN VALUE fastxml_node_children(VALUE self);
0
 RUBY_EXTERN VALUE fastxml_node_next(VALUE self);
0
 RUBY_EXTERN VALUE fastxml_node_prev(VALUE self);
...
34
35
36
37
38
39
40
41
42
43
44
...
50
51
52
53
 
 
 
 
 
 
 
 
 
 
 
 
54
55
56
...
89
90
91
 
 
 
 
 
 
 
 
 
 
 
 
92
93
...
34
35
36
 
 
 
 
 
37
38
39
...
45
46
47
 
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
0
@@ -34,11 +34,6 @@ describe FastXml::Node, ' functionality' do
0
     @node.name.should_not be_nil
0
   end
0
   
0
- it 'should provide a content accessor' do
0
- @node.should respond_to( :content )
0
- @node.content.should_not be_nil
0
- end
0
-
0
   it 'should provide a to_s method' do
0
     @node.should respond_to( :to_s )
0
     @node.to_s.should_not be_nil
0
@@ -50,7 +45,18 @@ describe FastXml::Node, ' functionality' do
0
   end
0
   
0
   it 'should provide an attribute accessor method named attr' do
0
- @node.should respond_to( :attr )
0
+ @node.should respond_to( 'attr' )
0
+ @node.attr.should respond_to( '[]' )
0
+ @node.attr[:ab].should be_nil
0
+ @node.attr["ab"].should be_nil
0
+ end
0
+
0
+ it 'should provide an attribute mutator method that responds to symbols' do
0
+ @node.should respond_to( 'attr' )
0
+ @node.attr.should respond_to( '[]' )
0
+ @node.attr[:ab] = "test"
0
+ @node.attr[:ab].should == "test"
0
+ @node.attr["ab"].should == "test"
0
   end
0
   
0
   it 'should provide a next method' do
0
@@ -89,4 +95,16 @@ describe FastXml::Node, ' functionality' do
0
     @node.should respond_to( :at )
0
     @node.at( "entry" ).should_not be_nil
0
   end
0
+
0
+ it 'should provide a content accessor' do
0
+ @node.should respond_to( :content )
0
+ @node.content.should_not be_nil
0
+ end
0
+
0
+ it 'should provide a content mutator' do
0
+ @node.should respond_to( :content= )
0
+ @node.content = "test"
0
+ @node.content.should_not be_nil
0
+ @node.content.should == "test"
0
+ end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.