blob: 003620c48507c8f5bb333a490dd7d1830e358e0e [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2000, 2016 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
*******************************************************************************/
/**
* Parses feature.xml, plugin.xml, and fragment.xml files
*
*/
package org.eclipse.releng;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.tools.ant.BuildException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ElementParser extends DefaultHandler {
// Test
public static void main(final String[] args) {
final ElementParser xmlParser = new ElementParser();
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.win32-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.linux.motif-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.linux.gtk-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.solaris.motif-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.aix.motif-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.qnx.photon-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.jdt-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.pde-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.sdk.examples-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.sdk.tests-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.source-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.win32.source-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.linux.motif.source-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.linux.gtk.source-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.solaris.motif.source-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.aix.motif.source-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.platform.qnx.photon.source-feature");
xmlParser.parse("l:/vabase/team/sonia", "feature", "org.eclipse.jdt.source-feature");
System.out.println(xmlParser.plugins);
System.out.println(xmlParser.features);
System.out.println(xmlParser.plugins.size() + " plugins expected");
System.out.println(xmlParser.features.size() + " features expected");
}
private SAXParser parser;
private final Vector<String> plugins;
private final Vector<String> features;
public ElementParser() {
// Create a Xerces SAX Parser
final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
try {
parser = saxParserFactory.newSAXParser();
}
catch (final ParserConfigurationException e) {
e.printStackTrace();
}
catch (final SAXException e) {
e.printStackTrace();
}
// instantiate vectors that will hold lists of plugins and features read
// from feature.xml
plugins = new Vector<>();
features = new Vector<>();
}
public void add(final String element, final Vector<String> v) {
if (!v.contains(element)) {
v.add(element);
}
}
public Vector<String> getFeatures() {
return features;
}
public Vector<String> getPlugins() {
return plugins;
}
public void parse(final String xmlFile) {
// Parse the Document
try {
parser.parse(xmlFile, this);
}
catch (final SAXException e) {
System.err.println(e);
}
catch (final IOException e) {
System.err.println(e);
}
}
public void parse(final String install, final String type, final String id) {
String xmlFile = null;
if (type.equals("feature")) {
xmlFile = install + "/features/" + id + "/" + "feature.xml";
}
if (type.equals("plugin")) {
xmlFile = install + "/plugins/" + id + "/" + "plugin.xml";
}
if (type.equals("fragment")) {
xmlFile = install + "/plugins/" + "/" + id + "/" + "fragment.xml";
}
if (new File(xmlFile).exists()) {
parse(xmlFile);
} else {
throw new BuildException("The following " + type + " " + id + " did not get fetched.");
}
}
// Start Element Event Handler
@Override
public void startElement(final String uri, final String local, final String qName, final Attributes atts) {
if (local.equals("plugin") || local.equals("fragment")) {
add(atts.getValue("id"), plugins);
}
if (local.equals("feature")) {
add(atts.getValue("id") + "-feature", features);
}
}
}