1402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll/*******************************************************************************
2402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * Copyright (c) 2000, 2006 IBM Corporation and others.
3402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * All rights reserved. This program and the accompanying materials
4402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * are made available under the terms of the Eclipse Public License v1.0
5402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * which accompanies this distribution, and is available at
6402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * http://www.eclipse.org/legal/epl-v10.html
7402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll *
8402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * Contributors:
9402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll *     IBM Corporation - initial API and implementation
10402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll *******************************************************************************/
11402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll/**
12402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll * Parses feature.xml, plugin.xml, and fragment.xml files
13402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll *
14402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll */
15402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
16402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollpackage org.eclipse.releng;
17402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport javax.xml.parsers.ParserConfigurationException;
18402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport javax.xml.parsers.SAXParser;
19402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport javax.xml.parsers.SAXParserFactory;
20402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.xml.sax.Attributes;
21402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.xml.sax.helpers.DefaultHandler;
22402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.xml.sax.SAXException;
23402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport java.io.IOException;
24402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport java.util.Vector;
25402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport java.io.File;
26402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollimport org.apache.tools.ant.BuildException;
27402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
28402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
29402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Mollpublic class ElementParser extends DefaultHandler {
30402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
31402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	private SAXParser parser;
32402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	private Vector plugins;
33402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	private Vector features;
34402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
35402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	public Vector getPlugins(){return plugins;}
36402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	public Vector getFeatures(){return features;}
37402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
38402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    public ElementParser() {
39402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        //  Create a Xerces SAX Parser
40402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    	SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
41402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
42402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    	try {
43402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			parser = saxParserFactory.newSAXParser();
44402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		} catch (ParserConfigurationException e) {
45402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			e.printStackTrace();
46402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		} catch (SAXException e) {
47402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			e.printStackTrace();
48402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		}
49402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
50402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
51402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
52402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        // instantiate vectors that will hold lists of plugins and features read from feature.xml
53402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        plugins = new Vector();
54402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        features = new Vector();
55402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    }
56402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
57402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    public void parse(String xmlFile){
58402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
59402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	    //  Parse the Document
60402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        try {
61402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll            parser.parse(xmlFile,this);
62402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        } catch (SAXException e) {
63402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll            System.err.println (e);
64402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        } catch (IOException e) {
65402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll            System.err.println (e);
66402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
67402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        }
68402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    }
69402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
70402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	public void parse(String install, String type, String id){
71402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
72402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		String xmlFile=null;
73402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
74402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		if (type.equals("feature"))
75402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			xmlFile=install+"/features/"+id+"/"+"feature.xml";
76402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		if (type.equals("plugin"))
77402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			xmlFile=install+"/plugins/"+id+"/"+"plugin.xml";
78402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		if (type.equals("fragment"))
79402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			xmlFile=install+"/plugins/"+"/"+id+"/"+"fragment.xml";
80402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
81402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		if (new File(xmlFile).exists())
82402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			parse(xmlFile);
83402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
84402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		else{
85402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll			throw new BuildException("The following "+type+" "+id+" did not get fetched.");
86402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll		}
87402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
88402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll	}
89402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
90402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    //  Start Element Event Handler
91402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    public void startElement (String uri, String local,
92402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        String qName, Attributes atts)  {
93402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        if (local.equals("plugin")||local.equals("fragment"))
94402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    		add(atts.getValue("id"), plugins);
95402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    	if (local.equals("feature"))
96402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    		add(atts.getValue("id")+"-feature", features);
97402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    }
98402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
99402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    public void add(String element, Vector v){
100402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    	if (!v.contains(element))
101402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    		v.add(element);
102402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    }
103402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
104402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    // Test
105402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    public static void main (String[] args) {
106402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        ElementParser xmlParser = new ElementParser();
107402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform-feature");
108402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.win32-feature");
109402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.linux.motif-feature");
110402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.linux.gtk-feature");
111402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.solaris.motif-feature");
112402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.aix.motif-feature");
113402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.qnx.photon-feature");
114402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.jdt-feature");
115402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.pde-feature");
116402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.sdk.examples-feature");
117402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.sdk.tests-feature");
118402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
119402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.source-feature");
120402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.win32.source-feature");
121402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.linux.motif.source-feature");
122402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.linux.gtk.source-feature");
123402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.solaris.motif.source-feature");
124402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.aix.motif.source-feature");
125402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.qnx.photon.source-feature");
126402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.jdt.source-feature");
127402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
128402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        System.out.println(xmlParser.plugins);
129402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        System.out.println(xmlParser.features);
130402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll
131402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        System.out.println(xmlParser.plugins.size()+" plugins expected");
132402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll        System.out.println(xmlParser.features.size()+" features expected");
133402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll    }
134402794e73aed8611d62eb4b01cd155e2d76fcb87Raphael Moll}
135