View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.checkstyle;
6   
7   import java.io.File;
8   import java.io.IOException;
9   import java.nio.charset.StandardCharsets;
10  import org.apache.commons.io.FileUtils;
11  import org.cactoos.text.IoCheckedText;
12  import org.cactoos.text.Joined;
13  
14  /**
15   * Builder of {@code LICENSE.txt} content.
16   *
17   * @since 0.4
18   */
19  public final class License {
20  
21      /**
22       * The text.
23       */
24      private String[] lines;
25  
26      /**
27       * EOL.
28       */
29      private String eol;
30  
31      /**
32       * Package name.
33       */
34      private String pkg;
35  
36      /**
37       * Directory for package-info.java.
38       */
39      private File directory;
40  
41      /**
42       * Ctor.
43       */
44      public License() {
45          this.pkg = "foo";
46      }
47  
48      /**
49       * Use this EOL.
50       *
51       * @param txt What to use as end-of-line character
52       * @return This object
53       */
54      public License withEol(final String txt) {
55          this.eol = txt;
56          return this;
57      }
58  
59      /**
60       * Use this text (lines).
61       *
62       * @param lns The lines to use
63       * @return This object
64       */
65      public License withLines(final String... lns) {
66          this.lines = new String[lns.length];
67          System.arraycopy(lns, 0, this.lines, 0, lns.length);
68          return this;
69      }
70  
71      /**
72       * Use this package name.
73       *
74       * @param name The name of package
75       * @return This object
76       */
77      public License withPackage(final String name) {
78          this.pkg = name;
79          return this;
80      }
81  
82      /**
83       * Save package-info.java into this folder.
84       *
85       * @param dir The folder to save to
86       * @return This object
87       */
88      public License savePackageInfo(final File dir) {
89          this.directory = dir;
90          return this;
91      }
92  
93      /**
94       * Make a file.
95       *
96       * @return The location of LICENSE.txt
97       * @throws IOException If something wrong happens inside
98       */
99      public File file() throws IOException {
100         final File license = File.createTempFile("LICENSE", ".txt");
101         FileUtils.forceDeleteOnExit(license);
102         FileUtils.writeStringToFile(
103             license,
104             new IoCheckedText(new Joined(this.eol, this.lines)).asString(),
105             StandardCharsets.UTF_8
106         );
107         if (this.directory != null) {
108             this.makePackageInfo(this.directory);
109         }
110         return license;
111     }
112 
113     private void makePackageInfo(final File dir) throws IOException {
114         final StringBuilder body = new StringBuilder(100);
115         body.append("/*").append(this.eol);
116         for (final String line : this.lines) {
117             body.append(" *");
118             if (!line.isEmpty()) {
119                 body.append(' ').append(line);
120             }
121             body.append(this.eol);
122         }
123         body.append(" */").append(this.eol)
124             .append("/**").append(this.eol)
125             .append(" * Hm...").append(this.eol)
126             .append(" */").append(this.eol)
127             .append("package ").append(this.pkg)
128             .append(';').append(this.eol);
129         FileUtils.writeStringToFile(
130             new File(dir, "package-info.java"),
131             body.toString(),
132             StandardCharsets.UTF_8
133         );
134     }
135 }