[NEW] Gestalts for silverlight + popular JS libs
[ohcount] / ruby / gestalt / rules / maven_rule.rb
1 require 'rexml/document'
2 module Ohcount
3         module Gestalt
4                 class MavenRule < FileRule
5
6                         attr_accessor :group, :artifact, :type
7
8                         # Type is either 'dependency' or 'plugin'
9                         def initialize(*args)
10                                 @type = args.shift
11
12                                 @group = args.shift
13                                 @group = /^#{Regexp.escape(@group.to_s)}$/ unless @group.is_a? Regexp
14
15                                 @artifact = args.shift || /.*/
16                                 @artifact = /^#{Regexp.escape(@artifact.to_s)}$/ unless @artifact.is_a? Regexp
17
18                                 super(args)
19                         end
20
21       def process_source_file(source_file)
22                                 callback = lambda do |type, group, artifact|
23                                         if type == @type and group =~ @group and artifact =~ @artifact
24                                                 @count += 1
25                                         end
26                                 end
27
28         if source_file.filename =~ /\bpom\.xml$/ && source_file.language_breakdown('xml')
29                                         begin
30                 REXML::Document.parse_stream(source_file.language_breakdown('xml').code, MavenListener.new(callback))
31                                         rescue REXML::ParseException
32                                                 # Malformed XML! -- ignore and move on
33                                         end
34         end
35       end
36
37                 end
38         end
39 end