View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */ 
19  package org.apache.rat.document.impl;
20  
21  import java.io.File;
22  import java.io.FileReader;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.Reader;
26  import java.nio.file.Files;
27  import java.nio.file.Path;
28  import java.util.Arrays;
29  import java.util.Collections;
30  import java.util.SortedSet;
31  import java.util.TreeSet;
32  
33  import org.apache.rat.api.Document;
34  
35  /**
36   * Document wrapping a File object
37   */
38  public class FileDocument extends Document {
39  
40      /** the wrapped file */
41      private final File file;
42  
43      /**
44       * Creates a File document.
45       * @param file the file to wrap.
46       */
47      public FileDocument(final File file) {
48          super(normalizeFileName(file));
49          this.file = file;
50      }
51  
52      /**
53       * Normalizes a file name to linux style.  Accounts for Windows to Unix conversion.
54       * @param file The file to normalize
55       * @return the String for the file name.
56       */
57      public final static String normalizeFileName(final File file) {
58          String path = file.getPath();
59          return path.replace('\\', '/');
60      }
61  
62      @Override
63      public Path getPath() {
64          return file.toPath();
65      }
66  
67      @Override
68      public boolean isDirectory() {
69          return file.isDirectory();
70      }
71  
72      @Override
73      public SortedSet<Document> listChildren() {
74          if (isDirectory()) {
75              SortedSet<Document> result = new TreeSet<>();
76              Arrays.stream(file.listFiles()).map(f -> new FileDocument(f)).forEach(result::add);
77              return result;
78          }
79          return Collections.emptySortedSet();
80      }
81  
82      public Reader reader() throws IOException {
83          return new FileReader(file);
84      }
85  
86      public InputStream inputStream() throws IOException {
87          return Files.newInputStream(file.toPath());
88      }
89  }