View Javadoc
1   /*
2    * SPDX-License-Identifier: Apache-2.0
3    * See LICENSE file for details.
4    *
5    * Copyright 2018-2026 Hazendaz
6    * Copyright 2011-2018 tunyk
7    */
8   package com.tunyk.mvn.plugins.htmlcompressor;
9   
10  import java.io.IOException;
11  import java.nio.charset.Charset;
12  import java.nio.file.Files;
13  import java.nio.file.Path;
14  import java.util.Map.Entry;
15  import java.util.concurrent.ConcurrentMap;
16  
17  /**
18   * The Class HtmlCompressor.
19   */
20  public class HtmlCompressor {
21  
22      /** The Constant FILE_EXT. */
23      private static final String[] FILE_EXT = { "htm", "html" };
24  
25      /** The file extensions. */
26      private String[] fileExtensions;
27  
28      /** The src dir path. */
29      private String srcDirPath;
30  
31      /** The target dir path. */
32      private String targetDirPath;
33  
34      /** The file encoding. */
35      private Charset fileEncoding;
36  
37      /** The create json file. */
38      private boolean createJsonFile;
39  
40      /** The target json file path. */
41      private String targetJsonFilePath;
42  
43      /** The json integration file path. */
44      private String jsonIntegrationFilePath;
45  
46      /** The html compressor. */
47      private com.googlecode.htmlcompressor.compressor.HtmlCompressor htmlCompressor;
48  
49      /**
50       * Instantiates a new html compressor.
51       *
52       * @param srcDirPath
53       *            the src dir path
54       * @param targetDirPath
55       *            the target dir path
56       */
57      public HtmlCompressor(String srcDirPath, String targetDirPath) {
58          this.srcDirPath = srcDirPath;
59          this.targetDirPath = targetDirPath;
60      }
61  
62      /**
63       * Instantiates a new html compressor.
64       *
65       * @param srcDirPath
66       *            the src dir path
67       * @param targetDirPath
68       *            the target dir path
69       * @param createJsonFile
70       *            the create json file
71       * @param targetJsonFilePath
72       *            the target json file path
73       * @param jsonIntegrationFilePath
74       *            the json integration file path
75       */
76      public HtmlCompressor(String srcDirPath, String targetDirPath, boolean createJsonFile, String targetJsonFilePath,
77              String jsonIntegrationFilePath) {
78          this.srcDirPath = srcDirPath;
79          this.targetDirPath = targetDirPath;
80          this.createJsonFile = createJsonFile;
81          this.targetJsonFilePath = targetJsonFilePath;
82          this.jsonIntegrationFilePath = jsonIntegrationFilePath;
83      }
84  
85      /**
86       * Compress.
87       *
88       * @throws IOException
89       *             Signals that an I/O exception has occurred.
90       */
91      public void compress() throws IOException {
92          if (fileExtensions == null || fileExtensions.length == 0) {
93              fileExtensions = FILE_EXT;
94          }
95  
96          FileTool fileTool = new FileTool(srcDirPath, fileExtensions, true);
97          fileTool.setFileEncoding(fileEncoding);
98          ConcurrentMap<String, String> map = fileTool.getFiles();
99  
100         if (htmlCompressor == null) {
101             htmlCompressor = new com.googlecode.htmlcompressor.compressor.HtmlCompressor();
102         }
103 
104         for (Entry<String, String> key : map.entrySet()) {
105             map.put(key.getKey(), htmlCompressor.compress(key.getValue()));
106         }
107 
108         fileTool.writeFiles(map, targetDirPath);
109         if (createJsonFile) {
110             String jsonIntegrationCode = Files.readString(Path.of(jsonIntegrationFilePath),
111                     fileEncoding == null ? Charset.defaultCharset() : fileEncoding);
112             fileTool.writeToJsonFile(map, targetJsonFilePath, jsonIntegrationCode);
113         }
114     }
115 
116     /**
117      * Gets the file extensions.
118      *
119      * @return the file extensions
120      */
121     public String[] getFileExtensions() {
122         return fileExtensions;
123     }
124 
125     /**
126      * Sets the file extensions.
127      *
128      * @param fileExtensions
129      *            the new file extensions
130      */
131     public void setFileExtensions(String[] fileExtensions) {
132         this.fileExtensions = fileExtensions;
133     }
134 
135     /**
136      * Gets the src dir path.
137      *
138      * @return the src dir path
139      */
140     public String getSrcDirPath() {
141         return srcDirPath;
142     }
143 
144     /**
145      * Sets the src dir path.
146      *
147      * @param srcDirPath
148      *            the new src dir path
149      */
150     public void setSrcDirPath(String srcDirPath) {
151         this.srcDirPath = srcDirPath;
152     }
153 
154     /**
155      * Gets the target dir path.
156      *
157      * @return the target dir path
158      */
159     public String getTargetDirPath() {
160         return targetDirPath;
161     }
162 
163     /**
164      * Sets the target dir path.
165      *
166      * @param targetDirPath
167      *            the new target dir path
168      */
169     public void setTargetDirPath(String targetDirPath) {
170         this.targetDirPath = targetDirPath;
171     }
172 
173     /**
174      * Gets the file encoding.
175      *
176      * @return the file encoding
177      */
178     public Charset getFileEncoding() {
179         return fileEncoding;
180     }
181 
182     /**
183      * Sets the file encoding.
184      *
185      * @param fileEncoding
186      *            the new file encoding
187      */
188     public void setFileEncoding(Charset fileEncoding) {
189         this.fileEncoding = fileEncoding == null ? Charset.defaultCharset() : fileEncoding;
190     }
191 
192     /**
193      * Checks if is creates the json file.
194      *
195      * @return true, if is creates the json file
196      */
197     public boolean isCreateJsonFile() {
198         return createJsonFile;
199     }
200 
201     /**
202      * Sets the creates the json file.
203      *
204      * @param createJsonFile
205      *            the new creates the json file
206      */
207     public void setCreateJsonFile(boolean createJsonFile) {
208         this.createJsonFile = createJsonFile;
209     }
210 
211     /**
212      * Gets the target json file path.
213      *
214      * @return the target json file path
215      */
216     public String getTargetJsonFilePath() {
217         return targetJsonFilePath;
218     }
219 
220     /**
221      * Sets the target json file path.
222      *
223      * @param targetJsonFilePath
224      *            the new target json file path
225      */
226     public void setTargetJsonFilePath(String targetJsonFilePath) {
227         this.targetJsonFilePath = targetJsonFilePath;
228     }
229 
230     /**
231      * Gets the json integration file path.
232      *
233      * @return the json integration file path
234      */
235     public String getJsonIntegrationFilePath() {
236         return jsonIntegrationFilePath;
237     }
238 
239     /**
240      * Sets the json integration file path.
241      *
242      * @param jsonIntegrationFilePath
243      *            the new json integration file path
244      */
245     public void setJsonIntegrationFilePath(String jsonIntegrationFilePath) {
246         this.jsonIntegrationFilePath = jsonIntegrationFilePath;
247     }
248 
249     /**
250      * Gets the html compressor.
251      *
252      * @return the html compressor
253      */
254     public com.googlecode.htmlcompressor.compressor.HtmlCompressor getHtmlCompressor() {
255         return htmlCompressor;
256     }
257 
258     /**
259      * Sets the html compressor.
260      *
261      * @param htmlCompressor
262      *            the new html compressor
263      */
264     public void setHtmlCompressor(com.googlecode.htmlcompressor.compressor.HtmlCompressor htmlCompressor) {
265         this.htmlCompressor = htmlCompressor;
266     }
267 }