[Schmitzm-commits] r1654 - in trunk/schmitzm-core/src: main/java/de/schmitzm main/java/de/schmitzm/console main/java/de/schmitzm/io test/java/de/schmitzm/testing

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Jul 28 21:49:17 CEST 2011


Author: alfonx
Date: 2011-07-28 21:49:15 +0200 (Thu, 28 Jul 2011)
New Revision: 1654

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/console/
   trunk/schmitzm-core/src/main/java/de/schmitzm/console/CharacterDevice.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/console/ConsoleDevice.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/console/ConsoleException.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/console/TextDevice.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/console/TextDevices.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
   trunk/schmitzm-core/src/test/java/de/schmitzm/testing/TestingUtil.java
Log:


Added: trunk/schmitzm-core/src/main/java/de/schmitzm/console/CharacterDevice.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/console/CharacterDevice.java	2011-07-27 19:14:48 UTC (rev 1653)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/console/CharacterDevice.java	2011-07-28 19:49:15 UTC (rev 1654)
@@ -0,0 +1,74 @@
+/*
+Copyright (c) 2010 McDowell
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+ */
+
+package de.schmitzm.console;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Reader;
+
+/**
+ * @{link TextDevice} implementation wrapping character streams.
+ * 
+ * @author McDowell
+ */
+public class CharacterDevice extends TextDevice {
+  private final BufferedReader reader;
+  private final PrintWriter writer;
+
+  public CharacterDevice(BufferedReader reader, PrintWriter writer) {
+    this.reader = reader;
+    this.writer = writer;
+  }
+
+  @Override
+  public CharacterDevice printf(String fmt, Object... params)
+      throws ConsoleException {
+    writer.printf(fmt, params);
+    return this;
+  }
+
+  @Override
+  public String readLine() throws ConsoleException {
+    try {
+      return reader.readLine();
+    } catch (IOException e) {
+      throw new IllegalStateException();
+    }
+  }
+
+  @Override
+  public char[] readPassword() throws ConsoleException {
+    return readLine().toCharArray();
+  }
+
+  @Override
+  public Reader reader() throws ConsoleException {
+    return reader;
+  }
+
+  @Override
+  public PrintWriter writer() throws ConsoleException {
+    return writer;
+  }
+}


Property changes on: trunk/schmitzm-core/src/main/java/de/schmitzm/console/CharacterDevice.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/console/ConsoleDevice.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/console/ConsoleDevice.java	2011-07-27 19:14:48 UTC (rev 1653)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/console/ConsoleDevice.java	2011-07-28 19:49:15 UTC (rev 1654)
@@ -0,0 +1,67 @@
+/*
+Copyright (c) 2010 McDowell
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+ */
+
+package de.schmitzm.console;
+
+import java.io.Console;
+import java.io.PrintWriter;
+import java.io.Reader;
+
+/**
+ * {@link TextDevice} implementation wrapping a {@link Console}.
+ * 
+ * @author McDowell
+ */
+public class ConsoleDevice extends TextDevice {
+  private final Console console;
+
+  public ConsoleDevice(Console console) {
+    this.console = console;
+  }
+
+  @Override
+  public TextDevice printf(String fmt, Object... params)
+      throws ConsoleException {
+    console.format(fmt, params);
+    return this;
+  }
+
+  @Override
+  public Reader reader() throws ConsoleException {
+    return console.reader();
+  }
+
+  @Override
+  public String readLine() throws ConsoleException {
+    return console.readLine();
+  }
+
+  @Override
+  public char[] readPassword() throws ConsoleException {
+    return console.readPassword();
+  }
+
+  @Override
+  public PrintWriter writer() throws ConsoleException {
+    return console.writer();
+  }
+}
\ No newline at end of file


Property changes on: trunk/schmitzm-core/src/main/java/de/schmitzm/console/ConsoleDevice.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/console/ConsoleException.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/console/ConsoleException.java	2011-07-27 19:14:48 UTC (rev 1653)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/console/ConsoleException.java	2011-07-28 19:49:15 UTC (rev 1654)
@@ -0,0 +1,36 @@
+/*
+Copyright (c) 2010 McDowell
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+ */
+
+package de.schmitzm.console;
+
+/**
+ * Runtime exception for handling console errors.
+ * 
+ * @author McDowell
+ */
+public class ConsoleException extends RuntimeException {
+  private static final long serialVersionUID = 1L;
+
+  public ConsoleException(Throwable t) {
+    super(t);
+  }
+}


Property changes on: trunk/schmitzm-core/src/main/java/de/schmitzm/console/ConsoleException.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/console/TextDevice.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/console/TextDevice.java	2011-07-27 19:14:48 UTC (rev 1653)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/console/TextDevice.java	2011-07-28 19:49:15 UTC (rev 1654)
@@ -0,0 +1,47 @@
+/*
+Copyright (c) 2010 McDowell
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+ */
+
+package de.schmitzm.console;
+
+import java.io.PrintWriter;
+import java.io.Reader;
+
+/**
+ * Abstraction representing a text input/output device.
+ * 
+ * @author McDowell
+ */
+public abstract class TextDevice {
+  public abstract TextDevice printf(String fmt, Object... params)
+      throws ConsoleException;
+
+  public abstract String readLine() throws ConsoleException;
+
+  /**
+   * Wenn eine Console verfügbar ist, dann werden die Zeichen nicht angezeigt. Wenn nicht (z.B. beim Start aus IDE) werden die Zeichen angezeigt.
+   */
+  public abstract char[] readPassword() throws ConsoleException;
+
+  public abstract Reader reader() throws ConsoleException;
+
+  public abstract PrintWriter writer() throws ConsoleException;
+}


