forked from orofarne/mapnik-xml-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_xml
More file actions
executable file
·101 lines (80 loc) · 1.97 KB
/
patch_xml
File metadata and controls
executable file
·101 lines (80 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env ruby
require 'bundler/setup'
require 'optparse'
require 'nokogiri'
$options = {}
opt_parser = OptionParser.new do |opts|
opts.banner = "Usage: patch_xml.rb [options]"
opts.on("-i", "--input FILE", "Input Mapnik XML style file") do |v|
$options[:input] = v
end
opts.on("-o", "--output FILE", "Output Mapnik XML style file") do |v|
$options[:output] = v
end
opts.on("--host HOST", "Override host option") do |v|
$options[:host] = v
end
opts.on("--user USER", "Override user option") do |v|
$options[:user] = v
end
opts.on("--password PASS", "Override password option") do |v|
$options[:password] = v
end
opts.on("--dbname DBNAME", "Override dbname option") do |v|
$options[:dbname] = v
end
opts.on("-h", "--help", "Show help") do
$stderr.puts opt_parser.help
exit 1
end
end
opt_parser.parse!
if $options[:input].nil? then
$stderr.puts opt_parser.help
exit 1
end
def to_text(node)
if node.nil? then
nil
else
node.text
end
end
def set_val(node, val)
tmp = Nokogiri::XML::Document.new
node.children = tmp.create_cdata(val)
end
f = File.open($options[:input])
doc = Nokogiri::XML(f)
f.close
doc.xpath('/Map/Layer/Datasource').each { |datasource|
next if 'postgis' != to_text(
datasource.xpath('./Parameter[@name="type"]').first
)
if !$options[:host].nil? then
datasource.xpath('./Parameter[@name="host"]').each { |v|
set_val(v, $options[:host])
}
end
if !$options[:user].nil? then
datasource.xpath('./Parameter[@name="user"]').each { |v|
set_val(v, $options[:user])
}
end
if !$options[:password].nil? then
datasource.xpath('./Parameter[@name="password"]').each { |v|
set_val(v, $options[:password])
}
end
if !$options[:dbname].nil? then
datasource.xpath('./Parameter[@name="dbname"]').each { |v|
set_val(v, $options[:dbname])
}
end
}
out_xml = doc.to_xhtml(:indent => 4, :encoding => 'UTF-8')
if $options[:output] then
File.open($options[:output], 'w') { |file| file.write(out_xml) }
else
puts out_xml
end