Search in sources :

Example 1 with RangeExpression

use of aQute.bnd.osgi.resource.FilterParser.RangeExpression in project bndtools by bndtools.

the class R5LabelFormatter method appendRequirementLabel.

public static void appendRequirementLabel(StyledString label, Requirement requirement, boolean shorten) {
    String namespace = requirement.getNamespace();
    String filter = requirement.getDirectives().get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
    boolean optional = Namespace.RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE));
    FilterParser fp = new FilterParser();
    if (filter == null) {
        if (namespace.contains("$")) {
            Pattern pattern = Pattern.compile("\\{(.*?)\\}");
            Matcher matcher = pattern.matcher(namespace);
            label.append(namespace);
            while (matcher.find()) {
                int begin = matcher.start(1);
                int end = matcher.end(1);
                label.setStyle(begin, end - begin, BoldStyler.INSTANCE_DEFAULT);
            }
        } else {
            label.append(namespace + ": <no filter>", ItalicStyler.INSTANCE_ERROR);
        }
    } else {
        try {
            Expression exp = fp.parse(filter);
            if (exp instanceof WithRangeExpression) {
                appendNamespaceWithValue(label, namespace, ((WithRangeExpression) exp).printExcludingRange(), shorten);
                RangeExpression range = ((WithRangeExpression) exp).getRangeExpression();
                if (range != null)
                    label.append(" ").append(formatRangeString(range), StyledString.COUNTER_STYLER);
            } else if (ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE.equals(namespace)) {
                Matcher matcher = EE_PATTERN.matcher(filter);
                if (matcher.find()) {
                    String eename = matcher.group(1);
                    String version = matcher.group(2);
                    appendNamespaceWithValue(label, namespace, eename, true);
                    label.append(" ").append(version, StyledString.COUNTER_STYLER);
                } else {
                    appendNamespaceWithValue(label, namespace, filter, true);
                }
            } else {
                appendNamespaceWithValue(label, namespace, filter, true);
            }
        } catch (Exception e) {
            label.append(namespace + ": ", StyledString.QUALIFIER_STYLER);
            label.append("<parse error>", ItalicStyler.INSTANCE_ERROR);
        }
    }
    boolean first = true;
    for (Entry<String, String> directive : requirement.getDirectives().entrySet()) {
        if (Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE.equals(directive.getKey()) || Namespace.REQUIREMENT_FILTER_DIRECTIVE.equals(directive.getKey()))
            // deal with the filter: and resolution: directives separately
            continue;
        StringBuilder buf = new StringBuilder();
        buf.append(first ? " " : ", ");
        buf.append(directive.getKey()).append(":=").append(directive.getValue());
        label.append(buf.toString(), StyledString.QUALIFIER_STYLER);
        first = false;
    }
    if (optional) {
        label.setStyle(0, label.length(), StyledString.QUALIFIER_STYLER);
        label.append(" <optional>", ItalicStyler.INSTANCE_DEFAULT);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) RangeExpression(aQute.bnd.osgi.resource.FilterParser.RangeExpression) WithRangeExpression(aQute.bnd.osgi.resource.FilterParser.WithRangeExpression) SimpleExpression(aQute.bnd.osgi.resource.FilterParser.SimpleExpression) Expression(aQute.bnd.osgi.resource.FilterParser.Expression) WithRangeExpression(aQute.bnd.osgi.resource.FilterParser.WithRangeExpression) RangeExpression(aQute.bnd.osgi.resource.FilterParser.RangeExpression) WithRangeExpression(aQute.bnd.osgi.resource.FilterParser.WithRangeExpression) StyledString(org.eclipse.jface.viewers.StyledString) FilterParser(aQute.bnd.osgi.resource.FilterParser)

Aggregations

FilterParser (aQute.bnd.osgi.resource.FilterParser)1 Expression (aQute.bnd.osgi.resource.FilterParser.Expression)1 RangeExpression (aQute.bnd.osgi.resource.FilterParser.RangeExpression)1 SimpleExpression (aQute.bnd.osgi.resource.FilterParser.SimpleExpression)1 WithRangeExpression (aQute.bnd.osgi.resource.FilterParser.WithRangeExpression)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 StyledString (org.eclipse.jface.viewers.StyledString)1