View Javadoc
1   /*
2    * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and contributors.
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the GNU Lesser General Public License
6    * (LGPL) version 2.1 which accompanies this distribution, and is available at
7    * http://www.gnu.org/licenses/lgpl-2.1.html
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * Contributors:
15   *     bstefanescu, slacoin, jcarsique
16   */
17  package org.nuxeo.build.maven.filter;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.maven.artifact.Artifact;
24  import org.eclipse.aether.graph.DependencyNode;
25  import org.nuxeo.build.maven.ArtifactDescriptor;
26  
27  /**
28   * TODO NXBT-258
29   */
30  public class AncestorFilter extends AbstractFilter {
31  
32      protected ArtifactDescriptor ad;
33  
34      protected List<Filter> filters;
35  
36      public AncestorFilter(String pattern) {
37          ad = new ArtifactDescriptor(pattern);
38          filters = new ArrayList<>();
39          if (ad.getGroupId() != null && !ad.getGroupId().equals("*")) {
40              addFilter(new GroupIdFilter(ad.getGroupId()));
41          }
42          if (ad.getArtifactId() != null && !ad.getArtifactId().equals("*")) {
43              addFilter(new ArtifactIdFilter(ad.getArtifactId()));
44          }
45          if (ad.getVersion() != null && !ad.getVersion().equals("*")) {
46              addFilter(new VersionFilter(ad.getVersion()));
47          }
48          if (ad.getType() != null && !ad.getType().equals("*")) {
49              addFilter(new TypeFilter(ad.getType()));
50          }
51          if (ad.getClassifier() != null && !ad.getClassifier().equals("*")) {
52              addFilter(new ClassifierFilter(ad.getClassifier()));
53          }
54          if (ad.getScope() != null && !ad.getScope().equals("*")) {
55              addFilter(new ScopeFilter(ad.getScope()));
56          }
57      }
58  
59      protected void addFilter(Filter filter) {
60          filters.add(filter);
61      }
62  
63      @Override
64      public boolean accept(Artifact artifact) {
65          throw new UnsupportedOperationException("Ancestor folder cannot be applied on artifact objects");
66      }
67  
68      @Override
69      public boolean accept(DependencyNode node, List<DependencyNode> parents) {
70          for (DependencyNode parent : parents) {
71              if (accept(parent)) {
72                  return true;
73              }
74          }
75          return result(false, node.toString());
76      }
77  
78      /**
79       * @since 2.0
80       */
81      protected boolean accept(DependencyNode parent) {
82          for (Filter filter : filters) {
83              if (!filter.accept(parent, Collections.<DependencyNode> emptyList())) {
84                  return false;
85              }
86          }
87          return true;
88      }
89  
90  }