Class: Resedit::FormatXml

Inherits:
TextFormat show all
Defined in:
lib/resedit/text/format_xml.rb

Instance Method Summary collapse

Constructor Details

#initialize(encoding) ⇒ FormatXml

Returns a new instance of FormatXml.



9
10
11
# File 'lib/resedit/text/format_xml.rb', line 9

def initialize(encoding)
    super('utf-8')
end

Instance Method Details

#loadLines(fname) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/resedit/text/format_xml.rb', line 27

def loadLines(fname)
    hs={}
    open(fname+".xml", "r:"+@encoding) {|f|
        doc=REXML::Document.new(f)
        doc.elements.each("body/text"){|e|
            hs[e.attributes['id']] = e.text
        }
    }
    raise "No data in xml" if !hs.length
    lns=[]
    for i in 0..hs.length-1
        raise "Text not found: "+i if !hs[i]
        lns+=[ hs[i] ]
    end
    return lns
end

#saveLines(fname, lines, meta) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/resedit/text/format_xml.rb', line 13

def saveLines(fname, lines, meta)
    open(fname+".xml", "w:"+@encoding) {|f|
        xml=Builder::XmlMarkup.new(:indent => 2 , :target=>f)
        xml.instruct! :xml, :encoding => @encoding
        xml.body {|b|
            lines.each.with_index{|l,i|
                mt = {'id' => i}
                mt.update(meta[i]) if meta[i]
                b.text(l, mt)
            }
        }
    }
end