ruby-xml-smart 0.1.11-i486-linux
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +4 -0
- data/COPYING +504 -0
- data/Changelog +192 -0
- data/README +52 -0
- data/Rakefile +112 -0
- data/TODO +6 -0
- data/examples/EXAMPLE.xml +17 -0
- data/examples/EXAMPLE.xml.sic +17 -0
- data/examples/Visualise/EXAMPLE.xml +18 -0
- data/examples/Visualise/term-ansicolor-0.0.4/CHANGES +11 -0
- data/examples/Visualise/term-ansicolor-0.0.4/GPL +340 -0
- data/examples/Visualise/term-ansicolor-0.0.4/README.en +23 -0
- data/examples/Visualise/term-ansicolor-0.0.4/Rakefile +72 -0
- data/examples/Visualise/term-ansicolor-0.0.4/VERSION +1 -0
- data/examples/Visualise/term-ansicolor-0.0.4/examples/cdiff.rb +20 -0
- data/examples/Visualise/term-ansicolor-0.0.4/examples/example.rb +82 -0
- data/examples/Visualise/term-ansicolor-0.0.4/install.rb +12 -0
- data/examples/Visualise/term-ansicolor-0.0.4/lib/term/ansicolor.rb +78 -0
- data/examples/Visualise/xpath_visual.rb +45 -0
- data/examples/add_children.rb +14 -0
- data/examples/add_elements.rb +13 -0
- data/examples/attrs.rb +15 -0
- data/examples/children.rb +14 -0
- data/examples/concurrent.rb +30 -0
- data/examples/copy.rb +23 -0
- data/examples/create.rb +18 -0
- data/examples/delete.rb +30 -0
- data/examples/move_elements.rb +12 -0
- data/examples/namespace.rb +14 -0
- data/examples/namespace_detailed.rb +36 -0
- data/examples/namespace_find.rb +20 -0
- data/examples/pull.rb +18 -0
- data/examples/qname.rb +16 -0
- data/examples/replace.rb +14 -0
- data/examples/set_OR_replace.rb +32 -0
- data/examples/signals.rb +28 -0
- data/examples/string.rb +27 -0
- data/examples/write.rb +11 -0
- data/examples/xpath_attrs.rb +19 -0
- data/examples/xpath_functions.rb +7 -0
- data/examples/xpath_root.rb +6 -0
- data/extconf.rb +29 -0
- data/rbxs.c +136 -0
- data/rbxs.h +53 -0
- data/rbxs_dom.c +483 -0
- data/rbxs_dom.h +32 -0
- data/rbxs_domattribute.c +189 -0
- data/rbxs_domattribute.h +18 -0
- data/rbxs_domattributeset.c +182 -0
- data/rbxs_domattributeset.h +17 -0
- data/rbxs_domelement.c +656 -0
- data/rbxs_domelement.h +18 -0
- data/rbxs_domnamespace.c +127 -0
- data/rbxs_domnamespace.h +18 -0
- data/rbxs_domnamespaceset.c +276 -0
- data/rbxs_domnamespaceset.h +17 -0
- data/rbxs_domnodeset.c +284 -0
- data/rbxs_domnodeset.h +19 -0
- data/rbxs_domother.c +121 -0
- data/rbxs_domother.h +18 -0
- data/rbxs_domtext.c +165 -0
- data/rbxs_domtext.h +18 -0
- data/rbxs_pull.c +244 -0
- data/rbxs_pull.h +17 -0
- data/rbxs_pullattribute.c +124 -0
- data/rbxs_pullattribute.h +18 -0
- data/rbxs_pullattributeset.c +156 -0
- data/rbxs_pullattributeset.h +17 -0
- data/rbxs_qname.c +267 -0
- data/rbxs_qname.h +18 -0
- data/rbxs_utils.h +39 -0
- data/test/namespace_test.rb +83 -0
- metadata +125 -0
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# No closure, so changes are temporary
|
5
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
6
|
+
node = doc.root.find("/test/names").first
|
7
|
+
|
8
|
+
# append children
|
9
|
+
node.add("test_node", {"attr" => 12}, "Muahaha")
|
10
|
+
node.add("test_node", "Muahaha", {"attr" => 13})
|
11
|
+
node.add("test_node", {"attr" => 14})
|
12
|
+
node.add("test_node", "Muahaha")
|
13
|
+
node.add("test_node")
|
14
|
+
puts doc
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# No closure, so changes are temporary
|
5
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
6
|
+
node = doc.root.find("/test/names/name[@team='2']").first
|
7
|
+
|
8
|
+
# add siblings
|
9
|
+
node.add_before("test_node",{"attr" => "12"}, "Muahaha")
|
10
|
+
node.add_after("test_node", "Muahaha", {"attr" => "13"})
|
11
|
+
|
12
|
+
# print doc
|
13
|
+
puts doc
|
data/examples/attrs.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
5
|
+
attrs = doc.root.attributes
|
6
|
+
|
7
|
+
attrs.each { |n| puts n.inspect }
|
8
|
+
puts attrs.include?("attr2")
|
9
|
+
|
10
|
+
doc.find("//@xml:lang").each do |attr|
|
11
|
+
p attr.value
|
12
|
+
attr.value = "en"
|
13
|
+
end
|
14
|
+
|
15
|
+
puts doc.to_s
|
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
5
|
+
node = doc.find("/test/names").first
|
6
|
+
|
7
|
+
$stdout.sync = true
|
8
|
+
nums = 1000000
|
9
|
+
# Watch the power
|
10
|
+
0.upto(nums) { |i|
|
11
|
+
test = node.children[0].to_s
|
12
|
+
test = node.children[0].to_s
|
13
|
+
print "#{i}/#{nums} runs#{13.chr}" if i%1000 == 0
|
14
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
nums = 100000
|
6
|
+
# Watch the power
|
7
|
+
|
8
|
+
id = 16
|
9
|
+
e_matnr = "9906264"
|
10
|
+
e_name = "mangler"
|
11
|
+
e_secid = 1
|
12
|
+
exam = true
|
13
|
+
|
14
|
+
p = []
|
15
|
+
0.upto(nums) do |i|
|
16
|
+
p[i] = Thread.new do
|
17
|
+
XML::Smart.modify("concurrent.xml","<solutions/>") do |xml|
|
18
|
+
res = xml.find("/solutions/solution[@matnr=\"#{e_matnr}\" or @secid=\"#{e_secid}\"]").delete_all! if exam
|
19
|
+
node = xml.root.add("solution",{:matnr => e_matnr, :name => e_name, :secid => e_secid, :when => Time.now.xmlschema, :assessment => id})
|
20
|
+
1.upto(3) do |qbi|
|
21
|
+
qid = qbi*2
|
22
|
+
ques = node.add("question", :block => qbi, :question => qid)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
p.each do |t|
|
29
|
+
t.join
|
30
|
+
end
|
data/examples/copy.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# No closure, so changes are temporary
|
5
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
6
|
+
soc = XML::Smart.string("<?xml version='1.0'?><root><node id='1'><text>I am a text</text></node><node id='2'/></root>")
|
7
|
+
|
8
|
+
# copy a node
|
9
|
+
nodeA = doc.root.find("/test/names/name[5]").first
|
10
|
+
nodeB = soc.root.find("/root/node[@id='1']").first
|
11
|
+
nodeS = soc.root.find("/root/*")
|
12
|
+
doc.root.add(nodeA) # move the node (default, XML::Smart::Dom::Element::MOVE)
|
13
|
+
doc.root.add(nodeB) # copy the foreign node (default, XML::Smart::Dom::Element::COPY)
|
14
|
+
|
15
|
+
# copy the node 3 times
|
16
|
+
3.times { doc.root.add(nodeA,XML::Smart::Dom::Element::COPY) }
|
17
|
+
|
18
|
+
# copy all elements in nodeset
|
19
|
+
doc.root.add(nodeS)
|
20
|
+
|
21
|
+
puts soc
|
22
|
+
puts "-" * 79
|
23
|
+
puts doc
|
data/examples/create.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# When a string as second or third paramter is provided, then a empty
|
5
|
+
# xml file is created if it not exists. A block has to be supplied
|
6
|
+
#
|
7
|
+
# XML::Smart.modify(FILE,LOCKTIMEOUT,STRING) {} ... set timeout for lock file and create file if !exists?
|
8
|
+
# XML::Smart.modify(FILE,LOCKTIMEOUT) {} ... set timeout for lock
|
9
|
+
# XML::Smart.modify(FILE,STRING) {} ... create file if !exists?
|
10
|
+
# XML::Smart.modify(FILE) {} ... just open file change and write back (LOCKTIMEOUT defaults to 7)
|
11
|
+
|
12
|
+
XML::Smart.modify(File.dirname($0) + "/EXAMPLE.tmp.xml","<elements/>") { |doc|
|
13
|
+
node = doc.root.add("element",Time.now)
|
14
|
+
}
|
15
|
+
|
16
|
+
# Print the document
|
17
|
+
puts XML::Smart.open(File.dirname($0) + "/EXAMPLE.tmp.xml")
|
18
|
+
|
data/examples/delete.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
5
|
+
nodes = doc.root.find("/test/names/name")
|
6
|
+
|
7
|
+
puts "Original : #{nodes.to_a.inspect}"
|
8
|
+
|
9
|
+
nodes.delete_if!{ |n| n.text == "egon"}
|
10
|
+
puts "#delete_if! : #{nodes.to_a.inspect}"
|
11
|
+
puts "#length : #{nodes.length}"
|
12
|
+
puts "#nitems : #{nodes.nitems}"
|
13
|
+
|
14
|
+
nodes.delete_at!(2)
|
15
|
+
puts "#delete_at!(2) : #{nodes.to_a.inspect}"
|
16
|
+
puts "#length : #{nodes.length}"
|
17
|
+
puts "#nitems : #{nodes.nitems}"
|
18
|
+
|
19
|
+
nodes.delete_at!(-2)
|
20
|
+
puts "#delete_at!(-2): #{nodes.to_a.inspect}"
|
21
|
+
puts "#length : #{nodes.length}"
|
22
|
+
puts "#nitems : #{nodes.nitems}"
|
23
|
+
|
24
|
+
puts "\n"
|
25
|
+
nodes = doc.root.find("/test/names/name/@team")
|
26
|
+
puts "Original : #{nodes.to_a.inspect}"
|
27
|
+
nodes.delete_if!{ |n| n.to_i % 2 != 0}
|
28
|
+
puts "#delete_if!: #{nodes.to_a.inspect}"
|
29
|
+
|
30
|
+
puts doc
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# No closure, so changes are temporary
|
5
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
6
|
+
node = doc.find("/test/names/name[@team='2']").first
|
7
|
+
|
8
|
+
# add all elements that have a bigger team number as previous siblings
|
9
|
+
# (Egon is now before Kathrin in the document)
|
10
|
+
node.add_before(doc.find("/test/names/name[@team>'2']"))
|
11
|
+
|
12
|
+
puts doc
|
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# No closure, so changes are temporary
|
5
|
+
# add namespaces through a Hash
|
6
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
7
|
+
node = doc.root.find("/test/s:hallo", {"s" => "http://schemas.xmlsoap.org/wsdl/soap/"} ).first
|
8
|
+
|
9
|
+
# various aspects of the node
|
10
|
+
p node.name
|
11
|
+
puts node.name
|
12
|
+
puts node.name.prefix
|
13
|
+
puts node.name.namespace
|
14
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# No closure, so changes are temporary
|
5
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
6
|
+
|
7
|
+
# add an element hallo to root
|
8
|
+
nsnode = doc.root.add("hallo")
|
9
|
+
|
10
|
+
# add a namespace to the element
|
11
|
+
ns = nsnode.namespaces.add("test","http://heise")
|
12
|
+
|
13
|
+
# has the element a namespace? - No
|
14
|
+
p nsnode.namespace?
|
15
|
+
|
16
|
+
# set one
|
17
|
+
nsnode.namespace = ns
|
18
|
+
|
19
|
+
# has the element a namespace? - Yes
|
20
|
+
p nsnode.namespace?
|
21
|
+
|
22
|
+
# inspect the namespace + print (to_s)
|
23
|
+
p ns
|
24
|
+
|
25
|
+
# add a node, a namespace, then add the namespace to the element (via a string)
|
26
|
+
nsnode = doc.root.add("hallo")
|
27
|
+
nsnode.namespaces["sulu"] = "http://slashdot"
|
28
|
+
nsnode.namespace = "sulu" # nsnode.namespace = "http://slashdot" would have worked also
|
29
|
+
nsnode.namespaces["sulu"] = "http://slashdot/test"
|
30
|
+
|
31
|
+
# find namspaces
|
32
|
+
p nsnode.namespaces.find("soap")
|
33
|
+
p nsnode.namespaces.find("http://slashdot/test")
|
34
|
+
|
35
|
+
# print document
|
36
|
+
puts doc
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# No closure, so changes are temporary
|
5
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
6
|
+
|
7
|
+
# add a node with namespace
|
8
|
+
nsnode = doc.root.add("hallo")
|
9
|
+
ns = nsnode.namespaces.add("test","http://heise")
|
10
|
+
nsnode.add("test:entry")
|
11
|
+
|
12
|
+
# find node
|
13
|
+
puts "Find 1: #{doc.find("test:node").empty?}" rescue puts "Find 1: undefined namespace prefix"
|
14
|
+
puts "Find 2: #{doc.find("test:node",{"test" => "http://heise"}).empty?}"
|
15
|
+
|
16
|
+
doc.namespaces = {"t" => "http://heise"}
|
17
|
+
puts "Find 3: #{doc.find("t:node").empty?}"
|
18
|
+
|
19
|
+
# print document
|
20
|
+
puts doc
|
data/examples/pull.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# with next
|
5
|
+
puller = XML::Smart.pull(File.open(File.dirname($0) + "/EXAMPLE.xml"))
|
6
|
+
while puller.next do
|
7
|
+
puts puller.to_s
|
8
|
+
p puller.node_type
|
9
|
+
end
|
10
|
+
|
11
|
+
# or with closure
|
12
|
+
XML::Smart.pull(File.open(File.dirname($0) + "/EXAMPLE.xml")) { |puller|
|
13
|
+
unless puller.attributes.empty?
|
14
|
+
puller.attributes.each { |qname,value|
|
15
|
+
puts "#{qname}=#{value}"
|
16
|
+
}
|
17
|
+
end
|
18
|
+
}
|
data/examples/qname.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# No closure, so changes are temporary
|
5
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
6
|
+
node = doc.root.find("/test/s:hallo", {"s" => "http://schemas.xmlsoap.org/wsdl/soap/"} ).first
|
7
|
+
|
8
|
+
# check
|
9
|
+
if node.name == "hallo"
|
10
|
+
p node.name.name
|
11
|
+
p node.name.prefix
|
12
|
+
p node.name.namespace
|
13
|
+
p node.name.to_s
|
14
|
+
p node.name.inspect
|
15
|
+
p node.name
|
16
|
+
end
|
data/examples/replace.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
$stdout.sync = true
|
5
|
+
stime = Time.now.to_f
|
6
|
+
nums = 100000
|
7
|
+
# Watch the power
|
8
|
+
0.upto(nums) { |i|
|
9
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
10
|
+
nodes = doc.root.find("/test/names/name[6]")
|
11
|
+
nodes.first.parent.add("name", { "team" => "2" }, "Egon")
|
12
|
+
print "#{i}/#{nums} runs#{13.chr}" if i % 100 == 0
|
13
|
+
}
|
14
|
+
puts "#{nums} parses from a file and some work done: #{Time.now.to_f - stime} seconds"
|
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
require 'profile'
|
4
|
+
|
5
|
+
$stdout.sync = true
|
6
|
+
stime = Time.now.to_f
|
7
|
+
nums = 1000
|
8
|
+
# Watch the power
|
9
|
+
1.upto(nums) { |i|
|
10
|
+
# create a XML document and copy the elements for each node to a file
|
11
|
+
soc = XML::Smart.string("<?xml version='1.0'?><root><node id='1'><text>I am a text</text></node><node id='2'/></root>")
|
12
|
+
soc.root.find("/root/node").each { |ele|
|
13
|
+
XML::Smart.modify(File.dirname($0) + "/#{ele.attributes["id"]}.xml","<services/>") { |cdoc|
|
14
|
+
cdoc.root.replace_by(ele)
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
# create a XML document and copy the elements for each node to a file
|
19
|
+
soc = XML::Smart.string("<?xml version='1.0'?><root><node id='1'><text>I am a text</text></node><node id='2'/></root>")
|
20
|
+
soc.root.find("/root/node").each { |ele|
|
21
|
+
XML::Smart.modify(File.dirname($0) + "/#{ele.attributes["id"]}.xml","<services/>") { |cdoc|
|
22
|
+
cdoc.root = ele
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
# create a file and copy a newly created XML document to it
|
27
|
+
XML::Smart.modify(File.dirname($0) + "/1.xml","<services/>") { |cdoc|
|
28
|
+
cdoc.root = XML::Smart.string("<root><node id='1'><text>I am a text</text></node><node id='2'/></root>").root
|
29
|
+
}
|
30
|
+
print "#{i}/#{nums} runs#{13.chr}" if i%100 == 0
|
31
|
+
}
|
32
|
+
puts "#{nums} parses from a file and some work done: #{Time.now.to_f - stime} seconds"
|
data/examples/signals.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# No closure, so changes are temporary
|
5
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
6
|
+
p = nil
|
7
|
+
1.upto(2) do
|
8
|
+
doc.on_change do |type,node|
|
9
|
+
p node
|
10
|
+
end
|
11
|
+
p doc.change_handlers
|
12
|
+
p = Proc.new do |type,node|
|
13
|
+
puts "1"
|
14
|
+
end
|
15
|
+
doc.change_handlers << p
|
16
|
+
end
|
17
|
+
p doc.change_handlers
|
18
|
+
doc.change_handlers.delete(p)
|
19
|
+
p doc.change_handlers
|
20
|
+
|
21
|
+
$stdout.sync = true
|
22
|
+
stime = Time.now.to_f
|
23
|
+
nums = 1
|
24
|
+
# Watch the power
|
25
|
+
0.upto(nums) { |i|
|
26
|
+
node = doc.find("/test/names/name").first
|
27
|
+
node.namespaces["a"] = "uri:b"
|
28
|
+
}
|
data/examples/string.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# create a string
|
5
|
+
xmldoc = "<?xml version='1.0' encoding='ISO-8859-1'?>\n"
|
6
|
+
xmldoc << "<contributors>
|
7
|
+
<guy id='1' nick='eTM'>J�rgen</guy>
|
8
|
+
<guy id='2' nick='chris2'>Christian</guy>
|
9
|
+
<guy id='3'>Emanuel</guy>
|
10
|
+
<guy id='4'>41</guy>\n"
|
11
|
+
xmldoc << "</contributors>"
|
12
|
+
|
13
|
+
# parse it
|
14
|
+
doc = XML::Smart.string(xmldoc)
|
15
|
+
|
16
|
+
# output all guys with a nickname (the � is a UTF-8 character in the output)
|
17
|
+
# cause (always) everything that is returned is UTF-8
|
18
|
+
doc.root.find("/contributors/guy[@nick]").each { |ele|
|
19
|
+
puts "#{ele} (#{ele.attributes.include?("ick")})"
|
20
|
+
puts "#{ele} (#{ele.attributes["nick"]})"
|
21
|
+
}
|
22
|
+
|
23
|
+
# query arthur dent
|
24
|
+
puts doc.root.find("/contributors/guy[@id='4']").first.to_i + 1
|
25
|
+
|
26
|
+
# write to a file
|
27
|
+
File.open(File.dirname($0) + "/EXAMPLE.str.xml",File::WRONLY|File::CREAT|File::TRUNC) { |f| doc.save_as f }
|
data/examples/write.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# When a closure is used, then the changes are written back to the disk
|
5
|
+
XML::Smart.modify(File.dirname($0) + "/EXAMPLE.xml") { |doc|
|
6
|
+
node = doc.root.find("/test/names/name")[0]
|
7
|
+
node.text = Time.now
|
8
|
+
}
|
9
|
+
|
10
|
+
# Print the document
|
11
|
+
puts XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require "#{File.dirname($0)}/../smart"
|
3
|
+
|
4
|
+
# No closure, so changes are temporary
|
5
|
+
doc = XML::Smart.open(File.dirname($0) + "/EXAMPLE.xml")
|
6
|
+
node = doc.root.find("/test/names/name[2]/@team").first
|
7
|
+
|
8
|
+
p node.name == "tea"
|
9
|
+
# print attribute name and value
|
10
|
+
puts node.name + "=" + node.value
|
11
|
+
puts node.name + "=" + node.to_s
|
12
|
+
puts node.inspect
|
13
|
+
|
14
|
+
# set the value of the attribute
|
15
|
+
node.value = "Value"
|
16
|
+
p node
|
17
|
+
|
18
|
+
node = doc.root.find("/test/names/name[2]/@team").first.value = "Hi all"
|
19
|
+
p node
|
data/extconf.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
require 'rbconfig'
|
3
|
+
|
4
|
+
PKG_NAME = 'xml/smart'
|
5
|
+
|
6
|
+
include Config
|
7
|
+
|
8
|
+
unless File.exists?('extconf.h')
|
9
|
+
$LIBPATH.push(Config::CONFIG['libdir'])
|
10
|
+
unless have_library("z", "inflate")
|
11
|
+
puts "Error: zlib is needed"
|
12
|
+
exit 1
|
13
|
+
else
|
14
|
+
$defs.push('-DHAVE_ZLIB_H')
|
15
|
+
end
|
16
|
+
unless have_library("xml2", "xmlXPtrNewRange")
|
17
|
+
puts "Error: libxml2 is needed"
|
18
|
+
puts " Install libxml2 library or try one of the following options to extconf.rb:"
|
19
|
+
puts " --with-xml2-dir=/path/to/libxml2"
|
20
|
+
puts " --with-xml2-lib=/path/to/libxml2/lib"
|
21
|
+
puts " --with-xml2-include=/path/to/libxml2/include"
|
22
|
+
exit 1
|
23
|
+
end
|
24
|
+
$LDFLAGS << ' ' + `xml2-config --libs`.chomp
|
25
|
+
$CFLAGS << ' ' + `xml2-config --cflags`.chomp
|
26
|
+
$CFLAGS = '-g -Wall ' + $CFLAGS
|
27
|
+
create_header()
|
28
|
+
create_makefile(PKG_NAME,".")
|
29
|
+
end
|
data/rbxs.c
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
#include "rbxs.h"
|
2
|
+
#include "rbxs_qname.h"
|
3
|
+
#include "rbxs_dom.h"
|
4
|
+
#include "rbxs_domnamespace.h"
|
5
|
+
#include "rbxs_domnamespaceset.h"
|
6
|
+
#include "rbxs_domelement.h"
|
7
|
+
#include "rbxs_domtext.h"
|
8
|
+
#include "rbxs_domother.h"
|
9
|
+
#include "rbxs_domnodeset.h"
|
10
|
+
#include "rbxs_domattribute.h"
|
11
|
+
#include "rbxs_domattributeset.h"
|
12
|
+
#include "rbxs_pull.h"
|
13
|
+
#include "rbxs_pullattribute.h"
|
14
|
+
#include "rbxs_pullattributeset.h"
|
15
|
+
#include "rbxs_utils.h"
|
16
|
+
|
17
|
+
/* -- */
|
18
|
+
//***********************************************************************************
|
19
|
+
// Constructors
|
20
|
+
//***********************************************************************************
|
21
|
+
/* ++ */
|
22
|
+
|
23
|
+
/*
|
24
|
+
* Documentation
|
25
|
+
*/
|
26
|
+
VALUE rbxs_main_modify(int argc, VALUE *argv, VALUE class)
|
27
|
+
{
|
28
|
+
char *root;
|
29
|
+
unsigned short int lock;
|
30
|
+
|
31
|
+
Check_Type(argv[0], T_STRING);
|
32
|
+
|
33
|
+
root = NULL;
|
34
|
+
lock = 7;
|
35
|
+
switch (argc) {
|
36
|
+
case 3:
|
37
|
+
if ((TYPE(argv[2]) == T_FIXNUM) && (TYPE(argv[1]) != T_STRING)) {
|
38
|
+
Check_Type(argv[2], T_FIXNUM);
|
39
|
+
lock = NUM2INT(argv[2]);
|
40
|
+
root = StringValuePtr(argv[1]);
|
41
|
+
} else if ((TYPE(argv[1]) == T_FIXNUM) && (TYPE(argv[2]) != T_STRING)) {
|
42
|
+
Check_Type(argv[1], T_FIXNUM);
|
43
|
+
lock = NUM2INT(argv[1]);
|
44
|
+
root = StringValuePtr(argv[2]);
|
45
|
+
} else
|
46
|
+
rb_raise(rb_eArgError, "argument 2 and 3 have to be Hash and Integer");
|
47
|
+
break;
|
48
|
+
case 2:
|
49
|
+
if (TYPE(argv[1]) == T_STRING)
|
50
|
+
root = StringValuePtr(argv[1]);
|
51
|
+
else if (TYPE(argv[1]) == T_FIXNUM) {
|
52
|
+
Check_Type(argv[2], T_FIXNUM);
|
53
|
+
lock = NUM2INT(argv[2]);
|
54
|
+
} else
|
55
|
+
rb_raise(rb_eArgError, "argument 2 has to be a String or an Integer");
|
56
|
+
break;
|
57
|
+
case 1:
|
58
|
+
break;
|
59
|
+
default:
|
60
|
+
rb_raise(rb_eArgError, "wrong number of arguments (need 1, 2 or 3)");
|
61
|
+
}
|
62
|
+
return(rbxs_dom_open(argv[0],root,lock,1));
|
63
|
+
}
|
64
|
+
|
65
|
+
/*
|
66
|
+
* Documentation
|
67
|
+
*/
|
68
|
+
VALUE rbxs_main_open(VALUE class, VALUE name)
|
69
|
+
{
|
70
|
+
Check_Type(name, T_STRING);
|
71
|
+
return(rbxs_dom_open(name,NULL,0,0));
|
72
|
+
}
|
73
|
+
/*
|
74
|
+
* Documentation
|
75
|
+
*/
|
76
|
+
VALUE rbxs_main_open_b(VALUE class, VALUE name, VALUE str)
|
77
|
+
{
|
78
|
+
Check_Type(name, T_STRING);
|
79
|
+
Check_Type(str, T_STRING);
|
80
|
+
return(rbxs_dom_open(name,StringValuePtr(str),0,0));
|
81
|
+
}
|
82
|
+
/*
|
83
|
+
* Documentation
|
84
|
+
*/
|
85
|
+
VALUE rbxs_main_string(VALUE class, VALUE string)
|
86
|
+
{
|
87
|
+
return(rbxs_dom_string(string));
|
88
|
+
}
|
89
|
+
/*
|
90
|
+
* Documentation
|
91
|
+
*/
|
92
|
+
VALUE rbxs_main_pull(VALUE class, VALUE what)
|
93
|
+
{
|
94
|
+
return(rbxs_pull_new(cSmartPull,what));
|
95
|
+
}
|
96
|
+
|
97
|
+
//***********************************************************************************
|
98
|
+
// Initialize module and class
|
99
|
+
//***********************************************************************************
|
100
|
+
VALUE mXML;
|
101
|
+
VALUE cSmart;
|
102
|
+
|
103
|
+
void Init_smart( void ) {
|
104
|
+
mXML = rb_define_module( "XML" );
|
105
|
+
cSmart = rb_define_class_under( mXML, "Smart", rb_cObject );
|
106
|
+
|
107
|
+
// libxml init
|
108
|
+
xmlKeepBlanksDefault(0);
|
109
|
+
xmlLineNumbersDefault(0);
|
110
|
+
defaultEntityLoader = xmlGetExternalEntityLoader();
|
111
|
+
xmlSetExternalEntityLoader(xmllintExternalEntityLoader);
|
112
|
+
|
113
|
+
rb_define_const(cSmart, "LIBXML_VERSION", rb_str_new2(LIBXML_DOTTED_VERSION));
|
114
|
+
rb_define_const(cSmart, "VERSION", rb_str_new2(RBXS_VERSION));
|
115
|
+
|
116
|
+
rb_define_singleton_method(cSmart, "modify", rbxs_main_modify, -1);
|
117
|
+
rb_define_singleton_method(cSmart, "open!", rbxs_main_open_b, 2);
|
118
|
+
rb_define_singleton_method(cSmart, "open", rbxs_main_open, 1);
|
119
|
+
rb_define_singleton_method(cSmart, "new", rbxs_main_string, 1);
|
120
|
+
rb_define_singleton_method(cSmart, "string", rbxs_main_string, 1);
|
121
|
+
rb_define_singleton_method(cSmart, "pull", rbxs_main_pull, 1);
|
122
|
+
|
123
|
+
init_rbxs_qname();
|
124
|
+
init_rbxs_dom();
|
125
|
+
init_rbxs_domnamespaceset();
|
126
|
+
init_rbxs_domnamespace();
|
127
|
+
init_rbxs_domelement();
|
128
|
+
init_rbxs_domtext();
|
129
|
+
init_rbxs_domother();
|
130
|
+
init_rbxs_domnodeset();
|
131
|
+
init_rbxs_domattribute();
|
132
|
+
init_rbxs_domattributeset();
|
133
|
+
init_rbxs_pull();
|
134
|
+
init_rbxs_pullattributeset();
|
135
|
+
init_rbxs_pullattribute();
|
136
|
+
}
|
data/rbxs.h
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
/* Please see the COPYING file for copyright and distribution information */
|
2
|
+
|
3
|
+
#ifndef __RBXS_H__
|
4
|
+
#define __RBXS_H__
|
5
|
+
|
6
|
+
#include <ruby.h>
|
7
|
+
#ifdef RUBY_VM
|
8
|
+
#include <ruby/io.h>
|
9
|
+
#include <ruby/st.h>
|
10
|
+
#else
|
11
|
+
#include <version.h>
|
12
|
+
#include <rubyio.h>
|
13
|
+
#include <st.h>
|
14
|
+
#if (RUBY_VERSION_MAJOR == 1 && RUBY_VERSION_MINOR == 8 && RUBY_VERSION_TEENY < 7)
|
15
|
+
#define RHASH_SIZE(h) (RHASH(h)->tbl ? RHASH(h)->tbl->num_entries : 0)
|
16
|
+
#endif
|
17
|
+
#define RHASH_EMPTY_P(h) (RHASH_SIZE(h) == 0)
|
18
|
+
#endif
|
19
|
+
|
20
|
+
#ifdef HAVE_RB_IO_T
|
21
|
+
#define RB_IO_T rb_io_t
|
22
|
+
#if (RUBY_VERSION_MAJOR == 1 && RUBY_VERSION_MINOR == 8 && RUBY_VERSION_TEENY < 7)
|
23
|
+
#define RB_IO_T_FD(o) o->fd
|
24
|
+
#else
|
25
|
+
#define RB_IO_T_FD(o) fileno(o->f)
|
26
|
+
#endif
|
27
|
+
#else
|
28
|
+
#define RB_IO_T OpenFile
|
29
|
+
#define RB_IO_T_FD(o) fileno(o->f)
|
30
|
+
#endif
|
31
|
+
|
32
|
+
#include <stdlib.h>
|
33
|
+
#include <stdio.h>
|
34
|
+
#include <string.h>
|
35
|
+
#include <assert.h>
|
36
|
+
|
37
|
+
#include <libxml/parser.h>
|
38
|
+
#include <libxml/parserInternals.h>
|
39
|
+
#include <libxml/tree.h>
|
40
|
+
#include <libxml/xpath.h>
|
41
|
+
#include <libxml/xpathInternals.h>
|
42
|
+
#include <libxml/xmlreader.h>
|
43
|
+
#include <libxml/xmlsave.h>
|
44
|
+
|
45
|
+
#define RBXS_VERSION "0.1.11"
|
46
|
+
|
47
|
+
#define RBXS_PARSER_TYPE_DOM 0
|
48
|
+
#define RBXS_PARSER_TYPE_READER 1
|
49
|
+
|
50
|
+
RUBY_EXTERN VALUE mXML;
|
51
|
+
RUBY_EXTERN VALUE cSmart;
|
52
|
+
|
53
|
+
#endif
|