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