1 2 25 26 package org.aspectj.ajde.internal; 27 28 import java.util.*; 29 import java.io.*; 30 31 import org.aspectj.ajde.Ajde; 32 import org.aspectj.ajde.ui.*; 33 import org.aspectj.util.ConfigParser; 34 35 40 class LstBuildConfigFileUpdater { 41 42 45 public void updateBuildConfigFile(String buildConfigFile, String update, boolean addToConfiguration) { 46 List fileContents = readConfigFile(buildConfigFile); 47 if (addToConfiguration) { 48 fileContents.add(update); 49 } else { 50 fileContents.remove(update); 51 } 52 writeConfigFile(buildConfigFile, fileContents); 53 } 54 55 58 public void updateBuildConfigFiles(List buildConfigFiles, List filesToUpdate, boolean addToConfiguration) { 59 for (int i = 0; i < buildConfigFiles.size(); i++) { 60 List fileContents = readConfigFile((String )buildConfigFiles.get(i)); 61 if (addToConfiguration) { 62 for (int j = 0; j < filesToUpdate.size(); j++) { 63 fileContents.add(filesToUpdate.get(j)); 64 } 65 } else { 66 for (int k =0; k < filesToUpdate.size(); k++) { 67 if (fileContents.contains(filesToUpdate.get(k))) { 68 fileContents.remove(filesToUpdate.get(k)); 69 } 70 } 71 } 72 writeConfigFile((String )buildConfigFiles.get(i), fileContents); 73 } 74 } 75 76 79 public boolean exists(String entry, String configFile) { 80 return exists(entry, configFile, ""); 81 } 82 83 public boolean exists(String entry, String configFile, String rootPath) { 84 Iterator it = readConfigFile(configFile).iterator(); 85 while (it.hasNext()) { 86 if ((entry).equals(rootPath + "/" + (String )it.next())) { 87 return true; 88 } 89 } 90 return false; 91 } 92 93 96 public List readConfigFile(String filePath) { 97 try { 98 File configFile = new File(filePath); 99 if (!configFile.exists()) { 100 Ajde.getDefault().getErrorHandler().handleWarning("Config file: " + filePath + 101 " does not exist. Update failed."); 102 } 103 List fileContents = new ArrayList(); 104 BufferedReader reader = new BufferedReader(new FileReader(configFile)); 105 String line = reader.readLine(); 106 while (line != null) { 107 fileContents.add(line.replace('\\', '/')); 108 line = reader.readLine(); 109 } 110 return fileContents; 111 } catch (IOException ioe) { 112 Ajde.getDefault().getErrorHandler().handleError("Could not update build config file.", ioe); 113 } 114 return null; 115 } 116 117 public void writeConfigFile(String filePath, List files, List importedNodes) { 118 String fileContentsString = ""; 120 Set includedFiles = new HashSet(); 122 for (Iterator it = importedNodes.iterator(); it.hasNext(); ) { 123 BuildConfigNode node = (BuildConfigNode)it.next(); 124 fileContentsString += '@' + node.getResourcePath() + "\n"; 125 String parentPath = new File(filePath).getParent(); 126 String importedFilePath = parentPath + File.separator + node.getResourcePath(); 127 includedFiles.addAll(getIncludedFiles(importedFilePath, parentPath)); 128 } 129 130 for (Iterator it = files.iterator(); it.hasNext(); ) { 131 BuildConfigNode node = (BuildConfigNode)it.next(); 132 if (node.getName().endsWith(".lst") && !node.getResourcePath().startsWith("..")) { 133 fileContentsString += '@'; 134 fileContentsString += node.getResourcePath() + "\n"; 135 } else { 136 if (!includedFiles.contains(node.getResourcePath())) { 137 fileContentsString += node.getResourcePath() + "\n"; 138 } 139 } 140 } 141 writeFile(fileContentsString, filePath); 142 } 143 144 private List getIncludedFiles(String path, String rootPath) { 145 try { 146 ConfigParser configParser = new ConfigParser(); 147 configParser.parseConfigFile(new File(path)); 148 List files = configParser.getFiles(); 149 List relativeFiles = new ArrayList(); 150 for (Iterator it = files.iterator(); it.hasNext(); ) { 151 relativeFiles.add(relativizePath(((File)it.next()).getPath(), rootPath)); 152 } 153 return relativeFiles; 154 } catch (ConfigParser.ParseException pe) { 155 return new ArrayList(); 156 } 157 } 158 159 private synchronized List getUniqueFileList(List list, Set set) { 160 List uniqueList = new ArrayList(); 161 for (Iterator it = list.iterator(); it.hasNext(); ) { 162 BuildConfigNode node = (BuildConfigNode)it.next(); 163 String file1 = node.getResourcePath(); 164 if (set.contains(file1) && !uniqueList.contains(file1)) { 165 uniqueList.add(file1); 166 } 167 } 168 return uniqueList; 169 } 170 171 public String relativizePath(String path, String rootPath) { 172 path = path.replace('\\', '/'); 173 rootPath = rootPath.replace('\\', '/'); 174 int pathIndex = path.indexOf(rootPath); 175 if (pathIndex > -1) { 176 return path.substring(pathIndex + rootPath.length() + 1); 177 } else { 178 return path; 179 } 180 } 181 182 187 public void writeConfigFile(String filePath, List fileContents) { 188 Set contentsSet = new TreeSet(fileContents); 189 String fileContentsString = ""; 190 Iterator it = contentsSet.iterator(); 191 while (it.hasNext()) { 192 fileContentsString += it.next().toString() + "\n"; 193 } 194 writeFile(fileContentsString, filePath); 195 } 196 197 private void writeFile(String contents, String filePath) { 198 try { 199 FileOutputStream fos = new FileOutputStream(filePath, false); 200 fos.write(contents.getBytes()); 201 fos.close(); 202 } catch (IOException ioe) { 203 Ajde.getDefault().getErrorHandler().handleError("Could not update build config file: " + filePath, ioe); 204 } 205 } 206 } 207 208 | Popular Tags |