| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2017 IBM Corporation and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* IBM Corporation - initial API and implementation |
| 13 |
*******************************************************************************/ |
| 14 |
package org.eclipse.equinox.internal.p2.updatesite; |
| 15 |
|
| 16 |
import java.io.*; |
| 17 |
import java.net.URI; |
| 18 |
import java.util.ArrayList; |
| 19 |
import java.util.List; |
| 20 |
import java.util.jar.JarEntry; |
| 21 |
import java.util.jar.JarFile; |
| 22 |
import javax.xml.parsers.*; |
| 23 |
import org.eclipse.core.runtime.IStatus; |
| 24 |
import org.eclipse.core.runtime.Status; |
| 25 |
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; |
| 26 |
import org.eclipse.equinox.internal.p2.core.helpers.SecureXMLUtil; |
| 27 |
import org.eclipse.equinox.internal.p2.publisher.eclipse.FeatureManifestParser; |
| 28 |
import org.eclipse.equinox.p2.publisher.eclipse.Feature; |
| 29 |
import org.eclipse.osgi.util.NLS; |
| 30 |
import org.xml.sax.*; |
| 31 |
import org.xml.sax.helpers.DefaultHandler; |
| 32 |
|
| 33 |
/** |
| 34 |
* Default feature parser. |
| 35 |
* Parses the feature manifest file as defined by the platform. |
| 36 |
* |
| 37 |
* @since 3.0 |
| 38 |
*/ |
| 39 |
public class DigestParser extends DefaultHandler { |
| 40 |
|
| 41 |
private SAXParser parser; |
| 42 |
private final List<Feature> features = new ArrayList<>(); |
| 43 |
private final FeatureManifestParser featureHandler = new FeatureManifestParser(false); |
| 44 |
|
| 45 |
public DigestParser() { |
| 46 |
super(); |
| 47 |
try { |
| 48 |
SAXParserFactory parserFactory = SecureXMLUtil.newSecureSAXParserFactory(); |
| 49 |
parserFactory.setNamespaceAware(true); |
| 50 |
this.parser = parserFactory.newSAXParser(); |
| 51 |
} catch (ParserConfigurationException e) { |
| 52 |
System.out.println(e); |
| 53 |
} catch (SAXException e) { |
| 54 |
System.out.println(e); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public void characters(char[] ch, int start, int length) throws SAXException { |
| 60 |
featureHandler.characters(ch, start, length); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void endElement(String uri, String localName, String qName) throws SAXException { |
| 65 |
if ("digest".equals(localName)) { //$NON-NLS-1$ |
| 66 |
return; |
| 67 |
} |
| 68 |
if ("feature".equals(localName)) { //$NON-NLS-1$ |
| 69 |
Feature feature = featureHandler.getResult(); |
| 70 |
features.add(feature); |
| 71 |
} else |
| 72 |
featureHandler.endElement(uri, localName, qName); |
| 73 |
} |
| 74 |
|
| 75 |
public Feature[] parse(File localFile, URI location) { |
| 76 |
if (!localFile.exists()) |
| 77 |
return null; |
| 78 |
|
| 79 |
if (location == null) |
| 80 |
location = localFile.toURI(); |
| 81 |
|
| 82 |
JarFile jar = null; |
| 83 |
InputStream is = null; |
| 84 |
try { |
| 85 |
jar = new JarFile(localFile); |
| 86 |
JarEntry entry = jar.getJarEntry("digest.xml"); //$NON-NLS-1$ |
| 87 |
if (entry == null) |
| 88 |
return null; |
| 89 |
is = new BufferedInputStream(jar.getInputStream(entry)); |
| 90 |
parser.parse(new InputSource(is), this); |
| 91 |
return features.toArray(new Feature[features.size()]); |
| 92 |
} catch (IOException e) { |
| 93 |
LogHelper.log(new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.ErrorReadingDigest, location), e)); |
| 94 |
} catch (SAXException e) { |
| 95 |
LogHelper.log(new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.ErrorReadingDigest, location), e)); |
| 96 |
} finally { |
| 97 |
try { |
| 98 |
if (is != null) |
| 99 |
is.close(); |
| 100 |
} catch (IOException e1) { |
| 101 |
// |
| 102 |
} |
| 103 |
try { |
| 104 |
if (jar != null) |
| 105 |
jar.close(); |
| 106 |
} catch (IOException e) { |
| 107 |
// |
| 108 |
} |
| 109 |
} |
| 110 |
return null; |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
| 115 |
if ("digest".equals(localName)) { //$NON-NLS-1$ |
| 116 |
return; |
| 117 |
} |
| 118 |
featureHandler.startElement(uri, localName, qName, attributes); |
| 119 |
} |
| 120 |
|
| 121 |
} |