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