Skip to main content
summaryrefslogtreecommitdiffstats
blob: 068e1a07d5e4390f86b032e26bfde8cbf06474d1 (plain) (blame)
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
/*******************************************************************************
 *  Copyright (c) 2000, 2017 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
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.updatesite;

import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javax.xml.parsers.*;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
import org.eclipse.equinox.internal.p2.core.helpers.SecureXMLUtil;
import org.eclipse.equinox.internal.p2.publisher.eclipse.FeatureManifestParser;
import org.eclipse.equinox.p2.publisher.eclipse.Feature;
import org.eclipse.osgi.util.NLS;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Default feature parser.
 * Parses the feature manifest file as defined by the platform.
 * 
 * @since 3.0
 */
public class DigestParser extends DefaultHandler {

	private final static SAXParserFactory parserFactory = SecureXMLUtil.newSecureSAXParserFactory();
	private SAXParser parser;
	private final List<Feature> features = new ArrayList<>();
	private final FeatureManifestParser featureHandler = new FeatureManifestParser(false);

	public DigestParser() {
		super();
		try {
			parserFactory.setNamespaceAware(true);
			this.parser = parserFactory.newSAXParser();
		} catch (ParserConfigurationException e) {
			System.out.println(e);
		} catch (SAXException e) {
			System.out.println(e);
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		featureHandler.characters(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if ("digest".equals(localName)) { //$NON-NLS-1$
			return;
		}
		if ("feature".equals(localName)) { //$NON-NLS-1$
			Feature feature = featureHandler.getResult();
			features.add(feature);
		} else
			featureHandler.endElement(uri, localName, qName);
	}

	public Feature[] parse(File localFile, URI location) {
		if (!localFile.exists())
			return null;

		if (location == null)
			location = localFile.toURI();

		JarFile jar = null;
		InputStream is = null;
		try {
			jar = new JarFile(localFile);
			JarEntry entry = jar.getJarEntry("digest.xml"); //$NON-NLS-1$
			if (entry == null)
				return null;
			is = new BufferedInputStream(jar.getInputStream(entry));
			parser.parse(new InputSource(is), this);
			return features.toArray(new Feature[features.size()]);
		} catch (IOException e) {
			LogHelper.log(new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.ErrorReadingDigest, location), e));
		} catch (SAXException e) {
			LogHelper.log(new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.ErrorReadingDigest, location), e));
		} finally {
			try {
				if (is != null)
					is.close();
			} catch (IOException e1) {
				//
			}
			try {
				if (jar != null)
					jar.close();
			} catch (IOException e) {
				//
			}
		}
		return null;
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if ("digest".equals(localName)) { //$NON-NLS-1$
			return;
		}
		featureHandler.startElement(uri, localName, qName, attributes);
	}

}

Back to the top