View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.buildcache;
20  
21  import javax.annotation.Nonnull;
22  
23  import java.util.List;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.maven.buildcache.xml.config.DirScanConfig;
27  import org.apache.maven.buildcache.xml.config.TagExclude;
28  import org.apache.maven.buildcache.xml.config.TagScanConfig;
29  
30  /**
31   * PluginScanConfigImpl
32   */
33  public class PluginScanConfigImpl implements PluginScanConfig {
34  
35      private final DirScanConfig dto;
36  
37      public PluginScanConfigImpl(DirScanConfig scanConfig) {
38          this.dto = scanConfig;
39      }
40  
41      @Override
42      public boolean isSkip() {
43          return StringUtils.equals(dto.getMode(), "skip");
44      }
45  
46      @Override
47      public boolean accept(String tagName) {
48          // include or exclude is a choice element, could be only obe property set
49  
50          //noinspection ConstantConditions
51          final List<TagScanConfig> includes = dto.getIncludes();
52          if (!includes.isEmpty()) {
53              return findTagScanProperties(tagName) != null;
54          }
55  
56          return !contains(dto.getExcludes(), tagName);
57      }
58  
59      private boolean contains(List<TagExclude> excludes, String tagName) {
60          for (TagExclude exclude : excludes) {
61              if (StringUtils.equals(exclude.getTagName(), tagName)) {
62                  return true;
63              }
64          }
65          return false;
66      }
67  
68      @Nonnull
69      @Override
70      public PluginScanConfig mergeWith(final PluginScanConfig overrideConfig) {
71          if (dto == null) {
72              return overrideConfig;
73          }
74  
75          final DirScanConfig override = overrideConfig.dto();
76          if (override == null) {
77              return this;
78          }
79  
80          if (override.isIgnoreParent()) {
81              return overrideConfig;
82          }
83  
84          DirScanConfig merged = new DirScanConfig();
85          if (override.getMode() != null) {
86              merged.setMode(override.getMode());
87          } else {
88              merged.setMode(dto.getMode());
89          }
90  
91          merged.getExcludes().addAll(dto.getExcludes());
92          merged.getExcludes().addAll(override.getExcludes());
93  
94          merged.getIncludes().addAll(dto.getIncludes());
95          merged.getIncludes().addAll(override.getIncludes());
96  
97          return new PluginScanConfigImpl(merged);
98      }
99  
100     @Nonnull
101     public ScanConfigProperties getTagScanProperties(String tagName) {
102         ScanConfigProperties scanProperties = findTagScanProperties(tagName);
103         return scanProperties != null ? scanProperties : defaultScanConfig();
104     }
105 
106     @Override
107     public DirScanConfig dto() {
108         return dto;
109     }
110 
111     private ScanConfigProperties findTagScanProperties(String tagName) {
112         ScanConfigProperties scanConfigProperties = findConfigByName(tagName, dto.getIncludes());
113         if (scanConfigProperties == null) {
114             scanConfigProperties = findConfigByName(tagName, dto.getTagScanConfigs());
115         }
116         return scanConfigProperties;
117     }
118 
119     private ScanConfigProperties findConfigByName(String tagName, List<TagScanConfig> configs) {
120         if (configs == null) {
121             return null;
122         }
123 
124         for (TagScanConfig config : configs) {
125             if (StringUtils.equals(tagName, config.getTagName())) {
126                 return new ScanConfigProperties(config.isRecursive(), config.getGlob());
127             }
128         }
129         return null;
130     }
131 
132     private static ScanConfigProperties defaultScanConfig() {
133         return new ScanConfigProperties(true, null);
134     }
135 }