KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > builder > AbstractBuildScriptGenerator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.build.builder;
12
13 import java.io.*;
14 import java.util.*;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.pde.internal.build.*;
17 import org.eclipse.update.core.IPlatformEnvironment;
18
19 /**
20  * Instance of this class and subclasses are created on a plugin / feature basis.
21  */

22 public abstract class AbstractBuildScriptGenerator extends AbstractScriptGenerator {
23     /** Additional dev entries for the compile classpath. */
24     protected DevClassPathHelper devEntries;
25
26     /** Contain the elements that will be assembled */
27     protected AssemblyInformation assemblyData;
28
29     /** The content of the build.properties file associated to the element for which the script is generated */
30     protected Properties buildProperties;
31     private Set compiledElements; //The elements we are compiling
32

33     private boolean includePlatformIndependent = true;
34
35     /** flag indicating whether or not the missing properties file should be logged */
36     private boolean ignoreMissingPropertiesFile = true;
37
38     static private Properties executionEnvironmentMappings = null;
39     
40     abstract protected Properties getBuildProperties() throws CoreException;
41
42     static public Properties getExecutionEnvironmentMappings(){
43         if(executionEnvironmentMappings != null)
44             return executionEnvironmentMappings;
45         
46         executionEnvironmentMappings = new Properties();
47         InputStream stream = null;
48         try {
49             stream = BundleHelper.getDefault().getBundle().getEntry("data/env.properties").openStream(); //$NON-NLS-1$
50
executionEnvironmentMappings.load(stream);
51         } catch (IOException e) {
52             //ignore
53
} finally {
54             try {
55                 if (stream != null)
56                     stream.close();
57             } catch (IOException e) {
58                 //ignore
59
}
60         }
61         return executionEnvironmentMappings;
62     }
63     
64     public void setDevEntries(String JavaDoc entries) {
65         devEntries = new DevClassPathHelper(entries);
66     }
67
68     public void setDevEntries(DevClassPathHelper entries) {
69         devEntries = entries;
70     }
71
72     public void includePlatformIndependent(boolean value) {
73         includePlatformIndependent = value;
74     }
75     
76     public boolean isPlatformIndependentIncluded() {
77         return includePlatformIndependent;
78     }
79
80     /**
81      *
82      * @param buf
83      * @param start
84      * @param target
85      * @return int
86      */

87     protected int scan(StringBuffer JavaDoc buf, int start, String JavaDoc target) {
88         return scan(buf, start, new String JavaDoc[] {target});
89     }
90
91     /**
92      *
93      * @param buf
94      * @param start
95      * @param targets
96      * @return int
97      */

98     protected int scan(StringBuffer JavaDoc buf, int start, String JavaDoc[] targets) {
99         for (int i = start; i < buf.length(); i++) {
100             for (int j = 0; j < targets.length; j++) {
101                 if (i < buf.length() - targets[j].length()) {
102                     String JavaDoc match = buf.substring(i, i + targets[j].length());
103                     if (targets[j].equals(match))
104                         return i;
105                 }
106             }
107         }
108         return -1;
109     }
110
111     /**
112      * Return a buffer containing the contents of the file at the specified location.
113      *
114      * @param target the file
115      * @return StringBuffer
116      * @throws IOException
117      */

118     protected StringBuffer JavaDoc readFile(File target) throws IOException {
119         return readFile(new FileInputStream(target));
120     }
121
122     protected StringBuffer JavaDoc readFile(InputStream stream) throws IOException {
123         InputStreamReader reader = new InputStreamReader(new BufferedInputStream(stream));
124         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
125         char[] buf = new char[4096];
126         int count;
127         try {
128             count = reader.read(buf, 0, buf.length);
129             while (count != -1) {
130                 result.append(buf, 0, count);
131                 count = reader.read(buf, 0, buf.length);
132             }
133         } finally {
134             try {
135                 reader.close();
136             } catch (IOException e) {
137                 // ignore exceptions here
138
}
139         }
140         return result;
141     }
142
143     /**
144      * Custom build scripts should have their version number matching the
145      * version number defined by the feature/plugin/fragment descriptor.
146      * This is a best effort job so do not worry if the expected tags were
147      * not found and just return without modifying the file.
148      *
149      * @param buildFile
150      * @param propertyName
151      * @param version
152      * @throws IOException
153      *
154      */

155     protected void updateVersion(File buildFile, String JavaDoc propertyName, String JavaDoc version) throws IOException {
156         StringBuffer JavaDoc buffer = readFile(buildFile);
157         int pos = scan(buffer, 0, propertyName);
158         if (pos == -1)
159             return;
160         pos = scan(buffer, pos, "value"); //$NON-NLS-1$
161
if (pos == -1)
162             return;
163         int begin = scan(buffer, pos, "\""); //$NON-NLS-1$
164
if (begin == -1)
165             return;
166         begin++;
167         int end = scan(buffer, begin, "\""); //$NON-NLS-1$
168
if (end == -1)
169             return;
170         String JavaDoc currentVersion = buffer.substring(begin, end);
171         String JavaDoc newVersion = version;
172         if (currentVersion.equals(newVersion))
173             return;
174         buffer.replace(begin, end, newVersion);
175         Utils.transferStreams(new ByteArrayInputStream(buffer.toString().getBytes()), new FileOutputStream(buildFile));
176     }
177
178     /**
179      * Method selectConfigs.
180      * Return a list containing all the configurations that are valid for the
181      * element
182      * @param element
183      * @return List
184      */

185     public List selectConfigs(IPlatformEnvironment element) {
186         List result = new ArrayList(getConfigInfos());
187
188         if (((element.getOS() == null || element.getOS().equals(Config.ANY)) && includePlatformIndependent == false) &&
189             ((element.getWS() == null || element.getWS().equals(Config.ANY)) && includePlatformIndependent == false) &&
190             ((element.getOSArch() == null || element.getOSArch().equals(Config.ANY)) && includePlatformIndependent == false)) {
191             result.clear();
192             return result;
193         }
194
195         if (element.getOS() != null && !element.getOS().equals(Config.ANY)) {
196             for (Iterator iter = result.iterator(); iter.hasNext();) {
197                 Config config = (Config) iter.next();
198                 if (! isMatching(element.getOS(), config.getOs()) )
199                     iter.remove();
200             }
201         }
202         if (element.getWS() != null && !element.getWS().equals(Config.ANY)) {
203             for (Iterator iter = result.iterator(); iter.hasNext();) {
204                 Config config = (Config) iter.next();
205                 if (! isMatching(element.getWS(), config.getWs()) )
206                     iter.remove();
207             }
208         }
209         if (element.getOSArch() != null && !element.getOSArch().equals(Config.ANY)) {
210             for (Iterator iter = result.iterator(); iter.hasNext();) {
211                 Config config = (Config) iter.next();
212                 if (! isMatching(element.getOSArch(), config.getArch()))
213                     iter.remove();
214             }
215         }
216         return result;
217     }
218
219     private boolean isMatching(String JavaDoc candidateValues, String JavaDoc configValue) {
220         StringTokenizer stok = new StringTokenizer(candidateValues, ","); //$NON-NLS-1$
221
while (stok.hasMoreTokens()) {
222             String JavaDoc token = stok.nextToken().toUpperCase();
223             if (configValue.equalsIgnoreCase(token)) return true;
224         }
225         return false;
226     }
227     
228     public Set getCompiledElements() {
229         if (compiledElements == null)
230             compiledElements = new HashSet();
231         return compiledElements;
232     }
233
234     /**
235      * Sets the compiledElements.
236      * @param compiledElements The compiledElements to set
237      */

238     public void setCompiledElements(Set compiledElements) {
239         this.compiledElements = compiledElements;
240     }
241
242     public void setReportResolutionErrors(boolean value) {
243         reportResolutionErrors = value;
244     }
245
246     /**
247      * @return Returns the ignoreMissingPropertiesFile.
248      */

249     public boolean isIgnoreMissingPropertiesFile() {
250         if (BundleHelper.getDefault().isDebugging())
251             return false;
252         return ignoreMissingPropertiesFile;
253     }
254     
255
256     /**
257      * @param value The ignoreMissingPropertiesFile to set.
258      */

259     public void setIgnoreMissingPropertiesFile(boolean value) {
260         ignoreMissingPropertiesFile = value;
261     }
262 }
263
Popular Tags