Class: Gooby::TrainingCenterXmlParser

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

Overview

Instances of this class are used to parse a Garmin TrainingCenter XML(TCX) 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/TrainingCenterDatabasev2.xsd for the XML
Schema Definition for Garmin TrainingCenter XML.

Constant Summary collapse

DETAIL_TAGS =
%w( Notes StartTime Duration Length Time
TotalTimeSeconds DistanceMeters   
LatitudeDegrees LongitudeDegrees AltitudeMeters HeartRateBpm Value BeginPosition EndPosition )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uom) ⇒ TrainingCenterXmlParser

Returns a new instance of TrainingCenterXmlParser.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gooby_training_center_xml_parser.rb', line 30

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
  @first_lap_start_time  = nil
  @curr_lap_start_time   = ''
  @in_heart_rate_bpm     = false
  @in_value              = false      
end

Instance Attribute Details

#cvHashObject (readonly)

Returns the value of attribute cvHash.



28
29
30
# File 'lib/gooby_training_center_xml_parser.rb', line 28

def cvHash
  @cvHash
end

#historyObject (readonly)

Returns the value of attribute history.



28
29
30
# File 'lib/gooby_training_center_xml_parser.rb', line 28

def history
  @history
end

#tagCountObject (readonly)

Returns the value of attribute tagCount.



28
29
30
# File 'lib/gooby_training_center_xml_parser.rb', line 28

def tagCount
  @tagCount
end

#uomObject (readonly)

Returns the value of attribute uom.



28
29
30
# File 'lib/gooby_training_center_xml_parser.rb', line 28

def uom
  @uom
end

Instance Method Details

#dumpObject

Iterate all parsed Run objects and print each with to_s.



193
194
195
# File 'lib/gooby_training_center_xml_parser.rb', line 193

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

#gdumpObject

Iterate all parsed Run objects and print each with to_s.



188
189
190
# File 'lib/gooby_training_center_xml_parser.rb', line 188

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.



203
204
205
# File 'lib/gooby_training_center_xml_parser.rb', line 203

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.



198
199
200
# File 'lib/gooby_training_center_xml_parser.rb', line 198

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’, ‘HeartRateBpm’, ‘Value’.



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
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/gooby_training_center_xml_parser.rb', line 105

def tag_end(tagname) 
  if @inDetail 
    if is_tag?('Value',  tagname) && @in_heart_rate_bpm
      @cv_hash['HeartRateBpm'] = "#{@curr_text.strip!}" 
    else
      @cv_hash[tagname] = @curr_text          
    end           
  else
    if is_tag?('Position', tagname)
      lat  = @cv_hash['LatitudeDegrees']
      long = @cv_hash['LongitudeDegrees']
      @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['LatitudeDegrees']
      long = @cv_hash['LongitudeDegrees']
      @curr_begin_position = Point.new(lat.strip, long.strip)      
    end
    
    if is_tag?('EndPosition', tagname)
      lat  = @cv_hash['LatitudeDegrees']
      long = @cv_hash['LongitudeDegrees']
      @@curr_end_position = Point.new(lat.strip, long.strip)      
    end
          
    if is_tag?('Trackpoint', tagname)
      @trackpoint_count = @trackpoint_count + 1
      lat  = @cv_hash['LatitudeDegrees']
      long = @cv_hash['LongitudeDegrees']
      alt  = @cv_hash['AltitudeMeters'] 
      hb   = @cv_hash['HeartRateBpm'] 
      hb ? hb = hb.to_s : hb = '-1'
      time = @cv_hash['Time'] 
      hash = Hash.new('')
      hash['lap_number']           = "#{@lap_count}"          
      hash['first_lap_start_time'] = "#{@first_lap_start_time}"
      hash['curr_lap_start_time']  = "#{@curr_lap_start_time}"                           
      tp = Trackpoint.new(@trackpoint_count, lat, long, alt, hb, time, @uom, hash)
      @curr_track.add_trackpoint(tp) if tp.has_coordinates?
      
      @cv_hash['LatitudeDegrees']  = ''
      @cv_hash['LongitudeDegrees'] = ''
      @cv_hash['AltitudeMeters']   = ''
      @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_run.add_lap(@curr_lap)
    end
  
    if is_tag?('Activity', tagname)
      @curr_run.notes =  @cv_hash['Notes'] 
    end
    
    if is_tag?('HeartRateBpm', tagname)
      @in_heart_rate_bpm = false
      return   
    end

  end

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

#tag_start(tagname, attrs) ⇒ Object

SAX API method; handles ‘Activity’, ‘Lap’, ‘Track’, ‘HeartRateBpm’.



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

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

  if detail_tag?(tagname)
    @inDetail = true
  end

  if is_tag?('Activity', 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)
    
    attrs.each { |attr|
      name = attr[0]
      val  = attr[1]
      if (name && (name == 'StartTime')) 
        if (@first_lap_start_time == nil)
          @first_lap_start_time = "#{val}"
        end
        @curr_lap_start_time = "#{val}" 
      end
    }
    return
  end

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

  if is_tag?('HeartRateBpm', tagname)
    @in_heart_rate_bpm = true
    return   
  end

end

#text(txt) ⇒ Object

SAX API method.



181
182
183
184
185
# File 'lib/gooby_training_center_xml_parser.rb', line 181

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