Class: SodaXML

Inherits:
Object
  • Object
show all
Defined in:
lib/SodaXML.rb

Overview

SodaXML – Class

This class converts XML documents into Soda Meta Data.
A valid Soda XML file always should start with <soda> and end with </soda>

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#docObject

Returns the value of attribute doc.



42
43
44
# File 'lib/SodaXML.rb', line 42

def doc
  @doc
end

Instance Method Details

#parse(file) ⇒ Object

parse – Method

This methos parses the XML document and returns Soda markup.

Params:

file: The soda XML file to parse into soda meta data.

Results:

returns an array of hashes from the processChildren method.


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/SodaXML.rb', line 106

def parse(file)
     data = nil
     parser = nil
     doc = nil
     fd = nil
  
     begin
        fd = File.new(file)
        doc = REXML::Document.new(fd)
        data = processChildren(doc.root)
        fd.close() 
     rescue Exception => e
        $curSoda.rep.log("Failed to parse XML file: \"#{file}\"!\n",
           SodaUtils::ERROR)
        $curSoda.rep.ReportException(e, file)

        data = nil
     ensure
        # make sure this exception doesn't make it up to the soda level #
     end

     return data
end

#processChildren(node) ⇒ Object

processChildren – Method

This method walks through the node and converts it into Soda Markup
*Tag(node) names indicate what action to take
*attributes are mapped directly over
*child nodes map to the children attribute

Params:

node: This is the root doc from the XML parser from a soda file.

Results:

retutns an array of hashes.


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
# File 'lib/SodaXML.rb', line 58

def processChildren(node)
	children = []

     node.elements.each do |child|
		if (child.name == 'text')
			next
		end
		
		cur = Hash.new()
        cur['line_number'] = 0
		cur['do'] = "#{child.name}"
       
        case (child.name)
           when /javascript/i
              cur['content'] = child.text
           when /ruby/i
              cur['content'] = child.text
           when /comment/i
              cur['content'] = child.text
           when /whitelist/i
              cur['content'] = child.text
        end

		child.attributes.each do |k,v|
			cur[k] = "#{v}"
		end

		if child.has_elements?()
			cur['children'] = self.processChildren(child)
		end

		children.push(cur)
	end

	return children
end