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.commons.vfs2;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  /**
23   * Info about a file.
24   */
25  public class FileInfo {
26  
27      String baseName;
28      FileType type;
29      String content;
30      Map<String, FileInfo> children = new HashMap<>();
31      FileInfo parent;
32  
33      public FileInfo(final String name, final FileType type) {
34          baseName = name;
35          this.type = type;
36          this.content = null;
37      }
38  
39      public FileInfo(final String name, final FileType type, final String content) {
40          baseName = name;
41          this.type = type;
42          this.content = content;
43      }
44  
45      /**
46       * Adds a child.
47       */
48      public void addChild(final FileInfo child) {
49          children.put(child.baseName, child);
50          child.parent = this;
51      }
52  
53      /**
54       * Adds a child file.
55       */
56      public FileInfo addFile(final String baseName, final String content) {
57          final FileInfonfo.html#FileInfo">FileInfo child = new FileInfo(baseName, FileType.FILE, content);
58          addChild(child);
59          return child;
60      }
61  
62      /**
63       * Adds a child folder.
64       */
65      public FileInfo addFolder(final String baseName) {
66          final FileInfonfo.html#FileInfo">FileInfo child = new FileInfo(baseName, FileType.FOLDER, null);
67          addChild(child);
68          return child;
69      }
70  
71      /**
72       * Returns the base name for the file.
73       *
74       * @return the base name
75       */
76      public String getBaseName() {
77          return baseName;
78      }
79  
80      /**
81       * Returns a {@code Map} of this {@code FileInfo}'s children.
82       * @return the {@code FileInfo}'s children
83       */
84      public Map<String, FileInfo> getChildren() {
85          return children;
86      }
87  
88      /**
89       * Returns file's content.
90       *
91       * @return the content as a {@code String}
92       */
93      public String getContent() {
94          return content;
95      }
96  
97      public FileInfo getParent() {
98          return parent;
99      }
100 
101     /**
102      * Returns the {@link FileType} of the file
103      *
104      * @return {@link FileType}
105      */
106     public FileType getType() {
107         return type;
108     }
109 }