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

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