Class: Gooby::ForerunnerXmlParser

Inherits:
Object
  • Object
show all
Includes:
REXML::StreamListener
Defined in:
lib/gooby_forerunner_xml_parser.rb

Overview

Instances of this class are used to parse a Forerunner XML file in a SAX-like

manner.  Instances of the model classes - History, Run, Track, Trackpoint,
etc. are created in this parsing process.  

See http://www.garmin.com/xmlschemas/ForerunnerLogbookv1.xsd for the XML Schema
Definition for the Garmin Forerunner XML.  The Gooby object model mirrors this XSD.

Constant Summary collapse

DETAIL_TAGS =
%w( Notes StartTime Duration Length Latitude Longitude Altitude Time BeginPosition EndPosition )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uom) ⇒ ForerunnerXmlParser

Returns a new instance of ForerunnerXmlParser.



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

def initialize(uom)
  @uom                 = uom
  @cv_hash             = Hash.new("")
  @tag_count           = 0
  @run_count           = 0
  @lap_count           = 0
  @track_count         = 0
  @trackpoint_count    = 0
  @curr_text           = "";
  @history             = History.new
  @curr_run            = nil
  @curr_lap            = nil
  @curr_track          = nil
  @curr_begin_position = nil
  @@curr_end_position  = nil
end

Instance Attribute Details

#cvHashObject (readonly)

Returns the value of attribute cvHash.



26
27
28
# File 'lib/gooby_forerunner_xml_parser.rb', line 26

def cvHash
  @cvHash
end

#historyObject (readonly)

Returns the value of attribute history.



26
27
28
# File 'lib/gooby_forerunner_xml_parser.rb', line 26

def history
  @history
end

#tagCountObject (readonly)

Returns the value of attribute tagCount.



26
27
28
# File 'lib/gooby_forerunner_xml_parser.rb', line 26

def tagCount
  @tagCount
end

#uomObject (readonly)

Returns the value of attribute uom.



26
27
28
# File 'lib/gooby_forerunner_xml_parser.rb', line 26

def uom
  @uom
end

Instance Method Details

#dumpObject

Iterate all parsed Run objects and print each with to_s.



160
161
162
# File 'lib/gooby_forerunner_xml_parser.rb', line 160

def dump()        
  @history.runs().each { |run| puts run.to_s } 
end

#gdumpObject

Iterate all parsed Run objects and print each with to_s.



155
156
157
# File 'lib/gooby_forerunner_xml_parser.rb', line 155

def gdump()      
  @history.runs().each { |run| puts run.to_s } 
end

#put_all_run_tkpt_csvObject

Iterate all parsed Run objects and print each with put_tkpt_csv.



170
171
172
# File 'lib/gooby_forerunner_xml_parser.rb', line 170

def put_all_run_tkpt_csv() 
  @history.runs.each { |run| run.put_tkpt_csv() } 
end

#put_run_csvObject

Iterate all parsed Run objects and print each with put_csv.



165
166
167
# File 'lib/gooby_forerunner_xml_parser.rb', line 165

def put_run_csv() 
  @history.runs().each { |run| run.put_csv() } 
end

#tag_end(tagname) ⇒ Object

SAX API method; handles ‘Position’, ‘Trackpoint’, ‘Track’, ‘Lap’, ‘Run’.



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

def tag_end(tagname) 
  if @inDetail    
    @cv_hash[tagname] = @curr_text
  else
    if is_tag?('Position', tagname)
      lat  = @cv_hash['Latitude']
      long = @cv_hash['Longitude']
      @curr_begin_position = Point.new(lat.strip, long.strip)
      @@curr_end_position  = Point.new(lat.strip, long.strip)       
    end 
    
    if is_tag?('BeginPosition', tagname)
      lat  = @cv_hash['Latitude']
      long = @cv_hash['Longitude']
      @curr_begin_position = Point.new(lat.strip, long.strip)      
    end
    
    if is_tag?('EndPosition', tagname)
      lat  = @cv_hash['Latitude']
      long = @cv_hash['Longitude']
      @@curr_end_position = Point.new(lat.strip, long.strip)      
    end
          
    if is_tag?('Trackpoint', tagname)
      @trackpoint_count = @trackpoint_count + 1
      lat  = @cv_hash['Latitude']
      long = @cv_hash['Longitude']
      alt  = @cv_hash['Altitude']   
      hb   = @cv_hash['HeartRateBpm']              
      time = @cv_hash['Time']            
      tp   = Trackpoint.new(@trackpoint_count, lat, long, alt, hb, time, @uom)
      @curr_track.add_trackpoint(tp)

      @cv_hash['Latitude']  = ''
      @cv_hash['Longitude'] = ''
      @cv_hash['Altitude']  = ''
      @cv_hash['HeartRateBpm'] = ''
      @cv_hash['Time'] = ''
    end
  
    if is_tag?('Track', tagname)
      if @curr_run != nil
        @curr_run.add_track(@curr_track)
      end
    end
  
    if is_tag?('Lap', tagname)
      @curr_lap.startTime = @cv_hash['StartTime']
      @curr_lap.duration  = Duration.new(@cv_hash['Duration'])
      @curr_lap.length    = @cv_hash['Length']                          
      @curr_lap.begin_position = @curr_begin_position
      @curr_lap.end_position   = @@curr_end_position        
      @curr_run.add_lap(@curr_lap)
    end
  
    if is_tag?('Run', tagname)
      @curr_run.notes =  @cv_hash['Notes'] 
    end
  end

  @inDetail = false    
  @curr_text = ""
  @currTag  = ""                     
end

#tag_start(tagname, attrs) ⇒ Object

SAX API method; handles ‘Run’, ‘Lap’, ‘Track’.



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

def tag_start(tagname, attrs) 
  @tag_count += 1
  @currTag = tagname
  @cv_hash[tagname] = ''

  if detail_tag?(tagname)
    @inDetail = true
  end

  if is_tag?('Run', tagname)
    @run_count = @run_count + 1
    @lap_count = 0
    @track_count = 0
    @trackpoint_count = 0        
    @curr_run  = Run.new(@run_count)
    @history.add_run(@curr_run)
    @cv_hash['Notes'] = ''
    return
  end

  if is_tag?('Lap', tagname)
    @lap_count = @lap_count + 1
    @curr_lap  = Lap.new(@lap_count)
    return
  end

  if is_tag?('Track', tagname)
    @track_count = @track_count + 1      
    @curr_track  = Track.new(@track_count)
    return   
  end
end

#text(txt) ⇒ Object

SAX API method.



148
149
150
151
152
# File 'lib/gooby_forerunner_xml_parser.rb', line 148

def text(txt)  
  if @inDetail
    @curr_text = @curr_text + txt   
  end
end