| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007, 2022 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 |
* Christoph Läubrich - Issue #20 - XMLParser should not require a bundle context but a Parser in the constructor |
| 14 |
*******************************************************************************/ |
| 15 |
package org.eclipse.equinox.internal.p2.engine; |
| 16 |
|
| 17 |
import java.util.LinkedHashMap; |
| 18 |
import java.util.Map; |
| 19 |
import javax.xml.parsers.SAXParserFactory; |
| 20 |
import org.eclipse.equinox.internal.p2.metadata.repository.io.MetadataParser; |
| 21 |
import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
| 22 |
import org.eclipse.equinox.p2.metadata.Version; |
| 23 |
import org.xml.sax.Attributes; |
| 24 |
|
| 25 |
/** |
| 26 |
* An abstract XML parser class for parsing profiles as written by the ProfileWriter. |
| 27 |
*/ |
| 28 |
public abstract class ProfileParser extends MetadataParser implements ProfileXMLConstants { |
| 29 |
|
| 30 |
public ProfileParser(String bundleId) { |
| 31 |
super(bundleId); |
| 32 |
} |
| 33 |
|
| 34 |
public ProfileParser(SAXParserFactory factory, String bundleId) { |
| 35 |
super(factory, bundleId); |
| 36 |
} |
| 37 |
|
| 38 |
protected class ProfileHandler extends RootHandler { |
| 39 |
|
| 40 |
private final String[] required = new String[] {ID_ATTRIBUTE}; |
| 41 |
|
| 42 |
private String profileId; |
| 43 |
private String parentId; |
| 44 |
private String timestamp; |
| 45 |
private PropertiesHandler propertiesHandler; |
| 46 |
private InstallableUnitsHandler unitsHandler; |
| 47 |
private IUsPropertiesHandler iusPropertiesHandler; |
| 48 |
|
| 49 |
public ProfileHandler() { |
| 50 |
// default |
| 51 |
} |
| 52 |
|
| 53 |
protected ProfileHandler(String profileId) { |
| 54 |
this.profileId = profileId; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
protected void handleRootAttributes(Attributes attributes) { |
| 59 |
profileId = parseRequiredAttributes(attributes, required)[0]; |
| 60 |
parentId = parseOptionalAttribute(attributes, PARENT_ID_ATTRIBUTE); |
| 61 |
timestamp = parseOptionalAttribute(attributes, TIMESTAMP_ATTRIBUTE); |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void startElement(String name, Attributes attributes) { |
| 67 |
if (PROPERTIES_ELEMENT.equals(name)) { |
| 68 |
if (propertiesHandler == null) { |
| 69 |
propertiesHandler = new PropertiesHandler(this, attributes); |
| 70 |
} else { |
| 71 |
duplicateElement(this, name, attributes); |
| 72 |
} |
| 73 |
} else if (INSTALLABLE_UNITS_ELEMENT.equals(name)) { |
| 74 |
if (unitsHandler == null) { |
| 75 |
unitsHandler = new InstallableUnitsHandler(this, attributes); |
| 76 |
} else { |
| 77 |
duplicateElement(this, name, attributes); |
| 78 |
} |
| 79 |
} else if (IUS_PROPERTIES_ELEMENT.equals(name)) { |
| 80 |
if (iusPropertiesHandler == null) { |
| 81 |
iusPropertiesHandler = new IUsPropertiesHandler(this, attributes); |
| 82 |
} else { |
| 83 |
duplicateElement(this, name, attributes); |
| 84 |
} |
| 85 |
} else { |
| 86 |
invalidElement(name, attributes); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
public String getProfileId() { |
| 91 |
return profileId; |
| 92 |
} |
| 93 |
|
| 94 |
public String getParentId() { |
| 95 |
return parentId; |
| 96 |
} |
| 97 |
|
| 98 |
public long getTimestamp() { |
| 99 |
if (timestamp != null) { |
| 100 |
try { |
| 101 |
return Long.parseLong(timestamp); |
| 102 |
} catch (NumberFormatException e) { |
| 103 |
// TODO: log |
| 104 |
} |
| 105 |
} |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
public Map<String, String> getProperties() { |
| 110 |
if (propertiesHandler == null) |
| 111 |
return null; |
| 112 |
return propertiesHandler.getProperties(); |
| 113 |
} |
| 114 |
|
| 115 |
public IInstallableUnit[] getInstallableUnits() { |
| 116 |
if (unitsHandler == null) |
| 117 |
return null; |
| 118 |
return unitsHandler.getUnits(); |
| 119 |
} |
| 120 |
|
| 121 |
public Map<String, String> getIUProperties(IInstallableUnit iu) { |
| 122 |
if (iusPropertiesHandler == null) |
| 123 |
return null; |
| 124 |
|
| 125 |
Map<String, Map<String, String>> iusPropertiesMap = iusPropertiesHandler.getIUsPropertiesMap(); |
| 126 |
if (iusPropertiesMap == null) |
| 127 |
return null; |
| 128 |
|
| 129 |
String iuIdentity = iu.getId() + "_" + iu.getVersion().toString(); //$NON-NLS-1$ |
| 130 |
return iusPropertiesMap.get(iuIdentity); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
protected class IUPropertiesHandler extends AbstractHandler { |
| 135 |
|
| 136 |
private final String[] required = new String[] {ID_ATTRIBUTE, VERSION_ATTRIBUTE}; |
| 137 |
|
| 138 |
private String iuIdentity; |
| 139 |
private Map<String, Map<String, String>> iusPropertiesMap; |
| 140 |
private PropertiesHandler propertiesHandler; |
| 141 |
|
| 142 |
public IUPropertiesHandler(AbstractHandler parentHandler, Attributes attributes, Map<String, Map<String, String>> iusPropertiesMap) { |
| 143 |
super(parentHandler, IU_PROPERTIES_ELEMENT); |
| 144 |
this.iusPropertiesMap = iusPropertiesMap; |
| 145 |
|
| 146 |
String values[] = parseRequiredAttributes(attributes, required); |
| 147 |
String id = values[0]; |
| 148 |
Version version = checkVersion(IU_PROPERTIES_ELEMENT, VERSION_ATTRIBUTE, values[1]); |
| 149 |
iuIdentity = id + "_" + version.toString(); //$NON-NLS-1$ |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
protected void finished() { |
| 154 |
if (isValidXML() && iuIdentity != null && propertiesHandler != null) { |
| 155 |
iusPropertiesMap.put(iuIdentity, propertiesHandler.getProperties()); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
public void startElement(String name, Attributes attributes) { |
| 161 |
if (name.equals(PROPERTIES_ELEMENT)) { |
| 162 |
propertiesHandler = new PropertiesHandler(this, attributes); |
| 163 |
} else { |
| 164 |
invalidElement(name, attributes); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
protected class IUsPropertiesHandler extends AbstractHandler { |
| 170 |
|
| 171 |
private Map<String, Map<String, String>> iusPropertiesMap; |
| 172 |
|
| 173 |
public IUsPropertiesHandler(AbstractHandler parentHandler, Attributes attributes) { |
| 174 |
super(parentHandler, IUS_PROPERTIES_ELEMENT); |
| 175 |
String sizeStr = parseOptionalAttribute(attributes, COLLECTION_SIZE_ATTRIBUTE); |
| 176 |
int size = (sizeStr != null ? Integer.parseInt(sizeStr) : 4); |
| 177 |
iusPropertiesMap = new LinkedHashMap<>(size); |
| 178 |
} |
| 179 |
|
| 180 |
public Map<String, Map<String, String>> getIUsPropertiesMap() { |
| 181 |
return iusPropertiesMap; |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public void startElement(String name, Attributes attributes) { |
| 186 |
if (name.equals(IU_PROPERTIES_ELEMENT)) { |
| 187 |
new IUPropertiesHandler(this, attributes, iusPropertiesMap); |
| 188 |
} else { |
| 189 |
invalidElement(name, attributes); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |