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 Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.releng;
12
13import org.apache.tools.ant.BuildException;
14import org.apache.tools.ant.Task;
15import java.util.StringTokenizer;
16import java.util.Vector;
17import java.util.Enumeration;
18import java.io.*;
19
20/**
21 * Uses an ElementParser to read a list of feature.xml files and to determine
22 * if all plugins defined in the features are present.
23 */
24
25public class FetchValidator extends Task {
26
27	//directory containing of build source, parent of features and plugins
28	private String install = "";
29
30	//Comma separated list of features to parse
31	private String list;
32
33	//Elements to check for post fetch (use name of project in dev.eclipse.org)
34	private Vector features;
35	private Vector plugins;
36
37	//keeps track of missing elements
38	private Vector missingPlugins;
39	private Vector missingFeatures;
40
41
42	//parser
43	ElementParser parser;
44
45	//test
46	public static void main(String args[]) {
47		FetchValidator validator = new FetchValidator();
48		validator.install = "l:/vabase/team/sonia";
49		validator.list =
50			"org.eclipse.platform-feature,org.eclipse.platform.win32-feature,org.eclipse.platform.linux.motif-feature";
51		validator.getListOfFeatures(validator.list);
52
53		validator.execute();
54	}
55
56	// entry point
57	public void execute() throws BuildException {
58		getListOfFeatures(list);
59
60		if (!allPresent()) {
61			String missingFeaturesDesc="";
62			String missingPluginsDesc="";
63
64			if (missingFeatures.size() > 0) {
65				for (int i = 0; i < missingFeatures.size(); i++) {
66					missingFeaturesDesc+="\n\r"+missingFeatures.get(i).toString();
67				}
68			}
69
70			if (missingPlugins.size() > 0) {
71				for (int i = 0; i < missingPlugins.size(); i++) {
72					missingPluginsDesc+="\n\t"+missingPlugins.get(i).toString();
73				}
74			}
75			throw new BuildException("The following projects did not get fetched: \n"+missingFeaturesDesc+missingPluginsDesc+"\n"
76			+"\n\nPossible causes of missing source files include an incorrect Tag entry in a .map file or problems with CVS repositories.");
77		}
78
79		System.out.println("Fetch Complete.");
80	}
81
82	public FetchValidator() {
83		parser = new ElementParser();
84		missingPlugins = new Vector();
85		missingFeatures = new Vector();
86	}
87
88	private void getListOfFeatures(String list) {
89
90		StringTokenizer tokenizer = new StringTokenizer(list, ",");
91
92		while (tokenizer.hasMoreTokens()) {
93			parser.parse(
94				install,
95				"feature",
96				(String) tokenizer.nextToken().trim());
97		}
98
99		features = parser.getFeatures();
100		plugins = parser.getPlugins();
101	}
102
103	private boolean allPresent() {
104		// verify presence of all source projects for the build.
105		// collect a list of missing plugins (or fragments), and features
106
107		boolean allPresent = true;
108		Enumeration enumeration = plugins.elements();
109
110		while (enumeration.hasMoreElements()) {
111			String plugin = (String) enumeration.nextElement();
112			if (new File(install + "/plugins/" + plugin).exists())
113				continue;
114			else {
115				missingPlugins.add(plugin);
116				allPresent = false;
117			}
118		}
119
120		enumeration = features.elements();
121
122		while (enumeration.hasMoreElements()) {
123			String feature = (String) enumeration.nextElement();
124			if (new File(install + "/features/" + feature).exists())
125				continue;
126			else {
127				missingFeatures.add(feature);
128				allPresent = false;
129			}
130		}
131
132		return allPresent;
133	}
134
135	/**
136	 * Gets the install.
137	 * @return Returns a String
138	 */
139	public String getInstall() {
140		return install;
141	}
142
143	/**
144	 * Gets the list.
145	 * @return Returns a String
146	 */
147	public String getList() {
148		return list;
149	}
150
151	/**
152	 * Sets the install.
153	 * @param install The install to set
154	 */
155	public void setInstall(String install) {
156		this.install = install;
157	}
158
159	/**
160	 * Sets the list.
161	 * @param list The list to set
162	 */
163	public void setList(String list) {
164		this.list = list;
165	}
166
167}
168