View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.creadur.tentacles;
18  
19  import java.io.File;
20  import java.io.FileFilter;
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.regex.Pattern;
24  
25  import org.apache.creadur.tentacles.filter.Filters;
26  
27  public class FileSystem {
28  
29      private final Filters filters;
30  
31      public FileSystem() {
32          this.filters = new Filters();
33      }
34  
35      public List<File> legalDocumentsUndeclaredIn(final File contents) {
36          return collect(contents,
37                  this.filters.legalDocumentsUndeclaredIn(contents));
38      }
39  
40      public List<File> legalDocumentsDeclaredIn(final File contents) {
41          return collect(contents,
42                  this.filters.legalDocumentsDeclaredIn(contents));
43      }
44  
45      public List<File> collect(final File dir, final String regex) {
46          return collect(dir, Pattern.compile(regex));
47      }
48  
49      private List<File> collect(final File dir, final Pattern pattern) {
50          return collect(dir, new FileFilter() {
51              @Override
52              public boolean accept(final File file) {
53                  return pattern.matcher(file.getAbsolutePath()).matches();
54              }
55          });
56      }
57  
58      public List<File> collect(final File dir, final FileFilter filter) {
59          final List<File> accepted = new ArrayList<File>();
60          if (filter.accept(dir)) {
61              accepted.add(dir);
62          }
63  
64          final File[] files = dir.listFiles();
65          if (files != null) {
66              for (final File file : files) {
67                  accepted.addAll(collect(file, filter));
68              }
69          }
70  
71          return accepted;
72      }
73  
74      public void mkparent(final File file) {
75          mkdirs(file.getParentFile());
76      }
77  
78      public void mkdirs(final File file) {
79  
80          if (!file.exists()) {
81  
82              final boolean success = file.mkdirs();
83              assert success : "mkdirs failed to create " + file;
84  
85              return;
86          }
87  
88          final boolean isDirectory = file.isDirectory();
89          assert isDirectory : "Not a directory: " + file;
90      }
91  
92      public List<File> documentsFrom(final File repository) {
93          return collect(repository, this.filters.filesOnly());
94      }
95  
96      public List<File> licensesFrom(final File directory) {
97          return collect(directory, this.filters.licensesOnly());
98      }
99  
100     public List<File> noticesOnly(final File directory) {
101         return collect(directory, this.filters.noticesOnly());
102     }
103 
104     public List<File> licensesDeclaredIn(final File contents) {
105         return collect(contents, this.filters.licensesDeclaredIn(contents));
106     }
107 
108     public List<File> noticesDeclaredIn(final File contents) {
109         return collect(contents, this.filters.noticesDeclaredIn(contents));
110     }
111 
112     public List<File> archivesInPath(final File file,
113             final String fileRepositoryPathNameFilter) {
114         return collect(file, this.filters.archivesInPath(fileRepositoryPathNameFilter));
115     }
116 }