Author: jdcasey Date: Tue May 31 16:03:59 2011 New Revision: 1129770 URL: http://svn.apache.org/viewvc?rev=1129770&view=rev Log: remove dependency on plexus-interactivity-api, and refactor mae-prompter-cli to stand on its own.
Added: maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompt.java (contents, props changed) - copied, changed from r1128363, maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompter.java maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/Prompt.java (with props) maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/PromptException.java (with props) Removed: maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompter.java Modified: maven/sandbox/trunk/mae/mae-api/pom.xml maven/sandbox/trunk/mae/mae-prompter-cli/pom.xml Modified: maven/sandbox/trunk/mae/mae-api/pom.xml URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/mae/mae-api/pom.xml?rev=1129770&r1=1129769&r2=1129770&view=diff ============================================================================== --- maven/sandbox/trunk/mae/mae-api/pom.xml (original) +++ maven/sandbox/trunk/mae/mae-api/pom.xml Tue May 31 16:03:59 2011 @@ -69,17 +69,6 @@ <artifactId>maven-core</artifactId> </dependency> <dependency> - <groupId>org.codehaus.plexus</groupId> - <artifactId>plexus-interactivity-api</artifactId> - <version>1.0-alpha-6</version> - <exclusions> - <exclusion> - <groupId>org.codehaus.plexus</groupId> - <artifactId>plexus-component-api</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-component-annotations</artifactId> </dependency> Modified: maven/sandbox/trunk/mae/mae-prompter-cli/pom.xml URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/mae/mae-prompter-cli/pom.xml?rev=1129770&r1=1129769&r2=1129770&view=diff ============================================================================== --- maven/sandbox/trunk/mae/mae-prompter-cli/pom.xml (original) +++ maven/sandbox/trunk/mae/mae-prompter-cli/pom.xml Tue May 31 16:03:59 2011 @@ -28,6 +28,11 @@ <artifactId>mae-api</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.5</version> + </dependency> </dependencies> <build> Copied: maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompt.java (from r1128363, maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompter.java) URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompt.java?p2=maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompt.java&p1=maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompter.java&r1=1128363&r2=1129770&rev=1129770&view=diff ============================================================================== --- maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompter.java (original) +++ maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompt.java Tue May 31 16:03:59 2011 @@ -19,12 +19,10 @@ package org.apache.maven.mae.prompt; +import org.apache.commons.lang.StringUtils; import org.apache.maven.mae.conf.MAEConfiguration; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; -import org.codehaus.plexus.components.interactivity.Prompter; -import org.codehaus.plexus.components.interactivity.PrompterException; -import org.codehaus.plexus.util.StringUtils; import javax.inject.Inject; @@ -32,14 +30,13 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; -import java.util.Iterator; import java.util.List; import jline.ConsoleReader; -@Component( role = Prompter.class, hint = MAEPrompter.NAME ) -public class MAEPrompter - implements Prompter +@Component( role = Prompt.class, hint = MAEPrompt.NAME ) +public class MAEPrompt + implements Prompt { public static final String NAME = "mae"; @@ -48,13 +45,13 @@ public class MAEPrompter private final MAEConfiguration config; @Inject - public MAEPrompter( final MAEConfiguration config ) + public MAEPrompt( final MAEConfiguration config ) { this.config = config; } - public String prompt( final String message ) - throws PrompterException + public String getInput( final String message ) + throws PromptException { try { @@ -62,7 +59,7 @@ public class MAEPrompter } catch ( final IOException e ) { - throw new PrompterException( "Failed to present prompt", e ); + throw new PromptException( "Failed to present prompt", e ); } try @@ -71,7 +68,7 @@ public class MAEPrompter } catch ( final IOException e ) { - throw new PrompterException( "Failed to read user response", e ); + throw new PromptException( "Failed to read user response", e ); } } @@ -81,23 +78,23 @@ public class MAEPrompter return new BufferedReader( new InputStreamReader( config.getStandardIn() ) ).readLine(); } - public String prompt( final String message, final String defaultReply ) - throws PrompterException + public String getInput( final String message, final String defaultReply ) + throws PromptException { try { - writePrompt( formatMessage( message, null, defaultReply ) ); + writePrompt( formatMessage( message, defaultReply ) ); } catch ( final IOException e ) { - throw new PrompterException( "Failed to present prompt", e ); + throw new PromptException( "Failed to present prompt", e ); } try { String line = readLine(); - if ( StringUtils.isEmpty( line ) ) + if ( isEmpty( line ) ) { line = defaultReply; } @@ -106,18 +103,17 @@ public class MAEPrompter } catch ( final IOException e ) { - throw new PrompterException( "Failed to read user response", e ); + throw new PromptException( "Failed to read user response", e ); } } - @SuppressWarnings( "rawtypes" ) - public String prompt( final String message, final List possibleValues, final String defaultReply ) - throws PrompterException + public int getSelection( final String message, final List<?> possibleValues, final int defaultSelection ) + throws PromptException { - final String formattedMessage = formatMessage( message, possibleValues, defaultReply ); + final String formattedMessage = formatMessage( message, possibleValues, defaultSelection ); + int result = -1; String line; - do { try @@ -126,7 +122,7 @@ public class MAEPrompter } catch ( final IOException e ) { - throw new PrompterException( "Failed to present prompt", e ); + throw new PromptException( "Failed to present prompt", e ); } try @@ -135,22 +131,35 @@ public class MAEPrompter } catch ( final IOException e ) { - throw new PrompterException( "Failed to read user response", e ); + throw new PromptException( "Failed to read user response", e ); } - if ( StringUtils.isEmpty( line ) ) + if ( isEmpty( line ) ) { - line = defaultReply; + result = defaultSelection; } - - if ( line != null && !possibleValues.contains( line ) ) + else { - writeLine( "Invalid selection." ); + line = line.trim(); + + if ( !possibleValues.contains( line ) ) + { + writeLine( "Invalid selection." ); + } + else + { + result = possibleValues.indexOf( line ) - 1; + } } } - while ( line == null || !possibleValues.contains( line ) ); + while ( result < 0 ); + + return result; + } - return line; + private boolean isEmpty( String line ) + { + return ( StringUtils.isEmpty( line ) || StringUtils.isEmpty( line.trim() ) ); } private void writeLine( final String message ) @@ -158,15 +167,14 @@ public class MAEPrompter config.getStandardOut().println( message ); } - @SuppressWarnings( "rawtypes" ) - public String prompt( final String message, final List possibleValues ) - throws PrompterException + public int getSelection( final String message, final List<?> possibleValues ) + throws PromptException { - return prompt( message, possibleValues, null ); + return getSelection( message, possibleValues, -1 ); } - public String promptForPassword( final String message ) - throws PrompterException + public String getPassword( final String message ) + throws PromptException { try { @@ -174,7 +182,7 @@ public class MAEPrompter } catch ( final IOException e ) { - throw new PrompterException( "Failed to present prompt", e ); + throw new PromptException( "Failed to present prompt", e ); } try @@ -184,37 +192,48 @@ public class MAEPrompter } catch ( final IOException e ) { - throw new PrompterException( "Failed to read user response", e ); + throw new PromptException( "Failed to read user response", e ); } } - @SuppressWarnings( "rawtypes" ) - private String formatMessage( final String message, final List possibleValues, final String defaultReply ) + private String formatMessage( final String message, final String defaultReply ) { - final StringBuffer formatted = new StringBuffer( message.length() * 2 ); + final StringBuilder formatted = new StringBuilder(); formatted.append( message ); - if ( possibleValues != null && !possibleValues.isEmpty() ) + if ( defaultReply != null ) { - formatted.append( " (" ); + formatted.append( ' ' ).append( defaultReply ).append( ": " ); + } - for ( final Iterator it = possibleValues.iterator(); it.hasNext(); ) + return formatted.toString(); + } + + private String formatMessage( final String message, final List<?> possibleValues, final int defaultReply ) + { + final StringBuilder formatted = new StringBuilder(); + + if ( possibleValues != null && !possibleValues.isEmpty() ) + { + for( int i =0; i< possibleValues.size(); i++ ) { - final String possibleValue = (String) it.next(); + Object possibleValue = possibleValues.get( i ); - formatted.append( possibleValue ); + formatted.append( i+1 ).append( ". " ).append( possibleValue ); - if ( it.hasNext() ) + if ( i+1 < possibleValues.size() ) { - formatted.append( '/' ); + formatted.append( '\n' ); } } - formatted.append( ')' ); + formatted.append( "\n\n" ); } - if ( defaultReply != null ) + formatted.append( message ); + + if ( defaultReply > -1 ) { formatted.append( ' ' ).append( defaultReply ).append( ": " ); } @@ -228,18 +247,4 @@ public class MAEPrompter config.getStandardOut().print( message + ": " ); } - public void showMessage( final String message ) - throws PrompterException - { - try - { - writePrompt( message ); - } - catch ( final IOException e ) - { - throw new PrompterException( "Failed to present prompt", e ); - } - - } - } Propchange: maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompt.java ------------------------------------------------------------------------------ svn:eol-style = native Added: maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/Prompt.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/Prompt.java?rev=1129770&view=auto ============================================================================== --- maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/Prompt.java (added) +++ maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/Prompt.java Tue May 31 16:03:59 2011 @@ -0,0 +1,23 @@ +package org.apache.maven.mae.prompt; + +import java.util.List; + +public interface Prompt +{ + + String getInput( String message ) + throws PromptException; + + String getInput( String message, String defaultReply ) + throws PromptException; + + int getSelection( String message, List<?> values ) + throws PromptException; + + int getSelection( String message, List<?> values, int defaultSelection ) + throws PromptException; + + String getPassword( String message ) + throws PromptException; + +} Propchange: maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/Prompt.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/Prompt.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/PromptException.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/PromptException.java?rev=1129770&view=auto ============================================================================== --- maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/PromptException.java (added) +++ maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/PromptException.java Tue May 31 16:03:59 2011 @@ -0,0 +1,20 @@ +package org.apache.maven.mae.prompt; + +import org.apache.maven.mae.MAEException; + +public class PromptException + extends MAEException +{ + private static final long serialVersionUID = 1L; + + public PromptException( String message, Object... params ) + { + super( message, params ); + } + + public PromptException( String message, Throwable cause, Object... params ) + { + super( message, cause, params ); + } + +} Propchange: maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/PromptException.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/PromptException.java ------------------------------------------------------------------------------ svn:mime-type = text/plain