Debian Codegraph @

bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/internal/p2/metadata/RequiredPropertiesMatch.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2007, 2017 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 * Todor Boev
13 *******************************************************************************/
14 package org.eclipse.equinox.internal.p2.metadata;
15
16 import static org.eclipse.equinox.internal.p2.metadata.InstallableUnit.MEMBER_PROVIDED_CAPABILITIES;
17 import static org.eclipse.equinox.internal.p2.metadata.ProvidedCapability.MEMBER_NAMESPACE;
18 import static org.eclipse.equinox.internal.p2.metadata.ProvidedCapability.MEMBER_PROPERTIES;
19
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.equinox.p2.metadata.IInstallableUnit;
22 import org.eclipse.equinox.p2.metadata.IProvidedCapability;
23 import org.eclipse.equinox.p2.metadata.IRequirement;
24 import org.eclipse.equinox.p2.metadata.expression.ExpressionUtil;
25 import org.eclipse.equinox.p2.metadata.expression.IExpression;
26 import org.eclipse.equinox.p2.metadata.expression.IExpressionFactory;
27 import org.eclipse.equinox.p2.metadata.expression.IFilterExpression;
28 import org.eclipse.equinox.p2.metadata.expression.IMatchExpression;
29
30 /**
31 * A required capability match represents some external constraint on an
32 * {@link IInstallableUnit}.
33 * <p>
34 * This is a flavor of the general {@link IRequirement} that searches for a
35 * capability that has {@link IProvidedCapability#getProperties() properties}
36 * that match a given expression. I.e. this is much more limited that an
37 * arbitrary match expression executed over all metadata of the IU.
38 */
39 public class RequiredPropertiesMatch extends Requirement {
40 /**
41 * Argument $0 must evaluate to a String Argument $2 must evaluate to an
42 * expression compatible with the match operator "~="
43 */
44 private static final IExpression PROPERTIES_MATCH = ExpressionUtil
45 .parse(String.format("%s.exists(cap | cap.%s == $0 && cap.%s ~= $1)", //$NON-NLS-1$
46 MEMBER_PROVIDED_CAPABILITIES, MEMBER_NAMESPACE, MEMBER_PROPERTIES));
47
48 public RequiredPropertiesMatch(String namespace, IFilterExpression attrFilter,
49 IMatchExpression<IInstallableUnit> envFilter, int min, int max, boolean greedy, String description) {
50 super(createMatchExpressionFromFilter(namespace, attrFilter, description), envFilter, min, max, greedy,
51 description);
52 }
53
54 @Override
55 public String toString() {
56 StringBuilder result = new StringBuilder();
57
58 result.append(extractNamespace(getMatches()));
59 result.append("; "); //$NON-NLS-1$
60 result.append(extractPropertiesMatch(getMatches()));
61
62 return result.toString();
63 }
64
65 public static IMatchExpression<IInstallableUnit> createMatchExpressionFromFilter(String namespace,
66 IFilterExpression attrFilter, String description) {
67 Assert.isNotNull(namespace);
68 Assert.isNotNull(attrFilter,
69 "Filter is missing for required capability " + namespace + " in bundle " + description); //$NON-NLS-1$ //$NON-NLS-2$
70 IExpressionFactory factory = ExpressionUtil.getFactory();
71 return factory.matchExpression(PROPERTIES_MATCH, namespace, attrFilter);
72 }
73
74 public static String extractNamespace(IMatchExpression<IInstallableUnit> matchExpression) {
75 assertValid(matchExpression);
76 return (String) matchExpression.getParameters()[0];
77 }
78
79 public static IFilterExpression extractPropertiesMatch(IMatchExpression<IInstallableUnit> matchExpression) {
80 assertValid(matchExpression);
81 return (IFilterExpression) matchExpression.getParameters()[1];
82 }
83
84 public static boolean isPropertiesMatchRequirement(IMatchExpression<IInstallableUnit> matchExpression) {
85 return PROPERTIES_MATCH.equals(ExpressionUtil.getOperand(matchExpression));
86 }
87
88 private static void assertValid(IMatchExpression<IInstallableUnit> matchExpression) {
89 if (!isPropertiesMatchRequirement(matchExpression)) {
90 throw new IllegalArgumentException();
91 }
92 }
93 }