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 *******************************************************************************/
11/**
12 * This class finds the version of a plug-in, or fragment listed in a feature
13 * and writes <element>=<element>_<version> for each in a properties file.
14 * The file produced from this task can be loaded by an Ant script to find files in the
15 * binary versions of plugins and fragments.
16 */
17package org.eclipse.releng.generators;
18
19import org.xml.sax.Attributes;
20import org.xml.sax.helpers.DefaultHandler;
21
22import javax.xml.parsers.ParserConfigurationException;
23import javax.xml.parsers.SAXParser;
24import javax.xml.parsers.SAXParserFactory;
25import org.xml.sax.SAXException;
26import java.io.*;
27import java.util.Hashtable;
28import java.util.Enumeration;
29import org.apache.tools.ant.Task;
30import java.util.Vector;
31
32public class VersionTrackerTask extends Task {
33
34	private String buildDirectory;
35	private Hashtable elements;
36	private SAXParser parser;
37	private Vector allElements;
38
39	//the feature to from which to collect version information
40	private String featurePath;
41
42	//the path to the file in which to write the results
43	private String outputFilePath;
44
45	public void execute(){
46		VersionTrackerTask tracker =
47			new VersionTrackerTask(getBuildDirectory());
48		tracker.parse(getFeaturePath(),new FeatureHandler());
49		tracker.parse(new PluginHandler());
50		tracker.writeProperties(getOutputFilePath(), true);
51	}
52
53	//test
54	public static void main(String[] args) {
55		VersionTrackerTask Tracker =
56			new VersionTrackerTask(args[1]);
57		Tracker.parse(args[0],Tracker.new FeatureHandler());
58		Tracker.parse(Tracker.new PluginHandler());
59			Tracker.writeProperties(args[2], true);
60	}
61
62	public VersionTrackerTask(){
63	}
64
65	public VersionTrackerTask(String install) {
66		elements = new Hashtable();
67		allElements=new Vector();
68
69		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
70		try {
71			parser = saxParserFactory.newSAXParser();
72		} catch (ParserConfigurationException e) {
73		  	e.printStackTrace();
74		} catch (SAXException e) {
75			e.printStackTrace();
76		}
77
78       	// directory containing the source for a given build
79		buildDirectory = install;
80	}
81
82	private void parse (DefaultHandler handler){
83		for (int i=0; i<allElements.size();i++){
84			parse(allElements.elementAt(i).toString(), handler);
85		}
86	}
87
88    public void parse(String xmlFile,DefaultHandler handler){
89         try {
90          parser.parse(xmlFile,handler);
91        } catch (SAXException e) {
92            System.err.println (e);
93        } catch (IOException e) {
94            System.err.println (e);
95        }
96    }
97
98    private class FeatureHandler extends DefaultHandler{
99    	//  Start Element Event Handler
100    	public void startElement(
101		 String uri,
102		 String local,
103			String qName,
104			Attributes atts) {
105
106    		String element = atts.getValue("id");
107    		//need to parse the plugin.xml or fragment.xml for the correct version value since the 3.0 features may list these as "0.0.0"
108    		if (qName.equals("plugin")) {
109    			try{
110     			allElements.add(getBuildDirectory()+File.separator+"plugins"+File.separator+element+File.separator+"plugin.xml");
111    			} catch (Exception e){
112    				e.printStackTrace();
113
114    			}
115 			} else if (qName.equals("fragment")){
116				allElements.add(getBuildDirectory()+File.separator+"plugins"+File.separator+element+File.separator+"fragment.xml");
117			}
118    	}
119    }
120
121    private class PluginHandler extends DefaultHandler{
122    	//  Start Element Event Handler
123    	public void startElement(
124								 String uri,
125								 String local,
126								 String qName,
127								 Attributes atts) {
128
129
130    		String element = atts.getValue("id");
131    		String version = atts.getValue("version");
132    		System.out.println("Examining "+element);
133
134    		if (qName.equals("plugin") || qName.equals("fragment")){
135    			System.out.println("Found plugin "+element);
136    			elements.put(element,element+"_"+version);
137    		}
138    	}
139    }
140
141	public void writeProperties(String propertiesFile,boolean append){
142		try{
143
144		PrintWriter writer = new PrintWriter(new FileWriter(propertiesFile,append));
145
146			Enumeration keys = elements.keys();
147
148			while (keys.hasMoreElements()){
149				Object key = keys.nextElement();
150				writer.println(key.toString()+"="+elements.get(key).toString());
151				writer.flush();
152			}
153			writer.close();
154
155		} catch (IOException e){
156			System.out.println("Unable to write to file "+propertiesFile);
157		}
158
159
160	}
161
162	/**
163	 * @return Returns the featurePath.
164	 */
165	public String getFeaturePath() {
166		return featurePath;
167	}
168
169	/**
170	 * @param featurePath The featurePath to set.
171	 */
172	public void setFeaturePath(String featurePath) {
173		this.featurePath = featurePath;
174	}
175
176	/**
177	 * @return Returns the installDirectory.
178	 */
179	public String getBuildDirectory() {
180		return buildDirectory;
181	}
182
183	/**
184	 * @param installDirectory The installDirectory to set.
185	 */
186	public void setBuildDirectory(String buildDirectory) {
187		this.buildDirectory = buildDirectory;
188	}
189
190	/**
191	 * @return Returns the outputFilePath.
192	 */
193	public String getOutputFilePath() {
194		return outputFilePath;
195	}
196
197	/**
198	 * @param outputFilePath The outputFilePath to set.
199	 */
200	public void setOutputFilePath(String outputFilePath) {
201		this.outputFilePath = outputFilePath;
202	}
203
204}
205