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