View Javadoc
1   /*
2    * Copyright (c) 2011-2025 Yegor Bugayenko
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.checkstyle;
32  
33  import java.io.File;
34  import java.io.IOException;
35  import java.nio.charset.StandardCharsets;
36  import org.apache.commons.io.FileUtils;
37  import org.cactoos.text.IoCheckedText;
38  import org.cactoos.text.Joined;
39  
40  /**
41   * Builder of {@code LICENSE.txt} content.
42   * @since 0.4
43   */
44  public final class License {
45  
46      /**
47       * The text.
48       */
49      private String[] lines;
50  
51      /**
52       * EOL.
53       */
54      private String eol;
55  
56      /**
57       * Package name.
58       */
59      private String pkg = "foo";
60  
61      /**
62       * Directory for package-info.java.
63       */
64      private File directory;
65  
66      /**
67       * Use this EOL.
68       * @param txt What to use as end-of-line character
69       * @return This object
70       */
71      public License withEol(final String txt) {
72          this.eol = txt;
73          return this;
74      }
75  
76      /**
77       * Use this text (lines).
78       * @param lns The lines to use
79       * @return This object
80       */
81      public License withLines(final String... lns) {
82          this.lines = new String[lns.length];
83          System.arraycopy(lns, 0, this.lines, 0, lns.length);
84          return this;
85      }
86  
87      /**
88       * Use this package name.
89       * @param name The name of package
90       * @return This object
91       */
92      public License withPackage(final String name) {
93          this.pkg = name;
94          return this;
95      }
96  
97      /**
98       * Save package-info.java into this folder.
99       * @param dir The folder to save to
100      * @return This object
101      */
102     public License savePackageInfo(final File dir) {
103         this.directory = dir;
104         return this;
105     }
106 
107     /**
108      * Make a file.
109      * @return The location of LICENSE.txt
110      * @throws IOException If something wrong happens inside
111      */
112     public File file() throws IOException {
113         final File license = File.createTempFile("LICENSE", ".txt");
114         FileUtils.forceDeleteOnExit(license);
115         FileUtils.writeStringToFile(
116             license,
117             new IoCheckedText(new Joined(this.eol, this.lines)).asString(),
118             StandardCharsets.UTF_8
119         );
120         if (this.directory != null) {
121             this.makePackageInfo(this.directory);
122         }
123         return license;
124     }
125 
126     /**
127      * Save package-info.java to the directory.
128      * @param dir The directory
129      * @throws IOException If something wrong happens inside
130      */
131     @SuppressWarnings("PMD.AvoidDuplicateLiterals")
132     private void makePackageInfo(final File dir) throws IOException {
133         final File info = new File(dir, "package-info.java");
134         final StringBuilder body = new StringBuilder(100);
135         body.append("/*").append(this.eol);
136         for (final String line : this.lines) {
137             body.append(" *");
138             if (!line.isEmpty()) {
139                 body.append(' ').append(line);
140             }
141             body.append(this.eol);
142         }
143         body.append(" */").append(this.eol)
144             .append("/**").append(this.eol)
145             .append(" * Hm...").append(this.eol)
146             .append(" */").append(this.eol)
147             .append("package ").append(this.pkg)
148             .append(';').append(this.eol);
149         FileUtils.writeStringToFile(
150             info,
151             body.toString(),
152             StandardCharsets.UTF_8
153         );
154     }
155 
156 }