1   package com.stateofflow.eclipse.metrics.properties.exclusions;
2   
3   import java.io.IOException;
4   import java.util.ArrayList;
5   import java.util.HashMap;
6   import java.util.Iterator;
7   import java.util.List;
8   import java.util.Map;
9   
10  import org.apache.oro.text.regex.MalformedPatternException;
11  import org.apache.oro.text.regex.Pattern;
12  import org.apache.oro.text.regex.Perl5Compiler;
13  import org.apache.oro.text.regex.Perl5Matcher;
14  import org.eclipse.core.resources.IFile;
15  import org.eclipse.core.resources.IProject;
16  import org.eclipse.core.runtime.CoreException;
17  
18  public final class ExcludedResources {
19      private boolean modified;
20      private List<IFile> excludedFiles;
21      private Map<String, Pattern> regexToPatternMap;
22      private final IProject project;
23  
24      public ExcludedResources(final IProject project) throws IOException {
25          this.project = project;
26          initializeExcludedResources();
27      }
28  
29      void addFile(final IFile file) {
30          if (!file.getFileExtension().equals("java")) {
31              return;
32          }
33  
34          excludedFiles.add(file);
35          modified = true;
36      }
37  
38      public void addRegex(final String regex) throws MalformedPatternException {
39          final Pattern pattern = getRegexCompiler().compile(regex);
40          regexToPatternMap.put(regex, pattern);
41          modified = true;
42      }
43  
44      public void clearFiles() {
45          excludedFiles.clear();
46      }
47  
48      public boolean contains(final IFile file) {
49          return containsFile(file) || containsMatchingRegex(file.getProjectRelativePath().toString());
50      }
51  
52      private boolean containsFile(final IFile file) {
53          return excludedFiles.contains(file);
54      }
55  
56      private boolean containsMatchingRegex(final String path) {
57          if (regexToPatternMap.isEmpty()) {
58              return false;
59          }
60  
61          final Perl5Matcher matcher = new Perl5Matcher();
62          final Iterator<Pattern> iter = regexToPatternMap.values().iterator();
63          while (iter.hasNext()) {
64              if (matcher.matches(path, iter.next())) {
65                  return true;
66              }
67          }
68  
69          return false;
70      }
71  
72      public IFile[] getFiles() {
73          return excludedFiles.toArray(new IFile[excludedFiles.size()]);
74      }
75  
76      public IProject getProject() {
77          return project;
78      }
79  
80      private Perl5Compiler getRegexCompiler() {
81          return new Perl5Compiler();
82      }
83  
84      public String[] getRegexes() {
85          return regexToPatternMap.keySet().toArray(new String[regexToPatternMap.size()]);
86      }
87  
88      private void initializeExcludedResources() throws IOException {
89          excludedFiles = new ArrayList<IFile>();
90          regexToPatternMap = new HashMap<String, Pattern>();
91  
92          new Persister().read(project, this);
93          modified = false;
94      }
95  
96      public boolean isModified() {
97          return modified;
98      }
99  
100     public boolean isValidRegex(final String regex) {
101         try {
102             getRegexCompiler().compile(regex);
103             return true;
104         } catch (final MalformedPatternException mpex) {
105             return false;
106         }
107     }
108 
109     public void removeFile(final IFile file) {
110         modified |= excludedFiles.remove(file);
111     }
112 
113     public void removeRegex(final String regex) {
114         modified |= regexToPatternMap.containsKey(regex);
115         regexToPatternMap.remove(regex);
116     }
117 
118     public void store() throws CoreException {
119         if (modified) {
120             new Persister().write(project, excludedFiles, regexToPatternMap.keySet());
121             modified = false;
122         }
123     }
124 }
125