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.StringUtil;
18  import com.liferay.portal.util.FileImpl;
19  
20  import java.io.File;
21  
22  import org.apache.tools.ant.DirectoryScanner;
23  
24  /**
25   * <a href="TextReplacer.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   */
29  public class TextReplacer {
30  
31      public static void main(String[] args) {
32          String dirs = System.getProperty("text.replacer.dirs");
33          String includes = System.getProperty("text.replacer.includes");
34          String excludes = System.getProperty("text.replacer.excludes");
35          String tokenFile = System.getProperty("text.replacer.token.file");
36          String valueFile = System.getProperty("text.replacer.value.file");
37  
38          new TextReplacer(dirs, includes, excludes, tokenFile, valueFile);
39      }
40  
41      public TextReplacer(
42          String dirs, String includes, String excludes, String tokenFile,
43          String valueFile) {
44  
45          try {
46              String token = _fileUtil.read(tokenFile);
47  
48              if (token == null) {
49                  System.out.println(tokenFile + " does not exist");
50  
51                  return;
52              }
53  
54              String value = _fileUtil.read(valueFile);
55  
56              if (value == null) {
57                  System.out.println(valueFile + " does not exist");
58  
59                  return;
60              }
61  
62              for (String dir : StringUtil.split(dirs)) {
63                  if (!dir.endsWith("/")) {
64                      dir += "/";
65                  }
66  
67                  if (!_fileUtil.exists(dir)) {
68                      System.out.println(dir + " does not exist");
69  
70                      continue;
71                  }
72  
73                  DirectoryScanner directoryScanner = new DirectoryScanner();
74  
75                  directoryScanner.setBasedir(dir);
76                  directoryScanner.setIncludes(StringUtil.split(includes));
77                  directoryScanner.setExcludes(StringUtil.split(excludes));
78  
79                  directoryScanner.scan();
80  
81                  for (String fileName : directoryScanner.getIncludedFiles()) {
82                      File file = new File(dir + fileName);
83  
84                      String content = _fileUtil.read(file);
85  
86                      String newContent = StringUtil.replace(
87                          content, token, value);
88  
89                      if (!content.equals(newContent)) {
90                          _fileUtil.write(file, newContent);
91                      }
92                  }
93              }
94          }
95          catch (Exception e) {
96              e.printStackTrace();
97          }
98      }
99  
100     private static FileImpl _fileUtil = FileImpl.getInstance();
101 
102 }