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   * @since 0.4
17   */
18  public final class License {
19  
20      /**
21       * The text.
22       */
23      private String[] lines;
24  
25      /**
26       * EOL.
27       */
28      private String eol;
29  
30      /**
31       * Package name.
32       */
33      private String pkg = "foo";
34  
35      /**
36       * Directory for package-info.java.
37       */
38      private File directory;
39  
40      /**
41       * Use this EOL.
42       * @param txt What to use as end-of-line character
43       * @return This object
44       */
45      public License withEol(final String txt) {
46          this.eol = txt;
47          return this;
48      }
49  
50      /**
51       * Use this text (lines).
52       * @param lns The lines to use
53       * @return This object
54       */
55      public License withLines(final String... lns) {
56          this.lines = new String[lns.length];
57          System.arraycopy(lns, 0, this.lines, 0, lns.length);
58          return this;
59      }
60  
61      /**
62       * Use this package name.
63       * @param name The name of package
64       * @return This object
65       */
66      public License withPackage(final String name) {
67          this.pkg = name;
68          return this;
69      }
70  
71      /**
72       * Save package-info.java into this folder.
73       * @param dir The folder to save to
74       * @return This object
75       */
76      public License savePackageInfo(final File dir) {
77          this.directory = dir;
78          return this;
79      }
80  
81      /**
82       * Make a file.
83       * @return The location of LICENSE.txt
84       * @throws IOException If something wrong happens inside
85       */
86      public File file() throws IOException {
87          final File license = File.createTempFile("LICENSE", ".txt");
88          FileUtils.forceDeleteOnExit(license);
89          FileUtils.writeStringToFile(
90              license,
91              new IoCheckedText(new Joined(this.eol, this.lines)).asString(),
92              StandardCharsets.UTF_8
93          );
94          if (this.directory != null) {
95              this.makePackageInfo(this.directory);
96          }
97          return license;
98      }
99  
100     /**
101      * Save package-info.java to the directory.
102      * @param dir The directory
103      * @throws IOException If something wrong happens inside
104      */
105     @SuppressWarnings("PMD.AvoidDuplicateLiterals")
106     private void makePackageInfo(final File dir) throws IOException {
107         final File info = new File(dir, "package-info.java");
108         final StringBuilder body = new StringBuilder(100);
109         body.append("/*").append(this.eol);
110         for (final String line : this.lines) {
111             body.append(" *");
112             if (!line.isEmpty()) {
113                 body.append(' ').append(line);
114             }
115             body.append(this.eol);
116         }
117         body.append(" */").append(this.eol)
118             .append("/**").append(this.eol)
119             .append(" * Hm...").append(this.eol)
120             .append(" */").append(this.eol)
121             .append("package ").append(this.pkg)
122             .append(';').append(this.eol);
123         FileUtils.writeStringToFile(
124             info,
125             body.toString(),
126             StandardCharsets.UTF_8
127         );
128     }
129 
130 }