All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.cedarsoft.utils.StringCmdLine Maven / Gradle / Ivy

The newest version!
package eu.cedarsoft.utils;

import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Command Line implementation that is based on strings.
 * 

* Date: 25.09.2006
* Time: 19:43:04
* * @author Johannes Schneider - * Xore Systems */ public class StringCmdLine extends AbstractCmdLine implements CmdLine { private int expectedIndex; private List expectedOut = new ArrayList(); private List answers = new ArrayList(); private ConsolePrinter consolePrinter = new SimpleConsolePrinter(); private void checkExpectedOut( @NotNull Object given ) { if ( expectedOut.size() <= expectedIndex ) { throw new IndexOutOfBoundsException( "No entries left for: " + given ); } @NonNls String nextExpected = this.expectedOut.get( expectedIndex ); expectedIndex++; if ( nextExpected == null ) { return; } if ( !nextExpected.equals( String.valueOf( given ) ) ) { throw new RuntimeException( "Did not match: \"" + nextExpected + "\" - but was: \"" + given + "\"" ); } } @Override @NotNull public PrintStream getOut() { throw new UnsupportedOperationException(); } @Override @NotNull protected ConsolePrinter getConsolePrinter() { return consolePrinter; } public boolean readBoolean( @NotNull String message ) throws IOException { messages.add( message ); return ( Boolean ) getNextAnswer(); } @NotNull public String read( @NotNull String message ) { messages.add( message ); return ( String ) getNextAnswer(); } private final List messages = new ArrayList(); @NotNull public List getMessages() { return Collections.unmodifiableList( messages ); } protected Object getNextAnswer() { if ( answers.isEmpty() ) { throw new IllegalStateException( "No answer left" ); } return answers.remove( 0 ); } @NotNull public String read( @NotNull String message, @Nullable String defaultValue ) { return read( message ); } public int readInt( @NotNull String message, int lower, int upper ) { messages.add( message ); return ( Integer ) getNextAnswer(); } public int readInt( @NotNull String message ) throws IOException { messages.add( message ); return ( Integer ) getNextAnswer(); } public void pause( int seconds ) { } @Override public void out( @NotNull String message, @NotNull Object... objects ) { messages.add( message ); checkExpectedOut( consolePrinter.createSuccess( message, objects ) ); } @Override public void out( @NotNull Process process ) { try { BufferedReader defaultIn = null; try { defaultIn = new BufferedReader( new InputStreamReader( process.getInputStream() ) ); String line; while ( ( line = defaultIn.readLine() ) != null ) { out( line ); } } finally { if ( defaultIn != null ) { defaultIn.close(); } } BufferedReader errorIn = null; try { errorIn = new BufferedReader( new InputStreamReader( process.getErrorStream() ) ); String line; while ( ( line = errorIn.readLine() ) != null ) { error( line ); } } finally { if ( errorIn != null ) { errorIn.close(); } } } catch ( IOException e ) { throw new RuntimeException( e ); } } @Override public void error( @NotNull String message, @NotNull Object... objects ) { messages.add( message ); checkExpectedOut( getConsolePrinter().createError( message, objects ) ); } @Override public void warning( @NotNull String message, @NotNull Object... objects ) { messages.add( message ); checkExpectedOut( getConsolePrinter().createWarning( message, objects ) ); } @Override public void success( @NotNull String message, @NotNull Object... objects ) { messages.add( message ); checkExpectedOut( getConsolePrinter().createSuccess( message, objects ) ); } @Override public void outNl() { checkExpectedOut( "" ); } public void addExpectedOut( @Nullable String expected ) { expectedOut.add( expected ); } @NotNull public List getExpectedOut() { return Collections.unmodifiableList( expectedOut ); } @NotNull public List getAnswers() { return Collections.unmodifiableList( answers ); } public void setAnswers( @NotNull List answers ) { this.answers.clear(); this.answers.addAll( answers ); } public void addAnswer( @NotNull Object answer ) { answers.add( answer ); } public void removeAnswer( @NotNull Object answer ) { answers.remove( answer ); } private static class SimpleConsolePrinter implements ConsolePrinter { @NotNull public String createError( @NotNull String message, @NotNull Object... objects ) { return "ERROR: " + MessageFormat.format( message, objects ); } @NotNull public String createWarning( @NotNull String message, @NotNull Object... objects ) { return "WARN: " + MessageFormat.format( message, objects ); } @NotNull public String createSuccess( @NotNull String message, @NotNull Object... objects ) { return MessageFormat.format( message, objects ); } } }