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.maven.jxr.pacman;
20  
21  import java.io.IOException;
22  import java.nio.file.Path;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  /**
27   * <p>
28   *
29   * Singleton that handles holding references to JavaFiles. This allows
30   * Alexandria to lookup and see if a file has already been parsed out and then
31   * it can load the information from memory instead of reparsing the file. </p>
32   * <p>
33   *
34   * Note. This assumes that the file will not be modified on disk while
35   * Alexandria is running. </p>
36   */
37  public class FileManager {
38      private Map<Path, JavaFile> files = new HashMap<>();
39  
40      private String encoding = null;
41  
42      /**
43       * Gets a file from its name.<br>
44       * If the file does not exist within the FileManager, creates a new one and returns it.
45       *
46       * @param path path of the file
47       * @return the {@link JavaFile} meta object for the specified file
48       * @throws IOException on parsing failure
49       */
50      public JavaFile getFile(Path path) throws IOException {
51  
52          JavaFile real = this.files.get(path);
53  
54          if (real == null) {
55              real = new JavaFileImpl(path, this.getEncoding());
56              this.addFile(real);
57          }
58  
59          return real;
60      }
61  
62      /**
63       * Add a file to this file manager.
64       * @param file file to add
65       */
66      public void addFile(JavaFile file) {
67          this.files.put(file.getPath(), file);
68      }
69  
70      /**
71       * Sets the encoding of source files.
72       *
73       * @param encoding encoding of source files
74       */
75      public void setEncoding(String encoding) {
76          this.encoding = encoding;
77      }
78  
79      /**
80       * Gets the encoding of source files.
81       *
82       * @return encoding
83       */
84      public String getEncoding() {
85          return encoding;
86      }
87  }