Coverage Report - org.apache.maven.archetype.ui.DefaultArchetypeSelectionQueryer
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArchetypeSelectionQueryer
61%
28/46
79%
11/14
2
 
 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  
 
 20  
 package org.apache.maven.archetype.ui;
 21  
 
 22  
 //import org.apache.maven.archetype.common.Archetype;
 23  
 import org.apache.maven.archetype.catalog.Archetype;
 24  
 import org.apache.maven.archetype.common.ArchetypeDefinition;
 25  
 import org.codehaus.plexus.components.interactivity.Prompter;
 26  
 import org.codehaus.plexus.components.interactivity.PrompterException;
 27  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 28  
 
 29  
 import java.util.ArrayList;
 30  
 import java.util.Arrays;
 31  
 import java.util.HashMap;
 32  
 import java.util.Iterator;
 33  
 import java.util.List;
 34  
 import java.util.Map;
 35  
 
 36  
 /** @plexus.component */
 37  27
 public class DefaultArchetypeSelectionQueryer
 38  
     extends AbstractLogEnabled
 39  
     implements ArchetypeSelectionQueryer
 40  
 {
 41  
     /** @plexus.requirement */
 42  
     private Prompter prompter;
 43  
 
 44  
     public boolean confirmSelection( ArchetypeDefinition archetypeDefinition )
 45  
         throws
 46  
         PrompterException
 47  
     {
 48  0
         String query =
 49  
             "Confirm archetype selection: \n" + archetypeDefinition.getGroupId() + "/"
 50  
                 + archetypeDefinition.getName() + "\n";
 51  
 
 52  0
         String answer = prompter.prompt( query, Arrays.asList( new String[]{"Y", "N"} ), "Y" );
 53  
 
 54  0
         return "Y".equalsIgnoreCase( answer );
 55  
     }
 56  
 
 57  
     public Archetype selectArchetype( List archetypes )
 58  
         throws PrompterException
 59  
     {
 60  0
         String query = "Choose archetype:\n";
 61  0
         Map answerMap = new HashMap();
 62  0
         List answers = new ArrayList();
 63  0
         Iterator archetypeIterator = archetypes.iterator();
 64  0
         int counter = 1;
 65  0
         while ( archetypeIterator.hasNext() )
 66  
         {
 67  0
             org.apache.maven.archetype.catalog.Archetype archetype = (org.apache.maven.archetype.catalog.Archetype) archetypeIterator.next();
 68  
 
 69  0
             answerMap.put( "" + counter, archetype );
 70  0
             query +=
 71  
                 "" + counter + ": " + archetype.getArtifactId() + " (" + archetype.getDescription() + ":"
 72  
                     + archetype.getArtifactId() + ")\n";
 73  0
             answers.add( "" + counter );
 74  
 
 75  0
             counter++;
 76  0
         }
 77  0
         query += "Choose a number: ";
 78  
 
 79  0
         String answer = prompter.prompt( query, answers );
 80  
 
 81  0
         return (org.apache.maven.archetype.catalog.Archetype) answerMap.get( answer );
 82  
     }
 83  
 
 84  
     public Archetype selectArchetype( Map catalogs )
 85  
         throws PrompterException
 86  
     {
 87  3
         return selectArchetype( catalogs, null );
 88  
     }
 89  
 
 90  
     public Archetype selectArchetype( Map catalogs, ArchetypeDefinition defaultDefinition )
 91  
         throws PrompterException
 92  
     {
 93  12
         String query = "Choose archetype:\n";
 94  12
         Map answerMap = new HashMap();
 95  12
         List answers = new ArrayList();
 96  
 
 97  12
         Iterator catalogIterator = catalogs.keySet().iterator();
 98  12
         int counter = 1;
 99  12
         int defaultSelection = 0;
 100  24
         while ( catalogIterator.hasNext() )
 101  
         {
 102  12
             String catalog = (String) catalogIterator.next();
 103  
 
 104  12
             Iterator archetypeIterator = ((List) catalogs.get( catalog )).iterator();
 105  36
             while ( archetypeIterator.hasNext() )
 106  
             {
 107  24
                 org.apache.maven.archetype.catalog.Archetype archetype = (org.apache.maven.archetype.catalog.Archetype) archetypeIterator.next();
 108  
 
 109  24
                 answerMap.put( "" + counter, archetype );
 110  24
                 query +=
 111  
                     "" + counter + ": " + catalog +
 112  
                     " -> " + archetype.getArtifactId() + " (" + archetype.getDescription() + ")\n";
 113  24
                 answers.add( "" + counter );
 114  
 
 115  
                 // the version is not tested. This is intentional.
 116  24
                 if ( defaultDefinition != null && archetype.getGroupId().equals( defaultDefinition.getGroupId() ) &&
 117  
                     archetype.getArtifactId().equals( defaultDefinition.getArtifactId() ) )
 118  
                 {
 119  6
                     defaultSelection = counter;
 120  
                 }
 121  
 
 122  24
                 counter++;
 123  24
             }
 124  
 
 125  12
         }
 126  
 
 127  12
         query += "Choose a number: ";
 128  
 
 129  
         String answer;
 130  12
         if ( defaultSelection == 0 )
 131  
         {
 132  6
             answer = prompter.prompt( query, answers );
 133  
         }
 134  
         else
 135  
         {
 136  6
             answer = prompter.prompt( query, answers, Integer.toString( defaultSelection ) );
 137  
         }
 138  
 
 139  12
         return (org.apache.maven.archetype.catalog.Archetype) answerMap.get( answer );
 140  
     }
 141  
 
 142  
 //
 143  
 //    public String selectGroup( List groups )
 144  
 //        throws
 145  
 //        PrompterException
 146  
 //    {
 147  
 //        String query = "Choose group:\n";
 148  
 //        Map answerMap = new HashMap();
 149  
 //        List answers = new ArrayList();
 150  
 //        Iterator groupIterator = groups.iterator();
 151  
 //        int counter = 1;
 152  
 //        while ( groupIterator.hasNext() )
 153  
 //        {
 154  
 //            String group = (String) groupIterator.next();
 155  
 //
 156  
 //            answerMap.put( "" + counter, group );
 157  
 //            query += "" + counter + ": " + group + "\n";
 158  
 //            answers.add( "" + counter );
 159  
 //
 160  
 //            counter++;
 161  
 //        }
 162  
 //        query += "Choose a number: ";
 163  
 //
 164  
 //        String answer = prompter.prompt( query, answers );
 165  
 //
 166  
 //        return (String) answerMap.get( answer );
 167  
 //    }
 168  
 //
 169  
 //    public Archetype selectArtifact( List archetypes )
 170  
 //        throws
 171  
 //        PrompterException
 172  
 //    {
 173  
 //        String query = "Choose archetype:\n";
 174  
 //        Map answerMap = new HashMap();
 175  
 //        List answers = new ArrayList();
 176  
 //        Iterator archetypeIterator = archetypes.iterator();
 177  
 //        int counter = 1;
 178  
 //        while ( archetypeIterator.hasNext() )
 179  
 //        {
 180  
 //            Archetype archetype = (Archetype) archetypeIterator.next();
 181  
 //
 182  
 //            answerMap.put( "" + counter, archetype );
 183  
 //            query +=
 184  
 //                "" + counter + ": " + archetype.getName() + " (" + archetype.getGroupId() + ":"
 185  
 //                    + archetype.getArtifactId() + ")\n";
 186  
 //            answers.add( "" + counter );
 187  
 //
 188  
 //            counter++;
 189  
 //        }
 190  
 //        query += "Choose a number: ";
 191  
 //
 192  
 //        String answer = prompter.prompt( query, answers );
 193  
 //
 194  
 //        return (Archetype) answerMap.get( answer );
 195  
 //    }
 196  
 //
 197  
 //    public String selectVersion( List archetypeVersions )
 198  
 //        throws
 199  
 //        PrompterException
 200  
 //    {
 201  
 //        String query = "Choose version: \n";
 202  
 //        Map answerMap = new HashMap();
 203  
 //        List answers = new ArrayList();
 204  
 //
 205  
 //        Iterator archetypeVersionsKeys = archetypeVersions.iterator();
 206  
 //        int counter = 1;
 207  
 //        while ( archetypeVersionsKeys.hasNext() )
 208  
 //        {
 209  
 //            String archetypeVersion = (String) archetypeVersionsKeys.next();
 210  
 //
 211  
 //            answerMap.put( "" + counter, archetypeVersion );
 212  
 //            query += "" + counter + ": " + archetypeVersion + "\n";
 213  
 //            answers.add( "" + counter );
 214  
 //
 215  
 //            counter++;
 216  
 //        }
 217  
 //        query += "Choose a number: ";
 218  
 //
 219  
 //        String answer = prompter.prompt( query, answers );
 220  
 //
 221  
 //        return (String) answerMap.get( answer );
 222  
 //    }
 223  
 
 224  
     public void setPrompter( Prompter prompter )
 225  
     {
 226  12
         this.prompter = prompter;
 227  12
     }
 228  
 }