001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.impl.filter.ruletree;
020
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.List;
024import java.util.concurrent.atomic.AtomicInteger;
025import java.util.stream.Stream;
026
027import static java.util.stream.Collectors.toList;
028
029/**
030 * Group tree for Maven groupIDs.
031 * This class parses a text file that has a directive on each line. Directive examples:
032 * <ul>
033 *     <li>ignored/formatting - each line starting with {@code '#'} (hash) or being empty/blank is ignored.</li>
034 *     <li>modifier {@code !} is negation (disallow; by def entry allows). If present must be first character.</li>
035 *     <li>modifier {@code =} is limiter (to given G; by def is "G and below"). If present, must be first character. If negation present, must be second character.</li>
036 *     <li>a valid Maven groupID ie "org.apache.maven".</li>
037 * </ul>
038 * By default, a G entry ie {@code org.apache.maven} means "allow {@code org.apache.maven} G and all Gs below
039 * (so {@code org.apache.maven.plugins} etc. are all allowed). There is one special entry {@code "*"} (asterisk)
040 * that means "root" and defines the default acceptance: {@code "*"} means "by default accept" and {@code "!*"}
041 * means "by default deny" (same effect as when this character is not present in file). Use of limiter modifier
042 * on "root" like {@code "=*"} has no effect, is simply ignored.
043 *
044 * <p>
045 * Examples:
046 * <pre>
047 * {@code
048 * # this is my group filter list
049 *
050 * org.apache.maven
051 * !=org.apache.maven.foo
052 * !org.apache.maven.indexer
053 * =org.apache.bar
054 * }
055 * </pre>
056 *
057 * File meaning: "allow all {@code org.apache.maven} and below", "disallow {@code org.apache.maven.foo} groupId ONLY"
058 * (hence {@code org.apache.maven.foo.bar} is allowed due first line), "disallow {@code org.apache.maven.indexer} and below"
059 * and "allow {@code org.apache.bar} groupID ONLY".
060 *
061 * <p>
062 * In case of conflicting rules, parsing happens by "last wins", so line closer to last line in file "wins", and conflicting
063 * line value is lost.
064 */
065public class GroupTree extends Node<GroupTree> {
066    /**
067     * Creates root, that is special: accept is never null.
068     */
069    public static GroupTree create(String name) {
070        GroupTree result = new GroupTree(name);
071        result.accept = false;
072        return result;
073    }
074
075    private static final String ROOT = "*";
076    private static final String MOD_EXCLUSION = "!";
077    private static final String MOD_STOP = "=";
078
079    private static List<String> elementsOfGroup(final String groupId) {
080        return Arrays.stream(groupId.split("\\.")).filter(e -> !e.isEmpty()).collect(toList());
081    }
082
083    private boolean stop;
084    private Boolean accept;
085
086    private GroupTree(String name) {
087        super(name);
088    }
089
090    public int loadNodes(Stream<String> linesStream) {
091        AtomicInteger counter = new AtomicInteger(0);
092        linesStream.forEach(line -> {
093            if (loadNode(line)) {
094                counter.incrementAndGet();
095            }
096        });
097        return counter.get();
098    }
099
100    public boolean loadNode(String line) {
101        if (!line.startsWith("#") && !line.trim().isEmpty()) {
102            GroupTree currentNode = this;
103            boolean accept = true;
104            if (line.startsWith(MOD_EXCLUSION)) {
105                accept = false;
106                line = line.substring(MOD_EXCLUSION.length());
107            }
108            boolean stop = false;
109            if (line.startsWith(MOD_STOP)) {
110                stop = true;
111                line = line.substring(MOD_STOP.length());
112            }
113            if (ROOT.equals(line)) {
114                this.accept = accept;
115                return true;
116            }
117            List<String> groupElements = elementsOfGroup(line);
118            for (String groupElement : groupElements.subList(0, groupElements.size() - 1)) {
119                currentNode = currentNode.siblings.computeIfAbsent(groupElement, GroupTree::new);
120            }
121            String lastElement = groupElements.get(groupElements.size() - 1);
122            currentNode = currentNode.siblings.computeIfAbsent(lastElement, GroupTree::new);
123            currentNode.stop = stop;
124            currentNode.accept = accept;
125            return true;
126        }
127        return false;
128    }
129
130    public boolean acceptedGroupId(String groupId) {
131        final List<String> current = new ArrayList<>();
132        final List<String> groupElements = elementsOfGroup(groupId);
133        Boolean accepted = null;
134        GroupTree currentNode = this;
135        for (String groupElement : groupElements) {
136            current.add(groupElement);
137            currentNode = currentNode.siblings.get(groupElement);
138            if (currentNode == null) {
139                // we stepped off the tree; use value we got so far
140                break;
141            } else if (currentNode.stop && groupElements.equals(current)) {
142                // exact match
143                accepted = currentNode.accept;
144                break;
145            } else if (!currentNode.stop && currentNode.accept != null) {
146                // "inherit" if not STOP and allow is set; and most probably we loop more
147                accepted = currentNode.accept;
148            }
149        }
150        // use 'accepted', if defined; otherwise fallback to root (it always has 'allow' set)
151        return accepted != null ? accepted : this.accept;
152    }
153
154    @Override
155    public String toString() {
156        return (accept != null ? (accept ? "+" : "-") : "?") + (stop ? "=" : "") + name;
157    }
158
159    @Override
160    public void dump(String prefix) {
161        System.out.println(prefix + this);
162        for (GroupTree node : siblings.values()) {
163            node.dump(prefix + "  ");
164        }
165    }
166}