source: extensions/gsdl-video/trunk/installed/cmdline/lib/ruby/site_ruby/1.8/mixml.rb@ 18425

Last change on this file since 18425 was 18425, checked in by davidb, 15 years ago

Video extension to Greenstone

File size: 4.0 KB
Line 
1# Copyright (c) 2005 Norman Timmler (inlet media e.K., Hamburg, Germany)
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10# notice, this list of conditions and the following disclaimer in the
11# documentation and/or other materials provided with the distribution.
12# 3. The name of the author may not be used to endorse or promote products
13# derived from this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26class MiXML
27
28 def self.parse(xml)
29 current = {}
30 xml.scan( /<([!\?].*?)>|<([^\/].*?)>|<\/(.+?)>|([^<>]*)/m ) do |ignore, open_tag, close_tag, cdata|
31 if open_tag
32 tag_name = open_tag.match( /(\w+)\s?/ )[1]
33
34 parameters = {}
35 open_tag.scan( /(\w+)=\"(.*?)\"/ ) { |key, value| parameters[key] = value }
36
37 parameters[:parent] = current
38
39 if current[tag_name].class == Array
40 current[tag_name] << parameters
41 current = current[tag_name].last
42 else
43 if current[tag_name]
44 current[tag_name] = [ current[tag_name], parameters ]
45 current = current[tag_name].last
46 else
47 current[tag_name] = parameters
48 current = current[tag_name]
49 end
50 end
51
52 elsif close_tag
53 parent = current[:parent]
54 current.delete( :parent )
55 current = parent unless parent.nil?
56 elsif cdata
57 cdata.strip!
58 current[:cdata] = cdata unless cdata.empty?
59 end
60 end
61
62 return normalize_cdata( current )
63 end
64
65 def self.normalize_cdata(branch)
66 if branch.class == Array
67 branch.collect do |item|
68 normalize_cdata( item )
69 end
70 elsif branch.class == Hash && branch[:cdata].nil?
71 branch.inject( {} ) do |hash, key_value|
72 key, value = key_value
73 value = normalize_cdata( value )
74 hash[key] = value
75 hash
76 end
77 else
78 if branch[:cdata] && branch.size == 1
79 branch[:cdata]
80 else
81 branch
82 end
83 end
84 end
85
86 def self.dump(object, indent = 0)
87 ' ' * indent << dump_object(object, indent).strip
88 end
89
90 def self.dump_object(object, indent = 0)
91 xml = ''
92
93 if object.class == Object
94 object = object.instance_variables.inject( {} ) { |hash, var| hash[var.gsub('@', '')] = object.instance_variable_get(var); hash }
95 end
96
97 case object
98 when Array
99 object.each do |value|
100 xml << indenter(indent) << "<value>" << dump_object( value, indent + 1 ) << "</value>"
101 end
102 xml << indenter(indent - 1)
103 when Hash
104 object.each do |key, value|
105 xml << indenter(indent) << "<#{key}>" << dump_object( value, indent + 1 ) << "</#{key}>"
106 end
107 xml << indenter(indent - 1)
108 else
109 xml << object.to_s
110 end
111
112 return xml
113 end
114
115 def self.indenter(indent = 0)
116 "\n" << ' ' * indent
117 end
118end
119
Note: See TracBrowser for help on using the repository browser.