001package org.jszip.maven;
002
003import org.apache.maven.artifact.Artifact;
004import org.codehaus.plexus.util.StringUtils;
005
006/**
007 * Represents a mapping
008 */
009public class Mapping {
010    private String select;
011    private String path;
012
013    public Mapping() {
014    }
015
016    public Mapping(String select, String path) {
017        this.select = select;
018        this.path = path;
019    }
020
021    public String getPath() {
022        return path;
023    }
024
025    public void setPath(String path) {
026        this.path = path;
027    }
028
029    public String getSelect() {
030        return select;
031    }
032
033    public void setSelect(String select) {
034        this.select = select;
035    }
036
037    public boolean isMatch(Artifact artifact) {
038        if (StringUtils.isBlank(select)) {
039            return true;
040        }
041        int index = select.indexOf(':');
042        String groupId = index == -1 ? "*" : select.substring(0, index);
043        String artifactId = index == -1 ? select : select.substring(index + 1);
044        return matches(groupId, artifact.getGroupId()) && matches(artifactId, artifact.getArtifactId());
045    }
046
047    private boolean matches(String pattern, String token) {
048        boolean matches;
049
050        // support full wildcard and implied wildcard
051        if ("*".equals(pattern) || pattern.length() == 0) {
052            return true;
053        }
054        // support contains wildcard
055        if (pattern.startsWith("*") && pattern.endsWith("*")) {
056            final String contains = pattern.substring(1, pattern.length() - 1);
057
058            return token.contains(contains);
059        }
060        // support leading wildcard
061        if (pattern.startsWith("*")) {
062            final String suffix = pattern.substring(1, pattern.length());
063
064            return token.endsWith(suffix);
065        }
066        // support trailing wildcard
067        if (pattern.endsWith("*")) {
068            final String prefix = pattern.substring(0, pattern.length() - 1);
069
070            return token.startsWith(prefix);
071        }
072        // support wildcards in the middle of a pattern segment
073        if (pattern.indexOf('*') > -1) {
074            String[] parts = pattern.split("\\*");
075            int lastPartEnd = -1;
076
077            for (String part : parts) {
078                int idx = token.indexOf(part);
079                if (idx <= lastPartEnd) {
080                    return false;
081                }
082
083                lastPartEnd = idx + part.length();
084            }
085
086            return true;
087        }
088        return token.equals(pattern);
089
090    }
091
092    public static String getArtifactPath(Mapping[] mappings, Artifact artifact) {
093        if (mappings == null) {
094            return "/virtual";
095        }
096        for (Mapping mapping: mappings) {
097            if (mapping.isMatch(artifact)) {
098                final String path = StringUtils.clean(mapping.getPath());
099                if (StringUtils.isBlank(path) || "/".equals(path)) {
100                    return "/virtual";
101                }
102                return "/virtual/" + StringUtils.strip(path, "/");
103            }
104        }
105        return "/virtual";
106    }
107
108
109}