| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2005, 2018 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.util.*; |
| 17 |
import org.eclipse.core.runtime.Assert; |
| 18 |
import org.eclipse.equinox.p2.engine.spi.ProvisioningAction; |
| 19 |
import org.eclipse.equinox.p2.metadata.*; |
| 20 |
import org.eclipse.osgi.util.NLS; |
| 21 |
|
| 22 |
public class InstructionParser { |
| 23 |
|
| 24 |
public class ActionEntry { |
| 25 |
|
| 26 |
protected final VersionRange versionRange; |
| 27 |
protected final String actionId; |
| 28 |
|
| 29 |
public ActionEntry(String actionId, VersionRange versionRange) { |
| 30 |
this.actionId = actionId; |
| 31 |
this.versionRange = versionRange; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
private static final String VERSION_EQUALS = "version="; //$NON-NLS-1$ |
| 36 |
private ActionManager actionManager; |
| 37 |
|
| 38 |
public InstructionParser(ActionManager actionManager) { |
| 39 |
Assert.isNotNull(actionManager); |
| 40 |
this.actionManager = actionManager; |
| 41 |
} |
| 42 |
|
| 43 |
public List<ProvisioningAction> parseActions(ITouchpointInstruction instruction, ITouchpointType touchpointType) { |
| 44 |
List<ProvisioningAction> actions = new ArrayList<>(); |
| 45 |
Map<String, ActionEntry> importMap = parseImportAttribute(instruction.getImportAttribute()); |
| 46 |
StringTokenizer tokenizer = new StringTokenizer(instruction.getBody(), ";"); //$NON-NLS-1$ |
| 47 |
while (tokenizer.hasMoreTokens()) { |
| 48 |
actions.add(parseAction(tokenizer.nextToken(), importMap, touchpointType)); |
| 49 |
} |
| 50 |
return actions; |
| 51 |
} |
| 52 |
|
| 53 |
private Map<String, ActionEntry> parseImportAttribute(String importAttribute) { |
| 54 |
if (importAttribute == null) |
| 55 |
return Collections.emptyMap(); |
| 56 |
|
| 57 |
Map<String, ActionEntry> result = new HashMap<>(); |
| 58 |
StringTokenizer tokenizer = new StringTokenizer(importAttribute, ","); //$NON-NLS-1$ |
| 59 |
while (tokenizer.hasMoreTokens()) { |
| 60 |
StringTokenizer actionTokenizer = new StringTokenizer(tokenizer.nextToken(), ";"); //$NON-NLS-1$ |
| 61 |
String actionId = actionTokenizer.nextToken().trim(); |
| 62 |
int lastDot = actionId.lastIndexOf('.'); |
| 63 |
String actionKey = (lastDot == -1) ? actionId : actionId.substring(lastDot + 1); |
| 64 |
VersionRange actionVersionRange = null; |
| 65 |
while (actionTokenizer.hasMoreTokens()) { |
| 66 |
String actionAttribute = actionTokenizer.nextToken().trim(); |
| 67 |
if (actionAttribute.startsWith(VERSION_EQUALS)) |
| 68 |
actionVersionRange = VersionRange.create(actionAttribute.substring(VERSION_EQUALS.length() + 1)); |
| 69 |
} |
| 70 |
result.put(actionKey, new ActionEntry(actionId, actionVersionRange)); |
| 71 |
result.put(actionId, new ActionEntry(actionId, actionVersionRange)); |
| 72 |
} |
| 73 |
return result; |
| 74 |
} |
| 75 |
|
| 76 |
private ProvisioningAction parseAction(String statement, Map<String, ActionEntry> qualifier, ITouchpointType touchpointType) { |
| 77 |
int openBracket = statement.indexOf('('); |
| 78 |
int closeBracket = statement.lastIndexOf(')'); |
| 79 |
if (openBracket == -1 || closeBracket == -1 || openBracket > closeBracket) |
| 80 |
throw new IllegalArgumentException(NLS.bind(Messages.action_syntax_error, statement)); |
| 81 |
String actionName = statement.substring(0, openBracket).trim(); |
| 82 |
ProvisioningAction action = lookupAction(actionName, qualifier, touchpointType); |
| 83 |
if (action instanceof MissingAction) |
| 84 |
return action; |
| 85 |
|
| 86 |
String nameValuePairs = statement.substring(openBracket + 1, closeBracket); |
| 87 |
if (nameValuePairs.length() == 0) |
| 88 |
return new ParameterizedProvisioningAction(action, Collections.emptyMap(), statement); |
| 89 |
|
| 90 |
StringTokenizer tokenizer = new StringTokenizer(nameValuePairs, ","); //$NON-NLS-1$ |
| 91 |
Map<String, String> parameters = new HashMap<>(); |
| 92 |
while (tokenizer.hasMoreTokens()) { |
| 93 |
String nameValuePair = tokenizer.nextToken(); |
| 94 |
int colonIndex = nameValuePair.indexOf(":"); //$NON-NLS-1$ |
| 95 |
if (colonIndex == -1) |
| 96 |
throw new IllegalArgumentException(NLS.bind(Messages.action_syntax_error, statement)); |
| 97 |
String name = nameValuePair.substring(0, colonIndex).trim(); |
| 98 |
String value = nameValuePair.substring(colonIndex + 1).trim(); |
| 99 |
parameters.put(name, value); |
| 100 |
} |
| 101 |
return new ParameterizedProvisioningAction(action, parameters, statement); |
| 102 |
} |
| 103 |
|
| 104 |
private ProvisioningAction lookupAction(String actionId, Map<String, ActionEntry> importMap, ITouchpointType touchpointType) { |
| 105 |
VersionRange versionRange = null; |
| 106 |
ActionEntry actionEntry = importMap.get(actionId); |
| 107 |
if (actionEntry != null) { |
| 108 |
actionId = actionEntry.actionId; |
| 109 |
versionRange = actionEntry.versionRange; |
| 110 |
} |
| 111 |
|
| 112 |
actionId = actionManager.getTouchpointQualifiedActionId(actionId, touchpointType); |
| 113 |
ProvisioningAction action = actionManager.getAction(actionId, versionRange); |
| 114 |
if (action == null) |
| 115 |
action = new MissingAction(actionId, versionRange); |
| 116 |
|
| 117 |
return action; |
| 118 |
} |
| 119 |
} |