aboutsummaryrefslogtreecommitdiff
path: root/basebuilder-3.6.2/org.eclipse.releng.basebuilder/plugins/org.eclipse.build.tools/src/org/eclipse/releng/generators/VersionTrackerTask.java
blob: 68c1631c935c7c7a4bd56d7de9a651ec61dcc845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/**
 * This class finds the version of a plug-in, or fragment listed in a feature
 * and writes <element>=<element>_<version> for each in a properties file.
 * The file produced from this task can be loaded by an Ant script to find files in the
 * binary versions of plugins and fragments.
 */
package org.eclipse.releng.generators;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import java.io.*;
import java.util.Hashtable;
import java.util.Enumeration;
import org.apache.tools.ant.Task;
import java.util.Vector;

public class VersionTrackerTask extends Task {

	private String buildDirectory;
	private Hashtable elements;
	private SAXParser parser;
	private Vector allElements;
	
	//the feature to from which to collect version information
	private String featurePath;
	
	//the path to the file in which to write the results
	private String outputFilePath;
	
	public void execute(){
		VersionTrackerTask tracker =
			new VersionTrackerTask(getBuildDirectory());
		tracker.parse(getFeaturePath(),new FeatureHandler());
		tracker.parse(new PluginHandler());
		tracker.writeProperties(getOutputFilePath(), true);
	}
	
	//test
	public static void main(String[] args) {
		VersionTrackerTask Tracker =
			new VersionTrackerTask(args[1]);
		Tracker.parse(args[0],Tracker.new FeatureHandler());
		Tracker.parse(Tracker.new PluginHandler());
			Tracker.writeProperties(args[2], true);
	}

	public VersionTrackerTask(){
	}
	
	public VersionTrackerTask(String install) {
		elements = new Hashtable();
		allElements=new Vector();
		
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		try {
			parser = saxParserFactory.newSAXParser();
		} catch (ParserConfigurationException e) {
		  	e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
        
       	// directory containing the source for a given build
		buildDirectory = install;
	}

	private void parse (DefaultHandler handler){
		for (int i=0; i<allElements.size();i++){
			parse(allElements.elementAt(i).toString(), handler);
		}
	}
	
    public void parse(String xmlFile,DefaultHandler handler){
         try {
          parser.parse(xmlFile,handler);
        } catch (SAXException e) {
            System.err.println (e);
        } catch (IOException e) {
            System.err.println (e);    
        } 
    }

    private class FeatureHandler extends DefaultHandler{
    	//  Start Element Event Handler
    	public void startElement(
		 String uri,
		 String local,
			String qName,
			Attributes atts) {

    		String element = atts.getValue("id");
    		//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"
    		if (qName.equals("plugin")) {
    			try{
     			allElements.add(getBuildDirectory()+File.separator+"plugins"+File.separator+element+File.separator+"plugin.xml");
    			} catch (Exception e){
    				e.printStackTrace();
    		
    			}
 			} else if (qName.equals("fragment")){
				allElements.add(getBuildDirectory()+File.separator+"plugins"+File.separator+element+File.separator+"fragment.xml");
			}
    	}
    }
 
    private class PluginHandler extends DefaultHandler{
    	//  Start Element Event Handler
    	public void startElement(
								 String uri,
								 String local,
								 String qName,
								 Attributes atts) {

    		
    		String element = atts.getValue("id");
    		String version = atts.getValue("version");
    		System.out.println("Examining "+element);
    		
    		if (qName.equals("plugin") || qName.equals("fragment")){
    			System.out.println("Found plugin "+element);
    			elements.put(element,element+"_"+version);
    		}
    	}
    }
	
	public void writeProperties(String propertiesFile,boolean append){
		try{
			
		PrintWriter writer = new PrintWriter(new FileWriter(propertiesFile,append));
				
			Enumeration keys = elements.keys();

			while (keys.hasMoreElements()){
				Object key = keys.nextElement();
				writer.println(key.toString()+"="+elements.get(key).toString());
				writer.flush();
			}
			writer.close();
		
		} catch (IOException e){
			System.out.println("Unable to write to file "+propertiesFile);
		}
		
		
	}

	/**
	 * @return Returns the featurePath.
	 */
	public String getFeaturePath() {
		return featurePath;
	}

	/**
	 * @param featurePath The featurePath to set.
	 */
	public void setFeaturePath(String featurePath) {
		this.featurePath = featurePath;
	}

	/**
	 * @return Returns the installDirectory.
	 */
	public String getBuildDirectory() {
		return buildDirectory;
	}

	/**
	 * @param installDirectory The installDirectory to set.
	 */
	public void setBuildDirectory(String buildDirectory) {
		this.buildDirectory = buildDirectory;
	}

	/**
	 * @return Returns the outputFilePath.
	 */
	public String getOutputFilePath() {
		return outputFilePath;
	}

	/**
	 * @param outputFilePath The outputFilePath to set.
	 */
	public void setOutputFilePath(String outputFilePath) {
		this.outputFilePath = outputFilePath;
	}

}