diff options
author | Pascal Rapicault | 2011-02-01 18:36:24 +0000 |
---|---|---|
committer | Pascal Rapicault | 2011-02-01 18:36:24 +0000 |
commit | 68443278be26dce0ca7071c19590381f06928be7 (patch) | |
tree | 0335781f3bce14b54b2751bbdea27525c45ea8ed /bundles/org.eclipse.equinox.p2.metadata.repository | |
parent | 035b561f23d015e4bbde67fae5633c72a140f9f6 (diff) | |
download | rt.equinox.p2-68443278be26dce0ca7071c19590381f06928be7.tar.gz rt.equinox.p2-68443278be26dce0ca7071c19590381f06928be7.tar.xz rt.equinox.p2-68443278be26dce0ca7071c19590381f06928be7.zip |
Bug 335865 - [metadata] Provide serialization API
Diffstat (limited to 'bundles/org.eclipse.equinox.p2.metadata.repository')
4 files changed, 189 insertions, 6 deletions
diff --git a/bundles/org.eclipse.equinox.p2.metadata.repository/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.p2.metadata.repository/META-INF/MANIFEST.MF index 92ea0e436..ee10c5c9e 100644 --- a/bundles/org.eclipse.equinox.p2.metadata.repository/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.equinox.p2.metadata.repository/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.equinox.p2.metadata.repository;singleton:=true -Bundle-Version: 1.1.100.qualifier +Bundle-Version: 1.2.0.qualifier Bundle-Activator: org.eclipse.equinox.internal.p2.metadata.repository.Activator Bundle-Vendor: %providerName Bundle-Localization: plugin @@ -15,7 +15,8 @@ Export-Package: org.eclipse.equinox.internal.p2.metadata.repository; org.eclipse.equinox.p2.updatesite, org.eclipse.equinox.p2.extensionlocation, org.eclipse.equinox.p2.repository.tools", - org.eclipse.equinox.internal.p2.metadata.repository.io;x-friends:="org.eclipse.equinox.p2.engine" + org.eclipse.equinox.internal.p2.metadata.repository.io;x-friends:="org.eclipse.equinox.p2.engine", + org.eclipse.equinox.p2.metadata.io Require-Bundle: org.eclipse.equinox.common;bundle-version="[3.5.0,4.0.0)", org.eclipse.equinox.registry, org.eclipse.ecf.filetransfer;bundle-version="2.0.0" diff --git a/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/internal/p2/metadata/repository/io/MetadataWriter.java b/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/internal/p2/metadata/repository/io/MetadataWriter.java index 98ff47f4c..995cb5f73 100644 --- a/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/internal/p2/metadata/repository/io/MetadataWriter.java +++ b/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/internal/p2/metadata/repository/io/MetadataWriter.java @@ -23,7 +23,7 @@ import org.eclipse.equinox.internal.p2.persistence.XMLWriter; import org.eclipse.equinox.p2.metadata.*; import org.eclipse.equinox.p2.metadata.expression.*; -public abstract class MetadataWriter extends XMLWriter implements XMLConstants { +public class MetadataWriter extends XMLWriter implements XMLConstants { public MetadataWriter(OutputStream output, ProcessingInstruction[] piElements) throws UnsupportedEncodingException { super(output, piElements); @@ -35,8 +35,8 @@ public abstract class MetadataWriter extends XMLWriter implements XMLConstants { * @param units An Iterator of {@link IInstallableUnit}. * @param size The number of units to write */ - protected void writeInstallableUnits(Iterator<IInstallableUnit> units, int size) { - if (size == 0) + public void writeInstallableUnits(Iterator<IInstallableUnit> units, int size) { + if (!units.hasNext()) return; start(INSTALLABLE_UNITS_ELEMENT); @@ -104,7 +104,7 @@ public abstract class MetadataWriter extends XMLWriter implements XMLConstants { return false; } } - + for (IRequirement r : iu.getMetaRequirements()) if (r.getMax() == 0 || !RequiredCapability.isSimpleRequirement(r.getMatches())) return false; diff --git a/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/p2/metadata/io/IUDeserializer.java b/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/p2/metadata/io/IUDeserializer.java new file mode 100644 index 000000000..ff7266cee --- /dev/null +++ b/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/p2/metadata/io/IUDeserializer.java @@ -0,0 +1,138 @@ +/******************************************************************************* + * Copyright (c) 2011 Sonatype, Inc. 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: + * Sonatype, Inc - initial API and implementation + *******************************************************************************/ +package org.eclipse.equinox.p2.metadata.io; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.Collection; +import javax.xml.parsers.*; +import org.eclipse.equinox.internal.p2.metadata.repository.io.MetadataParser; +import org.eclipse.equinox.internal.p2.persistence.Messages; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; +import org.xml.sax.*; + +/** + * This class allows to deserialize {@link IInstallableUnit}s that have been serialized with {@link IUSerializer}. + * The deserializer is able to read data that have been serialized with previous versions of the serializer. + * @since 1.2 + * + */ +public class IUDeserializer { + private IUDeserializerParser deserializer; + + /** + * Construct a new instance of the deserializer. + */ + public IUDeserializer() { + deserializer = new IUDeserializerParser(SAXParserFactory.newInstance()); + } + + /** + * Deserialize a set of {@link IInstallableUnit} from the input stream. + * @param input the input stream to deserialize {@link IInstallableUnit}s from. + * @return the collection of {@link IInstallableUnit}s read from the input stream. + * @throws IOException + */ + public Collection<IInstallableUnit> read(InputStream input) throws IOException { + return deserializer.parse(input); + } + + private class IUDeserializerParser extends MetadataParser { + private IUOnlyHandler iusHandler; + private SAXParserFactory parserFactory; + + public IUDeserializerParser(SAXParserFactory factory) { + super(null, null); + this.parserFactory = factory; + } + + public Collection<IInstallableUnit> parse(InputStream stream) throws IOException { + try { + getParser(); + iusHandler = new IUOnlyHandler(); + xmlReader.setContentHandler(iusHandler); + xmlReader.parse(new InputSource(stream)); + if (isValidXML()) { + return Arrays.asList(iusHandler.getInstallableUnits()); + } + throw new IOException(status.toString()); + } catch (ParserConfigurationException configException) { + throw new IOException(configException); + } catch (SAXException saxException) { + throw new IOException(saxException); + } + } + + protected SAXParser getParser() throws ParserConfigurationException, SAXException { + super.getParser(); + if (parserFactory == null) { + throw new SAXException(Messages.XMLParser_No_SAX_Parser); + } + parserFactory.setNamespaceAware(true); + parserFactory.setValidating(false); + try { + parserFactory.setFeature("http://xml.org/sax/features/string-interning", true); //$NON-NLS-1$ + } catch (SAXException se) { + // some parsers may not support string interning + } + SAXParser theParser = parserFactory.newSAXParser(); + if (theParser == null) { + throw new SAXException(Messages.XMLParser_No_SAX_Parser); + } + xmlReader = theParser.getXMLReader(); + return theParser; + } + + class IUOnlyHandler extends RootHandler { + + private InstallableUnitsHandler unitsHandler; + + public IUOnlyHandler() { + // default + } + + protected void handleRootAttributes(Attributes attributes) { + //Nothing to do + } + + public void startElement(String name, Attributes attributes) { + if (INSTALLABLE_UNITS_ELEMENT.equals(name)) { + if (unitsHandler == null) { + unitsHandler = new InstallableUnitsHandler(this, attributes); + } else { + duplicateElement(this, name, attributes); + } + } else { + invalidElement(name, attributes); + } + } + + public IInstallableUnit[] getInstallableUnits() { + if (unitsHandler == null) + return null; + return unitsHandler.getUnits(); + } + } + + @Override + protected Object getRootObject() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected String getErrorMessage() { + // TODO Auto-generated method stub + return null; + } + } +} diff --git a/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/p2/metadata/io/IUSerializer.java b/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/p2/metadata/io/IUSerializer.java new file mode 100644 index 000000000..4539326be --- /dev/null +++ b/bundles/org.eclipse.equinox.p2.metadata.repository/src/org/eclipse/equinox/p2/metadata/io/IUSerializer.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2011 Sonatype, Inc. 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: + * Sonatype, Inc - initial API and implementation + *******************************************************************************/ +package org.eclipse.equinox.p2.metadata.io; + +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.util.Collection; +import org.eclipse.equinox.internal.p2.metadata.repository.io.MetadataWriter; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; + +/** + * This class allows to serialize a collection of {@link IInstallableUnit}s. + * These serialized IUs can be read using the {@link IUDeserializer}. + * @since 1.2 + */ +public class IUSerializer { + MetadataWriter writer; + + /** + * Construct a serializer. + * @param os the output stream against which the serializer will work. + * @throws UnsupportedEncodingException + */ + public IUSerializer(OutputStream os) throws UnsupportedEncodingException { + writer = new MetadataWriter(os, null); + } + + /** + * Serialize the given collections of IU. + * @param ius the collection of {@link IInstallableUnit}s to serialize + */ + public void write(Collection<IInstallableUnit> ius) { + writer.writeInstallableUnits(ius.iterator(), ius.size()); + writer.flush(); + } +} |