Coverage Report - org.apache.maven.archetype.ui.ArchetypePrompter
 
Classes in this File Line Coverage Branch Coverage Complexity
ArchetypePrompter
2%
1/40
0%
0/14
2.889
 
 1  
 package org.apache.maven.archetype.ui;
 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.codehaus.plexus.component.annotations.Component;
 23  
 import org.codehaus.plexus.component.annotations.Requirement;
 24  
 import org.codehaus.plexus.components.interactivity.InputHandler;
 25  
 import org.codehaus.plexus.components.interactivity.OutputHandler;
 26  
 import org.codehaus.plexus.components.interactivity.Prompter;
 27  
 import org.codehaus.plexus.components.interactivity.PrompterException;
 28  
 import org.codehaus.plexus.util.StringUtils;
 29  
 
 30  
 import java.io.IOException;
 31  
 import java.util.List;
 32  
 
 33  
 /**
 34  
  * @author raphaelpieroni
 35  
  */
 36  
 @Component( role = Prompter.class, hint = "archetype" )
 37  26
 public class ArchetypePrompter
 38  
     implements Prompter
 39  
 {
 40  
 
 41  
     @Requirement
 42  
     private OutputHandler outputHandler;
 43  
 
 44  
     @Requirement
 45  
     private InputHandler inputHandler;
 46  
 
 47  
     public String prompt( String message )
 48  
         throws PrompterException
 49  
     {
 50  0
         writePrompt( message );
 51  
 
 52  0
         return readLine();
 53  
     }
 54  
 
 55  
     public String prompt( String message, String defaultReply )
 56  
         throws PrompterException
 57  
     {
 58  0
         writePrompt( formatMessage( message, null, defaultReply ) );
 59  
 
 60  0
         String line = readLine();
 61  
 
 62  0
         if ( StringUtils.isEmpty( line ) )
 63  
         {
 64  0
             line = defaultReply;
 65  
         }
 66  
 
 67  0
         return line;
 68  
     }
 69  
 
 70  
     @SuppressWarnings( { "rawtypes", "unchecked" })
 71  
     public String prompt( String message, List possibleValues, String defaultReply )
 72  
         throws PrompterException
 73  
     {
 74  0
         String formattedMessage = formatMessage( message, possibleValues, defaultReply );
 75  
 
 76  
         String line;
 77  
 
 78  
         do
 79  
         {
 80  0
             writePrompt( formattedMessage );
 81  
 
 82  0
             line = readLine();
 83  
 
 84  0
             if ( StringUtils.isEmpty( line ) )
 85  
             {
 86  0
                 line = defaultReply;
 87  
             }
 88  
 
 89  0
             if ( line != null && !possibleValues.contains( line ) )
 90  
             {
 91  
                 try
 92  
                 {
 93  0
                     outputHandler.writeLine( "Invalid selection." );
 94  
                 }
 95  0
                 catch ( IOException e )
 96  
                 {
 97  0
                     throw new PrompterException( "Failed to present feedback", e );
 98  0
                 }
 99  
             }
 100  
         }
 101  0
         while ( line == null || !possibleValues.contains( line ) );
 102  
 
 103  0
         return line;
 104  
     }
 105  
 
 106  
     @SuppressWarnings( "rawtypes" )
 107  
     public String prompt( String message, List possibleValues )
 108  
         throws PrompterException
 109  
     {
 110  0
         return prompt( message, possibleValues, null );
 111  
     }
 112  
 
 113  
     public String promptForPassword( String message )
 114  
         throws PrompterException
 115  
     {
 116  0
         writePrompt( message );
 117  
 
 118  
         try
 119  
         {
 120  0
             return inputHandler.readPassword();
 121  
         }
 122  0
         catch ( IOException e )
 123  
         {
 124  0
             throw new PrompterException( "Failed to read user response", e );
 125  
         }
 126  
     }
 127  
 
 128  
     private String formatMessage( String message, List<String> possibleValues, String defaultReply )
 129  
     {
 130  0
         StringBuffer formatted = new StringBuffer( message.length() * 2 );
 131  
 
 132  0
         formatted.append( message );
 133  
         /*
 134  
          * if ( possibleValues != null && !possibleValues.isEmpty() ) { formatted.append( " (" ); for ( Iterator it =
 135  
          * possibleValues.iterator(); it.hasNext(); ) { String possibleValue = (String) it.next(); formatted.append(
 136  
          * possibleValue ); if ( it.hasNext() ) { formatted.append( '/' ); } } formatted.append( ')' ); }
 137  
          */
 138  
 
 139  0
         if ( defaultReply != null )
 140  
         {
 141  0
             formatted.append( defaultReply );
 142  
         }
 143  
 
 144  0
         return formatted.toString();
 145  
     }
 146  
 
 147  
     private void writePrompt( String message )
 148  
         throws PrompterException
 149  
     {
 150  0
         showMessage( message + ": " );
 151  0
     }
 152  
 
 153  
     private String readLine()
 154  
         throws PrompterException
 155  
     {
 156  
         try
 157  
         {
 158  0
             return inputHandler.readLine();
 159  
         }
 160  0
         catch ( IOException e )
 161  
         {
 162  0
             throw new PrompterException( "Failed to read user response", e );
 163  
         }
 164  
     }
 165  
 
 166  
     public void showMessage( String message )
 167  
         throws PrompterException
 168  
     {
 169  
         try
 170  
         {
 171  0
             outputHandler.write( message );
 172  
         }
 173  0
         catch ( IOException e )
 174  
         {
 175  0
             throw new PrompterException( "Failed to show message", e );
 176  0
         }
 177  0
     }
 178  
 
 179  
 }