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

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