KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > ajde > internal > LstBuildConfigFileUpdater


1
2 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3  *
4  * This file is part of the IDE support for the AspectJ(tm)
5  * programming language; see http://aspectj.org
6  *
7  * The contents of this file are subject to the Mozilla Public License
8  * Version 1.1 (the "License"); you may not use this file except in
9  * compliance with the License. You may obtain a copy of the License at
10  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is AspectJ.
18  *
19  * The Initial Developer of the Original Code is Xerox Corporation. Portions
20  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
21  * All Rights Reserved.
22  *
23  * Contributor(s):
24  */

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 /**
36  * Used for reading and writing build configuration (".lst") files.
37  *
38  * @author Mik Kersten
39  */

40 class LstBuildConfigFileUpdater {
41
42     /**
43      * Adds an entry to a build configuration file.
44      */

45     public void updateBuildConfigFile(String JavaDoc buildConfigFile, String JavaDoc 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     /**
56      * Adds an entry to multiple build configuration files.
57      */

58     public void updateBuildConfigFiles(List buildConfigFiles, List filesToUpdate, boolean addToConfiguration) {
59         for (int i = 0; i < buildConfigFiles.size(); i++) {
60             List fileContents = readConfigFile((String JavaDoc)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 JavaDoc)buildConfigFiles.get(i), fileContents);
73         }
74     }
75
76     /**
77      * Checks if an entry exists within a build configuration file.
78      */

79     public boolean exists(String JavaDoc entry, String JavaDoc configFile) {
80         return exists(entry, configFile, "");
81     }
82
83     public boolean exists(String JavaDoc entry, String JavaDoc configFile, String JavaDoc rootPath) {
84         Iterator it = readConfigFile(configFile).iterator();
85         while (it.hasNext()) {
86             if ((entry).equals(rootPath + "/" + (String JavaDoc)it.next())) {
87                 return true;
88             }
89         }
90         return false;
91     }
92
93     /**
94      * Reads the entries of a configuration file.
95      */

96     public List readConfigFile(String JavaDoc 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 JavaDoc 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 JavaDoc filePath, List files, List importedNodes) {
118         //Set contentsSet = new TreeSet(fileContents);
119
String JavaDoc fileContentsString = "";
120         //List filesToWrite = null;
121
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 JavaDoc parentPath = new File(filePath).getParent();
126             String JavaDoc 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 JavaDoc path, String JavaDoc 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 JavaDoc 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 JavaDoc relativizePath(String JavaDoc path, String JavaDoc 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     /**
183      * Sorts and does not write duplicates.
184      *
185      * @param fileContents full paths representing file entries
186      */

187     public void writeConfigFile(String JavaDoc filePath, List fileContents) {
188         Set contentsSet = new TreeSet(fileContents);
189         String JavaDoc 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 JavaDoc contents, String JavaDoc 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