001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM - Initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.build.builder;
011:
012: import java.io.*;
013: import java.util.*;
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.pde.internal.build.*;
016: import org.eclipse.update.core.IPlatformEnvironment;
017:
018: /**
019: * Instance of this class and subclasses are created on a plugin / feature basis.
020: */
021: public abstract class AbstractBuildScriptGenerator extends
022: AbstractScriptGenerator {
023: /** Additional dev entries for the compile classpath. */
024: protected DevClassPathHelper devEntries;
025:
026: /** Contain the elements that will be assembled */
027: protected AssemblyInformation assemblyData;
028:
029: /** The content of the build.properties file associated to the element for which the script is generated */
030: protected Properties buildProperties;
031: private Set compiledElements; //The elements we are compiling
032:
033: private boolean includePlatformIndependent = true;
034:
035: /** flag indicating whether or not the missing properties file should be logged */
036: private boolean ignoreMissingPropertiesFile = true;
037:
038: static private Properties executionEnvironmentMappings = null;
039:
040: abstract protected Properties getBuildProperties()
041: throws CoreException;
042:
043: static public Properties getExecutionEnvironmentMappings() {
044: if (executionEnvironmentMappings != null)
045: return executionEnvironmentMappings;
046:
047: executionEnvironmentMappings = new Properties();
048: InputStream stream = null;
049: try {
050: stream = BundleHelper.getDefault().getBundle().getEntry(
051: "data/env.properties").openStream(); //$NON-NLS-1$
052: executionEnvironmentMappings.load(stream);
053: } catch (IOException e) {
054: //ignore
055: } finally {
056: try {
057: if (stream != null)
058: stream.close();
059: } catch (IOException e) {
060: //ignore
061: }
062: }
063: return executionEnvironmentMappings;
064: }
065:
066: public void setDevEntries(String entries) {
067: devEntries = new DevClassPathHelper(entries);
068: }
069:
070: public void setDevEntries(DevClassPathHelper entries) {
071: devEntries = entries;
072: }
073:
074: public void includePlatformIndependent(boolean value) {
075: includePlatformIndependent = value;
076: }
077:
078: public boolean isPlatformIndependentIncluded() {
079: return includePlatformIndependent;
080: }
081:
082: /**
083: *
084: * @param buf
085: * @param start
086: * @param target
087: * @return int
088: */
089: protected int scan(StringBuffer buf, int start, String target) {
090: return scan(buf, start, new String[] { target });
091: }
092:
093: /**
094: *
095: * @param buf
096: * @param start
097: * @param targets
098: * @return int
099: */
100: protected int scan(StringBuffer buf, int start, String[] targets) {
101: for (int i = start; i < buf.length(); i++) {
102: for (int j = 0; j < targets.length; j++) {
103: if (i < buf.length() - targets[j].length()) {
104: String match = buf.substring(i, i
105: + targets[j].length());
106: if (targets[j].equals(match))
107: return i;
108: }
109: }
110: }
111: return -1;
112: }
113:
114: /**
115: * Return a buffer containing the contents of the file at the specified location.
116: *
117: * @param target the file
118: * @return StringBuffer
119: * @throws IOException
120: */
121: protected StringBuffer readFile(File target) throws IOException {
122: return readFile(new FileInputStream(target));
123: }
124:
125: protected StringBuffer readFile(InputStream stream)
126: throws IOException {
127: InputStreamReader reader = new InputStreamReader(
128: new BufferedInputStream(stream));
129: StringBuffer result = new StringBuffer();
130: char[] buf = new char[4096];
131: int count;
132: try {
133: count = reader.read(buf, 0, buf.length);
134: while (count != -1) {
135: result.append(buf, 0, count);
136: count = reader.read(buf, 0, buf.length);
137: }
138: } finally {
139: try {
140: reader.close();
141: } catch (IOException e) {
142: // ignore exceptions here
143: }
144: }
145: return result;
146: }
147:
148: /**
149: * Custom build scripts should have their version number matching the
150: * version number defined by the feature/plugin/fragment descriptor.
151: * This is a best effort job so do not worry if the expected tags were
152: * not found and just return without modifying the file.
153: *
154: * @param buildFile
155: * @param propertyName
156: * @param version
157: * @throws IOException
158: *
159: */
160: protected void updateVersion(File buildFile, String propertyName,
161: String version) throws IOException {
162: StringBuffer buffer = readFile(buildFile);
163: int pos = scan(buffer, 0, propertyName);
164: if (pos == -1)
165: return;
166: pos = scan(buffer, pos, "value"); //$NON-NLS-1$
167: if (pos == -1)
168: return;
169: int begin = scan(buffer, pos, "\""); //$NON-NLS-1$
170: if (begin == -1)
171: return;
172: begin++;
173: int end = scan(buffer, begin, "\""); //$NON-NLS-1$
174: if (end == -1)
175: return;
176: String currentVersion = buffer.substring(begin, end);
177: String newVersion = version;
178: if (currentVersion.equals(newVersion))
179: return;
180: buffer.replace(begin, end, newVersion);
181: Utils.transferStreams(new ByteArrayInputStream(buffer
182: .toString().getBytes()),
183: new FileOutputStream(buildFile));
184: }
185:
186: /**
187: * Method selectConfigs.
188: * Return a list containing all the configurations that are valid for the
189: * element
190: * @param element
191: * @return List
192: */
193: public List selectConfigs(IPlatformEnvironment element) {
194: List result = new ArrayList(getConfigInfos());
195:
196: if (((element.getOS() == null || element.getOS().equals(
197: Config.ANY)) && includePlatformIndependent == false)
198: && ((element.getWS() == null || element.getWS().equals(
199: Config.ANY)) && includePlatformIndependent == false)
200: && ((element.getOSArch() == null || element.getOSArch()
201: .equals(Config.ANY)) && includePlatformIndependent == false)) {
202: result.clear();
203: return result;
204: }
205:
206: if (element.getOS() != null
207: && !element.getOS().equals(Config.ANY)) {
208: for (Iterator iter = result.iterator(); iter.hasNext();) {
209: Config config = (Config) iter.next();
210: if (!isMatching(element.getOS(), config.getOs()))
211: iter.remove();
212: }
213: }
214: if (element.getWS() != null
215: && !element.getWS().equals(Config.ANY)) {
216: for (Iterator iter = result.iterator(); iter.hasNext();) {
217: Config config = (Config) iter.next();
218: if (!isMatching(element.getWS(), config.getWs()))
219: iter.remove();
220: }
221: }
222: if (element.getOSArch() != null
223: && !element.getOSArch().equals(Config.ANY)) {
224: for (Iterator iter = result.iterator(); iter.hasNext();) {
225: Config config = (Config) iter.next();
226: if (!isMatching(element.getOSArch(), config.getArch()))
227: iter.remove();
228: }
229: }
230: return result;
231: }
232:
233: private boolean isMatching(String candidateValues,
234: String configValue) {
235: StringTokenizer stok = new StringTokenizer(candidateValues, ","); //$NON-NLS-1$
236: while (stok.hasMoreTokens()) {
237: String token = stok.nextToken().toUpperCase();
238: if (configValue.equalsIgnoreCase(token))
239: return true;
240: }
241: return false;
242: }
243:
244: public Set getCompiledElements() {
245: if (compiledElements == null)
246: compiledElements = new HashSet();
247: return compiledElements;
248: }
249:
250: /**
251: * Sets the compiledElements.
252: * @param compiledElements The compiledElements to set
253: */
254: public void setCompiledElements(Set compiledElements) {
255: this .compiledElements = compiledElements;
256: }
257:
258: public void setReportResolutionErrors(boolean value) {
259: reportResolutionErrors = value;
260: }
261:
262: /**
263: * @return Returns the ignoreMissingPropertiesFile.
264: */
265: public boolean isIgnoreMissingPropertiesFile() {
266: if (BundleHelper.getDefault().isDebugging())
267: return false;
268: return ignoreMissingPropertiesFile;
269: }
270:
271: /**
272: * @param value The ignoreMissingPropertiesFile to set.
273: */
274: public void setIgnoreMissingPropertiesFile(boolean value) {
275: ignoreMissingPropertiesFile = value;
276: }
277: }
|