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