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.StringUtil;
018    import com.liferay.portal.util.FileImpl;
019    
020    import java.io.File;
021    
022    import org.apache.tools.ant.DirectoryScanner;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class TextReplacer {
028    
029            public static void main(String[] args) {
030                    String dirs = System.getProperty("text.replacer.dirs");
031                    String includes = System.getProperty("text.replacer.includes");
032                    String excludes = System.getProperty("text.replacer.excludes");
033                    String tokenFile = System.getProperty("text.replacer.token.file");
034                    String valueFile = System.getProperty("text.replacer.value.file");
035    
036                    new TextReplacer(dirs, includes, excludes, tokenFile, valueFile);
037            }
038    
039            public TextReplacer(
040                    String dirs, String includes, String excludes, String tokenFile,
041                    String valueFile) {
042    
043                    try {
044                            String token = _fileUtil.read(tokenFile);
045    
046                            if (token == null) {
047                                    System.out.println(tokenFile + " does not exist");
048    
049                                    return;
050                            }
051    
052                            String value = _fileUtil.read(valueFile);
053    
054                            if (value == null) {
055                                    System.out.println(valueFile + " does not exist");
056    
057                                    return;
058                            }
059    
060                            for (String dir : StringUtil.split(dirs)) {
061                                    if (!dir.endsWith("/")) {
062                                            dir += "/";
063                                    }
064    
065                                    if (!_fileUtil.exists(dir)) {
066                                            System.out.println(dir + " does not exist");
067    
068                                            continue;
069                                    }
070    
071                                    DirectoryScanner directoryScanner = new DirectoryScanner();
072    
073                                    directoryScanner.setBasedir(dir);
074                                    directoryScanner.setIncludes(StringUtil.split(includes));
075                                    directoryScanner.setExcludes(StringUtil.split(excludes));
076    
077                                    directoryScanner.scan();
078    
079                                    for (String fileName : directoryScanner.getIncludedFiles()) {
080                                            File file = new File(dir + fileName);
081    
082                                            String content = _fileUtil.read(file);
083    
084                                            String newContent = StringUtil.replace(
085                                                    content, token, value);
086    
087                                            if (!content.equals(newContent)) {
088                                                    _fileUtil.write(file, newContent);
089                                            }
090                                    }
091                            }
092                    }
093                    catch (Exception e) {
094                            e.printStackTrace();
095                    }
096            }
097    
098            private static FileImpl _fileUtil = FileImpl.getInstance();
099    
100    }