1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 feature, plugin, or fragment in a given
13  * build source tree.
14  */
15 
16 import org.xml.sax.Attributes;
17 import org.xml.sax.helpers.DefaultHandler;
18 import org.apache.xerces.parsers.SAXParser;
19 import org.xml.sax.SAXException;
20 import java.io.*;
21 import java.util.Hashtable;
22 import java.util.Enumeration;
23 
24 public class TestVersionTracker extends DefaultHandler {
25 
26 	private String installDirectory;
27 	private Hashtable elements;
28 	private SAXParser parser;
29 	private String xmlFile;
30 
31 	//test
main(String[] args)32 	public static void main(String[] args) {
33 		TestVersionTracker Tracker =
34 			new TestVersionTracker(args[1]);
35 		Tracker.parse(args[0]);
36 		Tracker.writeProperties(args[2], true);
37 	}
38 
TestVersionTracker(String install, Hashtable elements)39 	public TestVersionTracker(String install, Hashtable elements) {
40 		//  Create a Xerces SAX Parser
41 		parser = new SAXParser();
42 
43 		//  Set Content Handler
44 		parser.setContentHandler (this);
45 
46 		// directory containing the source for a given build
47 		installDirectory = install;
48 
49 		//  instantiate hashtable that will hold directory names with versions for elements
50 		this.elements = elements;
51 	}
52 
TestVersionTracker(String install)53 	public TestVersionTracker(String install) {
54 		this(install, new Hashtable());
55 	}
56 
parse(String xmlFile)57 	public void parse(String xmlFile){
58 		this.xmlFile = xmlFile;
59 		//  Parse the Document
60 		try {
61 			parser.parse(this.xmlFile);
62 		} catch (SAXException e) {
63 			System.err.println (e);
64 		} catch (IOException e) {
65 			System.err.println (e);
66 
67 		}
68 	}
69 
70 	//  Start Element Event Handler
startElement( String uri, String local, String qName, Attributes atts)71 	public void startElement(
72 		String uri,
73 		String local,
74 		String qName,
75 		Attributes atts) {
76 
77 		String element = atts.getValue("id");
78 		String version = atts.getValue("version");
79 
80 		if (local.equals("plugin") || local.equals("fragment")) {
81 				elements.put(element,element+"_"+version);
82 		} else if (local.equals("feature"))
83 				elements.put(element+"-feature",element+"_"+version);
84 		else if (local.equals("includes")) {
85 			File thisFile = new File(xmlFile);
86 			String includeFile = thisFile.getParentFile().getParent() + '/' + element+"_"+version + "/feature.xml";
87 			TestVersionTracker recurseTracker = new TestVersionTracker(installDirectory, elements);
88 			recurseTracker.parse(includeFile);
89 		}
90 	}
91 
writeProperties(String propertiesFile,boolean append)92 	public void writeProperties(String propertiesFile,boolean append){
93 		try{
94 
95 		PrintWriter writer = new PrintWriter(new FileWriter(propertiesFile,append));
96 
97 			Enumeration keys = elements.keys();
98 
99 			while (keys.hasMoreElements()){
100 				Object key = keys.nextElement();
101 				writer.println(key.toString()+"="+elements.get(key).toString());
102 				writer.flush();
103 			}
104 			writer.close();
105 
106 		} catch (IOException e){
107 			System.out.println("Unable to write to file "+propertiesFile);
108 		}
109 
110 
111 	}
112 
113 }
114