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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. for (String s : readConfigFile(configFile)) {
  77. if ((entry).equals(rootPath + "/" + s)) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. /**
  84. * Reads the entries of a configuration file.
  85. */
  86. public List<String> readConfigFile(String filePath) {
  87. try {
  88. File configFile = new File(filePath);
  89. if (!configFile.exists()) {
  90. Message msg = new Message("Config file: " + filePath + " does not exist. Update failed.", IMessage.WARNING, null,
  91. null);
  92. Ajde.getDefault().getMessageHandler().handleMessage(msg);
  93. }
  94. List<String> fileContents = new ArrayList<>();
  95. BufferedReader reader = new BufferedReader(new FileReader(configFile));
  96. String line = reader.readLine();
  97. while (line != null) {
  98. fileContents.add(line.replace('\\', '/'));
  99. line = reader.readLine();
  100. }
  101. reader.close();
  102. return fileContents;
  103. } catch (IOException ioe) {
  104. Message msg = new Message("Could not update build config file.", IMessage.ERROR, ioe, null);
  105. Ajde.getDefault().getMessageHandler().handleMessage(msg);
  106. }
  107. return null;
  108. }
  109. public void writeConfigFile(String filePath, List<BuildConfigNode> files, List<BuildConfigNode> importedNodes) {
  110. // Set contentsSet = new TreeSet(fileContents);
  111. String fileContentsString = "";
  112. // List filesToWrite = null;
  113. Set<String> includedFiles = new HashSet<>();
  114. for (BuildConfigNode node : importedNodes) {
  115. fileContentsString += '@' + node.getResourcePath() + "\n";
  116. String parentPath = new File(filePath).getParent();
  117. String importedFilePath = parentPath + File.separator + node.getResourcePath();
  118. includedFiles.addAll(getIncludedFiles(importedFilePath, parentPath));
  119. }
  120. for (BuildConfigNode node : files) {
  121. if (node.getName().endsWith(".lst") && !node.getResourcePath().startsWith("..")) {
  122. fileContentsString += '@';
  123. fileContentsString += node.getResourcePath() + "\n";
  124. } else {
  125. if (!includedFiles.contains(node.getResourcePath())) {
  126. fileContentsString += node.getResourcePath() + "\n";
  127. }
  128. }
  129. }
  130. writeFile(fileContentsString, filePath);
  131. }
  132. private List<String> getIncludedFiles(String path, String rootPath) {
  133. try {
  134. ConfigParser configParser = new ConfigParser();
  135. configParser.parseConfigFile(new File(path));
  136. List<File> files = configParser.getFiles();
  137. List<String> relativeFiles = new ArrayList<>();
  138. for (File file : files) {
  139. relativeFiles.add(relativizePath(file.getPath(), rootPath));
  140. }
  141. return relativeFiles;
  142. } catch (ConfigParser.ParseException pe) {
  143. return new ArrayList<>();
  144. }
  145. }
  146. // private synchronized List getUniqueFileList(List list, Set set) {
  147. // List uniqueList = new ArrayList();
  148. // for (Iterator it = list.iterator(); it.hasNext(); ) {
  149. // BuildConfigNode node = (BuildConfigNode)it.next();
  150. // String file1 = node.getResourcePath();
  151. // if (set.contains(file1) && !uniqueList.contains(file1)) {
  152. // uniqueList.add(file1);
  153. // }
  154. // }
  155. // return uniqueList;
  156. // }
  157. public String relativizePath(String path, String rootPath) {
  158. path = path.replace('\\', '/');
  159. rootPath = rootPath.replace('\\', '/');
  160. int pathIndex = path.indexOf(rootPath);
  161. if (pathIndex > -1) {
  162. return path.substring(pathIndex + rootPath.length() + 1);
  163. } else {
  164. return path;
  165. }
  166. }
  167. /**
  168. * Sorts and does not write duplicates.
  169. *
  170. * @param fileContents full paths representing file entries
  171. */
  172. public void writeConfigFile(String filePath, List<String> fileContents) {
  173. Set<String> contentsSet = new TreeSet<>(fileContents);
  174. StringBuffer fileContentsSB = new StringBuffer();
  175. for (String s : contentsSet) {
  176. fileContentsSB.append(s.toString());
  177. fileContentsSB.append("\n");
  178. }
  179. writeFile(fileContentsSB.toString(), filePath);
  180. }
  181. private void writeFile(String contents, String filePath) {
  182. FileOutputStream fos = null;
  183. try {
  184. fos = new FileOutputStream(filePath, false);
  185. fos.write(contents.getBytes());
  186. } catch (IOException ioe) {
  187. Message msg = new Message("Could not update build config file: " + filePath, IMessage.ERROR, ioe, null);
  188. Ajde.getDefault().getMessageHandler().handleMessage(msg);
  189. } finally {
  190. if (fos != null)
  191. try {
  192. fos.close();
  193. } catch (IOException ioe) {
  194. }
  195. }
  196. }
  197. }