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.nio.file.Path;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Set;
27  
28  /**
29   * Interface for objects which wish to provide meta-info about a JavaFile.
30   *
31   * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
32   */
33  public abstract class JavaFile {
34      private Set<ImportType> imports = new HashSet<>();
35  
36      private List<ClassType> classTypes = new ArrayList<>();
37  
38      private PackageType packageType = new PackageType();
39  
40      private final Path path;
41  
42      private String filename;
43  
44      private final String encoding;
45  
46      protected JavaFile(Path path, String encoding) {
47          this.path = path;
48          this.encoding = encoding;
49          this.filename = getFilenameWithoutPathOrExtension(path);
50      }
51  
52      /**
53       * Gets the imported packages/files that this package has.
54       *
55       * @return import types
56       */
57      public Set<ImportType> getImportTypes() {
58          return Collections.unmodifiableSet(imports);
59      }
60  
61      /**
62       * Gets the name of this class.
63       * @return class type
64       */
65      public ClassType getClassType() {
66          if (classTypes.isEmpty()) {
67              return null;
68          } else {
69              // to retain backward compatibility, return the first class
70              return this.classTypes.get(0);
71          }
72      }
73  
74      /**
75       * Gets the names of the classes in this file.
76       *
77       * @return list of class types
78       */
79      public List<ClassType> getClassTypes() {
80          return this.classTypes;
81      }
82  
83      /**
84       * Gets the package of this class.
85       *
86       * @return package type
87       */
88      public PackageType getPackageType() {
89          return this.packageType;
90      }
91  
92      /**
93       * Add a class type to the current list of class types.
94       *
95       * @param classType class type
96       */
97      public void addClassType(ClassType classType) {
98          this.classTypes.add(classType);
99      }
100 
101     /**
102      * Add an import type.
103      *
104      * @param importType import type
105      */
106     public void addImportType(ImportType importType) {
107         this.imports.add(importType);
108     }
109 
110     /**
111      * Sets the name of this class.
112      *
113      * @param classType class type
114      */
115     public void setClassType(ClassType classType) {
116         // to retain backward compatibility, make sure the list contains only the supplied classType
117         this.classTypes.clear();
118         this.classTypes.add(classType);
119     }
120 
121     /**
122      * Sets the package type of this class.
123      *
124      * @param packageType package type
125      */
126     public void setPackageType(PackageType packageType) {
127         this.packageType = packageType;
128     }
129 
130     /**
131      * Gets the path attribute.
132      *
133      * @return path
134      */
135     public Path getPath() {
136         return this.path;
137     }
138 
139     /**
140      * Gets the file name without path and extension.
141      *
142      * @return file name
143      */
144     public String getFilename() {
145         return filename;
146     }
147 
148     /**
149      * Gets the encoding attribute.
150      *
151      * @return encoding
152      */
153     public String getEncoding() {
154         return this.encoding;
155     }
156 
157     /**
158      * Remove the path and the ".java" extension from a filename.
159      *
160      * @param path path to modify
161      * @return modified path
162      */
163     protected static String getFilenameWithoutPathOrExtension(Path path) {
164         String newFilename = path.getFileName().toString();
165         // Remove the ".java" extension from the filename, if it exists
166         int extensionIndex = newFilename.lastIndexOf(".java");
167         if (extensionIndex >= 0) {
168             newFilename = newFilename.substring(0, extensionIndex);
169         }
170         return newFilename;
171     }
172 }