Class: Labels::XML::Parser
- Inherits:
-
Object
- Object
- Labels::XML::Parser
- Defined in:
- lib/labels/xml/parser.rb
Overview
This class parses a Labels XML document and creates a returns an instantiated Labels::Document instance from the XML
Class Method Summary collapse
-
.parse(xml) ⇒ Object
Parses the provided XML into a Labels::Document instance.
Class Method Details
.parse(xml) ⇒ Object
Parses the provided XML into a Labels::Document instance
31 32 33 34 35 36 37 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 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/labels/xml/parser.rb', line 31 def parse(xml) doc = Nokogiri::XML(xml) if doc.nil? || doc.root.nil? return nil end root_node = doc.root paper_node = root_node.at_xpath('paper') template_node = root_node.at_xpath('template') layer_nodes = root_node.xpath('layers/layer') database_node = root_node.at_xpath('database') row_nodes = root_node.xpath('database/row') unless paper_node.nil? paper = Labels::Paper.new({ :title => paper_node['title'], :width => (paper_node.at_xpath('width').text.to_f unless paper_node.at_xpath('width').nil?), :height => (paper_node.at_xpath('height').text.to_f unless paper_node.at_xpath('height').nil?) }) end unless template_node.nil? template = Labels::Template.new({ :title => template_node['title'], :description => template_node['description'], :manufacturer => template_node['manufacturer'], :category => template_node['category'], :visible => template_node['visible'].to_bool, :width => (template_node.at_xpath('width').text.to_f unless template_node.at_xpath('width').nil?), :height => (template_node.at_xpath('height').text.to_f unless template_node.at_xpath('height').nil?), :columns => (template_node.at_xpath('columns').text.to_i unless template_node.at_xpath('columns').nil?), :rows => (template_node.at_xpath('rows').text.to_i unless template_node.at_xpath('rows').nil?), :left_margin => (template_node.at_xpath('left-margin').text.to_f unless template_node.at_xpath('left-margin').nil?), :right_margin => (template_node.at_xpath('right-margin').text.to_f unless template_node.at_xpath('right-margin').nil?), :top_margin => (template_node.at_xpath('top-margin').text.to_f unless template_node.at_xpath('top-margin').nil?), :bottom_margin => (template_node.at_xpath('bottom-margin').text.to_f unless template_node.at_xpath('bottom-margin').nil?), :horizontal_gutter => (template_node.at_xpath('horizontal-gutter').text.to_f unless template_node.at_xpath('horizontal-gutter').nil?), :vertical_gutter => (template_node.at_xpath('vertical-gutter').text.to_f unless template_node.at_xpath('vertical-gutter').nil?), :elliptical => (template_node.at_xpath('elliptical').text.to_bool unless template_node.at_xpath('elliptical').nil?), :corner_radius => (template_node.at_xpath('corner-radius').text.to_f unless template_node.at_xpath('corner-radius').nil?) }) end unless layer_nodes.nil? layers = [] layer_nodes.each do |layer_node| layer_type = layer_node['type'] case layer_type when 'shape' layer = Labels::Shape.new({ :corner_radius => (layer_node.at_xpath('corner-radius').text.to_f unless layer_node.at_xpath('corner-radius').nil?), :elliptical => (layer_node.at_xpath('elliptical').text.to_bool unless layer_node.at_xpath('elliptical').nil?), :fill => (layer_node.at_xpath('fill').text.to_bool unless layer_node.at_xpath('fill').nil?), :stroke => (layer_node.at_xpath('stroke').text.to_bool unless layer_node.at_xpath('stroke').nil?), :fill_color => (layer_node.at_xpath('fill-color').text unless layer_node.at_xpath('fill-color').nil?), :stroke_color => (layer_node.at_xpath('stroke-color').text unless layer_node.at_xpath('stroke-color').nil?), :stroke_weight => (layer_node.at_xpath('stroke-weight').text.to_f unless layer_node.at_xpath('stroke-weight').nil?) }) when 'text' layer = Labels::Text.new({ :font_family => (layer_node.at_xpath('font-family').text unless layer_node.at_xpath('font-family').nil?), :font_size => (layer_node.at_xpath('font-size').text.to_f unless layer_node.at_xpath('font-size').nil?), :font_color => (layer_node.at_xpath('font-color').text unless layer_node.at_xpath('font-color').nil?), :bold => (layer_node.at_xpath('bold').text.to_bool unless layer_node.at_xpath('bold').nil?), :italic => (layer_node.at_xpath('italic').text.to_bool unless layer_node.at_xpath('italic').nil?), :underline => (layer_node.at_xpath('underline').text.to_bool unless layer_node.at_xpath('underline').nil?), :justification => (layer_node.at_xpath('justification').text unless layer_node.at_xpath('justification').nil?) }) when 'image' layer = Labels::Image.new({ :fit => (layer_node.at_xpath('fit').text.to_bool unless layer_node.at_xpath('fit').nil?), :stretch => (layer_node.at_xpath('stretch').text.to_bool unless layer_node.at_xpath('stretch').nil?), }) when 'barcode' layer = Labels::Barcode.new({ :symbology => (layer_node.at_xpath('symbology').text unless layer_node.at_xpath('symbology').nil?), :bar_width => (layer_node.at_xpath('bar-width').text.to_i unless layer_node.at_xpath('bar-width').nil?), :bar_height => (layer_node.at_xpath('bar-height').text.to_i unless layer_node.at_xpath('bar-height').nil?), :color => (layer_node.at_xpath('color').text unless layer_node.at_xpath('color').nil?), :caption_height => (layer_node.at_xpath('caption-height').text.to_i unless layer_node.at_xpath('caption-height').nil?), :caption_size => (layer_node.at_xpath('caption-size').text.to_i unless layer_node.at_xpath('caption-size').nil?), :captioned => (layer_node.at_xpath('captioned').text.to_bool unless layer_node.at_xpath('captioned').nil?), }) when 'date' layer = Labels::Date.new({ :font_family => (layer_node.at_xpath('font-family').text unless layer_node.at_xpath('font-family').nil?), :font_size => (layer_node.at_xpath('font-size').text.to_f unless layer_node.at_xpath('font-size').nil?), :font_color => (layer_node.at_xpath('font-color').text unless layer_node.at_xpath('font-color').nil?), :bold => (layer_node.at_xpath('bold').text.to_bool unless layer_node.at_xpath('bold').nil?), :italic => (layer_node.at_xpath('italic').text.to_bool unless layer_node.at_xpath('italic').nil?), :underline => (layer_node.at_xpath('underline').text.to_bool unless layer_node.at_xpath('underline').nil?), :justification => (layer_node.at_xpath('justification').text unless layer_node.at_xpath('justification').nil?), :format => (layer_node.at_xpath('format').text unless layer_node.at_xpath('format').nil?) }) end layer.title = layer_node['title'] layer.type = layer_node['type'] layer.column = layer_node['column'] layer.locked = layer_node['locked'].to_bool layer.visible = layer_node['visible'].to_bool layer.x = layer_node.at_xpath('x').text.to_f unless layer_node.at_xpath('x').nil? layer.y = layer_node.at_xpath('y').text.to_f unless layer_node.at_xpath('y').nil? layer.z = layer_node.at_xpath('z').text.to_f unless layer_node.at_xpath('z').nil? layer.width = layer_node.at_xpath('width').text.to_f unless layer_node.at_xpath('width').nil? layer.height = layer_node.at_xpath('height').text.to_f unless layer_node.at_xpath('height').nil? layer.alpha = layer_node.at_xpath('alpha').text.to_f unless layer_node.at_xpath('alpha').nil? layer.rotation = layer_node.at_xpath('rotation').text.to_f unless layer_node.at_xpath('rotation').nil? layer.content = layer_node.at_xpath('content').text unless layer_node.at_xpath('content').nil? layers.push(layer) end end unless database_node.nil? database = Labels::Database.new({ :title => database_node['title'] }) unless row_nodes.nil? data = [] row_nodes.each do |row_node| row = {} keys_node = row_node.xpath('key') keys_node.each do |key_node| row[key_node['name']] = key_node.text end data.push(row) end database.data = data end end Labels::Document.new(paper, template, layers, database) end |