View Javadoc
1   /*
2    * SPDX-License-Identifier: LGPL-2.1-or-later
3    * See LICENSE file for details.
4    *
5    * Copyright 2012-2026 Hazendaz.
6    */
7   package net.alchim31.maven.yuicompressor;
8   
9   import java.io.File;
10  
11  /**
12   * The Class SourceFile.
13   */
14  public class SourceFile {
15  
16      /** The src root. */
17      private File srcRoot;
18  
19      /** The dest root. */
20      private File destRoot;
21  
22      /** The dest as source. */
23      private boolean destAsSource;
24  
25      /** The rpath. */
26      private String rpath;
27  
28      /** The extension. */
29      private String extension;
30  
31      /**
32       * Instantiates a new source file.
33       *
34       * @param srcRoot
35       *            the src root
36       * @param destRoot
37       *            the dest root
38       * @param name
39       *            the name
40       * @param destAsSource
41       *            the dest as source
42       */
43      public SourceFile(File srcRoot, File destRoot, String name, boolean destAsSource) {
44          this.srcRoot = srcRoot;
45          this.destRoot = destRoot;
46          this.destAsSource = destAsSource;
47          this.rpath = name;
48          int sep = this.rpath.lastIndexOf('.');
49          if (sep > 0) {
50              this.extension = this.rpath.substring(sep);
51              this.rpath = rpath.substring(0, sep);
52          } else {
53              this.extension = "";
54          }
55      }
56  
57      /**
58       * To file.
59       *
60       * @return the file
61       */
62      public File toFile() {
63          String frpath = rpath + extension;
64          if (destAsSource) {
65              File defaultDest = destRoot.toPath().resolve(frpath).toFile();
66              if (defaultDest.exists() && defaultDest.canRead()) {
67                  return defaultDest;
68              }
69          }
70          return srcRoot.toPath().resolve(frpath).toFile();
71      }
72  
73      /**
74       * To dest file.
75       *
76       * @param suffix
77       *            the suffix
78       *
79       * @return the file
80       */
81      public File toDestFile(String suffix) {
82          return destRoot.toPath().resolve(rpath + suffix + extension).toFile();
83      }
84  
85      /**
86       * Gets the extension.
87       *
88       * @return the extension
89       */
90      public String getExtension() {
91          return extension;
92      }
93  }