| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007, 2020 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.engine; |
| 15 |
|
| 16 |
import java.io.OutputStream; |
| 17 |
import java.util.*; |
| 18 |
import org.eclipse.equinox.internal.p2.metadata.repository.io.MetadataWriter; |
| 19 |
import org.eclipse.equinox.p2.engine.IProfile; |
| 20 |
import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
| 21 |
import org.eclipse.equinox.p2.query.QueryUtil; |
| 22 |
|
| 23 |
public class ProfileWriter extends MetadataWriter implements ProfileXMLConstants { |
| 24 |
|
| 25 |
public ProfileWriter(OutputStream output, ProcessingInstruction[] processingInstructions) { |
| 26 |
super(output, processingInstructions); |
| 27 |
} |
| 28 |
|
| 29 |
public void writeProfile(IProfile profile) { |
| 30 |
start(PROFILE_ELEMENT); |
| 31 |
attribute(ID_ATTRIBUTE, profile.getProfileId()); |
| 32 |
attribute(TIMESTAMP_ATTRIBUTE, Long.toString(profile.getTimestamp())); |
| 33 |
writeProperties(profile.getProperties()); |
| 34 |
ArrayList<IInstallableUnit> ius = new ArrayList<>(profile.query(QueryUtil.createIUAnyQuery(), null).toUnmodifiableSet()); |
| 35 |
ius.sort((iu1, iu2) -> { |
| 36 |
int IdCompare = iu1.getId().compareTo(iu2.getId()); |
| 37 |
if (IdCompare != 0) |
| 38 |
return IdCompare; |
| 39 |
|
| 40 |
return iu1.getVersion().compareTo(iu2.getVersion()); |
| 41 |
}); |
| 42 |
writeInstallableUnits(ius.iterator(), ius.size()); |
| 43 |
writeInstallableUnitsProperties(ius.iterator(), ius.size(), profile); |
| 44 |
end(PROFILE_ELEMENT); |
| 45 |
flush(); |
| 46 |
} |
| 47 |
|
| 48 |
private void writeInstallableUnitsProperties(Iterator<IInstallableUnit> it, int size, IProfile profile) { |
| 49 |
if (size == 0) |
| 50 |
return; |
| 51 |
start(IUS_PROPERTIES_ELEMENT); |
| 52 |
attribute(COLLECTION_SIZE_ATTRIBUTE, size); |
| 53 |
while (it.hasNext()) { |
| 54 |
IInstallableUnit iu = it.next(); |
| 55 |
Map<String, String> properties = profile.getInstallableUnitProperties(iu); |
| 56 |
if (properties.isEmpty()) |
| 57 |
continue; |
| 58 |
|
| 59 |
start(IU_PROPERTIES_ELEMENT); |
| 60 |
attribute(ID_ATTRIBUTE, iu.getId()); |
| 61 |
attribute(VERSION_ATTRIBUTE, iu.getVersion().toString()); |
| 62 |
writeProperties(properties); |
| 63 |
end(IU_PROPERTIES_ELEMENT); |
| 64 |
} |
| 65 |
end(IUS_PROPERTIES_ELEMENT); |
| 66 |
} |
| 67 |
} |