[NEW] Gestalts for silverlight + popular JS libs
[ohcount] / ruby / gestalt / rules / find_java_imports_rule.rb
1 module Ohcount
2         module Gestalt
3
4     # Will yield one trigger per java import - each with the name of the imported
5     # namespace. Example java source file:
6     #
7     # import com.foo.bar;
8     #
9     # will yield a trigger with name='com.foo.bar'
10                 class FindJavaImportsRule < FileRule
11                         attr_reader :keywords, :language
12
13       def initialize(*args)
14         @trigger_hash = {}
15         super(*args)
16       end
17
18                         def process_source_file(source_file)
19                                 return unless source_file.language_breakdown('java')
20
21         java_code = source_file.language_breakdown('java').code
22         java_code.scan(import_regexp).each do |matches|
23           name = matches[0]
24           @trigger_hash[name] = @trigger_hash[name].to_i + 1
25         end
26                         end
27
28       # implement deep clone
29       def clone
30         self.class.new(:min => @min_count)
31       end
32
33       def triggers(gestalt_engine)
34         triggers = []
35         @trigger_hash.each do |k,v|
36           triggers << Trigger.new(:name => FindJavaImportsRule.truncate_name(k, 3), :count => v)
37         end
38         triggers
39       end
40
41                         def import_regexp
42                                 /^\s*import\s+([a-zA-Z][\w\.\*\-]*)\b/
43                         end
44
45                         # Truncates the java import namespace to a maximum depth.
46                         # For instance,
47                         #    truncate_name("com.sun.identity.authentication", 3) => "com.sun.identity"
48                         def self.truncate_name(s, max_depth)
49                                 s.to_s.split('.')[0, max_depth].join('.')
50                         end
51
52                 end
53         end
54 end