1 package org.apache.maven.jxr.pacman;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.nio.file.Path;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28
29 /**
30 * Interface for objects which wish to provide meta-info about a JavaFile.
31 *
32 * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
33 * @version $Id$
34 */
35 public abstract class JavaFile
36 {
37 private Set<ImportType> imports = new HashSet<>();
38
39 private List<ClassType> classTypes = new ArrayList<>();
40
41 private PackageType packageType = new PackageType();
42
43 private final Path path;
44
45 private String filename;
46
47 private final String encoding;
48
49 protected JavaFile( Path path, String encoding )
50 {
51 this.path = path;
52 this.encoding = encoding;
53 this.filename = getFilenameWithoutPathOrExtension( path );
54 }
55
56 /**
57 * Get the imported packages/files that this package has.
58 */
59 public Set<ImportType> getImportTypes()
60 {
61 return Collections.unmodifiableSet( imports );
62 }
63
64 /**
65 * Get the name of this class.
66 */
67 public ClassType getClassType()
68 {
69 if ( classTypes.isEmpty() )
70 {
71 return null;
72 }
73 else
74 {
75 // To retain backward compatibility, return the first class
76 return this.classTypes.get( 0 );
77 }
78 }
79
80 /**
81 * Get the names of the classes in this file.
82 */
83 public List<ClassType> getClassTypes()
84 {
85 return this.classTypes;
86 }
87
88 /**
89 * Get the package of this class.
90 */
91 public PackageType getPackageType()
92 {
93 return this.packageType;
94 }
95
96
97 /**
98 * Add a ClassType to the current list of class types.
99 */
100 public void addClassType( ClassType classType )
101 {
102 this.classTypes.add( classType );
103 }
104
105 /**
106 * Add an ImportType to the current imports.
107 */
108 public void addImportType( ImportType importType )
109 {
110 this.imports.add( importType );
111 }
112
113 /**
114 * Set the name of this class.
115 */
116 public void setClassType( ClassType classType )
117 {
118 // To retain backward compatibility, make sure the list contains only the supplied classType
119 this.classTypes.clear();
120 this.classTypes.add( classType );
121 }
122
123 /**
124 * Set the PackageType of this class.
125 */
126 public void setPackageType( PackageType packageType )
127 {
128 this.packageType = packageType;
129 }
130
131
132 /**
133 * Gets the filename attribute of the JavaFile object
134 */
135 public Path getPath()
136 {
137 return this.path;
138 }
139
140 /**
141 * File name without path and extension.
142 */
143 public String getFilename()
144 {
145 return filename;
146 }
147
148 /**
149 * Gets the encoding attribute of the JavaFile object
150 */
151 public String getEncoding()
152 {
153 return this.encoding;
154 }
155
156 /**
157 * Remove the path and the ".java" extension from a filename.
158 */
159 protected static String getFilenameWithoutPathOrExtension( Path path )
160 {
161 String newFilename = path.getFileName().toString();
162 // Remove the ".java" extension from the filename, if it exists
163 int extensionIndex = newFilename.lastIndexOf( ".java" );
164 if ( extensionIndex >= 0 )
165 {
166 newFilename = newFilename.substring( 0, extensionIndex );
167 }
168 return newFilename;
169 }
170
171 }