1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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              _properties.remove(fileName);
89          }
90  
91          System.out.println(message);
92      }
93  
94      public List<String> scanForFiles(DirectoryScanner directoryScanner) {
95          directoryScanner.scan();
96  
97          String[] fileNamesArray = directoryScanner.getIncludedFiles();
98  
99          if (!_useProperties) {
100             return ListUtil.toList(fileNamesArray);
101         }
102 
103         List<String> fileNames = new ArrayList<String>(fileNamesArray.length);
104 
105         for (String fileName : fileNamesArray) {
106             File file = new File(fileName);
107 
108             String encodedFileName = StringUtil.replace(
109                 fileName, StringPool.BACK_SLASH, StringPool.SLASH);
110 
111             long timestamp = GetterUtil.getLong(
112                 _properties.getProperty(encodedFileName));
113 
114             if (timestamp < file.lastModified()) {
115                 fileNames.add(fileName);
116 
117                 _properties.setProperty(
118                     encodedFileName, String.valueOf(file.lastModified()));
119             }
120         }
121 
122         return fileNames;
123     }
124 
125     private static FileImpl _fileUtil = FileImpl.getInstance();
126 
127     private Properties _properties = new Properties();
128     private String _propertiesContent = StringPool.BLANK;
129     private File _propertiesFile;
130     private boolean _useProperties;
131 
132 }