1 /*
2 * YuiCompressor Maven plugin
3 *
4 * Copyright 2012-2025 Hazendaz.
5 *
6 * Licensed under the GNU Lesser General Public License (LGPL),
7 * version 2.1 or later (the "License").
8 * You may not use this file except in compliance with the License.
9 * You may read the licence in the 'lgpl.txt' file in the root folder of
10 * project or obtain a copy at
11 *
12 * https://www.gnu.org/licenses/lgpl-2.1.html
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" basis,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 package net.alchim31.maven.yuicompressor;
21
22 import java.io.File;
23
24 /**
25 * The Class SourceFile.
26 */
27 public class SourceFile {
28
29 /** The src root. */
30 private File srcRoot;
31
32 /** The dest root. */
33 private File destRoot;
34
35 /** The dest as source. */
36 private boolean destAsSource;
37
38 /** The rpath. */
39 private String rpath;
40
41 /** The extension. */
42 private String extension;
43
44 /**
45 * Instantiates a new source file.
46 *
47 * @param srcRoot
48 * the src root
49 * @param destRoot
50 * the dest root
51 * @param name
52 * the name
53 * @param destAsSource
54 * the dest as source
55 */
56 public SourceFile(File srcRoot, File destRoot, String name, boolean destAsSource) {
57 this.srcRoot = srcRoot;
58 this.destRoot = destRoot;
59 this.destAsSource = destAsSource;
60 this.rpath = name;
61 int sep = this.rpath.lastIndexOf('.');
62 if (sep > 0) {
63 this.extension = this.rpath.substring(sep);
64 this.rpath = rpath.substring(0, sep);
65 } else {
66 this.extension = "";
67 }
68 }
69
70 /**
71 * To file.
72 *
73 * @return the file
74 */
75 public File toFile() {
76 String frpath = rpath + extension;
77 if (destAsSource) {
78 File defaultDest = destRoot.toPath().resolve(frpath).toFile();
79 if (defaultDest.exists() && defaultDest.canRead()) {
80 return defaultDest;
81 }
82 }
83 return srcRoot.toPath().resolve(frpath).toFile();
84 }
85
86 /**
87 * To dest file.
88 *
89 * @param suffix
90 * the suffix
91 *
92 * @return the file
93 */
94 public File toDestFile(String suffix) {
95 return destRoot.toPath().resolve(rpath + suffix + extension).toFile();
96 }
97
98 /**
99 * Gets the extension.
100 *
101 * @return the extension
102 */
103 public String getExtension() {
104 return extension;
105 }
106 }