Debian Codegraph @

bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AdviceFileParser.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2009, 2020 IBM Corporation and others.
3 *
4 * This
5 * program and the accompanying materials are made available under the terms of
6 * the Eclipse Public License 2.0 which accompanies this distribution, and is
7 * available at
8 * https://www.eclipse.org/legal/epl-2.0/
9 *
10 * SPDX-License-Identifier: EPL-2.0
11 *
12 * Contributors: IBM Corporation - initial API and implementation
13 ******************************************************************************/
14 package org.eclipse.equinox.p2.publisher;
15
16 import java.net.URI;
17 import java.net.URISyntaxException;
18 import java.util.*;
19 import java.util.Map.Entry;
20 import org.eclipse.equinox.internal.p2.metadata.ArtifactKey;
21 import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
22 import org.eclipse.equinox.p2.metadata.*;
23 import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;
24 import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitFragmentDescription;
25 import org.eclipse.equinox.p2.metadata.expression.*;
26 import org.eclipse.equinox.spi.p2.publisher.PublisherHelper;
27
28 public class AdviceFileParser {
29
30 private static final String ADVICE_VERSION = "advice.version"; //$NON-NLS-1$
31
32 private static final String QUALIFIER_SUBSTITUTION = "$qualifier$"; //$NON-NLS-1$
33 private static final String VERSION_SUBSTITUTION = "$version$"; //$NON-NLS-1$
34
35 private static final String UPDATE_DESCRIPTION = "update.description"; //$NON-NLS-1$
36 private static final String UPDATE_SEVERITY = "update.severity"; //$NON-NLS-1$
37 private static final String UPDATE_RANGE = "update.range"; //$NON-NLS-1$
38 private static final String UPDATE_ID = "update.id"; //$NON-NLS-1$
39 private static final String UPDATE_MATCH_EXP = "update.matchExp"; //$NON-NLS-1$
40 private static final String CLASSIFIER = "classifier"; //$NON-NLS-1$
41 private static final String TOUCHPOINT_VERSION = "touchpoint.version"; //$NON-NLS-1$
42 private static final String TOUCHPOINT_ID = "touchpoint.id"; //$NON-NLS-1$
43 private static final String COPYRIGHT_LOCATION = "copyright.location"; //$NON-NLS-1$
44 private static final String COPYRIGHT = "copyright"; //$NON-NLS-1$
45 private static final String ID = "id"; //$NON-NLS-1$
46 private static final String SINGLETON = "singleton"; //$NON-NLS-1$
47 private static final String IMPORT = "import"; //$NON-NLS-1$
48 private static final String RANGE = "range"; //$NON-NLS-1$
49 private static final String MIN = "min"; //$NON-NLS-1$
50 private static final String MAX = "max"; //$NON-NLS-1$
51 private static final String FILTER = "filter"; //$NON-NLS-1$
52 private static final String MULTIPLE = "multiple"; //$NON-NLS-1$
53 private static final String OPTIONAL = "optional"; //$NON-NLS-1$
54 private static final String GREEDY = "greedy"; //$NON-NLS-1$
55 private static final String VERSION = "version"; //$NON-NLS-1$
56 private static final String NAMESPACE = "namespace"; //$NON-NLS-1$
57 private static final String NAME = "name"; //$NON-NLS-1$
58 private static final String MATCH_EXP = "matchExp"; //$NON-NLS-1$
59 private static final String LOCATION = "location"; //$NON-NLS-1$
60 private static final String VALUE = "value"; //$NON-NLS-1$
61
62 private static final String UNITS_PREFIX = "units."; //$NON-NLS-1$
63 private static final String INSTRUCTIONS_PREFIX = "instructions."; //$NON-NLS-1$
64 private static final String REQUIRES_PREFIX = "requires."; //$NON-NLS-1$
65 private static final String META_REQUIREMENTS_PREFIX = "metaRequirements."; //$NON-NLS-1$
66 private static final String PROVIDES_PREFIX = "provides."; //$NON-NLS-1$
67 private static final String PROPERTIES_PREFIX = "properties."; //$NON-NLS-1$
68 private static final String LICENSES_PREFIX = "licenses."; //$NON-NLS-1$
69 private static final String ARTIFACTS_PREFIX = "artifacts."; //$NON-NLS-1$
70 private static final String HOST_REQUIREMENTS_PREFIX = "hostRequirements."; //$NON-NLS-1$
71 private static final String UPDATE_DESCRIPTOR_PREFIX = "update."; //$NON-NLS-1$
72
73 public static final Version COMPATIBLE_VERSION = Version.createOSGi(1, 0, 0);
74 public static final VersionRange VERSION_TOLERANCE = new VersionRange(COMPATIBLE_VERSION, true,
75 Version.createOSGi(2, 0, 0), false);
76
77 private Map<String, String> adviceProperties = new HashMap<>();
78 private List<IProvidedCapability> adviceProvides = new ArrayList<>();
79 private List<IRequirement> adviceRequires = new ArrayList<>();
80 private List<IRequirement> adviceMetaRequires = new ArrayList<>();
81 private IUpdateDescriptor adviceUpdateDescriptor = null;
82 private Map<String, ITouchpointInstruction> adviceInstructions = new HashMap<>();
83 private List<InstallableUnitDescription> adviceOtherIUs = new ArrayList<>();
84
85 private final Map<String, String> advice;
86 private Iterator<String> keysIterator;
87 private String current;
88 private String hostId;
89 private Version hostVersion;
90
91 public AdviceFileParser(String id, Version version, Map<String, String> advice) {
92 this.hostId = id;
93 this.hostVersion = version;
94 this.advice = advice;
95 }
96
97 public void parse() {
98 String adviceVersion = advice.get(ADVICE_VERSION);
99 if (adviceVersion != null)
100 checkAdviceVersion(adviceVersion);
101
102 List<String> keys = new ArrayList<>(advice.keySet());
103 keys.sort(null);
104
105 keysIterator = keys.iterator();
106 next();
107
108 while (current != null) {
109 if (current.startsWith(PROPERTIES_PREFIX))
110 parseProperties(PROPERTIES_PREFIX, adviceProperties);
111 else if (current.startsWith(UPDATE_DESCRIPTOR_PREFIX))
112 this.adviceUpdateDescriptor = parseUpdateDescriptor(UPDATE_DESCRIPTOR_PREFIX, hostId);
113 else if (current.startsWith(PROVIDES_PREFIX))
114 parseProvides(PROVIDES_PREFIX, adviceProvides);
115 else if (current.startsWith(REQUIRES_PREFIX))
116 parseRequires(REQUIRES_PREFIX, adviceRequires);
117 else if (current.startsWith(META_REQUIREMENTS_PREFIX))
118 parseRequires(META_REQUIREMENTS_PREFIX, adviceMetaRequires);
119 else if (current.startsWith(INSTRUCTIONS_PREFIX))
120 parseInstructions(INSTRUCTIONS_PREFIX, adviceInstructions);
121 else if (current.startsWith(UNITS_PREFIX))
122 parseUnits(UNITS_PREFIX, adviceOtherIUs);
123 else if (current.equals(ADVICE_VERSION)) {
124 next();
125 } else {
126 // we ignore elements we do not understand
127 next();
128 }
129 }
130 }
131
132 private void checkAdviceVersion(String adviceVersion) {
133 Version version = Version.parseVersion(adviceVersion);
134 if (!VERSION_TOLERANCE.isIncluded(version))
135 throw new IllegalStateException("bad version: " + version + ". Expected range was " + VERSION_TOLERANCE); //$NON-NLS-1$ //$NON-NLS-2$
136 }
137
138 private void next() {
139 current = keysIterator.hasNext() ? keysIterator.next() : null;
140 }
141
142 private String currentValue() {
143 return advice.get(current).trim();
144 }
145
146 private void parseProperties(String prefix, Map<String, String> properties) {
147 while (current != null && current.startsWith(prefix)) {
148 int dotIndex = current.indexOf('.', prefix.length());
149 if (dotIndex == -1)
150 throw new IllegalStateException("bad token: " + current); //$NON-NLS-1$
151
152 parseProperty(current.substring(0, dotIndex + 1), properties);
153 }
154 }
155
156 private void parseProperty(String prefix, Map<String, String> properties) {
157 String propertyName = null;
158 String propertyValue = null;
159 while (current != null && current.startsWith(prefix)) {
160 String token = current.substring(prefix.length());
161 switch (token) {
162 case NAME:
163 propertyName = currentValue();
164 break;
165 case VALUE:
166 propertyValue = currentValue();
167 break;
168 // we ignore elements we do not understand
169 default:
170 break;
171 }
172 next();
173 }
174
175 properties.put(propertyName, propertyValue);
176 }
177
178 private IUpdateDescriptor parseUpdateDescriptor(String prefix, String id) {
179 String name = id;
180 String description = null;
181 String range = "[0.0.0,$version$)"; //$NON-NLS-1$
182 String severity = "0"; //$NON-NLS-1$
183 String match = null;
184
185 while (current != null && current.startsWith(prefix)) {
186 String token = current;
187 switch (token) {
188 case UPDATE_MATCH_EXP:
189 match = currentValue();
190 break;
191 case UPDATE_ID:
192 name = currentValue();
193 break;
194 case UPDATE_DESCRIPTION:
195 description = currentValue();
196 break;
197 case UPDATE_RANGE:
198 range = currentValue();
199 break;
200 case UPDATE_SEVERITY:
201 severity = currentValue();
202 break;
203 // ignore
204 default:
205 break;
206 }
207 next();
208 }
209
210 if (match != null) {
211 // When update.match is specified, versionRange and id are ignored
212 IExpression expr = ExpressionUtil.parse(substituteVersionAndQualifier(match));
213 IMatchExpression<IInstallableUnit> matchExpression = ExpressionUtil.getFactory().matchExpression(expr);
214 Collection<IMatchExpression<IInstallableUnit>> descriptors = new ArrayList<>(1);
215 descriptors.add(matchExpression);
216 return MetadataFactory.createUpdateDescriptor(descriptors, Integer.valueOf(severity), description,
217 (URI) null);
218 }
219 range = substituteVersionAndQualifier(range);
220 VersionRange versionRange = VersionRange.create(range);
221 return MetadataFactory.createUpdateDescriptor(name, versionRange, Integer.valueOf(severity), description);
222 }
223
224 private void parseProvides(String prefix, List<IProvidedCapability> provides) {
225 while (current != null && current.startsWith(prefix)) {
226 int dotIndex = current.indexOf('.', prefix.length());
227 if (dotIndex == -1)
228 throw new IllegalStateException("bad token: " + current); //$NON-NLS-1$
229
230 parseProvided(current.substring(0, dotIndex + 1), provides);
231 }
232 }
233
234 private void parseProvided(String prefix, List<IProvidedCapability> provides) {
235 String namespace = null;
236 String name = null;
237 Version capabilityVersion = null;
238 while (current != null && current.startsWith(prefix)) {
239 String token = current.substring(prefix.length());
240 switch (token) {
241 case NAME:
242 name = currentValue();
243 break;
244 case NAMESPACE:
245 namespace = currentValue();
246 break;
247 case VERSION:
248 capabilityVersion = Version.parseVersion(substituteVersionAndQualifier(currentValue()));
249 break;
250 // we ignore elements we do not understand
251 default:
252 break;
253 }
254 next();
255 }
256
257 IProvidedCapability capability = MetadataFactory.createProvidedCapability(namespace, name, capabilityVersion);
258 provides.add(capability);
259 }
260
261 private void parseRequires(String prefix, List<IRequirement> requires) {
262 while (current != null && current.startsWith(prefix)) {
263 int dotIndex = current.indexOf('.', prefix.length());
264 if (dotIndex == -1)
265 throw new IllegalStateException("bad token: " + current); //$NON-NLS-1$
266
267 parseRequired(current.substring(0, dotIndex + 1), requires);
268 }
269 }
270
271 private void parseRequired(String prefix, List<IRequirement> requires) {
272
273 String namespace = null;
274 String name = null;
275 VersionRange range = null;
276 String matchExp = null;
277 String filter = null;
278 boolean optional = false;
279 boolean multiple = false;
280 boolean greedy = true;
281
282 int min = -1;
283 int max = -1;
284
285 while (current != null && current.startsWith(prefix)) {
286 String token = current.substring(prefix.length());
287 switch (token) {
288 case GREEDY:
289 greedy = Boolean.parseBoolean(currentValue());
290 break;
291 case OPTIONAL:
292 optional = Boolean.parseBoolean(currentValue());
293 break;
294 case MULTIPLE:
295 multiple = Boolean.parseBoolean(currentValue());
296 break;
297 case FILTER:
298 filter = currentValue();
299 break;
300 case NAME:
301 name = currentValue();
302 break;
303 case NAMESPACE:
304 namespace = currentValue();
305 break;
306 case RANGE:
307 range = VersionRange.create(substituteVersionAndQualifier(currentValue()));
308 break;
309 case MIN:
310 min = Integer.valueOf(currentValue()).intValue();
311 break;
312 case MAX:
313 max = Integer.valueOf(currentValue()).intValue();
314 break;
315 case MATCH_EXP:
316 matchExp = currentValue();
317 break;
318 // we ignore elements we do not understand
319 default:
320 break;
321 }
322 next();
323 }
324 IRequirement capability = null;
325 if (matchExp == null) {
326 if (min >= 0 && max >= 0) {
327 capability = createRequirement(namespace, name, range, filter, min, max, greedy);
328 } else {
329 capability = createRequirement(namespace, name, range, filter, optional, multiple, greedy);
330 }
331 } else {
332 // When a match expression is specified, namespace, name and versionRange are
333 // ignored
334 if (optional && min == -1 && max == -1) {
335 min = 0;
336 max = 1;
337 }
338 capability = createRequirement(matchExp, filter, min, max, greedy, null);
339 }
340 if (capability != null) {
341 requires.add(capability);
342 }
343 }
344
345 protected IRequirement createRequirement(String requirement, String filter, int min, int max, boolean greedy,
346 String description) {
347 IExpression expr = ExpressionUtil.parse(substituteVersionAndQualifier(requirement));
348 IMatchExpression<IInstallableUnit> requirementExp = ExpressionUtil.getFactory().matchExpression(expr);
349 IMatchExpression<IInstallableUnit> filterExp = InstallableUnit.parseFilter(filter);
350 return MetadataFactory.createRequirement(requirementExp, filterExp, min, max, greedy, description);
351 }
352
353 protected IRequirement createRequirement(String namespace, String name, VersionRange range, String filter, int min,
354 int max, boolean greedy) {
355 IMatchExpression<IInstallableUnit> filterExpression = InstallableUnit.parseFilter(filter);
356 return MetadataFactory.createRequirement(namespace, name, range, filterExpression, min, max, greedy);
357 }
358
359 protected IRequirement createRequirement(String namespace, String name, VersionRange range, String filter,
360 boolean optional, boolean multiple, boolean greedy) {
361 return MetadataFactory.createRequirement(namespace, name, range, filter, optional, multiple, greedy);
362 }
363
364 private void parseInstructions(String prefix, Map<String, ITouchpointInstruction> instructions) {
365 while (current != null && current.startsWith(prefix)) {
366 int dotIndex = current.indexOf('.', prefix.length());
367 if (dotIndex != -1)
368 throw new IllegalStateException("bad token: " + current); //$NON-NLS-1$
369
370 parseInstruction(current, instructions);
371 }
372 }
373
374 private void parseInstruction(String prefix, Map<String, ITouchpointInstruction> instructions) {
375 String phase = current.substring(current.lastIndexOf('.') + 1);
376 String body = currentValue();
377 next();
378
379 prefix += '.';
380 String importAttribute = null;
381 if (current != null && current.startsWith(prefix)) {
382 if (current.substring(prefix.length()).equals(IMPORT)) {
383 importAttribute = currentValue();
384 } else {
385 // we ignore elements we do not understand
386 }
387 next();
388 }
389 ITouchpointInstruction instruction = MetadataFactory.createTouchpointInstruction(body, importAttribute);
390 instructions.put(phase, instruction);
391 }
392
393 private void parseUnits(String prefix, List<InstallableUnitDescription> ius) {
394 while (current != null && current.startsWith(prefix)) {
395 int dotIndex = current.indexOf('.', prefix.length());
396 if (dotIndex == -1)
397 throw new IllegalStateException("bad token: " + current + " = " + currentValue()); //$NON-NLS-1$ //$NON-NLS-2$
398
399 parseUnit(current.substring(0, dotIndex + 1), ius);
400 }
401 }
402
403 private void parseUnit(String prefix, List<InstallableUnitDescription> units) {
404 String unitId = null;
405 Version unitVersion = null;
406 boolean unitSingleton = false;
407 String unitFilter = null;
408 String unitCopyright = null;
409 String unitCopyrightLocation = null;
410 String unitTouchpointId = null;
411 Version unitTouchpointVersion = null;
412
413 String unitUpdateId = null;
414 VersionRange unitUpdateRange = null;
415 int unitUpdateSeverity = 0;
416 String unitUpdateDescription = null;
417
418 List<IArtifactKey> unitArtifacts = new ArrayList<>();
419 Map<String, String> unitProperties = new HashMap<>();
420 List<IRequirement> unitHostRequirements = new ArrayList<>();
421 List<IProvidedCapability> unitProvides = new ArrayList<>();
422 List<IRequirement> unitRequires = new ArrayList<>();
423 List<IRequirement> unitMetaRequirements = new ArrayList<>();
424 List<ILicense> unitLicenses = new ArrayList<>();
425 Map<String, ITouchpointInstruction> unitInstructions = new HashMap<>();
426 // updatedescriptor ??
427
428 while (current != null && current.startsWith(prefix)) {
429 String token = current.substring(prefix.length());
430 if (token.equals(ID)) {
431 unitId = currentValue();
432 next();
433 } else if (token.equals(VERSION)) {
434 unitVersion = Version.parseVersion(substituteVersionAndQualifier(currentValue()));
435 next();
436 } else if (token.equals(SINGLETON)) {
437 unitSingleton = Boolean.parseBoolean(currentValue());
438 next();
439 } else if (token.equals(FILTER)) {
440 unitFilter = currentValue();
441 next();
442 } else if (token.equals(COPYRIGHT)) {
443 unitCopyright = currentValue();
444 next();
445 } else if (token.equals(COPYRIGHT_LOCATION)) {
446 unitCopyrightLocation = currentValue();
447 next();
448 } else if (token.equals(TOUCHPOINT_ID)) {
449 unitTouchpointId = currentValue();
450 next();
451 } else if (token.equals(TOUCHPOINT_VERSION)) {
452 unitTouchpointVersion = Version.parseVersion(substituteVersionAndQualifier(currentValue()));
453 next();
454 } else if (token.equals(UPDATE_ID)) {
455 unitUpdateId = currentValue();
456 next();
457 } else if (token.equals(UPDATE_RANGE)) {
458 unitUpdateRange = VersionRange.create(substituteVersionAndQualifier(currentValue()));
459 next();
460 } else if (token.equals(UPDATE_SEVERITY)) {
461 unitUpdateSeverity = Integer.parseInt(currentValue());
462 next();
463 } else if (token.equals(UPDATE_DESCRIPTION)) {
464 unitUpdateDescription = currentValue();
465 next();
466 } else if (token.startsWith(HOST_REQUIREMENTS_PREFIX))
467 parseRequires(prefix + HOST_REQUIREMENTS_PREFIX, unitHostRequirements);
468 else if (token.startsWith(ARTIFACTS_PREFIX))
469 parseArtifacts(prefix + ARTIFACTS_PREFIX, unitArtifacts);
470 else if (token.startsWith(LICENSES_PREFIX))
471 parseLicenses(prefix + LICENSES_PREFIX, unitLicenses);
472 else if (token.startsWith(PROPERTIES_PREFIX))
473 parseProperties(prefix + PROPERTIES_PREFIX, unitProperties);
474 else if (token.startsWith(PROVIDES_PREFIX))
475 parseProvides(prefix + PROVIDES_PREFIX, unitProvides);
476 else if (token.startsWith(REQUIRES_PREFIX))
477 parseRequires(prefix + REQUIRES_PREFIX, unitRequires);
478 else if (token.startsWith(META_REQUIREMENTS_PREFIX))
479 parseRequires(prefix + META_REQUIREMENTS_PREFIX, unitMetaRequirements);
480 else if (token.startsWith(INSTRUCTIONS_PREFIX))
481 parseInstructions(prefix + INSTRUCTIONS_PREFIX, unitInstructions);
482 else {
483 // we ignore elements we do not understand
484 next();
485 }
486 }
487
488 InstallableUnitDescription description = unitHostRequirements.isEmpty() ? new InstallableUnitDescription()
489 : new InstallableUnitFragmentDescription();
490 description.setId(unitId);
491 description.setVersion(unitVersion);
492 description.setSingleton(unitSingleton);
493 description.setFilter(unitFilter);
494 if (unitCopyright != null || unitCopyrightLocation != null) {
495 try {
496 URI uri = unitCopyrightLocation != null ? new URI(unitCopyrightLocation) : null;
497 description.setCopyright(MetadataFactory.createCopyright(uri, unitCopyright));
498 } catch (URISyntaxException e) {
499 throw new IllegalStateException("bad copyright URI at token: " + current + ", " + currentValue()); //$NON-NLS-1$ //$NON-NLS-2$
500 }
501 }
502 if (unitTouchpointId != null)
503 description
504 .setTouchpointType(MetadataFactory.createTouchpointType(unitTouchpointId, unitTouchpointVersion));
505
506 if (unitUpdateId != null)
507 description.setUpdateDescriptor(MetadataFactory.createUpdateDescriptor(unitUpdateId, unitUpdateRange,
508 unitUpdateSeverity, unitUpdateDescription));
509
510 if (!unitLicenses.isEmpty())
511 description.setLicenses(unitLicenses.toArray(new ILicense[unitLicenses.size()]));
512
513 if (!unitArtifacts.isEmpty())
514 description.setArtifacts(unitArtifacts.toArray(new IArtifactKey[unitArtifacts.size()]));
515
516 if (!unitHostRequirements.isEmpty())
517 ((InstallableUnitFragmentDescription) description)
518 .setHost(unitHostRequirements.toArray(new IRequirement[unitHostRequirements.size()]));
519
520 if (!unitProperties.isEmpty()) {
521 for (Entry<String, String> entry : unitProperties.entrySet()) {
522 description.setProperty(entry.getKey(), entry.getValue());
523 }
524 }
525
526 if (!unitProvides.isEmpty())
527 description.setCapabilities(unitProvides.toArray(new IProvidedCapability[unitProvides.size()]));
528
529 if (!unitRequires.isEmpty())
530 description.setRequirements(unitRequires.toArray(new IRequirement[unitRequires.size()]));
531
532 if (!unitMetaRequirements.isEmpty())
533 description
534 .setMetaRequirements(unitMetaRequirements.toArray(new IRequirement[unitMetaRequirements.size()]));
535
536 if (!unitInstructions.isEmpty())
537 description.addTouchpointData(MetadataFactory.createTouchpointData(unitInstructions));
538
539 adviceOtherIUs.add(description);
540 }
541
542 private void parseLicenses(String prefix, List<ILicense> licenses) {
543 while (current != null && current.startsWith(prefix)) {
544 int dotIndex = current.indexOf('.', prefix.length());
545 if (dotIndex != -1)
546 throw new IllegalStateException("bad token: " + current + " = " + currentValue()); //$NON-NLS-1$ //$NON-NLS-2$
547
548 parseLicense(current, licenses);
549 }
550 }
551
552 private void parseLicense(String prefix, List<ILicense> licenses) {
553 String body = currentValue();
554 next();
555
556 prefix += '.';
557 String location = null;
558 if (current != null && current.startsWith(prefix)) {
559 if (current.substring(prefix.length()).equals(LOCATION)) {
560 location = currentValue();
561 } else {
562 // we ignore elements we do not understand
563 }
564 next();
565 }
566
567 try {
568 URI uri = location != null ? new URI(location) : null;
569 ILicense license = MetadataFactory.createLicense(uri, body);
570 licenses.add(license);
571 } catch (URISyntaxException e) {
572 throw new IllegalStateException("bad license URI at token: " + current + ", " + currentValue()); //$NON-NLS-1$ //$NON-NLS-2$
573 }
574 }
575
576 private void parseArtifacts(String prefix, List<IArtifactKey> artifacts) {
577 while (current != null && current.startsWith(prefix)) {
578 int dotIndex = current.indexOf('.', prefix.length());
579 if (dotIndex == -1)
580 throw new IllegalStateException("bad token: " + current + " = " + currentValue()); //$NON-NLS-1$ //$NON-NLS-2$
581
582 parseArtifact(current.substring(0, dotIndex + 1), artifacts);
583 }
584 }
585
586 private void parseArtifact(String prefix, List<IArtifactKey> artifacts) {
587 String artifactClassifier = null;
588 String artifactId = null;
589 Version artifactVersion = null;
590 while (current != null && current.startsWith(prefix)) {
591 String token = current.substring(prefix.length());
592 switch (token) {
593 case CLASSIFIER:
594 artifactClassifier = currentValue();
595 break;
596 case ID:
597 artifactId = currentValue();
598 break;
599 case VERSION:
600 artifactVersion = Version.parseVersion(substituteVersionAndQualifier(currentValue()));
601 break;
602 // we ignore elements we do not understand
603 default:
604 break;
605 }
606
607 next();
608 }
609 IArtifactKey artifactKey = new ArtifactKey(artifactClassifier, artifactId, artifactVersion);
610 artifacts.add(artifactKey);
611 }
612
613 private String substituteVersionAndQualifier(String version) {
614 if (version.contains(VERSION_SUBSTITUTION)) {
615 version = replace(version, VERSION_SUBSTITUTION, hostVersion.toString());
616 }
617
618 if (version.contains(QUALIFIER_SUBSTITUTION)) {
619 try {
620 String qualifier = PublisherHelper.toOSGiVersion(hostVersion).getQualifier();
621 if (qualifier == null)
622 qualifier = ""; //$NON-NLS-1$
623 if (qualifier.length() == 0) {
624 // Note: this works only for OSGi versions and version ranges
625 // where the qualifier if present must be at the end of a version string
626 version = replace(version, "." + QUALIFIER_SUBSTITUTION, ""); //$NON-NLS-1$ //$NON-NLS-2$
627 }
628 version = replace(version, QUALIFIER_SUBSTITUTION, qualifier);
629 } catch (UnsupportedOperationException e) {
630 // Version cannot be converted to OSGi
631 }
632 }
633 return version;
634 }
635
636 // originally from org.eclipse.core.internal.net.StringUtil
637 public static String replace(String source, String from, String to) {
638 if (from.length() == 0)
639 return source;
640 StringBuilder buffer = new StringBuilder();
641 int current = 0;
642 int pos = 0;
643 while (pos != -1) {
644 pos = source.indexOf(from, current);
645 if (pos == -1) {
646 buffer.append(source.substring(current));
647 } else {
648 buffer.append(source.substring(current, pos));
649 buffer.append(to);
650 current = pos + from.length();
651 }
652 }
653 return buffer.toString();
654 }
655
656 public Map<String, String> getProperties() {
657 if (adviceProperties.isEmpty())
658 return null;
659 return adviceProperties;
660 }
661
662 public IRequirement[] getRequiredCapabilities() {
663 if (adviceRequires.isEmpty())
664 return null;
665
666 return adviceRequires.toArray(new IRequirement[adviceRequires.size()]);
667 }
668
669 public IProvidedCapability[] getProvidedCapabilities() {
670 if (adviceProvides.isEmpty())
671 return null;
672
673 return adviceProvides.toArray(new IProvidedCapability[adviceProvides.size()]);
674 }
675
676 public IUpdateDescriptor getUpdateDescriptor() {
677 return adviceUpdateDescriptor;
678 }
679
680 public Map<String, ITouchpointInstruction> getTouchpointInstructions() {
681 if (adviceInstructions.isEmpty())
682 return null;
683
684 return adviceInstructions;
685 }
686
687 public InstallableUnitDescription[] getAdditionalInstallableUnitDescriptions() {
688 if (adviceOtherIUs.isEmpty())
689 return null;
690
691 return adviceOtherIUs.toArray(new InstallableUnitDescription[adviceOtherIUs.size()]);
692 }
693
694 public IRequirement[] getMetaRequiredCapabilities() {
695 if (adviceMetaRequires.isEmpty())
696 return null;
697
698 return adviceMetaRequires.toArray(new IRequirement[adviceMetaRequires.size()]);
699 }
700 }