Search in sources :

Example 21 with MavenArtifact

use of org.jetbrains.idea.maven.model.MavenArtifact in project intellij-plugins by JetBrains.

the class AbstractDependencyFilter method processInstructions.

protected final void processInstructions(String header) throws DependencyEmbedderException {
    Parameters instructions = OSGiHeader.parseHeader(MISSING_KEY_PATTERN.matcher(header).replaceAll("$1$2*;$3"));
    Collection<MavenArtifact> availableDependencies = new LinkedHashSet<>(m_dependencyArtifacts);
    DependencyFilter filter;
    for (Iterator<Map.Entry<String, Attrs>> clauseIterator = instructions.entrySet().iterator(); clauseIterator.hasNext(); ) {
        String inline = "false";
        // always start with a fresh *modifiable* collection for each unique clause
        Collection<MavenArtifact> filteredDependencies = new LinkedHashSet<>(availableDependencies);
        // CLAUSE: REGEXP --> { ATTRIBUTE MAP }
        Map.Entry<String, Attrs> clause = clauseIterator.next();
        String primaryKey = (clause.getKey()).replaceFirst("~+$", "");
        boolean isNegative = primaryKey.startsWith("!");
        if (isNegative) {
            primaryKey = primaryKey.substring(1);
        }
        if (!"*".equals(primaryKey)) {
            filter = new DependencyFilter(primaryKey) {

                boolean matches(MavenArtifact dependency) {
                    return super.matches(dependency.getArtifactId());
                }
            };
            // FILTER ON MAIN CLAUSE
            filter.filter(filteredDependencies);
        }
        for (Map.Entry<String, String> entry : (clause.getValue()).entrySet()) {
            // ATTRIBUTE: KEY --> REGEXP
            Map.Entry attr = (Map.Entry) entry;
            if ("groupId".equals(attr.getKey())) {
                filter = new DependencyFilter((String) attr.getValue()) {

                    boolean matches(MavenArtifact dependency) {
                        return super.matches(dependency.getGroupId());
                    }
                };
            } else if ("artifactId".equals(attr.getKey())) {
                filter = new DependencyFilter((String) attr.getValue()) {

                    boolean matches(MavenArtifact dependency) {
                        return super.matches(dependency.getArtifactId());
                    }
                };
            } else if ("version".equals(attr.getKey())) {
                filter = new DependencyFilter((String) attr.getValue()) {

                    boolean matches(MavenArtifact dependency) {
                        try {
                            // use the symbolic version if available (ie. 1.0.0-SNAPSHOT)
                            return super.matches(dependency.getVersion());
                        } catch (Exception e) {
                            return super.matches(dependency.getVersion());
                        }
                    }
                };
            } else if ("scope".equals(attr.getKey())) {
                filter = new DependencyFilter((String) attr.getValue(), "compile") {

                    boolean matches(MavenArtifact dependency) {
                        return super.matches(dependency.getScope());
                    }
                };
            } else if ("type".equals(attr.getKey())) {
                filter = new DependencyFilter((String) attr.getValue(), "jar") {

                    boolean matches(MavenArtifact dependency) {
                        return super.matches(dependency.getType());
                    }
                };
            } else if ("classifier".equals(attr.getKey())) {
                filter = new DependencyFilter((String) attr.getValue()) {

                    boolean matches(MavenArtifact dependency) {
                        return super.matches(dependency.getClassifier());
                    }
                };
            } else if ("optional".equals(attr.getKey())) {
                filter = new DependencyFilter((String) attr.getValue(), "false") {

                    boolean matches(MavenArtifact dependency) {
                        return super.matches(String.valueOf(dependency.isOptional()));
                    }
                };
            } else if ("inline".equals(attr.getKey())) {
                inline = (String) attr.getValue();
                continue;
            } else {
                throw new DependencyEmbedderException("Unexpected attribute " + attr.getKey());
            }
            // FILTER ON EACH ATTRIBUTE
            filter.filter(filteredDependencies);
        }
        if (isNegative) {
            // negative clauses reduce the set of available artifacts
            availableDependencies.removeAll(filteredDependencies);
            if (!clauseIterator.hasNext()) {
                // assume there's an implicit * missing at the end
                processDependencies(availableDependencies, inline);
            }
        } else {
            // positive clause; doesn't alter the available artifacts
            processDependencies(filteredDependencies, inline);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Parameters(aQute.bnd.header.Parameters) Attrs(aQute.bnd.header.Attrs) MavenArtifact(org.jetbrains.idea.maven.model.MavenArtifact) Map(java.util.Map)

Aggregations

MavenArtifact (org.jetbrains.idea.maven.model.MavenArtifact)21 File (java.io.File)5 NotNull (org.jetbrains.annotations.NotNull)4 MavenId (org.jetbrains.idea.maven.model.MavenId)4 MavenProject (org.jetbrains.idea.maven.project.MavenProject)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 Library (com.intellij.openapi.roots.libraries.Library)2 SmartList (com.intellij.util.SmartList)2 THashSet (gnu.trove.THashSet)2 List (java.util.List)2 Map (java.util.Map)2 Attrs (aQute.bnd.header.Attrs)1 Parameters (aQute.bnd.header.Parameters)1 Analyzer (aQute.bnd.osgi.Analyzer)1 AccessToken (com.intellij.openapi.application.AccessToken)1 Module (com.intellij.openapi.module.Module)1 DumbProgressIndicator (com.intellij.openapi.progress.DumbProgressIndicator)1 Project (com.intellij.openapi.project.Project)1 CompilerModuleExtension (com.intellij.openapi.roots.CompilerModuleExtension)1 ContentEntry (com.intellij.openapi.roots.ContentEntry)1