[NEW] Gestalts for silverlight + popular JS libs
[ohcount] / ruby / gestalt / rules / maven_parser.rb
1 #!/usr/bin/env ruby
2 require 'rexml/document'
3 require 'rexml/streamlistener'
4
5 class MavenListener
6         include REXML::StreamListener
7
8         attr_accessor :group_id, :artifact_id, :text
9
10         attr_accessor :callback
11         def initialize(callback)
12                 @callback = callback
13                 @is_pom_file = false
14         end
15
16         def tag_start(name, attrs)
17                 case name
18                 when 'project'
19                         @is_pom_file = true
20                         if attrs['xmlns'] and attrs['xmlns'] !~ /^http:\/\/maven\.apache\.org\/POM\//
21                                 # False alarm -- it contains a project element, but of another namespace
22                                 @is_pom_file = false
23                         end
24                 when 'plugin', 'dependency'
25                         @group_id = nil
26                         @artifact_id = nil
27                 end
28         end
29
30         def tag_end(name)
31                 case name
32                 when 'groupId'
33                         @group_id = clean(@text)
34                 when 'artifactId'
35                         @artifact_id = clean(@text)
36                 when /^(plugin|dependency)$/
37                         if @is_pom_file && @group_id && @artifact_id
38                                 @callback.call($1, @group_id, @artifact_id)
39                         end
40                 end
41         end
42
43         # Remove whitespace from text values.
44         # Also, clear out variable substitutions, which we are incapable of performing correctly
45         def clean(s)
46                 s.strip.gsub(/\$\{.*\}/, '')
47         end
48
49         def text(text)
50                 @text = text
51         end
52 end