Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#fileToArray(filename) ⇒ Object

takes a filename and returns an array of strings



8
9
10
11
12
13
14
15
16
# File 'lib/MergeAsXML.rb', line 8

def fileToArray (filename)
   lines = Array.new
   File.open(filename) do |fp|
      fp.each do | line |
         lines << line.strip
      end
   end
   return lines
end

#mergeOutputIntoXML(output, xmlfilename, xpath, action) ⇒ Object

output is required to be a string containing valid XML



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

def mergeOutputIntoXML(output, xmlfilename, xpath, action)

  #load XML doc from xmlfilename
  doc = REXML::Document.new ( File.new(xmlfilename) ) #getSourceXMLDoc(xmlfilename)
  fragment = REXML::Document.new("<wrapper>#{output}</wrapper>")

  #insertpoint = doc.at_xpath(xpath)
  insertpoint = REXML::XPath.first(doc, xpath)

  fragment.root.elements.each do | child |
    case action
      when "after"
        # insert as next siblings of specified xpath
        insertpoint.insert_after(xpath, child) 
      
      when "before"
        # insert as prev siblings of specified xpath
        insertpoint.insert_before(xpath, child)
      when "as child of"
        # insert as child of specified xpath
        insertpoint.add(child)    
    end
  end

  # return string
  return doc.to_s

end

#saveNewFileWithBackup(outputstring, filename) ⇒ Object



30
31
32
33
34
35
# File 'lib/MergeAsXML.rb', line 30

def saveNewFileWithBackup(outputstring, filename)
  #make backup, filename.backup
  FileUtils.cp(filename, "#{filename}.backup")
  # write output
  File.open(filename, 'w') {|f| f.write(outputstring) }
end

#stringsToArray(strings, delim) ⇒ Object

takes an array of strings and a delimiter returns a two-dimensional array based on the delimiter



20
21
22
23
24
25
26
27
28
# File 'lib/MergeAsXML.rb', line 20

def stringsToArray(strings, delim)
   lines = Array.new
   
   strings.each do | string |
      line = string.split(delim)
      lines << line
   end
   return lines
end