Coverage Report - org.owasp.dependencycheck.analyzer.DependencyBundlingAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
DependencyBundlingAnalyzer
40%
82/201
30%
64/212
8
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.analyzer;
 19  
 
 20  
 import java.io.File;
 21  
 import java.util.HashSet;
 22  
 import java.util.Iterator;
 23  
 import java.util.ListIterator;
 24  
 import java.util.Set;
 25  
 import java.util.regex.Matcher;
 26  
 import java.util.regex.Pattern;
 27  
 import org.owasp.dependencycheck.Engine;
 28  
 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
 29  
 import org.owasp.dependencycheck.dependency.Dependency;
 30  
 import org.owasp.dependencycheck.dependency.Identifier;
 31  
 import org.owasp.dependencycheck.utils.DependencyVersion;
 32  
 import org.owasp.dependencycheck.utils.DependencyVersionUtil;
 33  
 import org.slf4j.Logger;
 34  
 import org.slf4j.LoggerFactory;
 35  
 
 36  
 /**
 37  
  * <p>
 38  
  * This analyzer ensures dependencies that should be grouped together, to remove
 39  
  * excess noise from the report, are grouped. An example would be Spring, Spring
 40  
  * Beans, Spring MVC, etc. If they are all for the same version and have the
 41  
  * same relative path then these should be grouped into a single dependency
 42  
  * under the core/main library.</p>
 43  
  * <p>
 44  
  * Note, this grouping only works on dependencies with identified CVE
 45  
  * entries</p>
 46  
  *
 47  
  * @author Jeremy Long
 48  
  */
 49  10
 public class DependencyBundlingAnalyzer extends AbstractAnalyzer implements Analyzer {
 50  
 
 51  
     /**
 52  
      * The Logger.
 53  
      */
 54  1
     private static final Logger LOGGER = LoggerFactory.getLogger(DependencyBundlingAnalyzer.class);
 55  
 
 56  
     //<editor-fold defaultstate="collapsed" desc="Constants and Member Variables">
 57  
     /**
 58  
      * A pattern for obtaining the first part of a filename.
 59  
      */
 60  1
     private static final Pattern STARTING_TEXT_PATTERN = Pattern.compile("^[a-zA-Z0-9]*");
 61  
     /**
 62  
      * a flag indicating if this analyzer has run. This analyzer only runs once.
 63  
      */
 64  10
     private boolean analyzed = false;
 65  
     //</editor-fold>
 66  
     //<editor-fold defaultstate="collapsed" desc="All standard implementation details of Analyzer">
 67  
     /**
 68  
      * The name of the analyzer.
 69  
      */
 70  
     private static final String ANALYZER_NAME = "Dependency Bundling Analyzer";
 71  
     /**
 72  
      * The phase that this analyzer is intended to run in.
 73  
      */
 74  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.PRE_FINDING_ANALYSIS;
 75  
 
 76  
     /**
 77  
      * Returns the name of the analyzer.
 78  
      *
 79  
      * @return the name of the analyzer.
 80  
      */
 81  
     @Override
 82  
     public String getName() {
 83  17
         return ANALYZER_NAME;
 84  
     }
 85  
 
 86  
     /**
 87  
      * Returns the phase that the analyzer is intended to run in.
 88  
      *
 89  
      * @return the phase that the analyzer is intended to run in.
 90  
      */
 91  
     @Override
 92  
     public AnalysisPhase getAnalysisPhase() {
 93  5
         return ANALYSIS_PHASE;
 94  
     }
 95  
     //</editor-fold>
 96  
 
 97  
     /**
 98  
      * Analyzes a set of dependencies. If they have been found to have the same
 99  
      * base path and the same set of identifiers they are likely related. The
 100  
      * related dependencies are bundled into a single reportable item.
 101  
      *
 102  
      * @param ignore this analyzer ignores the dependency being analyzed
 103  
      * @param engine the engine that is scanning the dependencies
 104  
      * @throws AnalysisException is thrown if there is an error reading the JAR
 105  
      * file.
 106  
      */
 107  
     @Override
 108  
     public void analyze(Dependency ignore, Engine engine) throws AnalysisException {
 109  4
         if (!analyzed) {
 110  2
             analyzed = true;
 111  2
             final Set<Dependency> dependenciesToRemove = new HashSet<Dependency>();
 112  2
             final ListIterator<Dependency> mainIterator = engine.getDependencies().listIterator();
 113  
             //for (Dependency nextDependency : engine.getDependencies()) {
 114  6
             while (mainIterator.hasNext()) {
 115  4
                 final Dependency dependency = mainIterator.next();
 116  4
                 if (mainIterator.hasNext() && !dependenciesToRemove.contains(dependency)) {
 117  2
                     final ListIterator<Dependency> subIterator = engine.getDependencies().listIterator(mainIterator.nextIndex());
 118  4
                     while (subIterator.hasNext()) {
 119  2
                         final Dependency nextDependency = subIterator.next();
 120  2
                         Dependency main = null;
 121  2
                         if (hashesMatch(dependency, nextDependency) && !containedInWar(dependency.getFilePath())
 122  0
                                 && !containedInWar(nextDependency.getFilePath())) {
 123  0
                             if (firstPathIsShortest(dependency.getFilePath(), nextDependency.getFilePath())) {
 124  0
                                 mergeDependencies(dependency, nextDependency, dependenciesToRemove);
 125  
                             } else {
 126  0
                                 mergeDependencies(nextDependency, dependency, dependenciesToRemove);
 127  0
                                 break; //since we merged into the next dependency - skip forward to the next in mainIterator
 128  
                             }
 129  2
                         } else if (isShadedJar(dependency, nextDependency)) {
 130  0
                             if (dependency.getFileName().toLowerCase().endsWith("pom.xml")) {
 131  0
                                 mergeDependencies(nextDependency, dependency, dependenciesToRemove);
 132  0
                                 nextDependency.getRelatedDependencies().remove(dependency);
 133  0
                                 break;
 134  
                             } else {
 135  0
                                 mergeDependencies(dependency, nextDependency, dependenciesToRemove);
 136  0
                                 dependency.getRelatedDependencies().remove(nextDependency);
 137  
                             }
 138  2
                         } else if (cpeIdentifiersMatch(dependency, nextDependency)
 139  0
                                 && hasSameBasePath(dependency, nextDependency)
 140  0
                                 && fileNameMatch(dependency, nextDependency)) {
 141  0
                             if (isCore(dependency, nextDependency)) {
 142  0
                                 mergeDependencies(dependency, nextDependency, dependenciesToRemove);
 143  
                             } else {
 144  0
                                 mergeDependencies(nextDependency, dependency, dependenciesToRemove);
 145  0
                                 break; //since we merged into the next dependency - skip forward to the next in mainIterator
 146  
                             }
 147  2
                         } else if ((main = getMainGemspecDependency(dependency, nextDependency)) != null) {
 148  0
                             if (main == dependency) {
 149  0
                                 mergeDependencies(dependency, nextDependency, dependenciesToRemove);
 150  
                             } else {
 151  0
                                 mergeDependencies(nextDependency, dependency, dependenciesToRemove);
 152  0
                                 break; //since we merged into the next dependency - skip forward to the next in mainIterator
 153  
                             }
 154  2
                         } else if ((main = getMainSwiftDependency(dependency, nextDependency)) != null) {
 155  0
                             if (main == dependency) {
 156  0
                                 mergeDependencies(dependency, nextDependency, dependenciesToRemove);
 157  
                             } else {
 158  0
                                 mergeDependencies(nextDependency, dependency, dependenciesToRemove);
 159  0
                                 break; //since we merged into the next dependency - skip forward to the next in mainIterator
 160  
                             }
 161  
                         }
 162  2
                     }
 163  
                 }
 164  4
             }
 165  
             //removing dependencies here as ensuring correctness and avoiding ConcurrentUpdateExceptions
 166  
             // was difficult because of the inner iterator.
 167  2
             engine.getDependencies().removeAll(dependenciesToRemove);
 168  
         }
 169  4
     }
 170  
 
 171  
     /**
 172  
      * Adds the relatedDependency to the dependency's related dependencies.
 173  
      *
 174  
      * @param dependency the main dependency
 175  
      * @param relatedDependency a collection of dependencies to be removed from
 176  
      * the main analysis loop, this is the source of dependencies to remove
 177  
      * @param dependenciesToRemove a collection of dependencies that will be
 178  
      * removed from the main analysis loop, this function adds to this
 179  
      * collection
 180  
      */
 181  
     private void mergeDependencies(final Dependency dependency, final Dependency relatedDependency, final Set<Dependency> dependenciesToRemove) {
 182  0
         dependency.addRelatedDependency(relatedDependency);
 183  0
         final Iterator<Dependency> i = relatedDependency.getRelatedDependencies().iterator();
 184  0
         while (i.hasNext()) {
 185  0
             dependency.addRelatedDependency(i.next());
 186  0
             i.remove();
 187  
         }
 188  0
         if (dependency.getSha1sum().equals(relatedDependency.getSha1sum())) {
 189  0
             dependency.addAllProjectReferences(relatedDependency.getProjectReferences());
 190  
         }
 191  0
         dependenciesToRemove.add(relatedDependency);
 192  0
     }
 193  
 
 194  
     /**
 195  
      * Attempts to trim a maven repo to a common base path. This is typically
 196  
      * [drive]\[repo_location]\repository\[path1]\[path2].
 197  
      *
 198  
      * @param path the path to trim
 199  
      * @return a string representing the base path.
 200  
      */
 201  
     private String getBaseRepoPath(final String path) {
 202  0
         int pos = path.indexOf("repository" + File.separator) + 11;
 203  0
         if (pos < 0) {
 204  0
             return path;
 205  
         }
 206  0
         int tmp = path.indexOf(File.separator, pos);
 207  0
         if (tmp <= 0) {
 208  0
             return path;
 209  
         }
 210  0
         if (tmp > 0) {
 211  0
             pos = tmp + 1;
 212  
         }
 213  0
         tmp = path.indexOf(File.separator, pos);
 214  0
         if (tmp > 0) {
 215  0
             pos = tmp + 1;
 216  
         }
 217  0
         return path.substring(0, pos);
 218  
     }
 219  
 
 220  
     /**
 221  
      * Returns true if the file names (and version if it exists) of the two
 222  
      * dependencies are sufficiently similar.
 223  
      *
 224  
      * @param dependency1 a dependency2 to compare
 225  
      * @param dependency2 a dependency2 to compare
 226  
      * @return true if the identifiers in the two supplied dependencies are
 227  
      * equal
 228  
      */
 229  
     private boolean fileNameMatch(Dependency dependency1, Dependency dependency2) {
 230  0
         if (dependency1 == null || dependency1.getFileName() == null
 231  0
                 || dependency2 == null || dependency2.getFileName() == null) {
 232  0
             return false;
 233  
         }
 234  0
         final String fileName1 = dependency1.getActualFile().getName();
 235  0
         final String fileName2 = dependency2.getActualFile().getName();
 236  
 
 237  
         //version check
 238  0
         final DependencyVersion version1 = DependencyVersionUtil.parseVersion(fileName1);
 239  0
         final DependencyVersion version2 = DependencyVersionUtil.parseVersion(fileName2);
 240  0
         if (version1 != null && version2 != null && !version1.equals(version2)) {
 241  0
             return false;
 242  
         }
 243  
 
 244  
         //filename check
 245  0
         final Matcher match1 = STARTING_TEXT_PATTERN.matcher(fileName1);
 246  0
         final Matcher match2 = STARTING_TEXT_PATTERN.matcher(fileName2);
 247  0
         if (match1.find() && match2.find()) {
 248  0
             return match1.group().equals(match2.group());
 249  
         }
 250  
 
 251  0
         return false;
 252  
     }
 253  
 
 254  
     /**
 255  
      * Returns true if the CPE identifiers in the two supplied dependencies are
 256  
      * equal.
 257  
      *
 258  
      * @param dependency1 a dependency2 to compare
 259  
      * @param dependency2 a dependency2 to compare
 260  
      * @return true if the identifiers in the two supplied dependencies are
 261  
      * equal
 262  
      */
 263  
     private boolean cpeIdentifiersMatch(Dependency dependency1, Dependency dependency2) {
 264  2
         if (dependency1 == null || dependency1.getIdentifiers() == null
 265  2
                 || dependency2 == null || dependency2.getIdentifiers() == null) {
 266  0
             return false;
 267  
         }
 268  2
         boolean matches = false;
 269  2
         int cpeCount1 = 0;
 270  2
         int cpeCount2 = 0;
 271  2
         for (Identifier i : dependency1.getIdentifiers()) {
 272  0
             if ("cpe".equals(i.getType())) {
 273  0
                 cpeCount1 += 1;
 274  
             }
 275  0
         }
 276  2
         for (Identifier i : dependency2.getIdentifiers()) {
 277  3
             if ("cpe".equals(i.getType())) {
 278  3
                 cpeCount2 += 1;
 279  
             }
 280  3
         }
 281  2
         if (cpeCount1 > 0 && cpeCount1 == cpeCount2) {
 282  0
             for (Identifier i : dependency1.getIdentifiers()) {
 283  0
                 if ("cpe".equals(i.getType())) {
 284  0
                     matches |= dependency2.getIdentifiers().contains(i);
 285  0
                     if (!matches) {
 286  0
                         break;
 287  
                     }
 288  
                 }
 289  0
             }
 290  
         }
 291  2
         LOGGER.debug("IdentifiersMatch={} ({}, {})", matches, dependency1.getFileName(), dependency2.getFileName());
 292  2
         return matches;
 293  
     }
 294  
 
 295  
     /**
 296  
      * Determines if the two dependencies have the same base path.
 297  
      *
 298  
      * @param dependency1 a Dependency object
 299  
      * @param dependency2 a Dependency object
 300  
      * @return true if the base paths of the dependencies are identical
 301  
      */
 302  
     private boolean hasSameBasePath(Dependency dependency1, Dependency dependency2) {
 303  0
         if (dependency1 == null || dependency2 == null) {
 304  0
             return false;
 305  
         }
 306  0
         final File lFile = new File(dependency1.getFilePath());
 307  0
         String left = lFile.getParent();
 308  0
         final File rFile = new File(dependency2.getFilePath());
 309  0
         String right = rFile.getParent();
 310  0
         if (left == null) {
 311  0
             return right == null;
 312  0
         } else if (right == null) {
 313  0
             return false;
 314  
         }
 315  0
         if (left.equalsIgnoreCase(right)) {
 316  0
             return true;
 317  
         }
 318  
 
 319  0
         if (left.matches(".*[/\\\\]repository[/\\\\].*") && right.matches(".*[/\\\\]repository[/\\\\].*")) {
 320  0
             left = getBaseRepoPath(left);
 321  0
             right = getBaseRepoPath(right);
 322  
         }
 323  0
         if (left.equalsIgnoreCase(right)) {
 324  0
             return true;
 325  
         }
 326  
         //new code
 327  0
         for (Dependency child : dependency2.getRelatedDependencies()) {
 328  0
             if (hasSameBasePath(dependency1, child)) {
 329  0
                 return true;
 330  
             }
 331  0
         }
 332  0
         return false;
 333  
     }
 334  
 
 335  
     /**
 336  
      * Bundling Ruby gems that are identified from different .gemspec files but
 337  
      * denote the same package path. This happens when Ruby bundler installs an
 338  
      * application's dependencies by running "bundle install".
 339  
      *
 340  
      * @param dependency1 dependency to compare
 341  
      * @param dependency2 dependency to compare
 342  
      * @return true if the the dependencies being analyzed appear to be the
 343  
      * same; otherwise false
 344  
      */
 345  
     private boolean isSameRubyGem(Dependency dependency1, Dependency dependency2) {
 346  2
         if (dependency1 == null || dependency2 == null
 347  2
                 || !dependency1.getFileName().endsWith(".gemspec")
 348  0
                 || !dependency2.getFileName().endsWith(".gemspec")
 349  0
                 || dependency1.getPackagePath() == null
 350  0
                 || dependency2.getPackagePath() == null) {
 351  2
             return false;
 352  
         }
 353  0
         if (dependency1.getPackagePath().equalsIgnoreCase(dependency2.getPackagePath())) {
 354  0
             return true;
 355  
         }
 356  
 
 357  0
         return false;
 358  
     }
 359  
 
 360  
     /**
 361  
      * Ruby gems installed by "bundle install" can have zero or more *.gemspec
 362  
      * files, all of which have the same packagePath and should be grouped. If
 363  
      * one of these gemspec is from <parent>/specifications/*.gemspec, because
 364  
      * it is a stub with fully resolved gem meta-data created by Ruby bundler,
 365  
      * this dependency should be the main one. Otherwise, use dependency2 as
 366  
      * main.
 367  
      *
 368  
      * This method returns null if any dependency is not from *.gemspec, or the
 369  
      * two do not have the same packagePath. In this case, they should not be
 370  
      * grouped.
 371  
      *
 372  
      * @param dependency1 dependency to compare
 373  
      * @param dependency2 dependency to compare
 374  
      * @return the main dependency; or null if a gemspec is not included in the
 375  
      * analysis
 376  
      */
 377  
     private Dependency getMainGemspecDependency(Dependency dependency1, Dependency dependency2) {
 378  2
         if (isSameRubyGem(dependency1, dependency2)) {
 379  0
             final File lFile = dependency1.getActualFile();
 380  0
             final File left = lFile.getParentFile();
 381  0
             if (left != null && left.getName().equalsIgnoreCase("specifications")) {
 382  0
                 return dependency1;
 383  
             }
 384  0
             return dependency2;
 385  
         }
 386  2
         return null;
 387  
     }
 388  
 
 389  
     /**
 390  
      * Bundling same swift dependencies with the same packagePath but identified
 391  
      * by different analyzers.
 392  
      *
 393  
      * @param dependency1 dependency to test
 394  
      * @param dependency2 dependency to test
 395  
      * @return <code>true</code> if the dependencies appear to be the same;
 396  
      * otherwise <code>false</code>
 397  
      */
 398  
     private boolean isSameSwiftPackage(Dependency dependency1, Dependency dependency2) {
 399  2
         if (dependency1 == null || dependency2 == null
 400  2
                 || (!dependency1.getFileName().endsWith(".podspec")
 401  2
                 && !dependency1.getFileName().equals("Package.swift"))
 402  0
                 || (!dependency2.getFileName().endsWith(".podspec")
 403  0
                 && !dependency2.getFileName().equals("Package.swift"))
 404  0
                 || dependency1.getPackagePath() == null
 405  0
                 || dependency2.getPackagePath() == null) {
 406  2
             return false;
 407  
         }
 408  0
         if (dependency1.getPackagePath().equalsIgnoreCase(dependency2.getPackagePath())) {
 409  0
             return true;
 410  
         }
 411  0
         return false;
 412  
     }
 413  
 
 414  
     /**
 415  
      * Determines which of the swift dependencies should be considered the
 416  
      * primary.
 417  
      *
 418  
      * @param dependency1 the first swift dependency to compare
 419  
      * @param dependency2 the second swift dependency to compare
 420  
      * @return the primary swift dependency
 421  
      */
 422  
     private Dependency getMainSwiftDependency(Dependency dependency1, Dependency dependency2) {
 423  2
         if (isSameSwiftPackage(dependency1, dependency2)) {
 424  0
             if (dependency1.getFileName().endsWith(".podspec")) {
 425  0
                 return dependency1;
 426  
             }
 427  0
             return dependency2;
 428  
         }
 429  2
         return null;
 430  
     }
 431  
 
 432  
     /**
 433  
      * This is likely a very broken attempt at determining if the 'left'
 434  
      * dependency is the 'core' library in comparison to the 'right' library.
 435  
      *
 436  
      * @param left the dependency to test
 437  
      * @param right the dependency to test against
 438  
      * @return a boolean indicating whether or not the left dependency should be
 439  
      * considered the "core" version.
 440  
      */
 441  
     boolean isCore(Dependency left, Dependency right) {
 442  2
         final String leftName = left.getFileName().toLowerCase();
 443  2
         final String rightName = right.getFileName().toLowerCase();
 444  
 
 445  
         final boolean returnVal;
 446  2
         if (!rightName.matches(".*\\.(tar|tgz|gz|zip|ear|war).+") && leftName.matches(".*\\.(tar|tgz|gz|zip|ear|war).+")
 447  2
                 || rightName.contains("core") && !leftName.contains("core")
 448  2
                 || rightName.contains("kernel") && !leftName.contains("kernel")) {
 449  0
             returnVal = false;
 450  2
         } else if (rightName.matches(".*\\.(tar|tgz|gz|zip|ear|war).+") && !leftName.matches(".*\\.(tar|tgz|gz|zip|ear|war).+")
 451  1
                 || !rightName.contains("core") && leftName.contains("core")
 452  1
                 || !rightName.contains("kernel") && leftName.contains("kernel")) {
 453  2
             returnVal = true;
 454  
 //        } else if (leftName.matches(".*struts2\\-core.*") && rightName.matches(".*xwork\\-core.*")) {
 455  
 //            returnVal = true;
 456  
 //        } else if (rightName.matches(".*struts2\\-core.*") && leftName.matches(".*xwork\\-core.*")) {
 457  
 //            returnVal = false;
 458  
         } else {
 459  
             /*
 460  
              * considered splitting the names up and comparing the components,
 461  
              * but decided that the file name length should be sufficient as the
 462  
              * "core" component, if this follows a normal naming protocol should
 463  
              * be shorter:
 464  
              * axis2-saaj-1.4.1.jar
 465  
              * axis2-1.4.1.jar       <-----
 466  
              * axis2-kernel-1.4.1.jar
 467  
              */
 468  0
             returnVal = leftName.length() <= rightName.length();
 469  
         }
 470  2
         LOGGER.debug("IsCore={} ({}, {})", returnVal, left.getFileName(), right.getFileName());
 471  2
         return returnVal;
 472  
     }
 473  
 
 474  
     /**
 475  
      * Compares the SHA1 hashes of two dependencies to determine if they are
 476  
      * equal.
 477  
      *
 478  
      * @param dependency1 a dependency object to compare
 479  
      * @param dependency2 a dependency object to compare
 480  
      * @return true if the sha1 hashes of the two dependencies match; otherwise
 481  
      * false
 482  
      */
 483  
     private boolean hashesMatch(Dependency dependency1, Dependency dependency2) {
 484  2
         if (dependency1 == null || dependency2 == null || dependency1.getSha1sum() == null || dependency2.getSha1sum() == null) {
 485  0
             return false;
 486  
         }
 487  2
         return dependency1.getSha1sum().equals(dependency2.getSha1sum());
 488  
     }
 489  
 
 490  
     /**
 491  
      * Determines if the jar is shaded and the created pom.xml identified the
 492  
      * same CPE as the jar - if so, the pom.xml dependency should be removed.
 493  
      *
 494  
      * @param dependency a dependency to check
 495  
      * @param nextDependency another dependency to check
 496  
      * @return true if on of the dependencies is a pom.xml and the identifiers
 497  
      * between the two collections match; otherwise false
 498  
      */
 499  
     private boolean isShadedJar(Dependency dependency, Dependency nextDependency) {
 500  2
         final String mainName = dependency.getFileName().toLowerCase();
 501  2
         final String nextName = nextDependency.getFileName().toLowerCase();
 502  2
         if (mainName.endsWith(".jar") && nextName.endsWith("pom.xml")) {
 503  0
             return dependency.getIdentifiers().containsAll(nextDependency.getIdentifiers());
 504  2
         } else if (nextName.endsWith(".jar") && mainName.endsWith("pom.xml")) {
 505  0
             return nextDependency.getIdentifiers().containsAll(dependency.getIdentifiers());
 506  
         }
 507  2
         return false;
 508  
     }
 509  
 
 510  
     /**
 511  
      * Determines which path is shortest; if path lengths are equal then we use
 512  
      * compareTo of the string method to determine if the first path is smaller.
 513  
      *
 514  
      * @param left the first path to compare
 515  
      * @param right the second path to compare
 516  
      * @return <code>true</code> if the leftPath is the shortest; otherwise
 517  
      * <code>false</code>
 518  
      */
 519  
     protected boolean firstPathIsShortest(String left, String right) {
 520  5
         final String leftPath = left.replace('\\', '/');
 521  5
         final String rightPath = right.replace('\\', '/');
 522  
 
 523  5
         final int leftCount = countChar(leftPath, '/');
 524  5
         final int rightCount = countChar(rightPath, '/');
 525  5
         if (leftCount == rightCount) {
 526  3
             return leftPath.compareTo(rightPath) <= 0;
 527  
         } else {
 528  2
             return leftCount < rightCount;
 529  
         }
 530  
     }
 531  
 
 532  
     /**
 533  
      * Counts the number of times the character is present in the string.
 534  
      *
 535  
      * @param string the string to count the characters in
 536  
      * @param c the character to count
 537  
      * @return the number of times the character is present in the string
 538  
      */
 539  
     private int countChar(String string, char c) {
 540  10
         int count = 0;
 541  10
         final int max = string.length();
 542  116
         for (int i = 0; i < max; i++) {
 543  106
             if (c == string.charAt(i)) {
 544  28
                 count++;
 545  
             }
 546  
         }
 547  10
         return count;
 548  
     }
 549  
 
 550  
     /**
 551  
      * Checks if the given file path is contained within a war or ear file.
 552  
      *
 553  
      * @param filePath the file path to check
 554  
      * @return true if the path contains '.war\' or '.ear\'.
 555  
      */
 556  
     private boolean containedInWar(String filePath) {
 557  0
         return filePath == null ? false : filePath.matches(".*\\.(ear|war)[\\\\/].*");
 558  
     }
 559  
 }