Coverage Report - org.apache.maven.archetype.ui.generation.ArchetypeSelectorUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ArchetypeSelectorUtils
84 %
16/19
60 %
12/20
3,75
 
 1  
 package org.apache.maven.archetype.ui.generation;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.commons.lang.StringUtils;
 23  
 import org.apache.maven.archetype.catalog.Archetype;
 24  
 
 25  
 import java.util.ArrayList;
 26  
 import java.util.Collections;
 27  
 import java.util.LinkedHashMap;
 28  
 import java.util.List;
 29  
 import java.util.Map;
 30  
 
 31  
 /**
 32  
  * @author Olivier Lamy
 33  
  * @since 2.1
 34  
  */
 35  
 public class ArchetypeSelectorUtils
 36  
 {
 37  
     private ArchetypeSelectorUtils()
 38  0
     {
 39  
         // no constructor for utility class
 40  0
     }
 41  
 
 42  
     private static String extractGroupIdFromFilter( String filter )
 43  
     {
 44  2
         return StringUtils.contains( filter, ':' ) ? StringUtils.substringBefore( filter, ":" ) : null;
 45  
     }
 46  
 
 47  
     private static String extractArtifactIdFromFilter( String filter )
 48  
     {
 49  
         // if no : the full text is considered as artifactId content
 50  2
         return StringUtils.contains( filter, ':' ) ? StringUtils.substringAfter( filter, ":" ) : filter;
 51  
     }
 52  
 
 53  
     /**
 54  
      * apply some filtering on archetypes.
 55  
      * currently only on artifactId contains filter
 56  
      *
 57  
      * @param archetypesPerCatalog
 58  
      * @return
 59  
      */
 60  
     public static Map<String, List<Archetype>> getFilteredArchetypesByCatalog(
 61  
         Map<String, List<Archetype>> archetypesPerCatalog, String filter )
 62  
     {
 63  1
         if ( archetypesPerCatalog == null || archetypesPerCatalog.isEmpty() )
 64  
         {
 65  0
             return Collections.emptyMap();
 66  
         }
 67  1
         Map<String, List<Archetype>> filtered =
 68  
             new LinkedHashMap<String, List<Archetype>>( archetypesPerCatalog.size() );
 69  
 
 70  1
         for ( Map.Entry<String, List<Archetype>> entry : archetypesPerCatalog.entrySet() )
 71  
         {
 72  1
             List<Archetype> archetypes = new ArrayList<Archetype>();
 73  
 
 74  1
             for ( Archetype archetype : entry.getValue() )
 75  
             {
 76  2
                 String groupId = ArchetypeSelectorUtils.extractGroupIdFromFilter( filter );
 77  2
                 String artifactId = ArchetypeSelectorUtils.extractArtifactIdFromFilter( filter );
 78  
 
 79  2
                 if ( ( ( groupId == null ) || StringUtils.contains( archetype.getGroupId(), groupId ) )
 80  
                     && StringUtils.contains( archetype.getArtifactId(), artifactId ) )
 81  
                 {
 82  1
                     archetypes.add( archetype );
 83  
                 }
 84  2
             }
 85  
 
 86  1
             if ( !archetypes.isEmpty() )
 87  
             {
 88  1
                 filtered.put( entry.getKey(), archetypes );
 89  
             }
 90  1
         }
 91  
 
 92  1
         return filtered;
 93  
     }
 94  
 
 95  
 }