You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LstBuildConfigFileUpdater.java 6.9KB

21 years ago
21 years ago
21 years ago
17 years ago
21 years ago
21 years ago
17 years ago
21 years ago
13 years ago
21 years ago
13 years ago
21 years ago
13 years ago
21 years ago
13 years ago
21 years ago
13 years ago
21 years ago
13 years ago
21 years ago
13 years ago
21 years ago
13 years ago
21 years ago
13 years ago
21 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * Helen Hawkins Converted to new interface (bug 148190)
  13. * ******************************************************************/
  14. package org.aspectj.ajde.internal;
  15. import java.io.BufferedReader;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.FileReader;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.HashSet;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Set;
  25. import java.util.TreeSet;
  26. import org.aspectj.ajde.Ajde;
  27. import org.aspectj.ajde.ui.BuildConfigNode;
  28. import org.aspectj.ajdt.ajc.ConfigParser;
  29. import org.aspectj.bridge.IMessage;
  30. import org.aspectj.bridge.Message;
  31. /**
  32. * Used for reading and writing build configuration (".lst") files.
  33. *
  34. * @author Mik Kersten
  35. */
  36. class LstBuildConfigFileUpdater {
  37. /**
  38. * Adds an entry to a build configuration file.
  39. */
  40. public void updateBuildConfigFile(String buildConfigFile, String update, boolean addToConfiguration) {
  41. List fileContents = readConfigFile(buildConfigFile);
  42. if (addToConfiguration) {
  43. fileContents.add(update);
  44. } else {
  45. fileContents.remove(update);
  46. }
  47. writeConfigFile(buildConfigFile, fileContents);
  48. }
  49. /**
  50. * Adds an entry to multiple build configuration files.
  51. */
  52. public void updateBuildConfigFiles(List buildConfigFiles, List<String> filesToUpdate, boolean addToConfiguration) {
  53. for (int i = 0; i < buildConfigFiles.size(); i++) {
  54. List<String> fileContents = readConfigFile((String) buildConfigFiles.get(i));
  55. if (addToConfiguration) {
  56. for (int j = 0; j < filesToUpdate.size(); j++) {
  57. fileContents.add(filesToUpdate.get(j));
  58. }
  59. } else {
  60. for (int k = 0; k < filesToUpdate.size(); k++) {
  61. if (fileContents.contains(filesToUpdate.get(k))) {
  62. fileContents.remove(filesToUpdate.get(k));
  63. }
  64. }
  65. }
  66. writeConfigFile((String) buildConfigFiles.get(i), fileContents);
  67. }
  68. }
  69. /**
  70. * Checks if an entry exists within a build configuration file.
  71. */
  72. public boolean exists(String entry, String configFile) {
  73. return exists(entry, configFile, "");
  74. }
  75. public boolean exists(String entry, String configFile, String rootPath) {
  76. Iterator it = readConfigFile(configFile).iterator();
  77. while (it.hasNext()) {
  78. if ((entry).equals(rootPath + "/" + (String) it.next())) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. /**
  85. * Reads the entries of a configuration file.
  86. */
  87. public List<String> readConfigFile(String filePath) {
  88. try {
  89. File configFile = new File(filePath);
  90. if (!configFile.exists()) {
  91. Message msg = new Message("Config file: " + filePath + " does not exist. Update failed.", IMessage.WARNING, null,
  92. null);
  93. Ajde.getDefault().getMessageHandler().handleMessage(msg);
  94. }
  95. List<String> fileContents = new ArrayList<String>();
  96. BufferedReader reader = new BufferedReader(new FileReader(configFile));
  97. String line = reader.readLine();
  98. while (line != null) {
  99. fileContents.add(line.replace('\\', '/'));
  100. line = reader.readLine();
  101. }
  102. reader.close();
  103. return fileContents;
  104. } catch (IOException ioe) {
  105. Message msg = new Message("Could not update build config file.", IMessage.ERROR, ioe, null);
  106. Ajde.getDefault().getMessageHandler().handleMessage(msg);
  107. }
  108. return null;
  109. }
  110. public void writeConfigFile(String filePath, List files, List importedNodes) {
  111. // Set contentsSet = new TreeSet(fileContents);
  112. String fileContentsString = "";
  113. // List filesToWrite = null;
  114. Set includedFiles = new HashSet();
  115. for (Iterator it = importedNodes.iterator(); it.hasNext();) {
  116. BuildConfigNode node = (BuildConfigNode) it.next();
  117. fileContentsString += '@' + node.getResourcePath() + "\n";
  118. String parentPath = new File(filePath).getParent();
  119. String importedFilePath = parentPath + File.separator + node.getResourcePath();
  120. includedFiles.addAll(getIncludedFiles(importedFilePath, parentPath));
  121. }
  122. for (Iterator it = files.iterator(); it.hasNext();) {
  123. BuildConfigNode node = (BuildConfigNode) it.next();
  124. if (node.getName().endsWith(".lst") && !node.getResourcePath().startsWith("..")) {
  125. fileContentsString += '@';
  126. fileContentsString += node.getResourcePath() + "\n";
  127. } else {
  128. if (!includedFiles.contains(node.getResourcePath())) {
  129. fileContentsString += node.getResourcePath() + "\n";
  130. }
  131. }
  132. }
  133. writeFile(fileContentsString, filePath);
  134. }
  135. private List<String> getIncludedFiles(String path, String rootPath) {
  136. try {
  137. ConfigParser configParser = new ConfigParser();
  138. configParser.parseConfigFile(new File(path));
  139. List<File> files = configParser.getFiles();
  140. List<String> relativeFiles = new ArrayList<String>();
  141. for (Iterator<File> it = files.iterator(); it.hasNext();) {
  142. relativeFiles.add(relativizePath(((File) it.next()).getPath(), rootPath));
  143. }
  144. return relativeFiles;
  145. } catch (ConfigParser.ParseException pe) {
  146. return new ArrayList<String>();
  147. }
  148. }
  149. // private synchronized List getUniqueFileList(List list, Set set) {
  150. // List uniqueList = new ArrayList();
  151. // for (Iterator it = list.iterator(); it.hasNext(); ) {
  152. // BuildConfigNode node = (BuildConfigNode)it.next();
  153. // String file1 = node.getResourcePath();
  154. // if (set.contains(file1) && !uniqueList.contains(file1)) {
  155. // uniqueList.add(file1);
  156. // }
  157. // }
  158. // return uniqueList;
  159. // }
  160. public String relativizePath(String path, String rootPath) {
  161. path = path.replace('\\', '/');
  162. rootPath = rootPath.replace('\\', '/');
  163. int pathIndex = path.indexOf(rootPath);
  164. if (pathIndex > -1) {
  165. return path.substring(pathIndex + rootPath.length() + 1);
  166. } else {
  167. return path;
  168. }
  169. }
  170. /**
  171. * Sorts and does not write duplicates.
  172. *
  173. * @param fileContents full paths representing file entries
  174. */
  175. public void writeConfigFile(String filePath, List fileContents) {
  176. Set contentsSet = new TreeSet(fileContents);
  177. StringBuffer fileContentsSB = new StringBuffer();
  178. Iterator it = contentsSet.iterator();
  179. while (it.hasNext()) {
  180. fileContentsSB.append(it.next().toString());
  181. fileContentsSB.append("\n");
  182. }
  183. writeFile(fileContentsSB.toString(), filePath);
  184. }
  185. private void writeFile(String contents, String filePath) {
  186. FileOutputStream fos = null;
  187. try {
  188. fos = new FileOutputStream(filePath, false);
  189. fos.write(contents.getBytes());
  190. } catch (IOException ioe) {
  191. Message msg = new Message("Could not update build config file: " + filePath, IMessage.ERROR, ioe, null);
  192. Ajde.getDefault().getMessageHandler().handleMessage(msg);
  193. } finally {
  194. if (fos != null)
  195. try {
  196. fos.close();
  197. } catch (IOException ioe) {
  198. }
  199. }
  200. }
  201. }