Property changes on: trunk/schmitzm-core/src/main/java/de/schmitzm/console/TextDevice.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/console/TextDevices.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/console/TextDevices.java	2011-07-27 19:14:48 UTC (rev 1653)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/console/TextDevices.java	2011-07-28 19:49:15 UTC (rev 1654)
@@ -0,0 +1,81 @@
+/*
+Copyright (c) 2010 McDowell
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+ */
+
+package de.schmitzm.console;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+
+/**
+ * Convenience class for providing {@link TextDevice} implementations.
+ * 
+ * @author McDowell
+ */
+public final class TextDevices {
+  private TextDevices() {}
+
+  private static TextDevice DEFAULT = (System.console() == null) ? streamDevice(
+      System.in, System.out)
+      : new ConsoleDevice(System.console());
+
+  /**
+   * The default system text I/O device.
+   * 
+   * @return the default device
+   */
+  public static TextDevice defaultTextDevice() {
+    return DEFAULT;
+  }
+
+  /**
+   * Returns a text I/O device wrapping the given streams. The default system
+   * encoding is used to decode/encode data.
+   * 
+   * @param in
+   *          an input source
+   * @param out
+   *          an output target
+   * @return a new device
+   */
+  public static TextDevice streamDevice(InputStream in, OutputStream out) {
+    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+    PrintWriter writer = new PrintWriter(out, true);
+    return new CharacterDevice(reader, writer);
+  }
+
+  /**
+   * Returns a text I/O device wrapping the given streams.
+   * 
+   * @param reader
+   *          an input source
+   * @param writer
+   *          an output target
+   * @return a new device
+   */
+  public static TextDevice characterDevice(BufferedReader reader,
+      PrintWriter writer) {
+    return new CharacterDevice(reader, writer);
+  }
+}


Property changes on: trunk/schmitzm-core/src/main/java/de/schmitzm/console/TextDevices.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2011-07-27 19:14:48 UTC (rev 1653)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2011-07-28 19:49:15 UTC (rev 1654)
@@ -42,6 +42,7 @@
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
 import java.io.Reader;
 import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
@@ -69,6 +70,9 @@
 import org.apache.commons.lang.SystemUtils;
 import org.apache.log4j.Logger;
 
+import de.schmitzm.console.CharacterDevice;
+import de.schmitzm.console.ConsoleDevice;
+import de.schmitzm.console.TextDevice;
 import de.schmitzm.lang.LangUtil;
 
 /**
@@ -693,7 +697,6 @@
 		return delCount;
 	}
 
-
 	/**
 	 * Fuehrt verschiedene Funktionen aus.
 	 * <ul>
@@ -1507,10 +1510,10 @@
 	 *            {@link InputStream} to read from
 	 */
 	public static String convertStreamToString(InputStream is)
-	throws IOException {
-		return convertStreamToString(is,"UTF-8");
+			throws IOException {
+		return convertStreamToString(is, "UTF-8");
 	}
-	
+
 	/**
 	 * Read a (not too big) Inputtream directly into a String.
 	 * 
@@ -1519,8 +1522,9 @@
 	 */
 	public static String convertStreamToString(InputStream is, String charset)
 			throws IOException {
-		
-		if (charset == null ) charset= "UTF-8";
+
+		if (charset == null)
+			charset = "UTF-8";
 		/*
 		 * To convert the InputStream to String we use the Reader.read(char[]
 		 * buffer) method. We iterate until the Reader return -1 which means
@@ -1609,4 +1613,19 @@
 		}
 
 	}
+
+	/**
+	 * Unter vielen Umständen liefert System.console() <code>null</code>. Diese
+	 * Hilfsklassen aus de.schmitzm.console stellen eine ähnliche Klasse bereit,
+	 * die nie <code>null</code> ist.
+	 */
+	public final static TextDevice CONSOLE = (System.console() == null) ? streamDevice(
+			System.in, System.out) : new ConsoleDevice(System.console());
+
+	public static TextDevice streamDevice(InputStream in, OutputStream out) {
+		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+		PrintWriter writer = new PrintWriter(out, true);
+		return new CharacterDevice(reader, writer);
+	}
+
 }

Modified: trunk/schmitzm-core/src/test/java/de/schmitzm/testing/TestingUtil.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/testing/TestingUtil.java	2011-07-27 19:14:48 UTC (rev 1653)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/testing/TestingUtil.java	2011-07-28 19:49:15 UTC (rev 1654)
@@ -310,6 +310,19 @@
 	/**
 	 * Wie assert assertEuqals, aber null == null ohne NPE
 	 */
+	public static void assertEqualsN(String msg, String soll, String ist) {
+		if (soll == null) {
+			assertNull(msg, ist);
+			return;
+		} else {
+			assertNotNull(msg,ist);
+			assertEquals(msg, soll, ist);
+		}
+	}
+	
+	/**
+	 * Wie assert assertEuqals, aber null == null ohne NPE
+	 */
 	public static void assertEqualsN(String msg, Integer soll, Integer ist) {
 		if (soll == null) {
 			assertNull(msg, ist);



More information about the Schmitzm-commits mailing list