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.nio.charset.Charset;
11  import java.util.Map.Entry;
12  import java.util.concurrent.ConcurrentMap;
13  
14  /**
15   * The Class XmlCompressor.
16   */
17  public class XmlCompressor {
18  
19      /** The Constant FILE_EXT. */
20      private static final String[] FILE_EXT = { "xml" };
21  
22      /** The file ext. */
23      private String[] fileExtensions;
24  
25      /** The src dir path. */
26      private String srcDirPath;
27  
28      /** The target dir path. */
29      private String targetDirPath;
30  
31      /** The file encoding. */
32      private Charset fileEncoding;
33  
34      /** The xml compressor. */
35      private com.googlecode.htmlcompressor.compressor.XmlCompressor xmlCompressor;
36  
37      /**
38       * Instantiates a new xml compressor.
39       *
40       * @param srcDirPath
41       *            the src dir path
42       * @param targetDirPath
43       *            the target dir path
44       */
45      public XmlCompressor(String srcDirPath, String targetDirPath) {
46          this.srcDirPath = srcDirPath;
47          this.targetDirPath = targetDirPath;
48      }
49  
50      /**
51       * Compress.
52       *
53       * @throws Exception
54       *             the exception
55       */
56      public void compress() throws Exception {
57          if (fileExtensions == null || fileExtensions.length == 0) {
58              fileExtensions = FILE_EXT;
59          }
60  
61          FileTool fileTool = new FileTool(srcDirPath, fileExtensions, true);
62          fileTool.setFileEncoding(fileEncoding);
63          ConcurrentMap<String, String> map = fileTool.getFiles();
64  
65          if (xmlCompressor == null) {
66              xmlCompressor = new com.googlecode.htmlcompressor.compressor.XmlCompressor();
67          }
68  
69          for (Entry<String, String> key : map.entrySet()) {
70              map.put(key.getKey(), xmlCompressor.compress(key.getValue()));
71          }
72  
73          fileTool.writeFiles(map, targetDirPath);
74      }
75  
76      /**
77       * Gets the file extension.
78       *
79       * @return the file extensions
80       */
81      public String[] getFileExtensions() {
82          return fileExtensions;
83      }
84  
85      /**
86       * Sets the file ext.
87       *
88       * @param fileExtensions
89       *            the new file extensions
90       */
91      public void setFileExtensions(String[] fileExtensions) {
92          this.fileExtensions = fileExtensions;
93      }
94  
95      /**
96       * Gets the src dir path.
97       *
98       * @return the src dir path
99       */
100     public String getSrcDirPath() {
101         return srcDirPath;
102     }
103 
104     /**
105      * Sets the src dir path.
106      *
107      * @param srcDirPath
108      *            the new src dir path
109      */
110     public void setSrcDirPath(String srcDirPath) {
111         this.srcDirPath = srcDirPath;
112     }
113 
114     /**
115      * Gets the target dir path.
116      *
117      * @return the target dir path
118      */
119     public String getTargetDirPath() {
120         return targetDirPath;
121     }
122 
123     /**
124      * Sets the target dir path.
125      *
126      * @param targetDirPath
127      *            the new target dir path
128      */
129     public void setTargetDirPath(String targetDirPath) {
130         this.targetDirPath = targetDirPath;
131     }
132 
133     /**
134      * Gets the file encoding.
135      *
136      * @return the file encoding
137      */
138     public Charset getFileEncoding() {
139         return fileEncoding;
140     }
141 
142     /**
143      * Sets the file encoding.
144      *
145      * @param fileEncoding
146      *            the new file encoding
147      */
148     public void setFileEncoding(Charset fileEncoding) {
149         this.fileEncoding = fileEncoding == null ? Charset.defaultCharset() : fileEncoding;
150     }
151 
152     /**
153      * Gets the xml compressor.
154      *
155      * @return the xml compressor
156      */
157     public com.googlecode.htmlcompressor.compressor.XmlCompressor getXmlCompressor() {
158         return xmlCompressor;
159     }
160 
161     /**
162      * Sets the xml compressor.
163      *
164      * @param xmlCompressor
165      *            the new xml compressor
166      */
167     public void setXmlCompressor(com.googlecode.htmlcompressor.compressor.XmlCompressor xmlCompressor) {
168         this.xmlCompressor = xmlCompressor;
169     }
170 }