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 * Parses feature.xml, plugin.xml, and fragment.xml files
13 *
14 */
15
16package org.eclipse.releng;
17import javax.xml.parsers.ParserConfigurationException;
18import javax.xml.parsers.SAXParser;
19import javax.xml.parsers.SAXParserFactory;
20import org.xml.sax.Attributes;
21import org.xml.sax.helpers.DefaultHandler;
22import org.xml.sax.SAXException;
23import java.io.IOException;
24import java.util.Vector;
25import java.io.File;
26import org.apache.tools.ant.BuildException;
27
28
29public class ElementParser extends DefaultHandler {
30
31	private SAXParser parser;
32	private Vector plugins;
33	private Vector features;
34
35	public Vector getPlugins(){return plugins;}
36	public Vector getFeatures(){return features;}
37
38    public ElementParser() {
39        //  Create a Xerces SAX Parser
40    	SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
41
42    	try {
43			parser = saxParserFactory.newSAXParser();
44		} catch (ParserConfigurationException e) {
45			e.printStackTrace();
46		} catch (SAXException e) {
47			e.printStackTrace();
48		}
49
50
51
52        // instantiate vectors that will hold lists of plugins and features read from feature.xml
53        plugins = new Vector();
54        features = new Vector();
55    }
56
57    public void parse(String xmlFile){
58
59	    //  Parse the Document
60        try {
61            parser.parse(xmlFile,this);
62        } catch (SAXException e) {
63            System.err.println (e);
64        } catch (IOException e) {
65            System.err.println (e);
66
67        }
68    }
69
70	public void parse(String install, String type, String id){
71
72		String xmlFile=null;
73
74		if (type.equals("feature"))
75			xmlFile=install+"/features/"+id+"/"+"feature.xml";
76		if (type.equals("plugin"))
77			xmlFile=install+"/plugins/"+id+"/"+"plugin.xml";
78		if (type.equals("fragment"))
79			xmlFile=install+"/plugins/"+"/"+id+"/"+"fragment.xml";
80
81		if (new File(xmlFile).exists())
82			parse(xmlFile);
83
84		else{
85			throw new BuildException("The following "+type+" "+id+" did not get fetched.");
86		}
87
88	}
89
90    //  Start Element Event Handler
91    public void startElement (String uri, String local,
92        String qName, Attributes atts)  {
93        if (local.equals("plugin")||local.equals("fragment"))
94    		add(atts.getValue("id"), plugins);
95    	if (local.equals("feature"))
96    		add(atts.getValue("id")+"-feature", features);
97    }
98
99    public void add(String element, Vector v){
100    	if (!v.contains(element))
101    		v.add(element);
102    }
103
104    // Test
105    public static void main (String[] args) {
106        ElementParser xmlParser = new ElementParser();
107        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform-feature");
108        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.win32-feature");
109        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.linux.motif-feature");
110        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.linux.gtk-feature");
111        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.solaris.motif-feature");
112        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.aix.motif-feature");
113        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.qnx.photon-feature");
114        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.jdt-feature");
115        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.pde-feature");
116        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.sdk.examples-feature");
117        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.sdk.tests-feature");
118
119        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.source-feature");
120        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.win32.source-feature");
121        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.linux.motif.source-feature");
122        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.linux.gtk.source-feature");
123        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.solaris.motif.source-feature");
124        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.aix.motif.source-feature");
125        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.qnx.photon.source-feature");
126        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.jdt.source-feature");
127
128        System.out.println(xmlParser.plugins);
129        System.out.println(xmlParser.features);
130
131        System.out.println(xmlParser.plugins.size()+" plugins expected");
132        System.out.println(xmlParser.features.size()+" features expected");
133    }
134}
135