001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.ListUtil;
019    import com.liferay.portal.kernel.util.PropertiesUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.util.FileImpl;
023    
024    import java.io.File;
025    import java.io.IOException;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    import java.util.Properties;
030    
031    import org.apache.tools.ant.DirectoryScanner;
032    
033    /**
034     * @author Igor Spasic
035     * @author Brian Wing Shun Chan
036     */
037    public class SourceFormatterHelper {
038    
039            public SourceFormatterHelper(boolean useProperties) {
040                    _useProperties = useProperties;
041            }
042    
043            public void close() throws IOException {
044                    if (!_useProperties) {
045                            return;
046                    }
047    
048                    String newPropertiesContent = PropertiesUtil.toString(_properties);
049    
050                    if (!_propertiesContent.equals(newPropertiesContent)) {
051                            _fileUtil.write(_propertiesFile, newPropertiesContent);
052                    }
053            }
054    
055            public void init() throws IOException {
056                    if (!_useProperties) {
057                            return;
058                    }
059    
060                    File basedirFile = new File("./");
061    
062                    String basedirAbsolutePath = StringUtil.replace(
063                            basedirFile.getAbsolutePath(),
064                            new String[] {".", ":", "/", "\\"},
065                            new String[] {"_", "_", "_", "_"});
066    
067                    String propertiesFileName =
068                            System.getProperty("java.io.tmpdir") + "/SourceFormatter." +
069                                    basedirAbsolutePath;
070    
071                    _propertiesFile = new File(propertiesFileName);
072    
073                    if (_propertiesFile.exists()) {
074                            _propertiesContent = _fileUtil.read(_propertiesFile);
075    
076                            PropertiesUtil.load(_properties, _propertiesContent);
077                    }
078            }
079    
080            public void printError(String fileName, File file) {
081                    printError(fileName, file.toString());
082            }
083    
084            public void printError(String fileName, String message) {
085                    if (_useProperties) {
086                            String encodedFileName = StringUtil.replace(
087                                    fileName, StringPool.BACK_SLASH, StringPool.SLASH);
088    
089                            _properties.remove(encodedFileName);
090                    }
091    
092                    System.out.println(message);
093            }
094    
095            public List<String> scanForFiles(DirectoryScanner directoryScanner) {
096                    directoryScanner.scan();
097    
098                    String[] fileNamesArray = directoryScanner.getIncludedFiles();
099    
100                    if (!_useProperties) {
101                            return ListUtil.toList(fileNamesArray);
102                    }
103    
104                    List<String> fileNames = new ArrayList<String>(fileNamesArray.length);
105    
106                    for (String fileName : fileNamesArray) {
107                            File file = new File(fileName);
108    
109                            String encodedFileName = StringUtil.replace(
110                                    fileName, StringPool.BACK_SLASH, StringPool.SLASH);
111    
112                            long timestamp = GetterUtil.getLong(
113                                    _properties.getProperty(encodedFileName));
114    
115                            if (timestamp < file.lastModified()) {
116                                    fileNames.add(fileName);
117    
118                                    _properties.setProperty(
119                                            encodedFileName, String.valueOf(file.lastModified()));
120                            }
121                    }
122    
123                    return fileNames;
124            }
125    
126            private static FileImpl _fileUtil = FileImpl.getInstance();
127    
128            private Properties _properties = new Properties();
129            private String _propertiesContent = StringPool.BLANK;
130            private File _propertiesFile;
131            private boolean _useProperties;
132    
133    }