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