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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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<String> 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 (Object buildConfigFile : buildConfigFiles) {
  54. List<String> fileContents = readConfigFile((String) buildConfigFile);
  55. if (addToConfiguration) {
  56. for (String s : filesToUpdate) {
  57. fileContents.add(s);
  58. }
  59. } else {
  60. for (String s : filesToUpdate) {
  61. if (fileContents.contains(s)) {
  62. fileContents.remove(s);
  63. }
  64. }
  65. }
  66. writeConfigFile((String) buildConfigFile, 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<BuildConfigNode> files, List<BuildConfigNode> importedNodes) {
  111. // Set contentsSet = new TreeSet(fileContents);
  112. String fileContentsString = "";
  113. // List filesToWrite = null;
  114. Set<String> includedFiles = new HashSet<>();
  115. for (BuildConfigNode node : importedNodes) {
  116. fileContentsString += '@' + node.getResourcePath() + "\n";
  117. String parentPath = new File(filePath).getParent();
  118. String importedFilePath = parentPath + File.separator + node.getResourcePath();
  119. includedFiles.addAll(getIncludedFiles(importedFilePath, parentPath));
  120. }
  121. for (BuildConfigNode node : files) {
  122. if (node.getName().endsWith(".lst") && !node.getResourcePath().startsWith("..")) {
  123. fileContentsString += '@';
  124. fileContentsString += node.getResourcePath() + "\n";
  125. } else {
  126. if (!includedFiles.contains(node.getResourcePath())) {
  127. fileContentsString += node.getResourcePath() + "\n";
  128. }
  129. }
  130. }
  131. writeFile(fileContentsString, filePath);
  132. }
  133. private List<String> getIncludedFiles(String path, String rootPath) {
  134. try {
  135. ConfigParser configParser = new ConfigParser();
  136. configParser.parseConfigFile(new File(path));
  137. List<File> files = configParser.getFiles();
  138. List<String> relativeFiles = new ArrayList<String>();
  139. for (File file : files) {
  140. relativeFiles.add(relativizePath(file.getPath(), rootPath));
  141. }
  142. return relativeFiles;
  143. } catch (ConfigParser.ParseException pe) {
  144. return new ArrayList<String>();
  145. }
  146. }
  147. // private synchronized List getUniqueFileList(List list, Set set) {
  148. // List uniqueList = new ArrayList();
  149. // for (Iterator it = list.iterator(); it.hasNext(); ) {
  150. // BuildConfigNode node = (BuildConfigNode)it.next();
  151. // String file1 = node.getResourcePath();
  152. // if (set.contains(file1) && !uniqueList.contains(file1)) {
  153. // uniqueList.add(file1);
  154. // }
  155. // }
  156. // return uniqueList;
  157. // }
  158. public String relativizePath(String path, String rootPath) {
  159. path = path.replace('\\', '/');
  160. rootPath = rootPath.replace('\\', '/');
  161. int pathIndex = path.indexOf(rootPath);
  162. if (pathIndex > -1) {
  163. return path.substring(pathIndex + rootPath.length() + 1);
  164. } else {
  165. return path;
  166. }
  167. }
  168. /**
  169. * Sorts and does not write duplicates.
  170. *
  171. * @param fileContents full paths representing file entries
  172. */
  173. public void writeConfigFile(String filePath, List<String> fileContents) {
  174. Set<String> contentsSet = new TreeSet<>(fileContents);
  175. StringBuffer fileContentsSB = new StringBuffer();
  176. Iterator<String> it = contentsSet.iterator();
  177. while (it.hasNext()) {
  178. fileContentsSB.append(it.next().toString());
  179. fileContentsSB.append("\n");
  180. }
  181. writeFile(fileContentsSB.toString(), filePath);
  182. }
  183. private void writeFile(String contents, String filePath) {
  184. FileOutputStream fos = null;
  185. try {
  186. fos = new FileOutputStream(filePath, false);
  187. fos.write(contents.getBytes());
  188. } catch (IOException ioe) {
  189. Message msg = new Message("Could not update build config file: " + filePath, IMessage.ERROR, ioe, null);
  190. Ajde.getDefault().getMessageHandler().handleMessage(msg);
  191. } finally {
  192. if (fos != null)
  193. try {
  194. fos.close();
  195. } catch (IOException ioe) {
  196. }
  197. }
  198. }
  199. }