1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  import com.liferay.portal.kernel.util.ListUtil;
19  import com.liferay.portal.kernel.util.PropertiesUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.util.FileImpl;
23  
24  import java.io.File;
25  import java.io.IOException;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Properties;
30  
31  import org.apache.tools.ant.DirectoryScanner;
32  
33  /**
34   * <a href="SourceFormatterHelper.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Igor Spasic
37   * @author Brian Wing Shun Chan
38   */
39  public class SourceFormatterHelper {
40  
41      public SourceFormatterHelper(boolean useProperties) {
42          _useProperties = useProperties;
43      }
44  
45      public void close() throws IOException {
46          if (!_useProperties) {
47              return;
48          }
49  
50          String newPropertiesContent = PropertiesUtil.toString(_properties);
51  
52          if (!_propertiesContent.equals(newPropertiesContent)) {
53              _fileUtil.write(_propertiesFile, newPropertiesContent);
54          }
55      }
56  
57      public void init() throws IOException {
58          if (!_useProperties) {
59              return;
60          }
61  
62          File basedirFile = new File("./");
63  
64          String basedirAbsolutePath = StringUtil.replace(
65              basedirFile.getAbsolutePath(),
66              new String[] {".", ":", "/", "\\"},
67              new String[] {"_", "_", "_", "_"});
68  
69          String propertiesFileName =
70              System.getProperty("java.io.tmpdir") + "/SourceFormatter." +
71                  basedirAbsolutePath;
72  
73          _propertiesFile = new File(propertiesFileName);
74  
75          if (_propertiesFile.exists()) {
76              _propertiesContent = _fileUtil.read(_propertiesFile);
77  
78              PropertiesUtil.load(_properties, _propertiesContent);
79          }
80      }
81  
82      public void printError(String fileName, File file) {
83          printError(fileName, file.toString());
84      }
85  
86      public void printError(String fileName, String message) {
87          if (_useProperties) {
88              String encodedFileName = StringUtil.replace(
89                  fileName, StringPool.BACK_SLASH, StringPool.SLASH);
90  
91              _properties.remove(encodedFileName);
92          }
93  
94          System.out.println(message);
95      }
96  
97      public List<String> scanForFiles(DirectoryScanner directoryScanner) {
98          directoryScanner.scan();
99  
100         String[] fileNamesArray = directoryScanner.getIncludedFiles();
101 
102         if (!_useProperties) {
103             return ListUtil.toList(fileNamesArray);
104         }
105 
106         List<String> fileNames = new ArrayList<String>(fileNamesArray.length);
107 
108         for (String fileName : fileNamesArray) {
109             File file = new File(fileName);
110 
111             String encodedFileName = StringUtil.replace(
112                 fileName, StringPool.BACK_SLASH, StringPool.SLASH);
113 
114             long timestamp = GetterUtil.getLong(
115                 _properties.getProperty(encodedFileName));
116 
117             if (timestamp < file.lastModified()) {
118                 fileNames.add(fileName);
119 
120                 _properties.setProperty(
121                     encodedFileName, String.valueOf(file.lastModified()));
122             }
123         }
124 
125         return fileNames;
126     }
127 
128     private static FileImpl _fileUtil = FileImpl.getInstance();
129 
130     private Properties _properties = new Properties();
131     private String _propertiesContent = StringPool.BLANK;
132     private File _propertiesFile;
133     private boolean _useProperties;
134 
135 }