org.python.core.PlainConsole Maven / Gradle / Ivy
Show all versions of jython-slim Show documentation
// Copyright (c) 2013 Jython Developers
package org.python.core;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
/**
* A base class for classes that can install a console wrapper for a specific console-handling
* library. The Jython command-line application, when it detects that the console is an interactive
* session, chooses and installs a class named in registry item python.console
, and
* implementing interface {@link Console}. PlainConsole
may be selected by the user
* through that registry, and is the default console when the selected one fails to load. It will
* also be installed by the Jython command-line application when in non-interactive mode.
*
* Unlike some consoles, PlainConsole
does not install a replacement for
* System.in
or System.out
, or use a native library. It prompts on
* System.out
and reads from System.in
(wrapped with the console
* encoding).
*
* @see org.python.core.RegistryKey#PYTHON_CONSOLE
*/
public class PlainConsole implements Console {
/** Encoding to use for line input. */
public final String encoding;
/** Encoding to use for line input as a Charset
. */
public final Charset encodingCharset;
/**
* Construct an instance of the console class specifying the character encoding. This encoding
* must be one supported by the JVM. The PlainConsole does not replace System.in
or
* System.out
, and does not add any line-editing capability to what is standard for
* your OS console.
*
* @param encoding name of a supported encoding or null
for
* Charset.defaultCharset()
*/
public PlainConsole(String encoding) throws IllegalCharsetNameException,
UnsupportedCharsetException {
if (encoding == null) {
encoding = Charset.defaultCharset().name();
}
this.encoding = encoding;
encodingCharset = Charset.forName(encoding);
}
@Override
public void install() {
// Nothing to do!
}
/**
* A PlainConsole
may be uninstalled. This method assumes any sub-class may not be
* uninstalled. Sub-classes that permit themselves to be uninstalled must override (and
* not call) this method.
*
* @throws UnsupportedOperationException unless this class is exactly PlainConsole
*/
@Override
public void uninstall() throws UnsupportedOperationException {
Class extends Console> myClass = this.getClass();
if (myClass != PlainConsole.class) {
throw new UnsupportedOperationException(myClass.getSimpleName()
+ " console may not be uninstalled.");
}
}
@Override
public String getEncoding() {
return encoding;
}
@Override
public Charset getEncodingCharset() {
return encodingCharset;
}
}