Class: MiniTransformer::Parser

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

Instance Method Summary collapse

Instance Method Details

#parseObject



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

def parse
  @book.id = @document.root.attribute_nodes.first.content if @document.root.attribute_nodes.size > 0
  @book.introduction = @document.xpath("//Book//Introduction//Para")

  keylist = @document.xpath("//Book//KeyList")
  entries = @document.xpath("//Book//KeyList//Entries")

  @book.key_list.name = keylist.at_xpath("//Name").text
  @book.key_list.key_label = keylist.at_xpath("//KeyLabel").text
  @book.key_list.description_label = keylist.at_xpath("//DescriptionLabel").text
  @book.key_list.entries = Array.new

  entries.children.each do |entry|
    if entry.class == Nokogiri::XML::Element
      book_entry = Entry.new
      entry.children.each do |child|
        if child.class == Nokogiri::XML::Element
          if child.name == "KeyName"
            book_entry.key_name = child.content
          elsif child.name == "Description"
            book_entry.description = child.content
          end
        end
      end
      @book.key_list.entries << book_entry
    end
  end
end

#setup(json_input_filename, xml_input_filename, output = nil, format = "html", mapping = nil) ⇒ Object



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

def setup(json_input_filename, xml_input_filename, output=nil, format="html", mapping=nil)
  if File.exists?(xml_input_filename) && File.exists?(json_input_filename)
    xml_input_file = File.open(xml_input_filename)
    json_input = File.open(json_input_filename)
    json_contents = ""
    json_input.each do |line|
      json_contents = json_contents + line
    end
    json_data = JSON.parse json_contents

    if File.extname(xml_input_filename) == ".xml"
      @document = Nokogiri::XML(xml_input_file, nil, 'UTF-8')
    end
  else
    raise "File Not Found."
  end

  unless output.nil?
    # set up the ouput HTML file to write to
    @output = output
  end

  unless mapping.nil?
    @mapping = open(mapping) {|f| YAML.load(f) }
  end

  @book = Book.new
  @book.key_list = KeyList.new

  @book.title = json_data["title"] if json_data["title"]
  @book.type = json_data["type"] if json_data["type"]
  @book.uid = json_data["uid"] if json_data["uid"]

  xml_input_file.close
  json_input.close
end

#to_htmlObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/mini_transformer.rb', line 105

def to_html
  html = MiniTransformer::generate_html(@book, @mapping)

  # Save the HTML file
  if @output
    File.open(@output, "w") { |f| f << html }
  else
    if @book.uid.nil?
      File.open("output.html", "w") { |f| f << html }
    else
      File.open("#{@book.uid}.html", "w") { |f| f << html }
    end
  end

  # return the HTML file as a string
  html
end

#to_jsonObject



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mini_transformer.rb', line 91

def to_json
  json = JSON.pretty_generate @book
  # Save the JSON file
  if @output
    File.open(@output, "w") { |f| f << json }
  else
    if @book.uid.nil?
      File.open("output.json", "w") { |f| f << json }
    else
      File.open("#{@book.uid}.json", "w") { |f| f << json }
    end
  end
end

#validateObject



55
56
57
58
59
60
# File 'lib/mini_transformer.rb', line 55

def validate
  if !@document.errors.empty?
    raise "Malformed XML"
  end
  @document.errors
end